forked from bitcoin-core/gui-qml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinegraph.cpp
More file actions
148 lines (124 loc) · 3.43 KB
/
Copy pathlinegraph.cpp
File metadata and controls
148 lines (124 loc) · 3.43 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2023 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <qml/controls/linegraph.h>
#include <QBrush>
#include <QColor>
#include <QLinearGradient>
#include <QObject>
#include <QPainter>
#include <QPainterPath>
#include <QPen>
#include <QQueue>
#include <QQuickPaintedItem>
LineGraph::LineGraph(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
setGradientColor(m_background_color);
}
void LineGraph::setBackgroundColor(QColor color)
{
m_background_color = color;
setGradientColor(color);
}
void LineGraph::setBorderColor(QColor color)
{
m_border_color = color;
update();
}
void LineGraph::setGradientColor(QColor color)
{
m_gradient_color = color;
update();
}
void LineGraph::setLineColor(QColor color)
{
m_line_color = color;
update();
}
void LineGraph::setMarkerLineColor(QColor color)
{
m_marker_line_color = color;
update();
}
void LineGraph::setMaxSamples(int max_samples)
{
m_max_samples = max_samples;
update();
}
void LineGraph::setMaxValue(float max_value)
{
m_max_value = max_value;
}
void LineGraph::setValueList(QQueue<float> value_list)
{
m_value_list = value_list;
update();
}
void LineGraph::paintGraphBorder(QPainter * painter)
{
painter->setPen(QPen(m_border_color, 2));
QRectF rect(0, 0, width(), height());
painter->drawRect(rect);
}
void LineGraph::paintMarkerLines(QPainter * painter)
{
painter->setPen(QPen(m_marker_line_color, 1));
qreal line_spacing = height() / 8;
for (int i = 0; i < 9; i++) {
qreal y = i * line_spacing;
if (i == 4) {
painter->setPen(QPen(m_border_color, 1));
painter->drawLine(0, y, width(), y);
painter->setPen(QPen(m_marker_line_color, 1));
} else {
painter->drawLine(0, y, width(), y);
}
}
}
void LineGraph::paintPath(QPainterPath * painter_path)
{
int item_count = std::min(m_max_samples, static_cast<int>(m_value_list.size()));
qreal h = height();
qreal w = width();
if(item_count > 0) {
int y_pos = h;
int x_pos = w;
painter_path->moveTo(x_pos, y_pos);
for(int i = 0; i < item_count; ++i) {
x_pos = w - w * i / m_max_samples;
y_pos = h - ((h - 10) * m_value_list.at(i) / m_max_value);
painter_path->lineTo(x_pos, y_pos);
}
painter_path->lineTo(x_pos - 2, h);
painter_path->lineTo(0, h);
}
}
void LineGraph::paintTraffic(QPainter * painter)
{
painter->setRenderHint(QPainter::Antialiasing);
if (m_max_value == 0.0f) {
return;
}
if(!m_value_list.empty()) {
QPainterPath p;
paintPath(&p);
setupGradient(&p);
painter->setPen(QPen(m_line_color, 1));
painter->drawPath(p);
painter->fillPath(p, QBrush(m_fill_gradient));
}
}
void LineGraph::setupGradient(QPainterPath * painter_path)
{
QLinearGradient gradient(painter_path->boundingRect().topLeft(), painter_path->boundingRect().bottomLeft());
gradient.setColorAt(0, QColor(m_gradient_color.red(), m_gradient_color.green(), m_gradient_color.blue(), 191));
gradient.setColorAt(1, QColor("transparent"));
m_fill_gradient = gradient;
}
void LineGraph::paint(QPainter *painter)
{
paintMarkerLines(painter);
paintTraffic(painter);
paintGraphBorder(painter);
}