-
Notifications
You must be signed in to change notification settings - Fork 6
/
QIntSliderEdit.cpp
324 lines (291 loc) · 6.08 KB
/
QIntSliderEdit.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "QIntSliderEdit.h"
#include <math.h>
static int log2(int val)
{
double l=log((double)val)/log(2.0);
return (int)(l+0.5);
}
QIntSliderEdit::QIntSliderEdit(QWidget *parent)
: QWidget(parent)
,labelWidth_(80)
,textWidth_(80)
,unitsWidth_(40)
,minimum_(0)
,maximum_(100)
,defaultValue_(0)
,value_(0)
,power_of_two_(false)
,readonly_text(false)
,showDefaultButton_(false)
{
ui.setupUi(this);
int h=this->height();
this->setMinimumHeight(h);
ui.reset->setDisabled(true);
setValue(0);
ui.slider->installEventFilter(this);
ui.label->setWhatsThis(whatsThis());
ui.lineEdit->setWhatsThis(whatsThis());
ui.slider->setWhatsThis(whatsThis());
setTextWidth(textWidth_);
}
QIntSliderEdit::~QIntSliderEdit()
{
}
bool QIntSliderEdit::eventFilter(QObject *object, QEvent *event)
{
if (object == ui.slider && event->type() == QEvent::Wheel)
{
// Do not want wheel to scroll the slider:
return true;
}
return false;
}
Qt::Orientation QIntSliderEdit::orientation() const
{
return ui.slider->orientation();
}
void QIntSliderEdit::setOrientation(Qt::Orientation o)
{
ui.slider->setOrientation(o);
QBoxLayout *l=(QBoxLayout*)this->layout();
if(o==Qt::Horizontal)
{
l->setDirection(QBoxLayout::LeftToRight);
ui.lineEdit->setMinimumWidth(64);
ui.lineEdit->setMaximumWidth(64);
ui.slider->setMinimumWidth(32);
ui.slider->setMinimumHeight(8);
ui.label->setAlignment(Qt::AlignLeft);
ui.lineEdit->setAlignment(Qt::AlignLeft);
}
else
{
l->setDirection(QBoxLayout::TopToBottom);
ui.slider->setMinimumWidth(8);
ui.slider->setMinimumHeight(32);
ui.lineEdit->setMinimumWidth(labelWidth_);
ui.lineEdit->setMaximumWidth(labelWidth_);
ui.label->setAlignment(Qt::AlignHCenter);
ui.lineEdit->setAlignment(Qt::AlignHCenter);
}
update();
}
void QIntSliderEdit::setLabelWidth(int w)
{
labelWidth_=w;
ui.label->setMinimumWidth(w);
ui.label->setMaximumWidth(w);
QSize s=ui.label->minimumSize();
s.setWidth(w);
ui.label->setMinimumSize(s);
s=ui.label->maximumSize();
s.setWidth(w);
ui.label->setMaximumSize(s);
update();
}
void QIntSliderEdit::setTextWidth(int w)
{
textWidth_=w;
QSize s=ui.lineEdit->minimumSize();
s.setWidth(w);
ui.lineEdit->setMinimumSize(s);
s=ui.lineEdit->maximumSize();
s.setWidth(w);
ui.lineEdit->setMaximumSize(s);
update();
}
void QIntSliderEdit::setMinimum(int f)
{
minimum_=f;
if(power_of_two_)
{
if(minimum_<=0)
minimum_=1;
ui.slider->setMinimum(log2(minimum_));
}
else
ui.slider->setMinimum(minimum_);
}
void QIntSliderEdit::setMaximum(int f)
{
maximum_=f;
if(power_of_two_)
{
if(maximum_<=minimum_)
maximum_=minimum_*2;
ui.slider->setMaximum(log2(maximum_));
}
else
ui.slider->setMaximum(maximum_);
}
void QIntSliderEdit::setPowerOfTwo(bool l)
{
power_of_two_=l;
setMinimum(minimum_);
setMaximum(maximum_);
updateSlider();
}
bool QIntSliderEdit::powerOfTwo() const
{
return power_of_two_;
}
void QIntSliderEdit::setReadOnlyText(bool l)
{
readonly_text=l;
ui.lineEdit->setReadOnly(readonly_text);
}
bool QIntSliderEdit::readOnlyText() const
{
return readonly_text;
}
void QIntSliderEdit::setEnums(QStringList e)
{
enums_=e;
setValue(value_);
}
QStringList QIntSliderEdit::enums() const
{
return enums_;
}
void QIntSliderEdit::setValue(QVariant f)
{
setValue(f,true);
}
void QIntSliderEdit::on_reset_clicked()
{
setValue(defaultValue_);
emit valueChanged();
}
void QIntSliderEdit::setValue(QVariant f,bool single_valued)
{
bool ok;
value_=f.toInt(&ok);
if(!ok)
return;
if(value_<minimum_)
value_=minimum_;
if(value_>maximum_)
value_=maximum_;
setShowDefaultButton(showDefaultButton_);
this->blockSignals(true);
ui.slider->blockSignals(true);
updateSlider();
if(single_valued)
ui.lineEdit->setText(valueToText(value_));
else
ui.lineEdit->setText("");
ui.slider->blockSignals(false);
this->blockSignals(false);
}
void QIntSliderEdit::setTitle(QString f)
{
ui.label->setText(f);
}
QString QIntSliderEdit::title() const
{
return ui.label->text();
}
void QIntSliderEdit::setUnits(QString f)
{
ui.units->setText(f);
}
QString QIntSliderEdit::units() const
{
return ui.units->text();
}
QString QIntSliderEdit::valueToText(int value)
{
if(enums_.size()&&value>=0&&value<enums_.size())
{
return enums_[value];
}
return QString("%1").arg(value);
}
int QIntSliderEdit::textToValue(QString s)
{
bool ok=false;
return s.toInt(&ok);
}
void QIntSliderEdit::on_slider_sliderMoved(int pos)
{
int val=pos;
if(power_of_two_)
{
val=1<<pos;
}
else
val=pos;
ui.lineEdit->blockSignals(true);
ui.lineEdit->setText(valueToText(val));
ui.lineEdit->blockSignals(false);
}
void QIntSliderEdit::on_slider_valueChanged(int pos)
{
int val=pos;
if(power_of_two_)
{
val=1<<pos;
}
if(val<minimum_)
val=minimum_;
if(val>maximum_)
val=maximum_;
value_=val;
setShowDefaultButton(showDefaultButton_);
ui.lineEdit->blockSignals(true);
ui.lineEdit->setText(valueToText(val));
ui.lineEdit->blockSignals(false);
emit valueChanged();
}
void QIntSliderEdit::on_lineEdit_editingFinished()
{
value_=textToValue(ui.lineEdit->text());
if(power_of_two_)
value_=1<<(log2(value_));
ui.slider->blockSignals(true);
updateSlider();
ui.slider->blockSignals(false);
setShowDefaultButton(showDefaultButton_);
emit valueChanged();
}
void QIntSliderEdit::updateSlider()
{
if(value_<minimum_)
value_=minimum_;
if(value_>maximum_)
value_=maximum_;
int val=(value_-minimum_);
if(power_of_two_)
val=log2(value_)-log2(minimum_);
int pos=ui.slider->minimum()+val;
ui.slider->blockSignals(true);
ui.slider->setValue((int)pos);
ui.slider->blockSignals(false);
emit valueChanged();
}
void QIntSliderEdit::setShowDefaultButton(bool l)
{
showDefaultButton_=l;
if(!showDefaultButton_||value_==defaultValue_)
ui.reset->setDisabled(true);
else
ui.reset->setDisabled(false);
}
bool QIntSliderEdit::showDefaultButton() const
{
return showDefaultButton_;
}
void QIntSliderEdit::setUnitsWidth(int w)
{
unitsWidth_=w;
ui.units->setMinimumWidth(w);
ui.units->setMaximumWidth(w);
QSize s=ui.units->minimumSize();
s.setWidth(w);
ui.units->setMinimumSize(s);
s=ui.units->maximumSize();
s.setWidth(w);
ui.units->setMaximumSize(s);
update();
}