]>
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);
39 m_itemView
->installEventFilter(this);
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()));
47 DolphinViewAutoScroller::~DolphinViewAutoScroller()
51 bool DolphinViewAutoScroller::eventFilter(QObject
* watched
, QEvent
* event
)
53 if (watched
== m_itemView
->viewport()) {
54 switch (event
->type()) {
55 case QEvent::MouseButtonPress
:
56 if (static_cast<QMouseEvent
*>(event
)->button() == Qt::LeftButton
) {
57 m_rubberBandSelection
= true;
61 case QEvent::MouseMove
:
62 if (m_rubberBandSelection
) {
67 case QEvent::MouseButtonRelease
:
68 m_rubberBandSelection
= false;
72 case QEvent::DragEnter
:
73 case QEvent::DragMove
:
74 m_rubberBandSelection
= false;
79 case QEvent::DragLeave
:
80 m_rubberBandSelection
= false;
87 } else if ((watched
== m_itemView
) && (event
->type() == QEvent::KeyPress
)) {
88 const int key
= static_cast<QKeyEvent
*>(event
)->key();
89 const bool arrowKeyPressed
= (key
== Qt::Key_Up
) || (key
== Qt::Key_Down
) ||
90 (key
== Qt::Key_Left
) || (key
== Qt::Key_Right
);
91 if (arrowKeyPressed
) {
92 QMetaObject::invokeMethod(this, "scrollToCurrentIndex", Qt::QueuedConnection
);
97 return QObject::eventFilter(watched
, event
);
100 void DolphinViewAutoScroller::scrollViewport()
102 QScrollBar
* verticalScrollBar
= m_itemView
->verticalScrollBar();
103 if (verticalScrollBar
!= 0) {
104 const int value
= verticalScrollBar
->value();
105 verticalScrollBar
->setValue(value
+ m_scrollInc
);
108 QScrollBar
* horizontalScrollBar
= m_itemView
->horizontalScrollBar();
109 if (horizontalScrollBar
!= 0) {
110 const int value
= horizontalScrollBar
->value();
111 horizontalScrollBar
->setValue(value
+ m_scrollInc
);
115 if (m_rubberBandSelection
) {
116 // The scrolling does not lead to an update of the rubberband
117 // selection. Fake a mouse move event to let the QAbstractItemView
118 // update the rubberband.
119 QWidget
* viewport
= m_itemView
->viewport();
120 const QPoint pos
= viewport
->mapFromGlobal(QCursor::pos());
121 QMouseEvent
event(QEvent::MouseMove
, pos
, Qt::LeftButton
, Qt::LeftButton
, Qt::NoModifier
);
122 QCoreApplication::sendEvent(viewport
, &event
);
126 void DolphinViewAutoScroller::scrollToCurrentIndex()
128 const QModelIndex index
= m_itemView
->currentIndex();
129 m_itemView
->scrollTo(index
);
132 void DolphinViewAutoScroller::triggerAutoScroll()
134 const bool verticalScrolling
= (m_itemView
->verticalScrollBar() != 0) &&
135 m_itemView
->verticalScrollBar()->isVisible();
136 const bool horizontalScrolling
= (m_itemView
->horizontalScrollBar() != 0) &&
137 m_itemView
->horizontalScrollBar()->isVisible();
138 if (!verticalScrolling
&& !horizontalScrolling
) {
139 // no scrollbars are shown at all, so no autoscrolling is necessary
143 QWidget
* viewport
= m_itemView
->viewport();
144 const QPoint pos
= viewport
->mapFromGlobal(QCursor::pos());
145 if (verticalScrolling
) {
146 calculateScrollIncrement(pos
.y(), viewport
->height());
148 if (horizontalScrolling
) {
149 calculateScrollIncrement(pos
.x(), viewport
->width());
152 if (m_timer
->isActive()) {
153 if (m_scrollInc
== 0) {
156 } else if (m_scrollInc
!= 0) {
161 void DolphinViewAutoScroller::stopAutoScroll()
167 void DolphinViewAutoScroller::calculateScrollIncrement(int cursorPos
, int rangeSize
)
169 const int minSpeed
= 2;
170 const int maxSpeed
= 32;
171 const int speedLimiter
= 8;
172 const int autoScrollBorder
= 32;
174 if (cursorPos
< autoScrollBorder
) {
175 m_scrollInc
= -minSpeed
+ (cursorPos
- autoScrollBorder
) / speedLimiter
;
176 if (m_scrollInc
< -maxSpeed
) {
177 m_scrollInc
= -maxSpeed
;
179 } else if (cursorPos
> rangeSize
- autoScrollBorder
) {
180 m_scrollInc
= minSpeed
+ (cursorPos
- rangeSize
+ autoScrollBorder
) / speedLimiter
;
181 if (m_scrollInc
> maxSpeed
) {
182 m_scrollInc
= maxSpeed
;
189 #include "dolphinviewautoscroller.moc"