-
Notifications
You must be signed in to change notification settings - Fork 6
/
indicator.cpp
221 lines (186 loc) · 5.36 KB
/
indicator.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (C) 2016 Pavel Demin
// Copyright (c) 2021-2021 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include <QtCore/qmath.h>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtGui/QMouseEvent>
#include "indicator.h"
//------------------------------------------------------------------------------
static const QString c_StyleSheets[3] =
{
"color: %1;",
"color: %1; background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(255, 153, 153, 255), stop:0.49 rgba(255, 153, 153, 255), stop:0.50 rgba(0, 0, 0, 0), stop:1 rgba(0, 0, 0, 0))",
"color: %1; background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(0, 0, 0, 0), stop:0.50 rgba(0, 0, 0, 0), stop:0.51 rgba(153, 204, 255, 255), stop:1 rgba(153, 204, 255, 255))"
};
//------------------------------------------------------------------------------
class CustomDigit: public QLabel
{
public:
CustomDigit(QWidget *parent = 0): QLabel(parent), m_Active(true), m_State(0), m_Delta(0), m_Color(Qt::black) {}
void mouseMoveEvent(QMouseEvent *event)
{
if(!m_Active) return;
if(event->pos().y() < height() / 2 && m_State != 1)
{
m_State = 1;
applyStyleSheet();
}
if(event->pos().y() > height() / 2 && m_State != 2)
{
m_State = 2;
applyStyleSheet();
}
}
void leaveEvent(QEvent *)
{
if(!m_Active) return;
m_State = 0;
applyStyleSheet();
}
void mousePressEvent(QMouseEvent *event)
{
if(!m_Active) return;
if(event->pos().y() < height() / 2)
{
m_Indicator->applyDelta(m_Delta);
}
if(event->pos().y() > height() / 2)
{
m_Indicator->applyDelta(-m_Delta);
}
}
void wheelEvent(QWheelEvent *event)
{
if(!m_Active) return;
m_Indicator->applyDelta(event->angleDelta().y() / 90 * m_Delta);
event->accept();
}
void applyStyleSheet(void)
{
setStyleSheet(c_StyleSheets[m_State].arg(m_Color.name()));
}
bool m_Active;
int m_State;
qint64 m_Delta;
QColor m_Color;
Indicator *m_Indicator;
};
//------------------------------------------------------------------------------
Indicator::Indicator(QWidget *parent):
QFrame(parent), m_Layout(0), m_Font(), m_Size(0),
m_Value(0), m_ValueMin(0), m_ValueMax(0)
{
m_Layout = new QHBoxLayout(this);
m_Layout->setContentsMargins(1, 1, 1, 1);
m_Layout->setSpacing(1);
setLayout(m_Layout);
}
//------------------------------------------------------------------------------
Indicator::~Indicator()
{
}
//------------------------------------------------------------------------------
void Indicator::setFont(QFont font)
{
m_Font = font;
foreach(CustomDigit *digit, findChildren<CustomDigit *>())
{
digit->setFont(font);
}
}
//------------------------------------------------------------------------------
void Indicator::setFontSize(int size)
{
m_Font.setPointSize(size);
setFont(m_Font);
setSize(m_Size);
}
//------------------------------------------------------------------------------
void Indicator::setSize(int size)
{
int i, width;
CustomDigit *digit;
QLayoutItem *item;
QFontMetrics fm(m_Font);
width = fm.horizontalAdvance('m') / 5 + 1;
while((digit = findChild<CustomDigit *>()))
{
delete digit;
}
while((item = m_Layout->takeAt(0)))
{
delete item;
}
m_Layout->addStretch();
for(i = size; i > 0; --i)
{
digit = new CustomDigit(this);
digit->setText(QString::number(0));
digit->setAlignment(Qt::AlignCenter);
digit->setFont(m_Font);
m_Layout->addWidget(digit);
digit->setMouseTracking(true);
digit->m_Delta = qint64(qPow(10.0, i - 1) + 0.5);
digit->m_Indicator = this;
if(i > 1 && i % 3 == 1) m_Layout->addSpacing(width);
}
m_Layout->addStretch();
m_Size = size;
setValue(m_Value);
}
//------------------------------------------------------------------------------
void Indicator::setValue(qint64 value)
{
qint64 quotient;
if(value < m_ValueMin || value > m_ValueMax) return;
foreach(CustomDigit *digit, findChildren<CustomDigit *>())
{
quotient = value / digit->m_Delta;
digit->m_Color = (quotient == 0)?Qt::lightGray:Qt::black;
digit->applyStyleSheet();
digit->setText(QString::number(quotient % 10));
}
m_Value = value;
emit valueChanged(value);
}
//------------------------------------------------------------------------------
void Indicator::setValueMin(qint64 value)
{
if(value > m_ValueMax) return;
m_ValueMin = value;
if(m_Value < value) setValue(value);
}
//------------------------------------------------------------------------------
void Indicator::setValueMax(qint64 value)
{
if(value < m_ValueMin) return;
m_ValueMax = value;
if(m_Value > value) setValue(value);
}
//------------------------------------------------------------------------------
void Indicator::setDeltaMin(qint64 delta)
{
foreach(CustomDigit *digit, findChildren<CustomDigit *>())
{
if(digit->m_Delta < delta)
{
digit->setText(QString::number(0));
digit->setMouseTracking(false);
digit->m_Active = false;
}
else
{
digit->setMouseTracking(true);
digit->m_Active = true;
}
}
}
//------------------------------------------------------------------------------
void Indicator::applyDelta(qint64 delta)
{
qint64 value = m_Value + delta;
if(delta < 0 && value < m_ValueMin) return;
if(delta > 0 && value > m_ValueMax) return;
setValue(value);
}