New SVG widget for displaying SVG images

This commit is contained in:
Nav
2021-08-22 20:38:05 +01:00
parent 9e3ca93264
commit 8259b7dc51
3 changed files with 115 additions and 0 deletions

View File

@@ -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

View File

@@ -0,0 +1,44 @@
#include "SvgWidget.hpp"
#include <QPainter>
#include <cmath>
#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<float>(this->containerWidth / 2)),
std::ceil(static_cast<float>(this->containerHeight / 2))
);
painter.rotate(90);
painter.translate(
-std::ceil(static_cast<float>(this->containerWidth / 2)),
-std::ceil(static_cast<float>(this->containerHeight / 2))
);
}
this->renderer.render(&painter, QRectF(
std::ceil(static_cast<float>(this->containerWidth - svgSize.width()) / 2 + static_cast<float>(margins.left())),
std::ceil(static_cast<float>(this->containerHeight - svgSize.height()) / 2 + static_cast<float>(margins.top())),
svgSize.width(),
svgSize.height()
));
}

View File

@@ -0,0 +1,70 @@
#pragma once
#include <QFrame>
#include <QSvgRenderer>
#include <QString>
#include <QSize>
#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);
// };
};
}