slider.cpp
4.45 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
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
#include "slider.h"
#include <QMouseEvent>
#include <QPainter>
#include <QDebug>
Slider::Slider(QWidget *parent) : QWidget(parent),
isSliderDown_(false),
sliderPosition_(0), value_(0), minimum_(0), maximum_(1),
subVisible_(true),
tickInterval(0), bigTickInterval(0)
{
groove.load(":/images/slider/groove.png");
sub.load(":/images/slider/humidity.png");
handle.load(":/images/slider/handle_big.png");
updatePixmapPosition();
}
void Slider::setSubPixmap(const QPixmap &pixmap)
{
sub = pixmap;
}
void Slider::setSubPixmap(const QString &fileName)
{
setSubPixmap(QPixmap(fileName));
}
void Slider::setSubVisible(bool visible)
{
if (subVisible_ == visible)
return;
subVisible_ = visible;
update();
}
void Slider::setRange(int min, int max)
{
if (minimum_ == min && maximum_ == max)
return;
minimum_ = min;
maximum_ = max;
if (value_ < min)
setValue(min);
else if (value_ > max)
setValue(max);
update();
}
void Slider::setMinimum(int min)
{
if (minimum_ == min)
return;
minimum_ = min;
if (value_ < min)
setValue(min);
update();
}
void Slider::setMaximum(int max)
{
if (maximum_ == max)
return;
maximum_ = max;
if (value_ > max)
setValue(max);
update();
}
int Slider::value()
{
return value_;
}
bool Slider::isSliderDown()
{
return isSliderDown_;
}
int Slider::sliderPosition()
{
return sliderPosition_;
}
void Slider::setSliderPosition(int value)
{
if (sliderPosition_ == value)
return;
sliderPosition_ = value;
emit sliderMoved(value);
update();
}
void Slider::mouseMoveEvent(QMouseEvent *event)
{
if (!isSliderDown_)
return;
setSliderPosition(calcSliderPosition(event->x()));
}
void Slider::mousePressEvent(QMouseEvent */*event*/)
{
isSliderDown_ = true;
emit sliderPressed();
}
void Slider::mouseReleaseEvent(QMouseEvent */*event*/)
{
isSliderDown_ = false;
if (sliderPosition_ == value_)
return;
emit sliderReleased();
setValue(sliderPosition_);
}
void Slider::paintEvent(QPaintEvent */*event*/)
{
QPainter painter(this);
painter.drawPixmap(groovePoint, groove);
QPen tickPen(QColor(71, 71, 71));
tickPen.setWidth(2);
painter.setPen(tickPen);
if (tickInterval)
for (int tick = minimum_; tick <= maximum_; tick += tickInterval)
{
int x = subPoint.x() + calcSubLength(tick);
painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 28);
}
else
foreach (int tick, ticks)
{
int x = subPoint.x() + calcSubLength(tick);
painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 28);
}
if (bigTickInterval)
for (int tick = minimum_; tick <= maximum_; tick += bigTickInterval)
{
int x = subPoint.x() + calcSubLength(tick);
painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 33);
}
else
foreach (int tick, bigTicks)
{
int x = subPoint.x() + calcSubLength(tick);
painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 33);
}
if (subVisible_)
{
int subLength = calcSubLength(sliderPosition_);
if (subLength > 0)
painter.drawPixmap(subPoint, sub, QRect(0, 0, subLength, sub.height()));
QPoint handlePoint(subPoint.x() + subLength - handle.width() / 2, (height() - handle.height()) / 2);
painter.drawPixmap(handlePoint, handle);
}
}
void Slider::resizeEvent(QResizeEvent */*event*/)
{
updatePixmapPosition();
}
int Slider::calcSliderPosition(int mouseX)
{
int x = qBound(grooveMin, mouseX, grooveMax);
qreal pos = (qreal) (x - grooveMin) / (grooveMax - grooveMin) * (maximum_ - minimum_) + minimum_;
return qRound(pos);
}
int Slider::calcSubLength(int value)
{
return handle.width() / 4 + (sub.width() - handle.width() / 2) * (value - minimum_) / (maximum_ - minimum_);
}
void Slider::updatePixmapPosition()
{
grooveMin = (width() - sub.width()) / 2;
grooveMax = width() - grooveMin;
groovePoint = QPoint((width() - groove.width()) / 2, (height() - groove.height()) / 2);
subPoint = QPoint((width() - sub.width()) / 2, (height() - sub.height()) / 2);
}
void Slider::setValue(int value)
{
if (value == value_)
return;
setSliderPosition(value);
value_ = value;
emit valueChanged(value);
}