forked from PALYGAP/MidyAX-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customdial.cpp
executable file
·167 lines (121 loc) · 3.43 KB
/
customdial.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "CustomDial.hpp"
#include <QPainter>
#include <QColor>
#include <QLabel>
#include <QPen>
#include <QResizeEvent>
CustomDial::CustomDial(QWidget* parent)
: QDial(parent)
{ }
CustomDial::CustomDial(const QString& text,
QWidget* parent,
int minimum,
int maximum)
: QDial(parent),
text_(text),
arcRect_(new QRectF),
valueRect_(new QRectF),
textRect_(new QRectF),
arcColor_(new QColor),
arcPen_(new QPen)
{
QDial::setRange(minimum, maximum);
QDial::setCursor(Qt::PointingHandCursor);
connect(this, &QDial::valueChanged,
this, &CustomDial::updateValue);
setMinimumSize(100,100);
setMaximumAngle(-360);
setStartAngle(270);
updateValue();
}
CustomDial::~CustomDial() = default;
void CustomDial::paintEvent(QPaintEvent*)
{
QPainter painter(this);
// So that we can use the background color
// Otherwise the background is transparent
painter.setBackgroundMode(Qt::OpaqueMode);
// Smooth out the circle
painter.setRenderHint(QPainter::Antialiasing);
// Use background color
painter.setBrush(painter.background());
// Get current pen before resetting so we have
// access to the color() method which returns the
// color from the stylesheet
QPen textPen = painter.pen();
// No border
painter.setPen(QPen(Qt::NoPen));
// Draw background circle
painter.drawEllipse(QDial::rect());
painter.setPen(textPen);
painter.drawText(*textRect_, Qt::AlignHCenter | Qt::AlignBottom, text_);
painter.drawText(*valueRect_, Qt::AlignCenter, valueString_);
painter.setPen(*arcPen_);
painter.drawArc(*arcRect_, startAngle_, angleSpan_);
}
void CustomDial::resizeEvent(QResizeEvent* event)
{
QDial::setMinimumSize(event->size());
double width = QDial::width() - (2 * arcWidth_);
double height = width / 2;
*textRect_ = QRectF(arcWidth_, arcWidth_, width, height);
*valueRect_ = QRectF(arcWidth_, height, width, height);
*arcRect_ = QRectF(arcWidth_ / 2,
arcWidth_ / 2,
QDial::width() - arcWidth_,
QDial::height() - arcWidth_);
}
void CustomDial::updateValue()
{
double value = QDial::value();
// Get ratio between current value and maximum to calculate angle
double ratio = value / QDial::maximum();
angleSpan_ = maximumAngleSpan_ * ratio;
valueString_ = QString::number(value);
}
void CustomDial::setArcWidth(double px)
{
arcWidth_ = px;
*arcRect_ = QRectF(arcWidth_ / 2,
arcWidth_ / 2,
QDial::width() - arcWidth_,
QDial::height() - arcWidth_);
arcPen_->setWidth(arcWidth_);
}
void CustomDial::setText(const QString& text)
{
text_ = text;
}
QString CustomDial::getText() const
{
return text_;
}
double CustomDial::getArcWidth() const
{
return arcWidth_;
}
void CustomDial::setMaximumAngle(double angle)
{
maximumAngleSpan_ = angle * 16;
}
double CustomDial::getMaximumAngle() const
{
return maximumAngleSpan_ / 16;
}
void CustomDial::setStartAngle(double angle)
{
startAngle_ = angle * 16;
}
double CustomDial::getStartAngle() const
{
return startAngle_ / 16;
}
void CustomDial::setArcColor(const QString& color)
{
arcColor_->setNamedColor(color);
arcPen_->setColor(*arcColor_);
}
QString CustomDial::getArcColor() const
{
return arcColor_->name();
}