Documentation
[quassel.git] / src / uisupport / treeviewtouch.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-2015 by the Quassel Project                        *
3 *   devel@quassel-irc.org                                                 *
4 *                                                                         *
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.                                           *
9 *                                                                         *
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.                          *
14 *                                                                         *
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 ***************************************************************************/
20 #include "treeviewtouch.h"
21
22 #include <QtCore>
23 #include <QTouchEvent>
24 #include <QScrollBar>
25
26 TreeViewTouch::TreeViewTouch(QWidget *parent)
27     : QTreeView(parent)
28 {
29     setAttribute(Qt::WA_AcceptTouchEvents);
30 }
31
32 bool TreeViewTouch::event(QEvent *event) {
33     if (event->type() == QEvent::TouchBegin) {
34         // Register that we may be scrolling, set the scroll mode to scroll-per-pixel
35         // and accept the event (return true) so that we will receive TouchUpdate and TouchEnd/TouchCancel
36         _touchScrollInProgress = true;
37         setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
38         return true;
39     }
40
41     if (event->type() == QEvent::TouchUpdate && _touchScrollInProgress) {
42         QTouchEvent::TouchPoint p = ((QTouchEvent*)event)->touchPoints().at(0);
43         if (!_firstTouchUpdateHappened) {
44             // After the first movement of a Touch-Point, calculate the distance in both axis
45             // and if the point moved more horizontally abort scroll.
46             double dx = abs(p.lastPos().x() - p.pos().x());
47             double dy = abs(p.lastPos().y() - p.pos().y());
48             if (dx > dy) {
49                 _touchScrollInProgress = false;
50             }
51             _firstTouchUpdateHappened = true;
52         }
53         // Apply touch movement to scrollbar
54         verticalScrollBar()->setValue(verticalScrollBar()->value() - (p.pos().y() - p.lastPos().y()));
55         return true;
56     }
57
58 #if QT_VERSION >= 0x050000
59     if (event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) {
60 #else
61     if (event->type() == QEvent::TouchEnd) {
62 #endif
63         // End scroll and reset variables
64         _touchScrollInProgress = false;
65         _firstTouchUpdateHappened = false;
66         return true;
67     }
68
69     return QTreeView::event(event);
70 }
71
72 void TreeViewTouch::mousePressEvent(QMouseEvent * event) {
73     if (!_touchScrollInProgress)
74         QTreeView::mousePressEvent(event);
75 }
76
77 void TreeViewTouch::mouseMoveEvent(QMouseEvent * event) {
78     if (!_touchScrollInProgress)
79         QTreeView::mouseMoveEvent(event);
80 };