]> cloud.milkyroute.net Git - dolphin.git/blob - src/ktreeview.cpp
Provide horizontal auto scrolling for the tree view (implemented by Harald Hvaal...
[dolphin.git] / src / ktreeview.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by <haraldhv (at) stud.ntnu.no> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include <KGlobalSettings>
21
22 #include <QDebug>
23 #include <QScrollBar>
24
25 #include "ktreeview.h"
26 #include "ktreeview_p.h"
27
28 KTreeView::KTreeViewPrivate::KTreeViewPrivate(KTreeView *parent)
29 : parent(parent),
30 autoHorizontalScroll(true),
31 scrollTowards(0),
32 scrollPixels(5),
33 scrollDelay(50),
34 leftSideMargin(30),
35 considerDelay(500),
36 topLeftPoint(QPoint(10,10))
37 {
38 Q_ASSERT(parent->verticalScrollBar());
39
40 considerDelayTimer.setInterval(considerDelay);
41
42 connect( &considerDelayTimer,
43 SIGNAL(timeout()),
44 this,
45 SLOT(considerAutoScroll())
46 );
47
48 connect( parent->verticalScrollBar(),
49 SIGNAL(rangeChanged(int, int)),
50 &considerDelayTimer,
51 SLOT(start())
52 );
53
54 connect( parent->verticalScrollBar(),
55 SIGNAL(valueChanged(int)),
56 &considerDelayTimer,
57 SLOT(start())
58 );
59
60 connect( parent,
61 SIGNAL( collapsed ( const QModelIndex &)),
62 &considerDelayTimer,
63 SLOT(start())
64 );
65
66 connect( parent,
67 SIGNAL( expanded ( const QModelIndex &)),
68 &considerDelayTimer,
69 SLOT(start())
70 );
71
72 }
73
74 void KTreeView::KTreeViewPrivate::considerAutoScroll()
75 {
76 qDebug() << "Considering auto scroll";
77
78 QModelIndex i = parent->indexAt(topLeftPoint);
79 int smallest = parent->width();
80
81 while (i.isValid())
82 {
83 QRect r = parent->visualRect(i);
84 if (r.top() > parent->height())
85 break;
86
87 int leftSide = r.left();
88
89 smallest = qMin(smallest, leftSide);
90 i = parent->indexBelow(i);
91 }
92
93 int currentScroll = parent->horizontalScrollBar()->value();
94
95 setScrollTowards(smallest + currentScroll - leftSideMargin);
96
97 considerDelayTimer.stop();
98
99 }
100
101 void KTreeView::KTreeViewPrivate::autoScrollTimeout()
102 {
103
104 Q_ASSERT(parent);
105
106 QScrollBar *scrollBar = parent->horizontalScrollBar();
107 if (scrollBar == NULL)
108 {
109 qDebug() << "Warning: no scrollbar present, but told to scroll.";
110 scrollTimer.stop();
111 return;
112 }
113
114 int currentScroll = scrollBar->value();
115
116 int difference = currentScroll - scrollTowards;
117
118 if (qAbs(difference) < scrollPixels)
119 {
120 scrollBar->setValue(scrollTowards);
121 scrollTimer.stop();
122 return;
123 }
124
125 if (difference < 0)
126 {
127 scrollBar->setValue(currentScroll + scrollPixels);
128 }
129 else
130 {
131 scrollBar->setValue(currentScroll - scrollPixels);
132 }
133 }
134
135 void KTreeView::KTreeViewPrivate::setScrollTowards( int scrollTowards )
136 {
137 if (scrollTowards < 0)
138 scrollTowards = 0;
139 this->scrollTowards = scrollTowards;
140 scrollTimer.start(scrollDelay);
141 }
142
143 //************************************************
144
145 KTreeView::KTreeView(QWidget *parent)
146 : QTreeView(parent)
147 , d(new KTreeViewPrivate(this))
148 {
149 /* The graphicEffectsLevel was not available in the 4.0.3 version of
150 * the libs I was compiling with, so this is left out for now and
151 * enabled by default...
152 */
153 //if (KGlobalSettings::graphicEffectsLevel() >=
154 //KGlobalSettings::SimpleAnimationEffects)
155 //{
156 setAutoHorizontalScroll(true);
157 //}
158 connect(
159 &d->scrollTimer,
160 SIGNAL(timeout()),
161 d,
162 SLOT(autoScrollTimeout())
163 );
164
165 }
166
167 void KTreeView::setAutoHorizontalScroll(bool value)
168 {
169 d->autoHorizontalScroll = value;
170 }
171
172 bool KTreeView::autoHorizontalScroll( void )
173 {
174 return d->autoHorizontalScroll;
175 }
176
177 #include "ktreeview.moc"
178 #include "ktreeview_p.moc"