Vuo  2.0.1
VuoInputEditorColor.cc
Go to the documentation of this file.
1 
10 #include "VuoInputEditorColor.hh"
11 
16 {
17  return new VuoInputEditorColor();
18 }
19 
23 VuoColor VuoInputEditorColor::vuoColorFromQColor(const QColor &qtColor)
24 {
25  return VuoColor_makeWithRGBA(qtColor.redF(), qtColor.greenF(), qtColor.blueF(), qtColor.alphaF());
26 }
27 
31 json_object *VuoInputEditorColor::show(QPoint portLeftCenter, json_object *originalValue, json_object *details, map<QString, json_object *> portNamesAndValues)
32 {
33  QColorDialog dialog;
34 
35  VuoColor colorVuo = VuoColor_makeFromJson(originalValue);
36  QColor colorQt;
37  colorQt.setRedF(colorVuo.r);
38  colorQt.setGreenF(colorVuo.g);
39  colorQt.setBlueF(colorVuo.b);
40  colorQt.setAlphaF(colorVuo.a);
41  dialog.setCurrentColor(colorQt);
42  dialog.setOption(QColorDialog::ShowAlphaChannel);
43 
44  // Position the right center of the dialog at the left center of the port.
45  // Estimate the dialog size, since QColorDialog doesn't report its actual size.
46  QPoint dialogTopLeft = portLeftCenter - QPoint(250, 400/2) /*QPoint(dialog.childrenRect().width(), dialog.childrenRect().height()/2.)*/;
47  dialog.move(dialogTopLeft);
48 
49  connect(&dialog, &QColorDialog::currentColorChanged, this, &VuoInputEditorColor::currentColorChanged);
50 
51  dialog.exec();
52 
53  if (dialog.result() == QDialog::Accepted)
54  return VuoColor_getJson(vuoColorFromQColor(dialog.selectedColor()));
55 
56  return originalValue;
57 }
58 
62 void VuoInputEditorColor::currentColorChanged(const QColor &color)
63 {
64  json_object *valueAsJson = VuoColor_getJson(vuoColorFromQColor(color));
65  emit valueChanged(valueAsJson);
66  json_object_put(valueAsJson);
67 }