This repository has been archived by the owner on Jun 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlabelslider.cpp
81 lines (72 loc) · 1.92 KB
/
labelslider.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
#include "labelslider.h"
#include <QSpinBox>
#include <QMouseEvent>
#include <cmath>
LabelSlider::LabelSlider(QWidget *parent) :
QLabel(parent),
spinBox_(nullptr),
dspinBox_(nullptr),
dragging_(false),
toNonLinearValue_([](double i){return i;}),
toLinearValue_([](double i){return i;})
{
setCursor(QCursor(Qt::SizeHorCursor));
setStyleSheet("color: #ff8f10; text-decoration: underline;");
}
LabelSlider::~LabelSlider()
{
}
void LabelSlider::bindSpinBox(QSpinBox *spinBox)
{
spinBox_ = spinBox;
}
void LabelSlider::bindSpinBox(QDoubleSpinBox *spinBox)
{
dspinBox_ = spinBox;
}
void LabelSlider::setNonLinearConversion(const std::function<double (double)> &forward,
const std::function<double (double)> &backward)
{
toNonLinearValue_ = forward;
toLinearValue_ = backward;
}
void LabelSlider::setLogarithmic()
{
setNonLinearConversion([](double i) -> double {
return std::log(i) * 20.;
}, [](double i) -> double {
return std::exp(i * 0.05);
});
}
void LabelSlider::mousePressEvent(QMouseEvent *e)
{
if (e->button() != Qt::LeftButton || (!spinBox_ && !dspinBox_)) {
return;
}
dragging_ = true;
if (spinBox_)
dragStartValue_ = toNonLinearValue_(spinBox_->value());
else if (dspinBox_)
dragStartValue_ = toNonLinearValue_(dspinBox_->value());
dragStartPos_ = e->pos();
}
void LabelSlider::mouseReleaseEvent(QMouseEvent *e)
{
if (e->button() != Qt::LeftButton) {
return;
}
dragging_ = false;
}
void LabelSlider::mouseMoveEvent(QMouseEvent *e)
{
if (dragging_) {
QPoint pt = e->pos();
pt -= dragStartPos_;
int delta = pt.x() - pt.y();
double nl = dragStartValue_ + delta * 0.23;
if (spinBox_)
spinBox_->setValue(toLinearValue_(nl));
else if (dspinBox_)
dspinBox_->setValue(toLinearValue_(nl));
}
}