]>
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>
30 DolphinViewAutoScroller::DolphinViewAutoScroller(QAbstractItemView
* parent
) :
32 m_rubberBandSelection(false),
37 m_itemView
->setAutoScroll(false);
38 m_itemView
->viewport()->installEventFilter(this);
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()));
46 DolphinViewAutoScroller::~DolphinViewAutoScroller()
50 bool DolphinViewAutoScroller::eventFilter(QObject
* watched
, QEvent
* event
)
52 if (watched
== m_itemView
->viewport()) {
53 switch (event
->type()) {
54 case QEvent::MouseButtonPress
:
55 m_rubberBandSelection
= true;
58 case QEvent::MouseMove
:
59 if (m_rubberBandSelection
) {
64 case QEvent::MouseButtonRelease
:
65 m_rubberBandSelection
= false;
69 case QEvent::DragEnter
:
70 case QEvent::DragMove
:
74 case QEvent::DragLeave
:
83 return QObject::eventFilter(watched
, event
);
86 void DolphinViewAutoScroller::scrollViewport()
88 // TODO: implement horizontal scrolling
89 QScrollBar
* verticalScrollBar
= m_itemView
->verticalScrollBar();
90 if (verticalScrollBar
!= 0) {
91 const int value
= verticalScrollBar
->value();
92 verticalScrollBar
->setValue(value
+ m_scrollInc
);
94 if (m_rubberBandSelection
) {
95 // The scrolling does not lead to an update of the rubberband
96 // selection. Fake a mouse move event to let the QAbstractItemView
97 // update the rubberband.
98 QWidget
* viewport
= m_itemView
->viewport();
99 const QPoint pos
= viewport
->mapFromGlobal(QCursor::pos());
100 QMouseEvent
event(QEvent::MouseMove
, pos
, Qt::LeftButton
, Qt::LeftButton
, Qt::NoModifier
);
101 QCoreApplication::sendEvent(viewport
, &event
);
106 void DolphinViewAutoScroller::triggerAutoScroll()
108 // TODO: implement horizontal scrolling
110 const int startSpeed
= 2;
111 const int speedLimiter
= 8;
112 const int scrollIncMax
= 32;
114 const int autoScrollBorder
= 32;
116 QWidget
* viewport
= m_itemView
->viewport();
117 const QPoint pos
= viewport
->mapFromGlobal(QCursor::pos());
118 if (pos
.y() < autoScrollBorder
) {
120 m_scrollInc
= -startSpeed
+ (pos
.y() - autoScrollBorder
) / speedLimiter
;
121 if (m_scrollInc
< -scrollIncMax
) {
122 m_scrollInc
= -scrollIncMax
;
124 } else if (pos
.y() > viewport
->height() - autoScrollBorder
) {
126 m_scrollInc
= startSpeed
+ (pos
.y() - viewport
->height() + autoScrollBorder
) / speedLimiter
;
127 if (m_scrollInc
> scrollIncMax
) {
128 m_scrollInc
= scrollIncMax
;
135 if (m_timer
->isActive()) {
136 if (m_scrollInc
== 0) {
139 } else if (m_scrollInc
!= 0) {
144 void DolphinViewAutoScroller::stopAutoScroll()
150 #include "dolphinviewautoscroller.moc"