Skip to content
This repository has been archived by the owner on Oct 17, 2019. It is now read-only.

Commit

Permalink
Don't clear selection when a context menu is requested
Browse files Browse the repository at this point in the history
  • Loading branch information
mujx committed Sep 26, 2018
1 parent c64a1bf commit bbf37bf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/ui/TextLabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@

#include "Utils.h"

bool
ContextMenuFilter::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress) {
emit contextMenuIsOpening();
return true;
}

return QObject::eventFilter(obj, event);
}

TextLabel::TextLabel(QWidget *parent)
: TextLabel(QString(), parent)
{}
Expand Down Expand Up @@ -39,13 +50,25 @@ TextLabel::TextLabel(const QString &text, QWidget *parent)
setFixedHeight(0);

connect(this, &TextLabel::linkActivated, this, &TextLabel::handleLinkActivation);

auto filter = new ContextMenuFilter(this);
installEventFilter(filter);
connect(filter, &ContextMenuFilter::contextMenuIsOpening, this, [this]() {
contextMenuRequested_ = true;
});
}

void
TextLabel::focusOutEvent(QFocusEvent *e)
{
QTextBrowser::focusOutEvent(e);

// We keep the selection available for the context menu.
if (contextMenuRequested_) {
contextMenuRequested_ = false;
return;
}

QTextCursor cursor = textCursor();
cursor.clearSelection();
setTextCursor(cursor);
Expand Down
17 changes: 17 additions & 0 deletions src/ui/TextLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ class QMouseEvent;
class QFocusEvent;
class QWheelEvent;

class ContextMenuFilter : public QObject
{
Q_OBJECT

public:
explicit ContextMenuFilter(QWidget *parent)
: QObject(parent)
{}

signals:
void contextMenuIsOpening();

protected:
bool eventFilter(QObject *obj, QEvent *event);
};

class TextLabel : public QTextBrowser
{
Q_OBJECT
Expand All @@ -35,4 +51,5 @@ private slots:

private:
QString link_;
bool contextMenuRequested_ = false;
};

0 comments on commit bbf37bf

Please sign in to comment.