preheattempgauge.cpp
1.7 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
#include "preheattempgauge.h"
#include <QPainter>
#include <QtDebug>
PreheatTempGauge::PreheatTempGauge(QWidget *parent) : QWidget(parent)
{
border.load(":/images/gauge/bar_short_frame.png");
indicator.load(":/images/gauge/bar_indicator.png");
body.load(":/images/gauge/bar_short_red.png");
val = 0;
min = 0;
max = 1;
}
void PreheatTempGauge::setValue(int value)
{
if (value == val)
return;
val = qBound(min, value, max);
update();
}
void PreheatTempGauge::setMinimum(int minimum)
{
if (minimum == min)
return;
min = minimum;
max = qMax(min, max);
val = qBound(min, val, max);
update();
}
void PreheatTempGauge::setMaximum(int maximum)
{
if (maximum == max)
return;
max = maximum;
min = qMin(min, max);
val = qBound(min, val, max);
update();
}
void PreheatTempGauge::paintEvent(QPaintEvent */*event*/)
{
QPainter painter(this);
painter.setBrush(Qt::NoBrush);
painter.setPen(Qt::NoPen);
qreal percentage = (qreal) (val - min) / qMax(max - min, 1);
percentage = qBound((qreal) 0.0, percentage, (qreal) 1.0);
QRect targetRect(
(border.size().width() - body.size().width()) / 2 + indicator.size().width() / 2,
(border.size().height() - body.size().height()) / 2 + indicator.size().height(),
body.size().width() * percentage, body.height());
QRect sourceRect(0, 0, body.size().width() * percentage, body.height());
painter.drawPixmap(targetRect, body, sourceRect);
painter.drawPixmap(indicator.size().width() / 2, indicator.size().height(), border);
painter.drawPixmap(targetRect.right() - indicator.size().width() / 2, 0, indicator);
}