Vuo  2.3.1
VuoInputEditorInteger.cc
Go to the documentation of this file.
1 
10 #include "VuoInputEditorInteger.hh"
11 
16 {
17  return new VuoInputEditorInteger();
18 }
19 
24 void 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 
138  setFirstWidgetInTabOrder(spinbox);
139  setLastWidgetInTabOrder(spinbox);
140 
141  spinbox->setFocus();
142 }
143 
147 void VuoInputEditorInteger::setAutoToggled(int state)
148 {
149  // Avoid call to onSpinboxUpdate prompted by setSpecialValueText.
150  disconnect(spinbox, static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged), this, &VuoInputEditorInteger::onSpinboxUpdate);
151 
152  if(state == Qt::Unchecked)
153  {
154  spinbox->setSpecialValueText("");
155  spinbox->unsetLineEditBounds();
156  current = previous;
157  }
158  else
159  {
160  previous = current;
161  current = autoValue;
162  spinbox->setSpecialValueText(" Auto ");
163  spinbox->setMinimum(autoValue);
164  spinbox->setMaximum(autoValue);
165  }
166 
167  connect(spinbox, static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged), this, &VuoInputEditorInteger::onSpinboxUpdate);
168 
169  spinbox->setValue(current);
170  spinbox->setEnabled(!autoToggle->isChecked());
171  if(slider) slider->setEnabled(!autoToggle->isChecked());
172 }
173 
177 void VuoInputEditorInteger::onSliderUpdate(int sliderValue)
178 {
179  current = sliderValue;
180 
181  // disconnect before setting spinbox value otherwise onSpinboxUpdate is called and the whole thing just cycles
182  disconnect(spinbox, static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged), this, &VuoInputEditorInteger::onSpinboxUpdate);
183  spinbox->setValue( current );
184  connect(spinbox, static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged), this, &VuoInputEditorInteger::onSpinboxUpdate);
185 
186  spinbox->setFocus();
187  spinbox->selectAll();
188 
189  emit valueChanged( getAcceptedValue() );
190 }
191 
195 void VuoInputEditorInteger::onSpinboxUpdate(QString spinboxValue)
196 {
197  current = QLocale().toInt(spinboxValue);
198 
199  if(slider != NULL)
200  {
201  disconnect(slider, &QSlider::valueChanged, this, &VuoInputEditorInteger::onSliderUpdate);
202  slider->setValue( current );
203  connect(slider, &QSlider::valueChanged, this, &VuoInputEditorInteger::onSliderUpdate);
204  }
205 
207 }
208 
213 {
214  return VuoInteger_getJson(current);
215 }