From 4ec399f169e9308cd5da194b6fa2104578c39e45 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 6 Sep 2022 11:20:23 +0400 Subject: [PATCH] Simplify labels with custom click handlers. --- ui/widgets/labels.cpp | 24 ++++++++++++++++++++++++ ui/widgets/labels.h | 2 ++ 2 files changed, 26 insertions(+) diff --git a/ui/widgets/labels.cpp b/ui/widgets/labels.cpp index 65bd7e5..034c75e 100644 --- a/ui/widgets/labels.cpp +++ b/ui/widgets/labels.cpp @@ -378,6 +378,30 @@ void FlatLabel::setClickHandlerFilter(ClickHandlerFilter &&filter) { _clickHandlerFilter = std::move(filter); } +void FlatLabel::overrideLinkClickHandler(Fn handler) { + setClickHandlerFilter([=]( + const ClickHandlerPtr &link, + Qt::MouseButton button) { + if (button != Qt::LeftButton) { + return true; + } + handler(); + return false; + }); +} + +void FlatLabel::overrideLinkClickHandler(Fn handler) { + setClickHandlerFilter([=]( + const ClickHandlerPtr &link, + Qt::MouseButton button) { + if (button != Qt::LeftButton) { + return true; + } + handler(link->dragText()); + return false; + }); +} + void FlatLabel::mouseMoveEvent(QMouseEvent *e) { _lastMousePos = e->globalPos(); dragActionUpdate(); diff --git a/ui/widgets/labels.h b/ui/widgets/labels.h index 656e6b2..b7ab34d 100644 --- a/ui/widgets/labels.h +++ b/ui/widgets/labels.h @@ -139,6 +139,8 @@ public: using ClickHandlerFilter = Fn; void setClickHandlerFilter(ClickHandlerFilter &&filter); + void overrideLinkClickHandler(Fn handler); + void overrideLinkClickHandler(Fn handler); // ClickHandlerHost interface void clickHandlerActiveChanged(const ClickHandlerPtr &action, bool active) override;