1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

ChatLog: Autoscroll while selecting

This commit is contained in:
krepa098 2015-01-04 15:18:23 +01:00
parent 64024c77a6
commit a74b9ce11c
2 changed files with 47 additions and 0 deletions

View File

@ -23,6 +23,7 @@
#include <QApplication>
#include <QClipboard>
#include <QAction>
#include <QTimer>
template<class T>
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;
}
}

View File

@ -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<ChatLine::Ptr> lines;
QList<ChatLine::Ptr> visibleLines;
@ -105,6 +115,8 @@ private:
QPointF clickPos;
QPointF lastPos;
QGraphicsRectItem* selGraphItem = nullptr;
QTimer* selectionTimer = nullptr;
AutoScrollDirection selectionScrollDir = NoDirection;
// actions
QAction* copyAction = nullptr;