From 27b5684037c88b7c15ede674fd98dc8a86f687b2 Mon Sep 17 00:00:00 2001 From: Nav Date: Sat, 4 Sep 2021 17:56:18 +0100 Subject: [PATCH] Moved disabled SVG widget functionality to SvgWidget class --- .../InsightWindow/Widgets/SvgToolButton.hpp | 31 ++----------------- .../InsightWindow/Widgets/SvgWidget.cpp | 13 ++++++++ .../InsightWindow/Widgets/SvgWidget.hpp | 12 +++++++ 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp index f9e7f1e0..f1a4b066 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp @@ -22,15 +22,6 @@ namespace Bloom::Widgets int buttonWidth = 0; int buttonHeight = 0; - protected: - void changeEvent(QEvent* event) override { - if (event->type() == QEvent::EnabledChange && this->disabledSvgWidget != nullptr) { - auto enabled = this->isEnabled(); - this->svgWidget->setVisible(enabled); - this->disabledSvgWidget->setVisible(!enabled); - } - }; - public: explicit SvgToolButton(QWidget* parent): QToolButton(parent) { this->setButtonWidth(10); @@ -46,31 +37,17 @@ namespace Bloom::Widgets } void setDisabledSvgFilePath(const QString& disabledSvgFilePath) { - if (this->disabledSvgWidget == nullptr) { - this->disabledSvgWidget = new SvgWidget(this); - this->disabledSvgWidget->setContainerWidth(this->buttonWidth); - this->disabledSvgWidget->setContainerHeight(this->buttonHeight); - } - - this->disabledSvgWidget->setSvgFilePath(disabledSvgFilePath); + this->svgWidget->setDisabledSvgFilePath(disabledSvgFilePath); } [[nodiscard]] QString getDisabledSvgFilePath() const { - if (this->disabledSvgWidget != nullptr) { - return this->disabledSvgWidget->getSvgFilePath(); - } - - return QString(); + return this->svgWidget->getDisabledSvgFilePath(); } void setButtonWidth(int buttonWidth) { this->buttonWidth = buttonWidth; this->setFixedWidth(buttonWidth); this->svgWidget->setContainerWidth(buttonWidth); - - if (this->disabledSvgWidget != nullptr) { - this->disabledSvgWidget->setContainerWidth(buttonWidth); - } } [[nodiscard]] int getButtonWidth() const { @@ -81,10 +58,6 @@ namespace Bloom::Widgets this->buttonHeight = buttonHeight; this->setFixedHeight(buttonHeight); this->svgWidget->setContainerHeight(buttonHeight); - - if (this->disabledSvgWidget != nullptr) { - this->disabledSvgWidget->setContainerHeight(buttonHeight); - } } [[nodiscard]] int getButtonHeight() const { diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp index d0917f5e..d28fac60 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp @@ -41,3 +41,16 @@ void SvgWidget::paintEvent(QPaintEvent* paintEvent) { svgSize.height() )); } + +void SvgWidget::changeEvent(QEvent* event) { + if (event->type() == QEvent::EnabledChange && !this->disabledSvgFilePath.isEmpty()) { + if (!this->isEnabled()) { + this->renderer.load(this->disabledSvgFilePath); + + } else { + this->renderer.load(this->svgFilePath); + } + + this->repaint(); + } +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.hpp index 996a85e8..ed2f6ef9 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include namespace Bloom::Widgets @@ -11,6 +12,7 @@ namespace Bloom::Widgets { Q_OBJECT Q_PROPERTY(QString svgFilePath READ getSvgFilePath WRITE setSvgFilePath DESIGNABLE true) + Q_PROPERTY(QString disabledSvgFilePath READ getDisabledSvgFilePath WRITE setDisabledSvgFilePath DESIGNABLE true) Q_PROPERTY(int containerWidth READ getContainerWidth WRITE setContainerWidth DESIGNABLE true) Q_PROPERTY(int containerHeight READ getContainerHeight WRITE setContainerHeight DESIGNABLE true) Q_PROPERTY(int angle READ getAngle WRITE setAngle DESIGNABLE true) @@ -18,12 +20,14 @@ namespace Bloom::Widgets private: QSvgRenderer renderer = new QSvgRenderer(this); QString svgFilePath; + QString disabledSvgFilePath; int containerWidth = 0; int containerHeight = 0; int angle = 0; protected: void paintEvent(QPaintEvent* paintEvent) override; + void changeEvent(QEvent* event) override; public: explicit SvgWidget(QWidget* parent); @@ -37,6 +41,14 @@ namespace Bloom::Widgets return this->svgFilePath; } + void setDisabledSvgFilePath(const QString& disabledSvgFilePath) { + this->disabledSvgFilePath = disabledSvgFilePath; + } + + [[nodiscard]] QString getDisabledSvgFilePath() const { + return this->disabledSvgFilePath; + } + void setContainerWidth(int containerWidth) { this->containerWidth = containerWidth; }