]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewautoscroller.cpp
also implement autoscrolling for horizontally aligned views (e. g. icons view in...
[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
40 m_timer = new QTimer(this);
41 m_timer->setSingleShot(false);
42 m_timer->setInterval(1000 / 25); // 25 frames per second
43 connect(m_timer, SIGNAL(timeout()), this, SLOT(scrollViewport()));
44 }
45
46 DolphinViewAutoScroller::~DolphinViewAutoScroller()
47 {
48 }
49
50 bool DolphinViewAutoScroller::eventFilter(QObject* watched, QEvent* event)
51 {
52 if (watched == m_itemView->viewport()) {
53 switch (event->type()) {
54 case QEvent::MouseButtonPress:
55 m_rubberBandSelection = true;
56 break;
57
58 case QEvent::MouseMove:
59 if (m_rubberBandSelection) {
60 triggerAutoScroll();
61 }
62 break;
63
64 case QEvent::MouseButtonRelease:
65 m_rubberBandSelection = false;
66 stopAutoScroll();
67 break;
68
69 case QEvent::DragEnter:
70 case QEvent::DragMove:
71 m_rubberBandSelection = false;
72 triggerAutoScroll();
73 break;
74
75 case QEvent::DragLeave:
76 m_rubberBandSelection = false;
77 stopAutoScroll();
78 break;
79
80 default:
81 break;
82 }
83 }
84
85 return QObject::eventFilter(watched, event);
86 }
87
88 void DolphinViewAutoScroller::scrollViewport()
89 {
90 QScrollBar* verticalScrollBar = m_itemView->verticalScrollBar();
91 if (verticalScrollBar != 0) {
92 const int value = verticalScrollBar->value();
93 verticalScrollBar->setValue(value + m_scrollInc);
94
95 }
96 QScrollBar* horizontalScrollBar = m_itemView->horizontalScrollBar();
97 if (horizontalScrollBar != 0) {
98 const int value = horizontalScrollBar->value();
99 horizontalScrollBar->setValue(value + m_scrollInc);
100
101 }
102
103 if (m_rubberBandSelection) {
104 // The scrolling does not lead to an update of the rubberband
105 // selection. Fake a mouse move event to let the QAbstractItemView
106 // update the rubberband.
107 QWidget* viewport = m_itemView->viewport();
108 const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
109 QMouseEvent event(QEvent::MouseMove, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
110 QCoreApplication::sendEvent(viewport, &event);
111 }
112 }
113
114 void DolphinViewAutoScroller::triggerAutoScroll()
115 {
116 const bool verticalScrolling = (m_itemView->verticalScrollBar() != 0) &&
117 m_itemView->verticalScrollBar()->isVisible();
118 const bool horizontalScrolling = (m_itemView->horizontalScrollBar() != 0) &&
119 m_itemView->horizontalScrollBar()->isVisible();
120 if (!verticalScrolling && !horizontalScrolling) {
121 // no scrollbars are shown at all, so no autoscrolling is necessary
122 return;
123 }
124
125 QWidget* viewport = m_itemView->viewport();
126 const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
127 if (verticalScrolling) {
128 calculateScrollIncrement(pos.y(), viewport->height());
129 }
130 if (horizontalScrolling) {
131 calculateScrollIncrement(pos.x(), viewport->width());
132 }
133
134 if (m_timer->isActive()) {
135 if (m_scrollInc == 0) {
136 m_timer->stop();
137 }
138 } else if (m_scrollInc != 0) {
139 m_timer->start();
140 }
141 }
142
143 void DolphinViewAutoScroller::stopAutoScroll()
144 {
145 m_timer->stop();
146 m_scrollInc = 0;
147 }
148
149 void DolphinViewAutoScroller::calculateScrollIncrement(int cursorPos, int rangeSize)
150 {
151 const int minSpeed = 2;
152 const int maxSpeed = 32;
153 const int speedLimiter = 8;
154 const int autoScrollBorder = 32;
155
156 if (cursorPos < autoScrollBorder) {
157 m_scrollInc = -minSpeed + (cursorPos - autoScrollBorder) / speedLimiter;
158 if (m_scrollInc < -maxSpeed) {
159 m_scrollInc = -maxSpeed;
160 }
161 } else if (cursorPos > rangeSize - autoScrollBorder) {
162 m_scrollInc = minSpeed + (cursorPos - rangeSize + autoScrollBorder) / speedLimiter;
163 if (m_scrollInc > maxSpeed) {
164 m_scrollInc = maxSpeed;
165 }
166 } else {
167 m_scrollInc = 0;
168 }
169 }
170
171 #include "dolphinviewautoscroller.moc"