2021-08-22 20:38:05 +01:00
|
|
|
#include "SvgWidget.hpp"
|
|
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
|
|
using namespace Bloom::Widgets;
|
|
|
|
|
|
|
|
|
|
SvgWidget::SvgWidget(QWidget* parent): QFrame(parent) {
|
|
|
|
|
this->renderer.setAspectRatioMode(Qt::AspectRatioMode::KeepAspectRatioByExpanding);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 21:21:54 +00:00
|
|
|
void SvgWidget::startSpin() {
|
|
|
|
|
if (this->spinningAnimation == nullptr) {
|
|
|
|
|
this->spinningAnimation = new QPropertyAnimation(this, "angle", this);
|
|
|
|
|
this->spinningAnimation->setDuration(2000);
|
|
|
|
|
this->spinningAnimation->setStartValue(0);
|
|
|
|
|
this->spinningAnimation->setEndValue(360);
|
|
|
|
|
|
|
|
|
|
QObject::connect(this->spinningAnimation, &QPropertyAnimation::finished, this, [this] {
|
|
|
|
|
this->spinningAnimation->start();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->spinningAnimation->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SvgWidget::stopSpin() {
|
|
|
|
|
if (this->spinningAnimation != nullptr) {
|
|
|
|
|
this->spinningAnimation->stop();
|
|
|
|
|
this->setAngle(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 20:38:05 +01:00
|
|
|
void SvgWidget::paintEvent(QPaintEvent* paintEvent) {
|
|
|
|
|
auto painter = QPainter(this);
|
|
|
|
|
auto svgSize = this->renderer.defaultSize();
|
|
|
|
|
auto margins = this->contentsMargins();
|
2021-11-08 19:56:12 +00:00
|
|
|
const auto containerSize = this->frameSize();
|
2021-08-22 20:38:05 +01:00
|
|
|
|
|
|
|
|
painter.setRenderHints(QPainter::RenderHint::Antialiasing | QPainter::RenderHint::SmoothPixmapTransform, true);
|
|
|
|
|
|
|
|
|
|
if (this->angle % 360 != 0) {
|
|
|
|
|
painter.translate(
|
2021-11-08 19:56:12 +00:00
|
|
|
std::ceil(static_cast<float>(containerSize.width() / 2)),
|
|
|
|
|
std::ceil(static_cast<float>(containerSize.height() / 2))
|
2021-08-22 20:38:05 +01:00
|
|
|
);
|
2021-09-03 21:51:14 +01:00
|
|
|
painter.rotate(this->angle);
|
2021-08-22 20:38:05 +01:00
|
|
|
painter.translate(
|
2021-11-08 19:56:12 +00:00
|
|
|
-std::ceil(static_cast<float>(containerSize.width() / 2)),
|
|
|
|
|
-std::ceil(static_cast<float>(containerSize.height() / 2))
|
2021-08-22 20:38:05 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->renderer.render(&painter, QRectF(
|
2021-11-08 19:56:12 +00:00
|
|
|
std::ceil(static_cast<float>(containerSize.width() - svgSize.width()) / 2 + static_cast<float>(margins.left())),
|
|
|
|
|
std::ceil(static_cast<float>(containerSize.height() - svgSize.height()) / 2 + static_cast<float>(margins.top())),
|
2021-08-22 20:38:05 +01:00
|
|
|
svgSize.width(),
|
|
|
|
|
svgSize.height()
|
|
|
|
|
));
|
|
|
|
|
}
|
2021-09-04 17:56:18 +01:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|