1 /***************************************************************************
2 * Copyright (C) 2005-2016 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "treeviewtouch.h"
25 #include <QTouchEvent>
28 TreeViewTouch::TreeViewTouch(QWidget *parent)
31 setAttribute(Qt::WA_AcceptTouchEvents);
35 bool TreeViewTouch::event(QEvent *event) {
36 #if QT_VERSION >= 0x050000
37 if (event->type() == QEvent::TouchBegin && ((QTouchEvent*)event)->device()->type()==QTouchDevice::TouchScreen) {
39 if (event->type() == QEvent::TouchBegin && ((QTouchEvent*)event)->deviceType()==QTouchEvent::TouchScreen) {
41 // Register that we may be scrolling, set the scroll mode to scroll-per-pixel
42 // and accept the event (return true) so that we will receive TouchUpdate and TouchEnd/TouchCancel
43 _touchScrollInProgress = true;
44 setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
48 if (event->type() == QEvent::TouchUpdate && _touchScrollInProgress) {
49 QTouchEvent::TouchPoint p = ((QTouchEvent*)event)->touchPoints().at(0);
50 if (!_firstTouchUpdateHappened) {
51 // After the first movement of a Touch-Point, calculate the distance in both axis
52 // and if the point moved more horizontally abort scroll.
53 double dx = qAbs(p.lastPos().x() - p.pos().x());
54 double dy = qAbs(p.lastPos().y() - p.pos().y());
56 _touchScrollInProgress = false;
58 _firstTouchUpdateHappened = true;
60 // Apply touch movement to scrollbar
61 verticalScrollBar()->setValue(verticalScrollBar()->value() - (p.pos().y() - p.lastPos().y()));
65 #if QT_VERSION >= 0x050000
66 if (event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) {
68 if (event->type() == QEvent::TouchEnd) {
70 // End scroll and reset variables
71 _touchScrollInProgress = false;
72 _firstTouchUpdateHappened = false;
76 return QTreeView::event(event);
80 void TreeViewTouch::mousePressEvent(QMouseEvent *event) {
81 if (!_touchScrollInProgress)
82 QTreeView::mousePressEvent(event);
86 void TreeViewTouch::mouseMoveEvent(QMouseEvent *event) {
87 if (!_touchScrollInProgress)
88 QTreeView::mouseMoveEvent(event);