]>
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 <QApplication>
26 #include <QMouseEvent>
31 DolphinViewAutoScroller::DolphinViewAutoScroller(QAbstractItemView
* parent
) :
33 m_rubberBandSelection(false),
35 m_horizontalScrollInc(0),
36 m_verticalScrollInc(0),
40 m_itemView
->setAutoScroll(false);
41 m_itemView
->viewport()->installEventFilter(this);
42 m_itemView
->installEventFilter(this);
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()));
50 DolphinViewAutoScroller::~DolphinViewAutoScroller()
54 bool DolphinViewAutoScroller::isActive() const
56 return m_timer
->isActive();
59 void DolphinViewAutoScroller::handleCurrentIndexChange(const QModelIndex
& current
,
60 const QModelIndex
& previous
)
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
);
71 bool DolphinViewAutoScroller::eventFilter(QObject
* watched
, QEvent
* event
)
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;
81 case QEvent::MouseMove
:
82 if (m_rubberBandSelection
) {
87 case QEvent::MouseButtonRelease
:
88 m_rubberBandSelection
= false;
92 case QEvent::DragEnter
:
93 case QEvent::DragMove
:
94 m_rubberBandSelection
= false;
99 case QEvent::DragLeave
:
100 m_rubberBandSelection
= false;
107 } else if (watched
== m_itemView
) {
108 switch (event
->type()) {
109 case QEvent::KeyPress
:
113 case QEvent::KeyRelease
:
114 m_keyPressed
= false;
122 return QObject::eventFilter(watched
, event
);
125 void DolphinViewAutoScroller::scrollViewport()
127 QScrollBar
* verticalScrollBar
= m_itemView
->verticalScrollBar();
128 if (verticalScrollBar
!= 0) {
129 const int value
= verticalScrollBar
->value();
130 verticalScrollBar
->setValue(value
+ m_verticalScrollInc
);
133 QScrollBar
* horizontalScrollBar
= m_itemView
->horizontalScrollBar();
134 if (horizontalScrollBar
!= 0) {
135 const int value
= horizontalScrollBar
->value();
136 horizontalScrollBar
->setValue(value
+ m_horizontalScrollInc
);
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
);
151 void DolphinViewAutoScroller::triggerAutoScroll()
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
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());
167 if (horizontalScrolling
) {
168 m_horizontalScrollInc
= calculateScrollIncrement(pos
.x(), viewport
->width());
171 if (m_timer
->isActive()) {
172 if ((m_horizontalScrollInc
== 0) && (m_verticalScrollInc
== 0)) {
175 } else if ((m_horizontalScrollInc
!= 0) || (m_verticalScrollInc
!= 0)) {
180 void DolphinViewAutoScroller::stopAutoScroll()
183 m_horizontalScrollInc
= 0;
184 m_verticalScrollInc
= 0;
187 int DolphinViewAutoScroller::calculateScrollIncrement(int cursorPos
, int rangeSize
) const
191 const int minSpeed
= 4;
192 const int maxSpeed
= 768;
193 const int speedLimiter
= 48;
194 const int autoScrollBorder
= 64;
196 if (cursorPos
< autoScrollBorder
) {
197 inc
= -minSpeed
+ qAbs(cursorPos
- autoScrollBorder
) * (cursorPos
- autoScrollBorder
) / speedLimiter
;
198 if (inc
< -maxSpeed
) {
201 } else if (cursorPos
> rangeSize
- autoScrollBorder
) {
202 inc
= minSpeed
+ qAbs(cursorPos
- rangeSize
+ autoScrollBorder
) * (cursorPos
- rangeSize
+ autoScrollBorder
) / speedLimiter
;
203 if (inc
> maxSpeed
) {
211 #include "dolphinviewautoscroller.moc"