]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/ktreeview.cpp
Take care to not reset the preview-settings if they have not been changed.
[dolphin.git] / src / panels / folders / ktreeview.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by <haraldhv (at) stud.ntnu.no> *
3 * Copyright (C) 2008 by <peter.penz@gmx.at> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "ktreeview.h"
22 #include "ktreeview_p.h"
23
24 #include <KGlobalSettings>
25
26 #include <QItemSelectionModel>
27 #include <QScrollBar>
28 #include <QTimer>
29 #include <QTimeLine>
30
31 KTreeView::KTreeViewPrivate::KTreeViewPrivate(KTreeView *parent) :
32 parent(parent),
33 autoHorizontalScroll(false),
34 timeLine(0)
35 {
36 timeLine = new QTimeLine(500, this);
37 connect(timeLine, SIGNAL(frameChanged(int)),
38 this, SLOT(updateVerticalScrollBar(int)));
39
40 connect(parent->verticalScrollBar(), SIGNAL(rangeChanged(int, int)),
41 this, SLOT(startScrolling()));
42 connect(parent->verticalScrollBar(), SIGNAL(valueChanged(int)),
43 this, SLOT(startScrolling()));
44 connect(parent, SIGNAL(collapsed(const QModelIndex&)),
45 this, SLOT(startScrolling()));
46 connect(parent, SIGNAL(expanded(const QModelIndex&)),
47 this, SLOT(startScrolling()));
48 }
49
50 void KTreeView::KTreeViewPrivate::startScrolling()
51 {
52 if (!autoHorizontalScroll) {
53 return;
54 }
55
56 // Determine the most left visual index
57 QModelIndex visibleIndex = parent->indexAt(QPoint(0, 0));
58 if (!visibleIndex.isValid()) {
59 return;
60 }
61
62 QModelIndex index = visibleIndex;
63 int minimum = parent->width();
64 do {
65 const QRect rect = parent->visualRect(visibleIndex);
66 if (rect.top() > parent->viewport()->height()) {
67 // the current index and all successors are not visible anymore
68 break;
69 }
70 if (rect.left() < minimum) {
71 minimum = rect.left();
72 index = visibleIndex;
73 }
74 visibleIndex = parent->indexBelow(visibleIndex);
75 } while (visibleIndex.isValid());
76
77 // Start the horizontal scrolling to assure that the item indicated by 'index' gets fully visible
78 Q_ASSERT(index.isValid());
79 const QRect rect = parent->visualRect(index);
80
81 QScrollBar* scrollBar = parent->horizontalScrollBar();
82 const int oldScrollBarPos = scrollBar->value();
83
84 const int itemRight = oldScrollBarPos + rect.left() + rect.width() - 1;
85 const int availableWidth = parent->viewport()->width();
86 int scrollBarPos = itemRight - availableWidth;
87 const int scrollBarPosMax = oldScrollBarPos + rect.left() - parent->indentation();
88 if (scrollBarPos > scrollBarPosMax) {
89 scrollBarPos = scrollBarPosMax;
90 }
91
92 if (scrollBarPos != oldScrollBarPos) {
93 timeLine->setFrameRange(oldScrollBarPos, scrollBarPos);
94 timeLine->start();
95 }
96 }
97
98 void KTreeView::KTreeViewPrivate::updateVerticalScrollBar(int value)
99 {
100 QScrollBar *scrollBar = parent->horizontalScrollBar();
101 scrollBar->setValue(value);
102 }
103
104 // ************************************************
105
106 KTreeView::KTreeView(QWidget *parent) :
107 QTreeView(parent),
108 d(new KTreeViewPrivate(this))
109 {
110 if (KGlobalSettings::graphicEffectsLevel() >= KGlobalSettings::SimpleAnimationEffects) {
111 setAutoHorizontalScroll(true);
112 }
113 }
114
115 KTreeView::~KTreeView()
116 {
117 }
118
119 void KTreeView::setAutoHorizontalScroll(bool enable)
120 {
121 d->autoHorizontalScroll = enable;
122 if (!enable) {
123 d->timeLine->stop();
124 }
125 }
126
127 bool KTreeView::autoHorizontalScroll() const
128 {
129 return d->autoHorizontalScroll;
130 }
131
132 void KTreeView::scrollTo(const QModelIndex& index, ScrollHint hint)
133 {
134 const int value = horizontalScrollBar()->value();
135 QTreeView::scrollTo(index, hint);
136 horizontalScrollBar()->setValue(value);
137 }
138
139 void KTreeView::hideEvent(QHideEvent *event)
140 {
141 d->timeLine->stop();
142 QTreeView::hideEvent(event);
143 }
144
145 #include "ktreeview.moc"
146 #include "ktreeview_p.moc"