0cbd3823e
김태훈
슬라이더 새로 구현
|
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
|
#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)
{
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::setRange(int min, int max)
{
minimum_ = min;
maximum_ = max;
if (value_ < min)
setValue(min);
else if (value_ > max)
setValue(max);
}
void Slider::setMinimum(int min)
{
minimum_ = min;
if (value_ < min)
setValue(min);
}
void Slider::setMaximum(int max)
{
maximum_ = max;
if (value_ > max)
setValue(max);
}
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;
}
void Slider::mouseReleaseEvent(QMouseEvent */*event*/)
{
isSliderDown_ = false;
if (sliderPosition_ == value_)
return;
setValue(sliderPosition_);
}
void Slider::paintEvent(QPaintEvent */*event*/)
{
QPainter painter(this);
painter.drawPixmap(groovePoint, groove);
int subLength = calcSubLength(sliderPosition_);
if (subLength > 0)
painter.drawPixmap(subPoint, sub, QRect(0, 0, subLength, sub.height()));
QPen tickPen(QColor(71, 71, 71));
tickPen.setWidth(2);
painter.setPen(tickPen);
foreach (int tick, ticks)
{
int x = subPoint.x() + calcSubLength(tick);
painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 28);
}
foreach (int tick, bigTicks)
{
int x = subPoint.x() + calcSubLength(tick);
painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 33);
}
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);
}
|