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