diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.cpp index 6a856027..05e9ad0f 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.cpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.cpp @@ -1,7 +1,5 @@ #include "SvgToolButton.hpp" -#include - namespace Bloom::Widgets { SvgToolButton::SvgToolButton(QWidget* parent): QToolButton(parent) { @@ -17,8 +15,20 @@ namespace Bloom::Widgets */ auto* menuWidget = qobject_cast(childEvent->child()); if (menuWidget != nullptr && menuWidget != this->menu()) { - this->setMenu(menuWidget); + if (this->contextMenuEnabled) { + this->contextMenu = menuWidget; + this->setContextMenuPolicy(Qt::ContextMenuPolicy::DefaultContextMenu); + + } else { + this->setMenu(menuWidget); + } } } } + + void SvgToolButton::contextMenuEvent(QContextMenuEvent* event) { + if (this->contextMenu != nullptr) { + this->contextMenu->exec(this->mapToGlobal(QPoint(0, this->height()))); + } + } } diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp index 49fe50fd..c3bd8a13 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include "SvgWidget.hpp" @@ -16,6 +18,7 @@ namespace Bloom::Widgets Q_PROPERTY(int buttonWidth READ getButtonWidth WRITE setButtonWidth DESIGNABLE true) Q_PROPERTY(int buttonHeight READ getButtonHeight WRITE setButtonHeight DESIGNABLE true) + Q_PROPERTY(bool contextMenuEnabled READ getContextMenuEnabled WRITE setContextMenuEnabled DESIGNABLE true) public: explicit SvgToolButton(QWidget* parent); @@ -56,6 +59,14 @@ namespace Bloom::Widgets return this->buttonHeight; } + void setContextMenuEnabled(bool enabled) { + this->contextMenuEnabled = enabled; + } + + [[nodiscard]] bool getContextMenuEnabled() const { + return this->contextMenuEnabled; + } + void startSpin() { this->svgWidget->startSpin(); } @@ -65,10 +76,14 @@ namespace Bloom::Widgets } protected: void childEvent(QChildEvent* childEvent) override; + void contextMenuEvent(QContextMenuEvent* event) override; private: SvgWidget* svgWidget = new SvgWidget(this); int buttonWidth = 0; int buttonHeight = 0; + bool contextMenuEnabled = false; + + QMenu* contextMenu = nullptr; }; }