]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewautoscroller.cpp
Further pimpage of auto scrolling: make it quadratic! Better scales to HUGE file...
[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 bool DolphinViewAutoScroller::eventFilter(QObject* watched, QEvent* event)
58 {
59 if (watched == m_itemView->viewport()) {
60 switch (event->type()) {
61 case QEvent::MouseButtonPress:
62 if (static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton) {
63 m_rubberBandSelection = true;
64 }
65 break;
66
67 case QEvent::MouseMove:
68 if (m_rubberBandSelection) {
69 triggerAutoScroll();
70 }
71 break;
72
73 case QEvent::MouseButtonRelease:
74 m_rubberBandSelection = false;
75 stopAutoScroll();
76 break;
77
78 case QEvent::DragEnter:
79 case QEvent::DragMove:
80 m_rubberBandSelection = false;
81 triggerAutoScroll();
82 break;
83
84 case QEvent::Drop:
85 case QEvent::DragLeave:
86 m_rubberBandSelection = false;
87 stopAutoScroll();
88 break;
89
90 default:
91 break;
92 }
93 }
94
95 return QObject::eventFilter(watched, event);
96 }
97
98 void DolphinViewAutoScroller::scrollViewport()
99 {
100 QScrollBar* verticalScrollBar = m_itemView->verticalScrollBar();
101 if (verticalScrollBar != 0) {
102 const int value = verticalScrollBar->value();
103 verticalScrollBar->setValue(value + m_verticalScrollInc);
104
105 }
106 QScrollBar* horizontalScrollBar = m_itemView->horizontalScrollBar();
107 if (horizontalScrollBar != 0) {
108 const int value = horizontalScrollBar->value();
109 horizontalScrollBar->setValue(value + m_horizontalScrollInc);
110
111 }
112
113 if (m_rubberBandSelection) {
114 // The scrolling does not lead to an update of the rubberband
115 // selection. Fake a mouse move event to let the QAbstractItemView
116 // update the rubberband.
117 QWidget* viewport = m_itemView->viewport();
118 const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
119 QMouseEvent event(QEvent::MouseMove, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
120 QCoreApplication::sendEvent(viewport, &event);
121 }
122 }
123
124 void DolphinViewAutoScroller::triggerAutoScroll()
125 {
126 const bool verticalScrolling = (m_itemView->verticalScrollBar() != 0) &&
127 m_itemView->verticalScrollBar()->isVisible();
128 const bool horizontalScrolling = (m_itemView->horizontalScrollBar() != 0) &&
129 m_itemView->horizontalScrollBar()->isVisible();
130 if (!verticalScrolling && !horizontalScrolling) {
131 // no scrollbars are shown at all, so no autoscrolling is necessary
132 return;
133 }
134
135 QWidget* viewport = m_itemView->viewport();
136 const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
137 if (verticalScrolling) {
138 m_verticalScrollInc = calculateScrollIncrement(pos.y(), viewport->height());
139 }
140 if (horizontalScrolling) {
141 m_horizontalScrollInc = calculateScrollIncrement(pos.x(), viewport->width());
142 }
143
144 if (m_timer->isActive()) {
145 if ((m_horizontalScrollInc == 0) && (m_verticalScrollInc == 0)) {
146 m_timer->stop();
147 }
148 } else if ((m_horizontalScrollInc != 0) || (m_verticalScrollInc != 0)) {
149 m_timer->start();
150 }
151 }
152
153 void DolphinViewAutoScroller::stopAutoScroll()
154 {
155 m_timer->stop();
156 m_horizontalScrollInc = 0;
157 m_verticalScrollInc = 0;
158 }
159
160 int DolphinViewAutoScroller::calculateScrollIncrement(int cursorPos, int rangeSize) const
161 {
162 int inc = 0;
163
164 const int minSpeed = 4;
165 const int maxSpeed = 768;
166 const int speedLimiter = 48;
167 const int autoScrollBorder = 64;
168
169 if (cursorPos < autoScrollBorder) {
170 inc = -minSpeed + fabs(cursorPos - autoScrollBorder) * (cursorPos - autoScrollBorder) / speedLimiter;
171 if (inc < -maxSpeed) {
172 inc = -maxSpeed;
173 }
174 } else if (cursorPos > rangeSize - autoScrollBorder) {
175 inc = minSpeed + fabs(cursorPos - rangeSize + autoScrollBorder) * (cursorPos - rangeSize + autoScrollBorder) / speedLimiter;
176 if (inc > maxSpeed) {
177 inc = maxSpeed;
178 }
179 }
180
181 return inc;
182 }
183
184 #include "dolphinviewautoscroller.moc"