Vuo  2.0.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  if(state == Qt::Unchecked)
150  {
151  spinbox->setSpecialValueText("");
152  spinbox->setMinimum(suggestedMin);
153  spinbox->setMaximum(suggestedMax);
154  current = previous;
155  }
156  else
157  {
158  previous = current;
159  current = autoValue;
160  spinbox->setSpecialValueText(" Auto ");
161  spinbox->setMinimum(autoValue);
162  spinbox->setMaximum(autoValue);
163  }
164 
165  spinbox->setValue(current);
166  spinbox->setEnabled(!autoToggle->isChecked());
167  if(slider) slider->setEnabled(!autoToggle->isChecked());
168 }
169 
173 void VuoInputEditorInteger::onSliderUpdate(int sliderValue)
174 {
175  current = sliderValue;
176 
177  // disconnect before setting spinbox value otherwise onSpinboxUpdate is called and the whole thing just cycles
178  disconnect(spinbox, static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged), this, &VuoInputEditorInteger::onSpinboxUpdate);
179  spinbox->setValue( current );
180  connect(spinbox, static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged), this, &VuoInputEditorInteger::onSpinboxUpdate);
181 
182  spinbox->setFocus();
183  spinbox->selectAll();
184 
185  emit valueChanged( getAcceptedValue() );
186 }
187 
191 void VuoInputEditorInteger::onSpinboxUpdate(QString spinboxValue)
192 {
193  current = QLocale().toInt(spinboxValue);
194 
195  if(slider != NULL)
196  {
197  disconnect(slider, &QSlider::valueChanged, this, &VuoInputEditorInteger::onSliderUpdate);
198  slider->setValue( current );
199  connect(slider, &QSlider::valueChanged, this, &VuoInputEditorInteger::onSliderUpdate);
200  }
201 
203 }
204 
209 {
210  return VuoInteger_getJson(current);
211 }