From a74b9ce11cea45f3ad62a0e16ede8576fb12425d Mon Sep 17 00:00:00 2001 From: krepa098 Date: Sun, 4 Jan 2015 15:18:23 +0100 Subject: [PATCH] ChatLog: Autoscroll while selecting --- src/chatlog/chatlog.cpp | 35 +++++++++++++++++++++++++++++++++++ src/chatlog/chatlog.h | 12 ++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/chatlog/chatlog.cpp b/src/chatlog/chatlog.cpp index 93ff2a1fc..93a717f44 100644 --- a/src/chatlog/chatlog.cpp +++ b/src/chatlog/chatlog.cpp @@ -23,6 +23,7 @@ #include #include #include +#include template T clamp(T x, T min, T max) @@ -62,6 +63,12 @@ ChatLog::ChatLog(QWidget* parent) { copySelectedText(); }); + + selectionTimer = new QTimer(this); + selectionTimer->setInterval(1000/60); + selectionTimer->setSingleShot(false); + selectionTimer->start(); + connect(selectionTimer, &QTimer::timeout, this, &ChatLog::onSelectionTimerTimeout); } ChatLog::~ChatLog() @@ -243,6 +250,8 @@ void ChatLog::mouseReleaseEvent(QMouseEvent* ev) emit customContextMenuRequested(ev->pos()); } + + selectionScrollDir = NoDirection; } void ChatLog::mouseMoveEvent(QMouseEvent* ev) @@ -253,6 +262,15 @@ void ChatLog::mouseMoveEvent(QMouseEvent* ev) if(ev->buttons() & Qt::LeftButton) { + //autoscroll + if(ev->pos().y() < 0) + selectionScrollDir = Up; + else if(ev->pos().y() > height()) + selectionScrollDir = Down; + else + selectionScrollDir = NoDirection; + + //select if(selectionMode == None && (clickPos - ev->pos()).manhattanLength() > QApplication::startDragDistance()) { QPointF sceneClickPos = mapToScene(clickPos.toPoint()); @@ -579,3 +597,20 @@ void ChatLog::updateMultiSelectionRect() selGraphItem->hide(); } } + +void ChatLog::onSelectionTimerTimeout() +{ + const int scrollSpeed = 10; + + switch(selectionScrollDir) + { + case Up: + verticalScrollBar()->setValue(verticalScrollBar()->value() - scrollSpeed); + break; + case Down: + verticalScrollBar()->setValue(verticalScrollBar()->value() + scrollSpeed); + break; + default: + break; + } +} diff --git a/src/chatlog/chatlog.h b/src/chatlog/chatlog.h index f2f293c7b..e6e89f595 100644 --- a/src/chatlog/chatlog.h +++ b/src/chatlog/chatlog.h @@ -26,6 +26,7 @@ class QGraphicsScene; class QGraphicsRectItem; +class QTimer; class ChatLineContent; class ToxFile; @@ -81,6 +82,9 @@ protected: void updateMultiSelectionRect(); +private slots: + void onSelectionTimerTimeout(); + private: enum SelectionMode { None, @@ -88,6 +92,12 @@ private: Multi, }; + enum AutoScrollDirection { + NoDirection, + Up, + Down, + }; + QGraphicsScene* scene = nullptr; QList lines; QList visibleLines; @@ -105,6 +115,8 @@ private: QPointF clickPos; QPointF lastPos; QGraphicsRectItem* selGraphItem = nullptr; + QTimer* selectionTimer = nullptr; + AutoScrollDirection selectionScrollDir = NoDirection; // actions QAction* copyAction = nullptr;