forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
labelledslider.cpp
98 lines (81 loc) · 2.46 KB
/
labelledslider.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
/** Copyright (c) 2013, Sean Kasun */
#include <QVBoxLayout>
#include <cmath>
#include "labelledslider.h"
LabelledSlider::LabelledSlider(Qt::Orientation orientation, QWidget *parent) :
QWidget(parent) {
slider = new MSlider(orientation);
slider->setRange(0, 255);
slider->setTickInterval(64);
slider->setTickPosition(QSlider::TicksBelow);
label = new QLabel();
label->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
label->setNum(255);
connect(slider, &MSlider::valueChanged,
this, &LabelledSlider::intValueChange);
QBoxLayout *myLayout;
if (orientation == Qt::Vertical) {
myLayout = new QVBoxLayout;
myLayout->addWidget(label);
myLayout->addWidget(slider);
} else {
myLayout = new QHBoxLayout;
myLayout->addWidget(slider);
myLayout->addWidget(label);
}
setLayout(myLayout);
setFocusProxy(slider);
}
int LabelledSlider::value() const {
return slider->value();
}
// public slot
void LabelledSlider::setValue(double v) {
preciseValue = v;
if (preciseValue < slider->minimum()) preciseValue = slider->minimum();
if (preciseValue > slider->maximum()) preciseValue = slider->maximum();
slider->setValue(static_cast<int>(floor(preciseValue + 0.5)));
}
// public slot
void LabelledSlider::changeValue(double v) {
this->setValue(preciseValue + v);
}
// public slot
void LabelledSlider::setRange(int minVal, int maxVal)
{
slider->setRange(minVal, maxVal);
}
void LabelledSlider::setMinimum(int minVal)
{
slider->setMinimum(minVal);
}
void LabelledSlider::setMaximum(int maxVal)
{
slider->setMaximum(maxVal);
}
int LabelledSlider::minimum() const
{
return slider->minimum();
}
int LabelledSlider::maximum() const
{
return slider->maximum();
}
// private slot
void LabelledSlider::intValueChange(int v) {
// update preciceValue in case slider is moved by other means
preciseValue -= static_cast<int>(preciseValue); // preserve fractional part
preciseValue += v; // take over integer part
label->setNum(v);
emit valueChanged(v);
}
void LabelledSlider::wheelEvent(QWheelEvent* event) {
this->changeValue(event->angleDelta().y() / 120.0); // in order to make wheel intuitive
}
MSlider::MSlider(Qt::Orientation orientation, QWidget* parent) :
QSlider(orientation, parent) {}
// catch the wheelEvent to prevent it from scrolling the slider
// ignore the event to allow it to bubble up to the LabelledSlider
void MSlider::wheelEvent(QWheelEvent* event) {
event->ignore();
}