From 7108899884577a22de22013abf65b643b9df6118 Mon Sep 17 00:00:00 2001 From: Nav Date: Sat, 4 Dec 2021 21:23:58 +0000 Subject: [PATCH] Automatically associate SvgToolButton widgets to child QMenu. This means we can add a QMenu to a tool button, via the .ui file, and have it be automatically associated. --- CMakeLists.txt | 2 +- .../InsightWindow/Widgets/SvgToolButton.cpp | 23 +++++++++++++++++++ .../InsightWindow/Widgets/SvgToolButton.hpp | 9 ++++---- 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f273ccf8..ddba2bab 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -139,7 +139,7 @@ add_executable(Bloom src/Insight/UserInterfaces/InsightWindow/Widgets/PanelWidget.cpp src/Insight/UserInterfaces/InsightWindow/Widgets/RotatableLabel.cpp src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp - src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp + src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.cpp src/Insight/UserInterfaces/InsightWindow/Widgets/ClickableWidget.cpp src/Insight/UserInterfaces/InsightWindow/Widgets/ExpandingHeightScrollAreaWidget.hpp diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.cpp new file mode 100644 index 00000000..14ccca29 --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.cpp @@ -0,0 +1,23 @@ +#include "SvgToolButton.hpp" + +#include + +using namespace Bloom::Widgets; + +SvgToolButton::SvgToolButton(QWidget* parent): QToolButton(parent) { + this->setButtonWidth(10); + this->setButtonHeight(10); +} + +void SvgToolButton::childEvent(QChildEvent* childEvent) { + if ((childEvent->added() || childEvent->polished()) && childEvent->child()->isWidgetType()) { + /* + * If a menu widget has been added as a child to this SvgToolButton, associate the menu with the button + * via QToolButton::setMenu(). + */ + auto* menuWidget = qobject_cast(childEvent->child()); + if (menuWidget != nullptr && menuWidget != this->menu()) { + this->setMenu(menuWidget); + } + } +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp index 0a0df0f7..769a4a75 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp @@ -2,6 +2,7 @@ #include #include +#include #include "SvgWidget.hpp" @@ -17,10 +18,7 @@ namespace Bloom::Widgets Q_PROPERTY(int buttonHeight READ getButtonHeight WRITE setButtonHeight DESIGNABLE true) public: - explicit SvgToolButton(QWidget* parent): QToolButton(parent) { - this->setButtonWidth(10); - this->setButtonHeight(10); - }; + explicit SvgToolButton(QWidget* parent); void setSvgFilePath(const QString& svgFilePath) { this->svgWidget->setSvgFilePath(svgFilePath); @@ -58,6 +56,9 @@ namespace Bloom::Widgets return this->buttonHeight; } + protected: + void childEvent(QChildEvent* childEvent) override; + private: SvgWidget* svgWidget = new SvgWidget(this); int buttonWidth = 0;