Vuo  2.4.0
VuoInputEditorInteger.cc
Go to the documentation of this file.
1
11
16{
17 return new VuoInputEditorInteger();
18}
19
24void VuoInputEditorInteger::setUpDialog(QDialog &dialog, json_object *originalValue, json_object *details)
25{
26 current = VuoInteger_makeFromJson(originalValue);
27
28 suggestedMin = INT_MIN;
29 suggestedMax = INT_MAX;
30 VuoInteger suggestedStep = 1;
31 previous = current;
32 defaultValue = 0;
33
34 bool hasMinMax = details != NULL, hasAutoValue = details != NULL;
35
36 // Parse supported port annotations from the port's "details" JSON object:
37 if (details)
38 {
39 // "auto"
40 json_object *autoJsonValue = NULL;
41 if (json_object_object_get_ex(details, "auto", &autoJsonValue))
42 autoValue = VuoInteger_makeFromJson(autoJsonValue);
43 else
44 hasAutoValue = false;
45
46 // "suggestedMin"
47 json_object *suggestedMinValue = NULL;
48 if (json_object_object_get_ex(details, "suggestedMin", &suggestedMinValue))
49 suggestedMin = VuoInteger_makeFromJson(suggestedMinValue);
50 else
51 hasMinMax = false;
52
53 // "suggestedMax"
54 json_object *suggestedMaxValue = NULL;
55 if (json_object_object_get_ex(details, "suggestedMax", &suggestedMaxValue))
56 suggestedMax = VuoInteger_makeFromJson(suggestedMaxValue);
57 else
58 hasMinMax = false;
59
60 // "suggestedStep"
61 if (hasMinMax)
62 suggestedStep = fmax((suggestedMax - suggestedMin)/10, 1);
63 json_object *suggestedStepValue = NULL;
64 if (json_object_object_get_ex(details, "suggestedStep", &suggestedStepValue))
65 suggestedStep = VuoInteger_makeFromJson(suggestedStepValue);
66
67 // "default"
68 json_object *defaultJsonValue = NULL;
69 if (json_object_object_get_ex(details, "default", &defaultJsonValue))
70 defaultValue = VuoInteger_makeFromJson(defaultJsonValue);
71 }
72
73 spinbox = new VuoSpinBox(&dialog);
74 spinbox->setButtonMinimum(suggestedMin);
75 spinbox->setButtonMaximum(suggestedMax);
76 spinbox->setSingleStep(suggestedStep);
77 spinbox->setValue(current);
78 spinbox->setEnabled(autoToggle == NULL || !autoToggle->isChecked());
79
80 connect(spinbox, static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged), this, &VuoInputEditorInteger::onSpinboxUpdate);
81
82 if(hasMinMax)
83 {
84 slider = new QSlider(&dialog);
85 slider->setOrientation(Qt::Horizontal);
86 slider->setFocusPolicy(Qt::NoFocus);
87 slider->setMinimum(suggestedMin);
88 slider->setMaximum(suggestedMax);
89 slider->setSingleStep(suggestedStep);
90 slider->setValue(current);
91 slider->setEnabled(autoToggle == NULL || !autoToggle->isChecked());
92
93 connect(slider, &QSlider::valueChanged, this, &VuoInputEditorInteger::onSliderUpdate);
94 }
95
96 // layout dialog
97 QGridLayout* layout = new QGridLayout;
98 dialog.setLayout(layout);
99
100 // left, top, right, bottom
101 // when showing sliders, add a little extra margin on the bottom since QSlider takes up the
102 // entire vertical spacing
103 layout->setContentsMargins(4, 4, 12, 4);
104 layout->setSpacing(4);
105 if(hasAutoValue) layout->setHorizontalSpacing(8);
106
107 int row = 0;
108
109 if(hasAutoValue)
110 {
111 autoToggle = new QCheckBox("Auto");
112 connect(autoToggle, &QCheckBox::stateChanged, this, &VuoInputEditorInteger::setAutoToggled);
113 bool currentIsAuto = (current == autoValue);
114 autoToggle->setChecked(currentIsAuto);
115 if (currentIsAuto)
116 previous = defaultValue;
117
118 layout->addWidget(autoToggle, row++, 0);
119 autoToggle->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
120 }
121
122 layout->addWidget(spinbox, row++, 0);
123
124 if(hasMinMax)
125 {
126 // stick the slider in a horizontal layout so that we can apply a separate margin.
127 // necessary because QSlider aligns itself with about 4px left padding and 0px right
128 QHBoxLayout* sliderLayout = new QHBoxLayout();
129 sliderLayout->setContentsMargins(-2, 0, 2, 0);
130 sliderLayout->addWidget(slider);
131 layout->addLayout(sliderLayout, row++, 0);
132 }
133
134 dialog.setMaximumWidth(1);
135 dialog.setMaximumHeight(1);
136 dialog.adjustSize();
137
140
141 spinbox->setFocus();
142 spinbox->selectAll();
143}
144
148void VuoInputEditorInteger::setAutoToggled(int state)
149{
150 // Avoid call to onSpinboxUpdate prompted by setSpecialValueText.
151 disconnect(spinbox, static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged), this, &VuoInputEditorInteger::onSpinboxUpdate);
152
153 if(state == Qt::Unchecked)
154 {
155 spinbox->setSpecialValueText("");
156 spinbox->unsetLineEditBounds();
157 current = previous;
158 }
159 else
160 {
161 previous = current;
162 current = autoValue;
163 spinbox->setSpecialValueText(" Auto ");
164 spinbox->setMinimum(autoValue);
165 spinbox->setMaximum(autoValue);
166 }
167
168 connect(spinbox, static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged), this, &VuoInputEditorInteger::onSpinboxUpdate);
169
170 spinbox->setValue(current);
171 spinbox->setEnabled(!autoToggle->isChecked());
172 if(slider) slider->setEnabled(!autoToggle->isChecked());
173}
174
178void VuoInputEditorInteger::onSliderUpdate(int sliderValue)
179{
180 current = sliderValue;
181
182 // disconnect before setting spinbox value otherwise onSpinboxUpdate is called and the whole thing just cycles
183 disconnect(spinbox, static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged), this, &VuoInputEditorInteger::onSpinboxUpdate);
184 spinbox->setValue( current );
185 connect(spinbox, static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged), this, &VuoInputEditorInteger::onSpinboxUpdate);
186
187 spinbox->setFocus();
188 spinbox->selectAll();
189
191}
192
196void VuoInputEditorInteger::onSpinboxUpdate(QString spinboxValue)
197{
198 current = QLocale().toInt(spinboxValue);
199
200 if(slider != NULL)
201 {
202 disconnect(slider, &QSlider::valueChanged, this, &VuoInputEditorInteger::onSliderUpdate);
203 slider->setValue( current );
204 connect(slider, &QSlider::valueChanged, this, &VuoInputEditorInteger::onSliderUpdate);
205 }
206
208}
209
214{
215 return VuoInteger_getJson(current);
216}