From 8259b7dc510eb4fb63a13d5d553468d19df43c13 Mon Sep 17 00:00:00 2001 From: Nav Date: Sun, 22 Aug 2021 20:38:05 +0100 Subject: [PATCH] New SVG widget for displaying SVG images --- CMakeLists.txt | 1 + .../InsightWindow/Widgets/SvgWidget.cpp | 44 ++++++++++++ .../InsightWindow/Widgets/SvgWidget.hpp | 70 +++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2358c533..ff66353e 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,6 +126,7 @@ add_executable(Bloom src/Insight/InsightWorker.cpp src/Insight/UserInterfaces/InsightWindow/InsightWindow.cpp src/Insight/UserInterfaces/InsightWindow/AboutWindow.cpp + src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp # Target package widgets src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPackageWidget.hpp diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp new file mode 100644 index 00000000..eb94dc4e --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp @@ -0,0 +1,44 @@ +#include "SvgWidget.hpp" + +#include +#include +#include "src/Logger/Logger.hpp" + +using namespace Bloom::Widgets; + +SvgWidget::SvgWidget(QWidget* parent): QFrame(parent) { + this->containerWidth = parent->width(); + this->containerHeight = parent->height(); + this->setFixedSize(this->containerWidth, this->containerHeight); + this->renderer.setAspectRatioMode(Qt::AspectRatioMode::KeepAspectRatioByExpanding); +} + +void SvgWidget::paintEvent(QPaintEvent* paintEvent) { + auto painter = QPainter(this); + auto svgSize = this->renderer.defaultSize(); + auto margins = this->contentsMargins(); + + painter.setRenderHints(QPainter::RenderHint::Antialiasing | QPainter::RenderHint::SmoothPixmapTransform, true); + + this->setFixedHeight(this->containerHeight + margins.top() + margins.bottom()); + this->setFixedWidth(this->containerWidth + margins.left() + margins.right()); + + if (this->angle % 360 != 0) { + painter.translate( + std::ceil(static_cast(this->containerWidth / 2)), + std::ceil(static_cast(this->containerHeight / 2)) + ); + painter.rotate(90); + painter.translate( + -std::ceil(static_cast(this->containerWidth / 2)), + -std::ceil(static_cast(this->containerHeight / 2)) + ); + } + + this->renderer.render(&painter, QRectF( + std::ceil(static_cast(this->containerWidth - svgSize.width()) / 2 + static_cast(margins.left())), + std::ceil(static_cast(this->containerHeight - svgSize.height()) / 2 + static_cast(margins.top())), + svgSize.width(), + svgSize.height() + )); +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.hpp new file mode 100644 index 00000000..7647c205 --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.hpp @@ -0,0 +1,70 @@ +#pragma once + +#include +#include +#include +#include + +#include "src/Logger/Logger.hpp" + +namespace Bloom::Widgets +{ + class SvgWidget: public QFrame + { + Q_OBJECT + Q_PROPERTY(QString svgFilePath READ getSvgFilePath WRITE setSvgFilePath 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) + + private: + QSvgRenderer renderer = new QSvgRenderer(this); + QString svgFilePath; + int containerWidth = 0; + int containerHeight = 0; + int angle = 0; + + protected: + void paintEvent(QPaintEvent* paintEvent) override; + + public: + explicit SvgWidget(QWidget* parent); + + void setSvgFilePath(const QString& svgFilePath) { + this->svgFilePath = svgFilePath; + this->renderer.load(this->svgFilePath); + } + + QString getSvgFilePath() { + return this->svgFilePath; + } + + void setContainerWidth(int containerWidth) { + this->containerWidth = containerWidth; + } + + [[nodiscard]] int getContainerWidth() const { + return this->containerWidth; + } + + void setContainerHeight(int containerHeight) { + this->containerHeight = containerHeight; + } + + [[nodiscard]] int getContainerHeight() const { + return this->containerHeight; + } + + void setAngle(int angle) { + this->angle = angle; + } + + [[nodiscard]] int getAngle() const { + return this->angle; + } + +// [[nodiscard]] QSize sizeHint() const override { +// return QSize(this->containerWidth, this->containerHeight); +// }; + }; +}