]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewautoscroller.cpp
Update the statusbar synchronously when the directory lister has been completed....
[dolphin.git] / src / dolphinviewautoscroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
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 "dolphinviewautoscroller.h"
21
22 #include <QAbstractItemView>
23 #include <QCoreApplication>
24 #include <QCursor>
25 #include <QEvent>
26 #include <QMouseEvent>
27 #include <QScrollBar>
28 #include <QTimer>
29 #include <math.h>
30
31 DolphinViewAutoScroller::DolphinViewAutoScroller(QAbstractItemView* parent) :
32 QObject(parent),
33 m_rubberBandSelection(false),
34 m_horizontalScrollInc(0),
35 m_verticalScrollInc(0),
36 m_itemView(parent),
37 m_timer(0)
38 {
39 m_itemView->setAutoScroll(false);
40 m_itemView->viewport()->installEventFilter(this);
41
42 m_timer = new QTimer(this);
43 m_timer->setSingleShot(false);
44 m_timer->setInterval(1000 / 25); // 25 frames per second
45 connect(m_timer, SIGNAL(timeout()), this, SLOT(scrollViewport()));
46 }
47
48 DolphinViewAutoScroller::~DolphinViewAutoScroller()
49 {
50 }
51
52 bool DolphinViewAutoScroller::isActive() const
53 {
54 return m_timer->isActive();
55 }
56
57 void DolphinViewAutoScroller::handleCurrentIndexChange(const QModelIndex& current,
58 const QModelIndex& previous)
59 {
60 // When the autoscroller is inactive and a key has been pressed, it must be
61 // assured that the current item stays visible. The check whether the previous
62 // item is valid is important because of #197951.
63 if (current.isValid() && previous.isValid() && !isActive()) {
64 m_itemView->scrollTo(current);
65 }
66 }
67
68 bool DolphinViewAutoScroller::eventFilter(QObject* watched, QEvent* event)
69 {
70 if (watched == m_itemView->viewport()) {
71 switch (event->type()) {
72 case QEvent::MouseButtonPress:
73 if (static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton) {
74 m_rubberBandSelection = true;
75 }
76 break;
77
78 case QEvent::MouseMove:
79 if (m_rubberBandSelection) {
80 triggerAutoScroll();
81 }
82 break;
83
84 case QEvent::MouseButtonRelease:
85 m_rubberBandSelection = false;
86 stopAutoScroll();
87 break;
88
89 case QEvent::DragEnter:
90 case QEvent::DragMove:
91 m_rubberBandSelection = false;
92 triggerAutoScroll();
93 break;
94
95 case QEvent::Drop:
96 case QEvent::DragLeave:
97 m_rubberBandSelection = false;
98 stopAutoScroll();
99 break;
100
101 default:
102 break;
103 }
104 }
105
106 return QObject::eventFilter(watched, event);
107 }
108
109 void DolphinViewAutoScroller::scrollViewport()
110 {
111 QScrollBar* verticalScrollBar = m_itemView->verticalScrollBar();
112 if (verticalScrollBar != 0) {
113 const int value = verticalScrollBar->value();
114 verticalScrollBar->setValue(value + m_verticalScrollInc);
115
116 }
117 QScrollBar* horizontalScrollBar = m_itemView->horizontalScrollBar();
118 if (horizontalScrollBar != 0) {
119 const int value = horizontalScrollBar->value();
120 horizontalScrollBar->setValue(value + m_horizontalScrollInc);
121
122 }
123
124 if (m_rubberBandSelection) {
125 // The scrolling does not lead to an update of the rubberband
126 // selection. Fake a mouse move event to let the QAbstractItemView
127 // update the rubberband.
128 QWidget* viewport = m_itemView->viewport();
129 const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
130 QMouseEvent event(QEvent::MouseMove, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
131 QCoreApplication::sendEvent(viewport, &event);
132 }
133 }
134
135 void DolphinViewAutoScroller::triggerAutoScroll()
136 {
137 const bool verticalScrolling = (m_itemView->verticalScrollBar() != 0) &&
138 m_itemView->verticalScrollBar()->isVisible();
139 const bool horizontalScrolling = (m_itemView->horizontalScrollBar() != 0) &&
140 m_itemView->horizontalScrollBar()->isVisible();
141 if (!verticalScrolling && !horizontalScrolling) {
142 // no scrollbars are shown at all, so no autoscrolling is necessary
143 return;
144 }
145
146 QWidget* viewport = m_itemView->viewport();
147 const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
148 if (verticalScrolling) {
149 m_verticalScrollInc = calculateScrollIncrement(pos.y(), viewport->height());
150 }
151 if (horizontalScrolling) {
152 m_horizontalScrollInc = calculateScrollIncrement(pos.x(), viewport->width());
153 }
154
155 if (m_timer->isActive()) {
156 if ((m_horizontalScrollInc == 0) && (m_verticalScrollInc == 0)) {
157 m_timer->stop();
158 }
159 } else if ((m_horizontalScrollInc != 0) || (m_verticalScrollInc != 0)) {
160 m_timer->start();
161 }
162 }
163
164 void DolphinViewAutoScroller::stopAutoScroll()
165 {
166 m_timer->stop();
167 m_horizontalScrollInc = 0;
168 m_verticalScrollInc = 0;
169 }
170
171 int DolphinViewAutoScroller::calculateScrollIncrement(int cursorPos, int rangeSize) const
172 {
173 int inc = 0;
174
175 const int minSpeed = 4;
176 const int maxSpeed = 768;
177 const int speedLimiter = 48;
178 const int autoScrollBorder = 64;
179
180 if (cursorPos < autoScrollBorder) {
181 inc = -minSpeed + qAbs(cursorPos - autoScrollBorder) * (cursorPos - autoScrollBorder) / speedLimiter;
182 if (inc < -maxSpeed) {
183 inc = -maxSpeed;
184 }
185 } else if (cursorPos > rangeSize - autoScrollBorder) {
186 inc = minSpeed + qAbs(cursorPos - rangeSize + autoScrollBorder) * (cursorPos - rangeSize + autoScrollBorder) / speedLimiter;
187 if (inc > maxSpeed) {
188 inc = maxSpeed;
189 }
190 }
191
192 return inc;
193 }
194
195 #include "dolphinviewautoscroller.moc"