From d57c91811b8f989bcaa4d5a238c65e9ffcc3b1d4 Mon Sep 17 00:00:00 2001 From: romibi Date: Sat, 11 Jun 2016 00:42:26 +0200 Subject: [PATCH 1/1] Documentation and some fixed indentations --- src/qtui/chatview.cpp | 35 ++++++++++------- src/qtui/chatview.h | 2 +- src/uisupport/bufferview.cpp | 9 +++-- src/uisupport/nickview.cpp | 2 +- src/uisupport/treeviewtouch.cpp | 67 ++++++++++++++++++--------------- src/uisupport/treeviewtouch.h | 42 +++++++++++++++++---- 6 files changed, 98 insertions(+), 59 deletions(-) diff --git a/src/qtui/chatview.cpp b/src/qtui/chatview.cpp index 5e76a336..c6e62095 100644 --- a/src/qtui/chatview.cpp +++ b/src/qtui/chatview.cpp @@ -110,9 +110,12 @@ bool ChatView::event(QEvent *event) } if (event->type() == QEvent::TouchBegin) { + // Enable scrolling by draging, disable selecting/clicking content setDragMode(QGraphicsView::ScrollHandDrag); setInteractive(false); - if (verticalScrollBar()->isVisible()) return true; //if scrollbar is not visible we need to request backlog below + // if scrollbar is not visible we need to request backlog below else we need to accept + // the event now (return true) so that we will receive TouchUpdate and TouchEnd/TouchCancel + if (verticalScrollBar()->isVisible()) return true; } #if QT_VERSION >= 0x050000 @@ -120,24 +123,28 @@ bool ChatView::event(QEvent *event) #else if (event->type() == QEvent::TouchEnd) { #endif + // End scroll and reset settings to default setDragMode(QGraphicsView::NoDrag); setInteractive(true); - _firstTouchUpdateHappened = false; + _firstTouchUpdateHappened = false; return true; } - if (event->type() == QEvent::TouchUpdate) { - if (!_firstTouchUpdateHappened) { - QTouchEvent::TouchPoint p = ((QTouchEvent*)event)->touchPoints().at(0); - double dx = abs (p.lastPos().x() - p.pos().x()); - double dy = abs (p.lastPos().y() - p.pos().y()); - if (dx > dy) { - setDragMode(QGraphicsView::NoDrag); - setInteractive(true); - } - _firstTouchUpdateHappened = true; - } - } + if (event->type() == QEvent::TouchUpdate) { + if (!_firstTouchUpdateHappened) { + // After the first movement of a Touch-Point, calculate the distance in both axis + // and if the point moved more horizontally abort scroll. + QTouchEvent::TouchPoint p = ((QTouchEvent*)event)->touchPoints().at(0); + double dx = abs (p.lastPos().x() - p.pos().x()); + double dy = abs (p.lastPos().y() - p.pos().y()); + if (dx > dy) { + setDragMode(QGraphicsView::NoDrag); + setInteractive(true); + } + _firstTouchUpdateHappened = true; + } + // Applying the movement happens automatically by the drag-mode + } if (event->type() == QEvent::Wheel || event->type() == QEvent::TouchBegin || event->type() == QEvent::TouchUpdate) { if (!verticalScrollBar()->isVisible()) { diff --git a/src/qtui/chatview.h b/src/qtui/chatview.h index e037e46d..d66ea7ce 100644 --- a/src/qtui/chatview.h +++ b/src/qtui/chatview.h @@ -117,7 +117,7 @@ private: int _scrollOffset; bool _invalidateFilter; QSet _linesWithCache; - bool _firstTouchUpdateHappened = false; + bool _firstTouchUpdateHappened = false; }; diff --git a/src/uisupport/bufferview.cpp b/src/uisupport/bufferview.cpp index e01ca09a..da7d0371 100644 --- a/src/uisupport/bufferview.cpp +++ b/src/uisupport/bufferview.cpp @@ -210,13 +210,14 @@ void BufferView::joinChannel(const QModelIndex &index) Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName())); } + void BufferView::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) { event->accept(); removeSelectedBuffers(); } - TreeViewTouch::keyPressEvent(event); + TreeViewTouch::keyPressEvent(event); } @@ -287,7 +288,7 @@ void BufferView::removeSelectedBuffers(bool permanently) void BufferView::rowsInserted(const QModelIndex &parent, int start, int end) { - TreeViewTouch::rowsInserted(parent, start, end); + TreeViewTouch::rowsInserted(parent, start, end); // ensure that newly inserted network nodes are expanded per default if (parent.data(NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType) @@ -375,11 +376,11 @@ void BufferView::setExpandedState(const QModelIndex &networkIdx) #if QT_VERSION < 0x050000 void BufferView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) { - TreeViewTouch::dataChanged(topLeft, bottomRight); + TreeViewTouch::dataChanged(topLeft, bottomRight); #else void BufferView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles) { - TreeViewTouch::dataChanged(topLeft, bottomRight, roles); + TreeViewTouch::dataChanged(topLeft, bottomRight, roles); #endif // determine how many items have been changed and if any of them is a networkitem diff --git a/src/uisupport/nickview.cpp b/src/uisupport/nickview.cpp index 75e4d73d..31359c0e 100644 --- a/src/uisupport/nickview.cpp +++ b/src/uisupport/nickview.cpp @@ -149,4 +149,4 @@ void NickView::startQuery(const QModelIndex &index) return; Client::bufferModel()->switchToOrStartQuery(networkId, ircUser->nick()); -} \ No newline at end of file +} diff --git a/src/uisupport/treeviewtouch.cpp b/src/uisupport/treeviewtouch.cpp index 6b795f51..13db59db 100644 --- a/src/uisupport/treeviewtouch.cpp +++ b/src/uisupport/treeviewtouch.cpp @@ -24,52 +24,57 @@ #include TreeViewTouch::TreeViewTouch(QWidget *parent) - : QTreeView(parent) + : QTreeView(parent) { - setAttribute(Qt::WA_AcceptTouchEvents); + setAttribute(Qt::WA_AcceptTouchEvents); } - bool TreeViewTouch::event(QEvent *event) { - if (event->type() == QEvent::TouchBegin) { - _touchScrollInProgress = true; - setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); - return true; - } + if (event->type() == QEvent::TouchBegin) { + // Register that we may be scrolling, set the scroll mode to scroll-per-pixel + // and accept the event (return true) so that we will receive TouchUpdate and TouchEnd/TouchCancel + _touchScrollInProgress = true; + setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); + return true; + } - if (event->type() == QEvent::TouchUpdate && _touchScrollInProgress) { - QTouchEvent::TouchPoint p = ((QTouchEvent*)event)->touchPoints().at(0); - if (!_firstTouchUpdateHappened) { - double dx = abs(p.lastPos().x() - p.pos().x()); - double dy = abs(p.lastPos().y() - p.pos().y()); - if (dx > dy) { - _touchScrollInProgress = false; - } - _firstTouchUpdateHappened = true; - } - verticalScrollBar()->setValue(verticalScrollBar()->value() - (p.pos().y() - p.lastPos().y())); - return true; - } + if (event->type() == QEvent::TouchUpdate && _touchScrollInProgress) { + QTouchEvent::TouchPoint p = ((QTouchEvent*)event)->touchPoints().at(0); + if (!_firstTouchUpdateHappened) { + // After the first movement of a Touch-Point, calculate the distance in both axis + // and if the point moved more horizontally abort scroll. + double dx = abs(p.lastPos().x() - p.pos().x()); + double dy = abs(p.lastPos().y() - p.pos().y()); + if (dx > dy) { + _touchScrollInProgress = false; + } + _firstTouchUpdateHappened = true; + } + // Apply touch movement to scrollbar + verticalScrollBar()->setValue(verticalScrollBar()->value() - (p.pos().y() - p.lastPos().y())); + return true; + } #if QT_VERSION >= 0x050000 - if (event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) { + if (event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) { #else if (event->type() == QEvent::TouchEnd) { #endif - _touchScrollInProgress = false; - _firstTouchUpdateHappened = false; - return true; - } + // End scroll and reset variables + _touchScrollInProgress = false; + _firstTouchUpdateHappened = false; + return true; + } - return QTreeView::event(event); + return QTreeView::event(event); } void TreeViewTouch::mousePressEvent(QMouseEvent * event) { - if (!_touchScrollInProgress) - QTreeView::mousePressEvent(event); + if (!_touchScrollInProgress) + QTreeView::mousePressEvent(event); } void TreeViewTouch::mouseMoveEvent(QMouseEvent * event) { - if (!_touchScrollInProgress) - QTreeView::mouseMoveEvent(event); + if (!_touchScrollInProgress) + QTreeView::mouseMoveEvent(event); }; diff --git a/src/uisupport/treeviewtouch.h b/src/uisupport/treeviewtouch.h index efbdf4ac..d7f9f1bc 100644 --- a/src/uisupport/treeviewtouch.h +++ b/src/uisupport/treeviewtouch.h @@ -22,22 +22,48 @@ #define TREEVIEWTOUCH_H_ #include +/** +* This class handles Touch Events for TreeViews +*/ class TreeViewTouch : - public QTreeView + public QTreeView { - Q_OBJECT + Q_OBJECT public: - explicit TreeViewTouch(QWidget *parent = 0); + explicit TreeViewTouch(QWidget *parent = 0); protected: - virtual bool event(QEvent *event); - virtual void mouseMoveEvent(QMouseEvent *event); - virtual void mousePressEvent(QMouseEvent *event); + + /** + * Handles Events + * + * @param[in,out] an event + * @returns true if event got handled, false if event got ignored + */ + virtual bool event(QEvent *event); + + /** + * Handles Mouse Move Events + * + * Suppresses Events during Touch-Scroll + * + * @param[in,out] An Event + */ + virtual void mouseMoveEvent(QMouseEvent *event); + + /** + * Handles Mouse Press Events + * + * Suppresses Events during Touch-Scroll + * + * @param[in,out] An Event + */ + virtual void mousePressEvent(QMouseEvent *event); private: - bool _touchScrollInProgress = false; - bool _firstTouchUpdateHappened = false; + bool _touchScrollInProgress = false; + bool _firstTouchUpdateHappened = false; }; #endif \ No newline at end of file -- 2.20.1