Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat(chatform): mark message with triple click
Browse files Browse the repository at this point in the history
This is an implementation of proposal #4003.
  • Loading branch information
ezavod committed Mar 28, 2017
1 parent 2e4ac3c commit 3acbc14
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/chatlog/chatlinecontent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ void ChatLineContent::selectionDoubleClick(QPointF)
{
}

void ChatLineContent::selectionTripleClick(QPointF)
{
}

void ChatLineContent::selectionFocusChanged(bool)
{
}
Expand Down
1 change: 1 addition & 0 deletions src/chatlog/chatlinecontent.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ChatLineContent : public QObject, public QGraphicsItem
virtual void selectionStarted(QPointF scenePos);
virtual void selectionCleared();
virtual void selectionDoubleClick(QPointF scenePos);
virtual void selectionTripleClick(QPointF scenePos);
virtual void selectionFocusChanged(bool focusIn);
virtual bool isOverSelection(QPointF scenePos) const;
virtual QString getSelectedText() const;
Expand Down
52 changes: 52 additions & 0 deletions src/chatlog/chatlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ ChatLog::ChatLog(QWidget* parent)
workerTimer->setInterval(5);
connect(workerTimer, &QTimer::timeout, this, &ChatLog::onWorkerTimeout);

// This timer is used to detect multiple clicks
multiClickTimer = new QTimer(this);
multiClickTimer->setSingleShot(true);
multiClickTimer->setInterval(QApplication::doubleClickInterval());
connect(multiClickTimer, &QTimer::timeout, this, &ChatLog::onMultiClickTimeout);

// selection
connect(this, &ChatLog::selectionChanged, this, [this]() {
copyAction->setEnabled(hasTextToBeCopied());
Expand Down Expand Up @@ -190,13 +196,22 @@ void ChatLog::mousePressEvent(QMouseEvent* ev)
clickPos = ev->pos();
clearSelection();
}

// Counts only single clicks and first click of doule click
clickCount++;
lastClickPos = ev->pos();

// Triggers on odd click counts
handleMultiClickEvent();
}

void ChatLog::mouseReleaseEvent(QMouseEvent* ev)
{
QGraphicsView::mouseReleaseEvent(ev);

selectionScrollDir = NoDirection;

multiClickTimer->start();
}

void ChatLog::mouseMoveEvent(QMouseEvent* ev)
Expand Down Expand Up @@ -461,6 +476,13 @@ void ChatLog::mouseDoubleClickEvent(QMouseEvent* ev)

emit selectionChanged();
}

// Counts the second click of double click
clickCount++;
lastClickPos = ev->pos();

// Triggers on even click counts
handleMultiClickEvent();
}

QString ChatLog::getSelectedText() const
Expand Down Expand Up @@ -796,6 +818,36 @@ void ChatLog::onWorkerTimeout()
}
}

void ChatLog::onMultiClickTimeout()
{
clickCount = 0;
}

void ChatLog::handleMultiClickEvent()
{
// Ignore single or double clicks
if (clickCount < 2)
return;

switch (clickCount) {
case 3:
QPointF scenePos = mapToScene(lastClickPos);
ChatLineContent* content = getContentFromPos(scenePos);

if (content) {
content->selectionTripleClick(scenePos);
selClickedCol = content->getColumn();
selClickedRow = content->getRow();
selFirstRow = content->getRow();
selLastRow = content->getRow();
selectionMode = Precise;

emit selectionChanged();
}
break;
}
}

void ChatLog::showEvent(QShowEvent*)
{
// Empty.
Expand Down
5 changes: 5 additions & 0 deletions src/chatlog/chatlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public slots:
private slots:
void onSelectionTimerTimeout();
void onWorkerTimeout();
void onMultiClickTimeout();

protected:
QRectF calculateSceneRect() const;
Expand Down Expand Up @@ -111,6 +112,7 @@ private slots:
private:
void retranslateUi();
bool isActiveFileTransfer(ChatLine::Ptr l);
void handleMultiClickEvent();

private:
enum SelectionMode
Expand Down Expand Up @@ -147,7 +149,10 @@ private slots:
QGraphicsRectItem* selGraphItem = nullptr;
QTimer* selectionTimer = nullptr;
QTimer* workerTimer = nullptr;
QTimer* multiClickTimer = nullptr;
AutoScrollDirection selectionScrollDir = NoDirection;
int clickCount = 0;
QPoint lastClickPos;

// worker vars
int workerLastIndex = 0;
Expand Down
24 changes: 24 additions & 0 deletions src/chatlog/content/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@ void Text::selectionDoubleClick(QPointF scenePos)
update();
}

void Text::selectionTripleClick(QPointF scenePos)
{
if (!doc)
return;

int cur = cursorFromPos(scenePos);

if (cur >= 0) {
QTextCursor cursor(doc);
cursor.setPosition(cur);
cursor.select(QTextCursor::BlockUnderCursor);

selectionAnchor = cursor.selectionStart();
selectionEnd = cursor.selectionEnd();

if (cursor.block().isValid() && cursor.block().blockNumber() != 0)
selectionAnchor++;

selectedText = extractSanitizedText(getSelectionStart(), getSelectionEnd());
}

update();
}

void Text::selectionFocusChanged(bool focusIn)
{
selectionHasFocus = focusIn;
Expand Down
1 change: 1 addition & 0 deletions src/chatlog/content/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Text : public ChatLineContent
virtual void selectionStarted(QPointF scenePos) final;
virtual void selectionCleared() final;
virtual void selectionDoubleClick(QPointF scenePos) final;
virtual void selectionTripleClick(QPointF scenePos) final;
virtual void selectionFocusChanged(bool focusIn) final;
virtual bool isOverSelection(QPointF scenePos) const final;
virtual QString getSelectedText() const final;
Expand Down

0 comments on commit 3acbc14

Please sign in to comment.