-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubleslider.cpp
266 lines (221 loc) · 6.23 KB
/
doubleslider.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "doubleslider.h"
#include"math.h"
DoubleSlider::DoubleSlider(QWidget *parent) : QWidget(parent)
, m_min(0.0f)
, m_max(99.0f)
, m_singleStep(0.1f)
, m_minValue(0.0f)
, m_maxValue(99.0f)
, m_state(None)
{
setFixedHeight(50);
setFocusPolicy(Qt::StrongFocus);
}
void DoubleSlider::paintEvent(QPaintEvent* event)
{
QWidget::paintEvent(event);
QPainter painter(this);
paintValueLabel(&painter);
}
void DoubleSlider::paintColoredRect(QRect rect, QColor color ,QPainter* painter)
{
painter->fillRect(rect,QBrush(color));
}
void DoubleSlider::paintValueLabel(QPainter* painter)
{
painter->setBrush(Qt::NoBrush);
painter->setPen(Qt::black);
painter->setFont(QFont("Arial",12));
QString minValueString = QString::number(m_minValue,'f',1);
QString maxValueString = QString::number(m_maxValue,'f',1);
QFontMetrics metrics = painter->fontMetrics();
int mintextWidth = metrics.width(minValueString);
int maxtextWidth = metrics.width(maxValueString);
int textHeight = metrics.height();
//---- paint text
painter->drawText(QRectF(2,4,mintextWidth,textHeight),minValueString);
painter->drawText(QRectF(width() - maxtextWidth -2, 4, maxtextWidth,textHeight), maxValueString);
//----- paint label
painter->setFont(QFont("Arial",12));
metrics = painter->fontMetrics();
int labelWidth = metrics.width(m_label);
int labelHeight = metrics.height();
QRectF textRect = QRectF((width()/2 - labelWidth/2), 0,labelWidth,labelHeight);
painter->drawText(textRect,m_label);
int minPos = ( m_minValue - m_min ) * width() / (m_max - m_min);
int maxPos = (m_maxValue - m_min ) * width() / (m_max - m_min);
if(minPos <= 4){
minPos = 4;
}else if(minPos >= width() - 8){
minPos = width() - 8;
}else{}
if(maxPos <= 4){
maxPos = 4;
}else if(maxPos >= width() -8){
maxPos = width() - 8;
}else{}
//----- paint groove
paintColoredRect(QRect(4,37,width() - 8,2),Qt::gray,painter);
paintColoredRect(QRect(minPos + 4,37,maxPos - minPos,2),QColor(51,153,155),painter);
//----- handle
minHandleRegion = QRect(minPos ,30,8,16);
maxHandleRegion = QRect(maxPos ,30,8,16);
//-----paint Handle
QColor minColor = (m_state == MinHandle) ? QColor(51,153,155) : Qt::darkGray;
QColor maxColor = (m_state == MaxHandle) ? QColor(51,153,155) : Qt::darkGray;
paintColoredRect(minHandleRegion,minColor,painter);
paintColoredRect(maxHandleRegion,maxColor,painter);
}
inline float getValidValue(float val, float min, float max)
{
float tmp = std::max(val, min);
return std::min(tmp, max);
}
void DoubleSlider::setRange(float min, float max)
{
m_min = min;
m_max = max;
if(minValue() < m_min)
setMinValue(m_min);
if(maxValue() > m_max){
setMaxValue(m_max);
}
}
void DoubleSlider::setSingleStep(float step)
{
m_singleStep = step;
}
float DoubleSlider::minValue() const
{
return m_minValue;
}
void DoubleSlider::setMinValue(float val)
{
if(fabs( m_minValue - val ) > 0.0001 ){
m_minValue = val;
emit minValueChanged(val);
}
update();
}
float DoubleSlider::maxValue() const
{
return m_maxValue;
}
void DoubleSlider::setMaxValue(float val)
{
if(fabs(m_maxValue - val) > 0.0001){
m_maxValue = val;
if(m_max-m_maxValue<10) ;
emit maxValueChanged(val);
}
update();
}
void DoubleSlider::setMaxRange(float range)
{
m_max = range;
if(m_max<m_minValue)
{
m_minValue=range+0.001f;
emit minValueChanged(m_minValue);
}
if(m_maxValue>m_max)
{
m_maxValue = m_max-0.001f;
emit maxValueChanged(m_maxValue);
}
update();
}
void DoubleSlider::setMinRange(float range)
{
m_min = range;
if(m_maxValue<m_min)
{
m_maxValue=m_min+1;
emit maxValueChanged(m_maxValue);
}
if(m_minValue<m_min)
{
m_minValue = m_min;
emit minValueChanged(m_minValue);
}
update();
}
void DoubleSlider::setLabel(const QString& label)
{
m_label = label;
update();
}
void DoubleSlider::leaveEvent(QEvent* event)
{
QWidget::leaveEvent(event);
m_state = None;
update();
}
float DoubleSlider::minRange() const
{
return m_min;
}
float DoubleSlider::maxRange() const
{
return m_max;
}
void DoubleSlider::mousePressEvent(QMouseEvent* event)
{
if(minHandleRegion.contains(event->pos())){
m_state = MinHandle;
}else if(maxHandleRegion.contains(event->pos())){
m_state = MaxHandle;
}else{
m_state = None;
}
if(fabs(minValue() - maxValue()) < 0.001){
if(fabs(minValue() - m_min) < 0.001){
m_state = MaxHandle;
}else if(fabs(minValue() - m_max) < 0.001){
m_state = MinHandle;
}else{}
}
update();
}
void DoubleSlider::mouseMoveEvent(QMouseEvent* event)
{
if(event->buttons() & Qt::LeftButton){
float move = event->x() * (m_max - m_min) * 1.0/ width() + m_min;
switch(m_state){
case MinHandle:{
float val = getValidValue( move, m_min,maxValue());
setMinValue(val);
break;
}
case MaxHandle:{
float val = getValidValue(move, minValue(), m_max);
setMaxValue(val);
break;
}
case None:default: break;
}
}
update();
}
void DoubleSlider::keyPressEvent(QKeyEvent *event)
{
QWidget::keyPressEvent(event);
if(event->key() == Qt::Key_Left){
if(m_state == MinHandle){
float val = getValidValue(minValue()- m_singleStep, m_min ,maxValue());
setMinValue(val);
}else if(m_state == MaxHandle) {
float val = getValidValue(maxValue()- m_singleStep, minValue() ,m_max);
setMaxValue(val);
}else{}
}else if(event->key() == Qt::Key_Right){
if(m_state == MinHandle){
float val = getValidValue(minValue() + m_singleStep, m_min ,maxValue());
setMinValue(val);
}else if(m_state == MaxHandle) {
float val = getValidValue(maxValue() + m_singleStep, minValue() ,m_max);
setMaxValue(val);
}else{}
}
update();
}