]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewautoscroller.cpp
during drag operations no rubberband selection can be active
[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 #include <kdebug.h>
51 bool DolphinViewAutoScroller::eventFilter(QObject* watched, QEvent* event)
52 {
53 if (watched == m_itemView->viewport()) {
54 switch (event->type()) {
55 case QEvent::MouseButtonPress:
56 m_rubberBandSelection = true;
57 break;
58
59 case QEvent::MouseMove:
60 if (m_rubberBandSelection) {
61 triggerAutoScroll();
62 }
63 break;
64
65 case QEvent::MouseButtonRelease:
66 m_rubberBandSelection = false;
67 stopAutoScroll();
68 break;
69
70 case QEvent::DragEnter:
71 case QEvent::DragMove:
72 m_rubberBandSelection = false;
73 triggerAutoScroll();
74 break;
75
76 case QEvent::DragLeave:
77 m_rubberBandSelection = false;
78 stopAutoScroll();
79 break;
80
81 default:
82 break;
83 }
84 }
85
86 return QObject::eventFilter(watched, event);
87 }
88
89 void DolphinViewAutoScroller::scrollViewport()
90 {
91 // TODO: implement horizontal scrolling
92 QScrollBar* verticalScrollBar = m_itemView->verticalScrollBar();
93 if (verticalScrollBar != 0) {
94 const int value = verticalScrollBar->value();
95 verticalScrollBar->setValue(value + m_scrollInc);
96
97 if (m_rubberBandSelection) {
98 // The scrolling does not lead to an update of the rubberband
99 // selection. Fake a mouse move event to let the QAbstractItemView
100 // update the rubberband.
101 QWidget* viewport = m_itemView->viewport();
102 const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
103 QMouseEvent event(QEvent::MouseMove, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
104 QCoreApplication::sendEvent(viewport, &event);
105 }
106 }
107 }
108
109 void DolphinViewAutoScroller::triggerAutoScroll()
110 {
111 // TODO: implement horizontal scrolling
112
113 const int startSpeed = 2;
114 const int speedLimiter = 8;
115 const int scrollIncMax = 32;
116
117 const int autoScrollBorder = 32;
118
119 QWidget* viewport = m_itemView->viewport();
120 const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
121 if (pos.y() < autoScrollBorder) {
122 // scroll up
123 m_scrollInc = -startSpeed + (pos.y() - autoScrollBorder) / speedLimiter;
124 if (m_scrollInc < -scrollIncMax) {
125 m_scrollInc = -scrollIncMax;
126 }
127 } else if (pos.y() > viewport->height() - autoScrollBorder) {
128 // scroll down
129 m_scrollInc = startSpeed + (pos.y() - viewport->height() + autoScrollBorder) / speedLimiter;
130 if (m_scrollInc > scrollIncMax) {
131 m_scrollInc = scrollIncMax;
132 }
133 } else {
134 // no scrolling
135 m_scrollInc = 0;
136 }
137
138 if (m_timer->isActive()) {
139 if (m_scrollInc == 0) {
140 m_timer->stop();
141 }
142 } else if (m_scrollInc != 0) {
143 m_timer->start();
144 }
145 }
146
147 void DolphinViewAutoScroller::stopAutoScroll()
148 {
149 m_timer->stop();
150 m_scrollInc = 0;
151 }
152
153 #include "dolphinviewautoscroller.moc"