]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewautoscroller.cpp
Include page-up, page-down, home + end as keys that need autoscrolling. Thanks to...
[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
30 DolphinViewAutoScroller::DolphinViewAutoScroller(QAbstractItemView* parent) :
31 QObject(parent),
32 m_rubberBandSelection(false),
33 m_scrollInc(0),
34 m_itemView(parent),
35 m_timer()
36 {
37 m_itemView->setAutoScroll(false);
38 m_itemView->viewport()->installEventFilter(this);
39 m_itemView->installEventFilter(this);
40
41 m_timer = new QTimer(this);
42 m_timer->setSingleShot(false);
43 m_timer->setInterval(1000 / 25); // 25 frames per second
44 connect(m_timer, SIGNAL(timeout()), this, SLOT(scrollViewport()));
45 }
46
47 DolphinViewAutoScroller::~DolphinViewAutoScroller()
48 {
49 }
50
51 bool DolphinViewAutoScroller::eventFilter(QObject* watched, QEvent* event)
52 {
53 if (watched == m_itemView->viewport()) {
54 switch (event->type()) {
55 case QEvent::MouseButtonPress:
56 if (static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton) {
57 m_rubberBandSelection = true;
58 }
59 break;
60
61 case QEvent::MouseMove:
62 if (m_rubberBandSelection) {
63 triggerAutoScroll();
64 }
65 break;
66
67 case QEvent::MouseButtonRelease:
68 m_rubberBandSelection = false;
69 stopAutoScroll();
70 break;
71
72 case QEvent::DragEnter:
73 case QEvent::DragMove:
74 m_rubberBandSelection = false;
75 triggerAutoScroll();
76 break;
77
78 case QEvent::Drop:
79 case QEvent::DragLeave:
80 m_rubberBandSelection = false;
81 stopAutoScroll();
82 break;
83
84 default:
85 break;
86 }
87 } else if ((watched == m_itemView) && (event->type() == QEvent::KeyPress)) {
88 switch (static_cast<QKeyEvent*>(event)->key()) {
89 case Qt::Key_Up:
90 case Qt::Key_Down:
91 case Qt::Key_Left:
92 case Qt::Key_Right:
93 case Qt::Key_PageUp:
94 case Qt::Key_PageDown:
95 case Qt::Key_Home:
96 case Qt::Key_End:
97 QMetaObject::invokeMethod(this, "scrollToCurrentIndex", Qt::QueuedConnection);
98 break;
99 default:
100 break;
101 }
102 }
103
104
105 return QObject::eventFilter(watched, event);
106 }
107
108 void DolphinViewAutoScroller::scrollViewport()
109 {
110 QScrollBar* verticalScrollBar = m_itemView->verticalScrollBar();
111 if (verticalScrollBar != 0) {
112 const int value = verticalScrollBar->value();
113 verticalScrollBar->setValue(value + m_scrollInc);
114
115 }
116 QScrollBar* horizontalScrollBar = m_itemView->horizontalScrollBar();
117 if (horizontalScrollBar != 0) {
118 const int value = horizontalScrollBar->value();
119 horizontalScrollBar->setValue(value + m_scrollInc);
120
121 }
122
123 if (m_rubberBandSelection) {
124 // The scrolling does not lead to an update of the rubberband
125 // selection. Fake a mouse move event to let the QAbstractItemView
126 // update the rubberband.
127 QWidget* viewport = m_itemView->viewport();
128 const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
129 QMouseEvent event(QEvent::MouseMove, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
130 QCoreApplication::sendEvent(viewport, &event);
131 }
132 }
133
134 void DolphinViewAutoScroller::scrollToCurrentIndex()
135 {
136 const QModelIndex index = m_itemView->currentIndex();
137 m_itemView->scrollTo(index);
138 }
139
140 void DolphinViewAutoScroller::triggerAutoScroll()
141 {
142 const bool verticalScrolling = (m_itemView->verticalScrollBar() != 0) &&
143 m_itemView->verticalScrollBar()->isVisible();
144 const bool horizontalScrolling = (m_itemView->horizontalScrollBar() != 0) &&
145 m_itemView->horizontalScrollBar()->isVisible();
146 if (!verticalScrolling && !horizontalScrolling) {
147 // no scrollbars are shown at all, so no autoscrolling is necessary
148 return;
149 }
150
151 QWidget* viewport = m_itemView->viewport();
152 const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
153 if (verticalScrolling) {
154 calculateScrollIncrement(pos.y(), viewport->height());
155 }
156 if (horizontalScrolling) {
157 calculateScrollIncrement(pos.x(), viewport->width());
158 }
159
160 if (m_timer->isActive()) {
161 if (m_scrollInc == 0) {
162 m_timer->stop();
163 }
164 } else if (m_scrollInc != 0) {
165 m_timer->start();
166 }
167 }
168
169 void DolphinViewAutoScroller::stopAutoScroll()
170 {
171 m_timer->stop();
172 m_scrollInc = 0;
173 }
174
175 void DolphinViewAutoScroller::calculateScrollIncrement(int cursorPos, int rangeSize)
176 {
177 const int minSpeed = 2;
178 const int maxSpeed = 32;
179 const int speedLimiter = 8;
180 const int autoScrollBorder = 32;
181
182 if (cursorPos < autoScrollBorder) {
183 m_scrollInc = -minSpeed + (cursorPos - autoScrollBorder) / speedLimiter;
184 if (m_scrollInc < -maxSpeed) {
185 m_scrollInc = -maxSpeed;
186 }
187 } else if (cursorPos > rangeSize - autoScrollBorder) {
188 m_scrollInc = minSpeed + (cursorPos - rangeSize + autoScrollBorder) / speedLimiter;
189 if (m_scrollInc > maxSpeed) {
190 m_scrollInc = maxSpeed;
191 }
192 } else {
193 m_scrollInc = 0;
194 }
195 }
196
197 #include "dolphinviewautoscroller.moc"