Newer
Older
#include "svgrenderer.h"
namespace rendering {
SvgRenderer::SvgRenderer(int width, int height)
{
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
height_ = height;
}
void
SvgRenderer::addCircleNoTransform(QPointF &location, qreal diameter, bool counterClockwise)
{
path_->addCircle(location, diameter, counterClockwise);
}
void
SvgRenderer::addPolygonNoTransform(QList<QPointF> &points)
{
path_->addPolygon(points);
}
void
SvgRenderer::setBackgroundColor(const QColor &color)
{
backColor_ = color;
}
void
SvgRenderer::beginShape(const QColor &color)
{
if (pathsByColor_.contains(color.name())) {
path_ = pathsByColor_.value(color.name());
} else {
path_ = new SvgPath();
pathsByColor_.insert(color.name(), path_);
}
}
void
SvgRenderer::save(QTextStream &stream, bool fragment)
{
stream << toSvg(fragment) << endl;
}
QString
SvgRenderer::toSvg(bool fragment)
{
auto svg = QList<QString>();
auto widthAsString = QString::number(width_);
auto heightAsString = QString::number(height_);
if (!fragment) {
svg.append("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"" + widthAsString +
"\" height=\"" + heightAsString + "\" viewBox=\"0 0 " + widthAsString + " " +
heightAsString + "\" preserveAspectRatio=\"xMidYMid meet\">");
}
if (backColor_.alpha() > 0) {
svg.append("<rect fill=\"" + backColor_.name() + "\" fill-opacity=\"" +
QString::number(backColor_.alphaF()) + "\" x=\"0\" y=\"0\" width=\"" +
widthAsString + "\" height=\"" + heightAsString + "\"/>");
}
for (auto pair : pathsByColor_.keys()) {
svg.append("<path fill=\"" + pair + "\" d=\"" + pathsByColor_.value(pair)->toString() +
"\"/>");