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