Vuo  2.0.0
VuoInputEditorEdgeBlend.cc
Go to the documentation of this file.
1 
10 
15 {
16  return new VuoInputEditorEdgeBlend();
17 }
18 
22 void VuoInputEditorEdgeBlend::setupSpinBox(VuoDoubleSpinBox* spin, double min, double max, double step, double value)
23 {
24  const int decimalPrecision = DBL_MAX_10_EXP + DBL_DIG;
25  spin->setDecimals(decimalPrecision);
26  spin->setButtonMinimum(min);
27  spin->setButtonMaximum(max);
28  spin->setSingleStep(step);
29  spin->setValue(value);
30 }
31 
32 void VuoInputEditorEdgeBlend::setupSlider(QSlider* slider, double min, double max, double step, double value)
33 {
34  // slider->setAttribute(Qt::WA_MacSmallSize);
35  slider->setOrientation(Qt::Horizontal);
36  slider->setFocusPolicy(Qt::NoFocus);
37  slider->setMinimum(0);
38  slider->setMaximum(slider->width());
39  slider->setSingleStep(fmax(1, step*(slider->maximum()-slider->minimum())/(max-min)));
40  slider->setValue( VuoDoubleSpinBox::doubleToSlider(slider->minimum(), slider->maximum(), min, max, value) );
41 }
42 
46 bool getDetails(json_object* details, VuoEdgeBlend* suggestedMin, VuoEdgeBlend* suggestedMax, VuoEdgeBlend* suggestedStep)
47 {
48  bool hasMin = false, hasMax = false;
49 
50  if (details != NULL)
51  {
52  // "suggestedMin"
53  json_object *suggestedMinValue = NULL;
54  if (json_object_object_get_ex(details, "suggestedMin", &suggestedMinValue))
55  {
56  *suggestedMin = VuoEdgeBlend_makeFromJson(suggestedMinValue);
57  hasMin = true;
58  }
59 
60  // "suggestedMax"
61  json_object *suggestedMaxValue = NULL;
62  if (json_object_object_get_ex(details, "suggestedMax", &suggestedMaxValue))
63  {
64  *suggestedMax = VuoEdgeBlend_makeFromJson(suggestedMaxValue);
65  hasMax = true;
66  }
67 
68  // "suggestedStep"
69  json_object *suggestedStepValue = NULL;
70  if (json_object_object_get_ex(details, "suggestedStep", &suggestedStepValue))
71  *suggestedStep = VuoEdgeBlend_makeFromJson(suggestedStepValue);
72  }
73 
74  return hasMin && hasMax;
75 }
76 
80 void VuoInputEditorEdgeBlend::setUpDialog(QDialog &dialog, json_object *originalValue, json_object *details)
81 {
82  suggestedMin = VuoEdgeBlend_make(0,0,0);
83  suggestedMax = VuoEdgeBlend_make(0.5,3,0.5);
84  suggestedStep = VuoEdgeBlend_make(.1,.1,.1);
85 
86  // get suggestedMin/Max/Step if provided, otherwise use the defaults
87  bool hasMinMax = getDetails(details, &suggestedMin, &suggestedMax, &suggestedStep);
88 
89  bool tabCycleForward = true;
90 
91  if(details)
92  {
93  json_object *forwardTabTraversal = NULL;
94 
95  if (json_object_object_get_ex(details, "forwardTabTraversal", &forwardTabTraversal))
96  tabCycleForward = json_object_get_boolean(forwardTabTraversal);
97  }
98 
99  QGridLayout* layout = new QGridLayout;
100 
101  // left, top, right, bottom
102  // when showing sliders, add a little extra margin on the bottom since VuoDoubleSpinBox takes up the
103  // entire vertical spacing
104  layout->setContentsMargins(4, 6, 12, hasMinMax ? 6 : 4);
105  layout->setSpacing(8);
106 
107  currentValue = VuoEdgeBlend_makeFromJson(originalValue);
108 
109  unsigned int row = 0;
110 
111  // crop spinbox
112  {
113  spinBox_crop = new VuoDoubleSpinBox(&dialog, 7);
114  setupSpinBox(spinBox_crop, suggestedMin.crop, suggestedMax.crop, suggestedStep.crop, currentValue.crop);
115  connect(spinBox_crop, static_cast<void (QDoubleSpinBox::*)(const QString &)>(&QDoubleSpinBox::valueChanged), this, &VuoInputEditorEdgeBlend::setCrop);
116 
117  layout->addWidget( new QLabel("Crop"), row, 0 );
118  layout->addWidget( spinBox_crop, row, 1 );
119 
120  row++;
121 
122  // show sliders if min/max are both provided
123  if(hasMinMax)
124  {
125  slider_crop = new QSlider;
126  setupSlider(slider_crop, suggestedMin.crop, suggestedMax.crop, suggestedStep.crop, currentValue.crop);
127  crop_slider_range = slider_crop->maximum();
128  connect(slider_crop, &QSlider::valueChanged, this, &VuoInputEditorEdgeBlend::setCropSlider);
129 
130  layout->addWidget(slider_crop, row, 1);
131  row++;
132  }
133  else
134  {
135  slider_crop = NULL;
136  }
137  }
138 
139  row++;
140 
141  // cutoff spinbox
142  {
143  spinBox_cutoff = new VuoDoubleSpinBox(&dialog, 7);
144  setupSpinBox(spinBox_cutoff, suggestedMin.cutoff, suggestedMax.cutoff, suggestedStep.cutoff, currentValue.cutoff);
145 
146  connect(spinBox_cutoff, static_cast<void (QDoubleSpinBox::*)(const QString &)>(&QDoubleSpinBox::valueChanged), this, &VuoInputEditorEdgeBlend::setCutoff);
147  layout->addWidget( new QLabel("Cutoff"), row, 0 );
148  layout->addWidget( spinBox_cutoff, row, 1);
149 
150  row++;
151 
152  // show sliders if min/max are both provided
153  if(hasMinMax)
154  {
155  slider_cutoff = new QSlider;
156  setupSlider(slider_cutoff, suggestedMin.cutoff, suggestedMax.cutoff, suggestedStep.cutoff, currentValue.cutoff);
157  cutoff_slider_range = slider_cutoff->maximum();
158  connect(slider_cutoff, &QSlider::valueChanged, this, &VuoInputEditorEdgeBlend::setCutoffSlider);
159 
160  layout->addWidget(slider_cutoff, row, 1);
161 
162  row++;
163  }
164  else
165  {
166  slider_cutoff = NULL;
167  }
168  }
169 
170  row++;
171 
172  // gamma spinbox
173  {
174  spinBox_gamma = new VuoDoubleSpinBox(&dialog, 7);
175  setupSpinBox(spinBox_gamma, suggestedMin.gamma, suggestedMax.gamma, suggestedStep.gamma, currentValue.gamma);
176  connect(spinBox_gamma, static_cast<void (QDoubleSpinBox::*)(const QString &)>(&QDoubleSpinBox::valueChanged), this, &VuoInputEditorEdgeBlend::setGamma);
177 
178  layout->addWidget( new QLabel("Gamma"), row, 0 );
179  layout->addWidget( spinBox_gamma, row, 1 );
180 
181  row++;
182 
183  // show sliders if min/max are both provided
184  if(hasMinMax)
185  {
186  slider_gamma = new QSlider;
187  setupSlider(slider_gamma, suggestedMin.gamma, suggestedMax.gamma, suggestedStep.gamma, currentValue.gamma);
188  gamma_slider_range = slider_gamma->maximum();
189  connect(slider_gamma, &QSlider::valueChanged, this, &VuoInputEditorEdgeBlend::setGammaSlider);
190 
191  layout->addWidget(slider_gamma, row, 1);
192  row++;
193  }
194  else
195  {
196  slider_gamma = NULL;
197  }
198  }
199 
200  dialog.setLayout(layout);
201 
202  dialog.setMaximumWidth(32);
203  dialog.setMaximumHeight(32);
204 
205  setFirstWidgetInTabOrder(spinBox_crop);
206  setLastWidgetInTabOrder(spinBox_gamma);
207 
208  (tabCycleForward ? spinBox_crop : spinBox_gamma)->setFocus();
209  (tabCycleForward ? spinBox_crop : spinBox_gamma)->selectAll();
210 
211  return;
212 }
213 
214 void VuoInputEditorEdgeBlend::setCutoffSlider(int newSliderValue)
215 {
216  currentValue.cutoff = VuoDoubleSpinBox::sliderToDouble(slider_cutoff->minimum(), slider_cutoff->maximum(), suggestedMin.cutoff, suggestedMax.cutoff, newSliderValue);
217  spinBox_cutoff->setValue(currentValue.cutoff);
218  spinBox_cutoff->setFocus();
219  spinBox_cutoff->selectAll();
220  emit valueChanged( getAcceptedValue() );
221 }
222 
223 void VuoInputEditorEdgeBlend::setGammaSlider(int newSliderValue)
224 {
225  currentValue.gamma = VuoDoubleSpinBox::sliderToDouble(slider_gamma->minimum(), slider_gamma->maximum(), suggestedMin.gamma, suggestedMax.gamma, newSliderValue);
226  spinBox_gamma->setValue(currentValue.gamma);
227  spinBox_gamma->setFocus();
228  spinBox_gamma->selectAll();
229  emit valueChanged( getAcceptedValue() );
230 }
231 
232 void VuoInputEditorEdgeBlend::setCropSlider(int newSliderValue)
233 {
234  currentValue.crop = VuoDoubleSpinBox::sliderToDouble(slider_crop->minimum(), slider_crop->maximum(), suggestedMin.crop, suggestedMax.crop, newSliderValue);
235  spinBox_crop->setValue(currentValue.crop);
236  spinBox_crop->setFocus();
237  spinBox_crop->selectAll();
238  emit valueChanged( getAcceptedValue() );
239 }
240 
241 void VuoInputEditorEdgeBlend::setCutoff(QString newLineEditText)
242 {
243  double newLineEditValue = QLocale().toDouble(newLineEditText);
244 
245  if(slider_cutoff != NULL)
246  {
247  int newSliderValue = VuoDoubleSpinBox::doubleToSlider(slider_cutoff->minimum(), slider_cutoff->maximum(), suggestedMin.cutoff, suggestedMax.cutoff, newLineEditValue);
248  disconnect(slider_cutoff, &QSlider::valueChanged, this, &VuoInputEditorEdgeBlend::setCutoffSlider);
249  slider_cutoff->setValue(newSliderValue);
250  connect(slider_cutoff, &QSlider::valueChanged, this, &VuoInputEditorEdgeBlend::setCutoffSlider);
251  }
252 
253  currentValue.cutoff = newLineEditValue;
254  emit valueChanged( getAcceptedValue() );
255 }
256 
257 void VuoInputEditorEdgeBlend::setGamma(QString newLineEditText)
258 {
259  double newLineEditValue = QLocale().toDouble(newLineEditText);
260 
261  if(slider_gamma != NULL)
262  {
263  int newSliderValue = VuoDoubleSpinBox::doubleToSlider(slider_gamma->minimum(), slider_gamma->maximum(), suggestedMin.gamma, suggestedMax.gamma, newLineEditValue);
264 
265  disconnect(slider_gamma, &QSlider::valueChanged, this, &VuoInputEditorEdgeBlend::setGammaSlider);
266  slider_gamma->setValue(newSliderValue);
267  connect(slider_gamma, &QSlider::valueChanged, this, &VuoInputEditorEdgeBlend::setGammaSlider);
268  }
269 
270  currentValue.gamma = newLineEditValue;
271  emit valueChanged( getAcceptedValue() );
272 }
273 
274 void VuoInputEditorEdgeBlend::setCrop(QString newLineEditText)
275 {
276  double newLineEditValue = QLocale().toDouble(newLineEditText);
277 
278  if(slider_crop != NULL)
279  {
280  int newSliderValue = VuoDoubleSpinBox::doubleToSlider(slider_crop->minimum(), slider_crop->maximum(), suggestedMin.crop, suggestedMax.crop, newLineEditValue);
281 
282  disconnect(slider_crop, &QSlider::valueChanged, this, &VuoInputEditorEdgeBlend::setCropSlider);
283  slider_crop->setValue(newSliderValue);
284  connect(slider_crop, &QSlider::valueChanged, this, &VuoInputEditorEdgeBlend::setCropSlider);
285  }
286 
287  currentValue.crop = newLineEditValue;
288  emit valueChanged( getAcceptedValue() );
289 }
290 
295 {
296  return VuoEdgeBlend_getJson( currentValue );
297 }