]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewautoscroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "dolphinviewautoscroller.h"
22 #include <QAbstractItemView>
23 #include <QCoreApplication>
26 #include <QMouseEvent>
31 DolphinViewAutoScroller::DolphinViewAutoScroller(QAbstractItemView
* parent
) :
33 m_rubberBandSelection(false),
34 m_horizontalScrollInc(0),
35 m_verticalScrollInc(0),
39 m_itemView
->setAutoScroll(false);
40 m_itemView
->viewport()->installEventFilter(this);
42 m_timer
= new QTimer(this);
43 m_timer
->setSingleShot(false);
44 m_timer
->setInterval(1000 / 25); // 25 frames per second
45 connect(m_timer
, SIGNAL(timeout()), this, SLOT(scrollViewport()));
48 DolphinViewAutoScroller::~DolphinViewAutoScroller()
52 bool DolphinViewAutoScroller::isActive() const
54 return m_timer
->isActive();
57 bool DolphinViewAutoScroller::eventFilter(QObject
* watched
, QEvent
* event
)
59 if (watched
== m_itemView
->viewport()) {
60 switch (event
->type()) {
61 case QEvent::MouseButtonPress
:
62 if (static_cast<QMouseEvent
*>(event
)->button() == Qt::LeftButton
) {
63 m_rubberBandSelection
= true;
67 case QEvent::MouseMove
:
68 if (m_rubberBandSelection
) {
73 case QEvent::MouseButtonRelease
:
74 m_rubberBandSelection
= false;
78 case QEvent::DragEnter
:
79 case QEvent::DragMove
:
80 m_rubberBandSelection
= false;
85 case QEvent::DragLeave
:
86 m_rubberBandSelection
= false;
95 return QObject::eventFilter(watched
, event
);
98 void DolphinViewAutoScroller::scrollViewport()
100 QScrollBar
* verticalScrollBar
= m_itemView
->verticalScrollBar();
101 if (verticalScrollBar
!= 0) {
102 const int value
= verticalScrollBar
->value();
103 verticalScrollBar
->setValue(value
+ m_verticalScrollInc
);
106 QScrollBar
* horizontalScrollBar
= m_itemView
->horizontalScrollBar();
107 if (horizontalScrollBar
!= 0) {
108 const int value
= horizontalScrollBar
->value();
109 horizontalScrollBar
->setValue(value
+ m_horizontalScrollInc
);
113 if (m_rubberBandSelection
) {
114 // The scrolling does not lead to an update of the rubberband
115 // selection. Fake a mouse move event to let the QAbstractItemView
116 // update the rubberband.
117 QWidget
* viewport
= m_itemView
->viewport();
118 const QPoint pos
= viewport
->mapFromGlobal(QCursor::pos());
119 QMouseEvent
event(QEvent::MouseMove
, pos
, Qt::LeftButton
, Qt::LeftButton
, Qt::NoModifier
);
120 QCoreApplication::sendEvent(viewport
, &event
);
124 void DolphinViewAutoScroller::triggerAutoScroll()
126 const bool verticalScrolling
= (m_itemView
->verticalScrollBar() != 0) &&
127 m_itemView
->verticalScrollBar()->isVisible();
128 const bool horizontalScrolling
= (m_itemView
->horizontalScrollBar() != 0) &&
129 m_itemView
->horizontalScrollBar()->isVisible();
130 if (!verticalScrolling
&& !horizontalScrolling
) {
131 // no scrollbars are shown at all, so no autoscrolling is necessary
135 QWidget
* viewport
= m_itemView
->viewport();
136 const QPoint pos
= viewport
->mapFromGlobal(QCursor::pos());
137 if (verticalScrolling
) {
138 m_verticalScrollInc
= calculateScrollIncrement(pos
.y(), viewport
->height());
140 if (horizontalScrolling
) {
141 m_horizontalScrollInc
= calculateScrollIncrement(pos
.x(), viewport
->width());
144 if (m_timer
->isActive()) {
145 if ((m_horizontalScrollInc
== 0) && (m_verticalScrollInc
== 0)) {
148 } else if ((m_horizontalScrollInc
!= 0) || (m_verticalScrollInc
!= 0)) {
153 void DolphinViewAutoScroller::stopAutoScroll()
156 m_horizontalScrollInc
= 0;
157 m_verticalScrollInc
= 0;
160 int DolphinViewAutoScroller::calculateScrollIncrement(int cursorPos
, int rangeSize
) const
164 const int minSpeed
= 4;
165 const int maxSpeed
= 768;
166 const int speedLimiter
= 48;
167 const int autoScrollBorder
= 64;
169 if (cursorPos
< autoScrollBorder
) {
170 inc
= -minSpeed
+ fabs(cursorPos
- autoScrollBorder
) * (cursorPos
- autoScrollBorder
) / speedLimiter
;
171 if (inc
< -maxSpeed
) {
174 } else if (cursorPos
> rangeSize
- autoScrollBorder
) {
175 inc
= minSpeed
+ fabs(cursorPos
- rangeSize
+ autoScrollBorder
) * (cursorPos
- rangeSize
+ autoScrollBorder
) / speedLimiter
;
176 if (inc
> maxSpeed
) {
184 #include "dolphinviewautoscroller.moc"