-
Notifications
You must be signed in to change notification settings - Fork 6
/
fixedsvgwidget.cpp
55 lines (44 loc) · 1.46 KB
/
fixedsvgwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "fixedsvgwidget.h"
FixedSvgWidget::FixedSvgWidget(QWidget *parent) : QWidget(parent)
{
this->renderer = new QSvgRenderer(this);
}
void FixedSvgWidget::setRenderer(QSvgRenderer *renderer)
{
this->renderer = renderer;
}
QSvgRenderer *FixedSvgWidget::getRenderer()
{
return this->renderer;
}
void FixedSvgWidget::load(const QString &filename)
{
renderer->load(filename);
}
void FixedSvgWidget::paintEvent(QPaintEvent *event)
{
QPainter p(this);
//p.drawRect(0,0,this->width()-1,this->height()-1);
QRectF containerBounds(0,0, this->width(), this->height());
QRectF svgBounds(containerBounds);
double svgAspectRatio = renderer->defaultSize().width()/renderer->defaultSize().height();
double widgetAspectRatio = containerBounds.width()/containerBounds.height();
if(svgAspectRatio == 0)
{
return;
}
// SVG wider than its container, decrease SVG's height to fit AR.
else if(svgAspectRatio > widgetAspectRatio)
{
svgBounds.setHeight(containerBounds.width() / svgAspectRatio);
}
// SVG taller than its container, decrease SVG's width to fit AR
else if(svgAspectRatio < widgetAspectRatio)
{
svgBounds.setWidth(containerBounds.height() * svgAspectRatio);
}
// Center SVG inside widget
svgBounds.moveTop((containerBounds.height() - svgBounds.height())/2);
svgBounds.moveLeft((containerBounds.width() - svgBounds.width())/2);
renderer->render(&p, svgBounds);
}