-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsuperslider.cpp
134 lines (117 loc) · 3.76 KB
/
superslider.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
#include "superslider.h"
// Qt
#include <QApplication>
#include <QCursor>
#include <QDebug>
#include <QDir>
#include <QDrag>
#include <QGuiApplication>
#include <QMimeData>
#include <QMouseEvent>
#include <QPixmap>
#include <QProxyStyle>
#include <QWidgetAction>
class SliderProxy : public QProxyStyle {
public:
int pixelMetric(PixelMetric metric, const QStyleOption *option = 0,
const QWidget *widget = 0) const {
switch (metric) {
case PM_SliderThickness:
return 25;
case PM_SliderLength:
return 25;
default:
return (QProxyStyle::pixelMetric(metric, option, widget));
}
}
};
SuperSlider::SuperSlider(QWidget *parent) : QSlider(parent) {
// styling
setOrientation(Qt::Horizontal);
setAcceptDrops(true);
SliderProxy *aSliderProxy = new SliderProxy();
// Style
// hard coded path to image :/ sorry
QString path = QDir::fromNativeSeparators("://handle.png");
setStyleSheet("QSlider::handle { image: url(" + path + "); }");
setStyle(aSliderProxy);
// setting up the alternate handle
alt_handle = new SuperSliderHandle(this);
addAction(new QWidgetAction(alt_handle));
alt_handle->move(this->pos().x() + this->width() - alt_handle->width(),
this->pos().y());
}
SuperSliderHandle::SuperSliderHandle(SuperSlider *_parent) : QLabel(_parent) {
parent = _parent;
filter = new SliderEventFilter(parent);
// styling
setAcceptDrops(true);
// hard coded path to image :/ sorry
QPixmap pix = QPixmap("://handle.png");
pix = pix.scaled(21, 21, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
setPixmap(pix);
}
int SuperSlider::alt_value() { return alt_handle->value(); }
void SuperSlider::alt_setValue(int value) { alt_handle->setValue(value); }
void SuperSlider::mouseReleaseEvent(QMouseEvent *mouseEvent) {
if (mouseEvent->button() == Qt::LeftButton) {
alt_handle->show();
alt_handle->handleActivated = false;
}
mouseEvent->accept();
}
void SuperSlider::alt_update() {
QPoint posCursor(QCursor::pos());
QPoint posParent(mapToParent(mapToGlobal(pos())));
QPoint point(
alt_handle->mapToParent(alt_handle->mapFromGlobal(QCursor::pos())).x(),
alt_handle->y());
int horBuffer = (alt_handle->width());
bool lessThanMax = mapToParent(point).x() < pos().x() + width() - horBuffer;
bool greaterThanMin = mapToParent(point).x() > pos().x();
if (lessThanMax && greaterThanMin)
alt_handle->move(point);
emit alt_valueChanged(alt_value());
}
void SuperSliderHandle::mousePressEvent(QMouseEvent *mouseEvent) {
qGuiApp->installEventFilter(filter);
parent->clearFocus();
}
bool SliderEventFilter::eventFilter(QObject *obj, QEvent *event) {
switch (event->type()) {
case QEvent::MouseButtonRelease:
qGuiApp->removeEventFilter(this);
return true;
break;
case QEvent::MouseMove:
grandParent->alt_update();
return true;
break;
default:
return QObject::eventFilter(obj, event);
}
return false;
}
void SuperSliderHandle::setValue(double value) {
double width = parent->width(), position = pos().x();
double range = parent->maximum() - parent->minimum();
double location = (value - parent->minimum()) / range;
location = location * width;
move(y(), static_cast<int>(location));
}
int SuperSliderHandle::value() {
double width = parent->width(), position = pos().x();
double value = position / width;
double range = parent->maximum() - parent->minimum();
return static_cast<int>(parent->minimum() + (value * range));
}
void SuperSlider::Reset() {
int horBuffer = (alt_handle->width());
QPoint myPos = mapToGlobal(pos());
QPoint point(myPos.x() + width() - horBuffer,
myPos.y() - alt_handle->height());
point = alt_handle->mapFromParent(point);
alt_handle->move(point);
alt_handle->show();
alt_handle->raise();
}