1 #include "GraphBackgroundPainter.h"
2
3 #include <cmath>
4
5 #include "Util.h"
6
7 GraphBackgroundPainter::GraphBackgroundPainter() = default;
8
9 GraphBackgroundPainter::~GraphBackgroundPainter() = default;
10
resetConfig()11 void GraphBackgroundPainter::resetConfig() {
12 this->defaultForegroundColor1 = 0xBDBDBDU;
13 this->defaultAlternativeForegroundColor1 = 0x434343U;
14 this->lineWidth = 0.5;
15 this->drawRaster1 = 14.17;
16 this->margin1 = 0;
17 this->roundMargin = 0;
18 }
19
paint()20 void GraphBackgroundPainter::paint() {
21 this->paintBackgroundColor();
22 paintBackgroundGraph();
23 }
24
paintBackgroundGraph()25 void GraphBackgroundPainter::paintBackgroundGraph() {
26 Util::cairo_set_source_rgbi(cr, this->foregroundColor1);
27
28 cairo_set_line_width(cr, lineWidth * lineWidthFactor);
29 double marginTopBottom = margin1;
30 double marginLeftRight = margin1;
31 double snappingOffset = 2.5;
32
33 if (roundMargin) {
34 double w = width - 2 * marginLeftRight;
35 double r = w - floor(w / drawRaster1) * drawRaster1;
36 marginLeftRight += r / 2;
37 // startX = marginLeftRight;
38
39 double h = height - 2 * marginTopBottom;
40 r = h - floor(h / drawRaster1) * drawRaster1;
41 marginTopBottom += r / 2;
42 // startY = marginTopBottom;
43 }
44
45 auto pos = [dr1 = drawRaster1](int i) { return dr1 + i * dr1; };
46
47 for (int x = 0; pos(x) < width; ++x) {
48 if (pos(x) < margin1 || pos(x) > (width - margin1)) {
49 continue;
50 }
51 cairo_move_to(cr, pos(x), marginTopBottom - snappingOffset);
52 cairo_line_to(cr, pos(x), height - marginTopBottom - snappingOffset);
53 }
54
55 for (int y = 0; pos(y) < height; ++y) {
56 if (pos(y) < margin1 || pos(y) > (height - marginTopBottom)) {
57 continue;
58 }
59
60 cairo_move_to(cr, marginLeftRight, pos(y));
61 cairo_line_to(cr, width - marginLeftRight, pos(y));
62 }
63
64 cairo_stroke(cr);
65 }
66