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