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