]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/ktreeview.cpp
Update e-mail address from peter.penz@gmx.at to peter.penz19@gmail.com
[dolphin.git] / src / panels / folders / ktreeview.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by <haraldhv (at) stud.ntnu.no> *
3 * Copyright (C) 2008 by <peter.penz19@gmail.com> *
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 if (timeLine->state() == QTimeLine::Running) {
95 timeLine->stop();
96 }
97 timeLine->start();
98 }
99 }
100
101 void KTreeView::KTreeViewPrivate::updateVerticalScrollBar(int value)
102 {
103 QScrollBar *scrollBar = parent->horizontalScrollBar();
104 scrollBar->setValue(value);
105 }
106
107 // ************************************************
108
109 KTreeView::KTreeView(QWidget *parent) :
110 QTreeView(parent),
111 d(new KTreeViewPrivate(this))
112 {
113 if (KGlobalSettings::graphicEffectsLevel() >= KGlobalSettings::SimpleAnimationEffects) {
114 setAutoHorizontalScroll(true);
115 }
116 }
117
118 KTreeView::~KTreeView()
119 {
120 }
121
122 void KTreeView::setAutoHorizontalScroll(bool enable)
123 {
124 d->autoHorizontalScroll = enable;
125 if (!enable) {
126 d->timeLine->stop();
127 }
128 }
129
130 bool KTreeView::autoHorizontalScroll() const
131 {
132 return d->autoHorizontalScroll;
133 }
134
135 void KTreeView::scrollTo(const QModelIndex& index, ScrollHint hint)
136 {
137 const int value = horizontalScrollBar()->value();
138 QTreeView::scrollTo(index, hint);
139 horizontalScrollBar()->setValue(value);
140 }
141
142 void KTreeView::hideEvent(QHideEvent *event)
143 {
144 d->timeLine->stop();
145 QTreeView::hideEvent(event);
146 }
147
148 #include "ktreeview.moc"
149 #include "ktreeview_p.moc"