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