-
Notifications
You must be signed in to change notification settings - Fork 6
/
Slider.cpp
156 lines (134 loc) · 3.93 KB
/
Slider.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2014-2021 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include <Pothos/Framework.hpp>
#include "MyDoubleSlider.hpp"
#include <QVariant>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QMouseEvent>
/***********************************************************************
* |PothosDoc Slider
*
* A slider widget for graphical control of an integer value.
*
* |category /Widgets
* |keywords slider
*
* |param title The name of the value displayed by this widget
* |default "My Slider Value"
* |widget StringEntry()
*
* |param orientation The slider orientation (horizontal or vertical).
* |default "Horizontal"
* |option [Horizontal] "Horizontal"
* |option [Vertical] "Vertical"
* |preview disable
*
* |param value The initial value of this slider.
* |default 0.0
*
* |param minimum The minimum value of this slider.
* |default -1.0
*
* |param maximum The maximum value of this slider.
* |default 1.0
*
* |param step [Step Size] The increment between discrete values.
* |default 0.01
*
* |mode graphWidget
* |factory /widgets/slider(orientation)
* |setter setTitle(title)
* |setter setMinimum(minimum)
* |setter setMaximum(maximum)
* |setter setSingleStep(step)
* |setter setValue(value)
**********************************************************************/
class Slider : public QGroupBox, public Pothos::Block
{
Q_OBJECT
public:
static Block *make(const std::string &orientation)
{
return new Slider(orientation);
}
Slider(const std::string &orientation):
_slider(new MyDoubleSlider((orientation == "Horizontal")? Qt::Horizontal : Qt::Vertical))
{
auto layout = new QVBoxLayout(this);
layout->setContentsMargins(QMargins());
layout->addWidget(_slider);
this->setStyleSheet("QGroupBox {font-weight: bold;}");
this->registerCall(this, POTHOS_FCN_TUPLE(Slider, setTitle));
this->registerCall(this, POTHOS_FCN_TUPLE(Slider, widget));
this->registerCall(this, POTHOS_FCN_TUPLE(Slider, value));
this->registerCall(this, POTHOS_FCN_TUPLE(Slider, setValue));
this->registerCall(this, POTHOS_FCN_TUPLE(Slider, setMinimum));
this->registerCall(this, POTHOS_FCN_TUPLE(Slider, setMaximum));
this->registerCall(this, POTHOS_FCN_TUPLE(Slider, setSingleStep));
this->registerSignal("valueChanged");
connect(_slider, &MyDoubleSlider::valueChanged, this, &Slider::handleValueChanged);
}
QWidget *widget(void)
{
return this;
}
void setTitle(const QString &title)
{
QMetaObject::invokeMethod(this, "handleSetTitle", Qt::QueuedConnection, Q_ARG(QString, title));
}
double value(void) const
{
return _slider->value();
}
void setValue(const double value)
{
_slider->setValue(value);
}
void setMinimum(const double value)
{
_slider->setMinimum(value);
}
void setMaximum(const double value)
{
_slider->setMaximum(value);
}
void setSingleStep(const double value)
{
_slider->setSingleStep(value);
}
void activate(void)
{
//emit current value when design becomes active
this->emitSignal("valueChanged", this->value());
}
public slots:
QVariant saveState(void) const
{
return this->value();
}
void restoreState(const QVariant &state)
{
this->setValue(state.toDouble());
}
private slots:
void handleValueChanged(const double value)
{
this->emitSignal("valueChanged", value);
}
void handleSetTitle(const QString &title)
{
QGroupBox::setTitle(title);
}
protected:
void mousePressEvent(QMouseEvent *event)
{
QGroupBox::mousePressEvent(event);
event->ignore(); //allows for dragging from QGroupBox title
}
private:
MyDoubleSlider *_slider;
};
static Pothos::BlockRegistry registerSlider(
"/widgets/slider", &Slider::make);
#include "Slider.moc"