Vuo  2.0.1
VuoInputEditorWithMenu.cc
Go to the documentation of this file.
1 
11 
15 json_object * VuoInputEditorWithMenu::show(QPoint portLeftCenter, json_object *originalValue, json_object *details, map<QString, json_object *> portNamesAndValues)
16 {
17  acceptedValue = originalValue;
18  this->details = details;
19  VuoInputEditorMenuItem *menuTree = setUpMenuTree(details);
20 
21  QMenu *menu = new QMenu(reinterpret_cast<QWidget *>(parent()));
22  menu->setSeparatorsCollapsible(false);
23  menu->setFont(getDefaultFont());
24  QActionGroup *menuActionGroup;
25  menuActionGroup = new QActionGroup(menu);
26 
27  connect(menuActionGroup, &QActionGroup::triggered, this, &VuoInputEditorWithMenu::acceptAction);
28 
29  VuoInputEditorMenuItem::buildMenu(menu, menuActionGroup, menuTree);
30  menuActionGroup->setExclusive(true);
31 
32  // Select the menu item corresponding to the original value.
33  int checkIndex = 0;
34  int index = 0;
35  string originalValueAsString = json_object_to_json_string_ext(originalValue, JSON_C_TO_STRING_PLAIN);
36  foreach (QAction *action, menuActionGroup->actions())
37  {
38  json_object *actionValue = (json_object *)action->data().value<void *>();
39  string actionValueAsString = json_object_to_json_string_ext(actionValue, JSON_C_TO_STRING_PLAIN);
40 
41  bool isOriginalValue = (actionValueAsString == originalValueAsString)
42  // Support upgrading a VuoBoolean port to a named enum.
43  || (originalValueAsString == "false" && actionValueAsString == "0")
44  || (originalValueAsString == "true" && actionValueAsString == "1");
45 
46  action->setChecked(isOriginalValue);
47  if (isOriginalValue)
48  checkIndex = index;
49  ++index;
50  }
51 
52  // Position the right center of the selected menu item at the left center of the port.
53  int xOffset = menu->sizeHint().width();
54  int yOffset = (checkIndex + 0.5) / menu->actions().size() * menu->sizeHint().height();
55  portLeftCenter = portLeftCenter - QPoint(xOffset, yOffset);
56  menu->exec(portLeftCenter);
57 
58  return acceptedValue;
59 }
60 
62 {
63  return setUpMenuTree();
64 }
65 
67 {
68  return new VuoInputEditorMenuItem("root");
69 }
70 
74 void VuoInputEditorWithMenu::acceptAction(QAction *action)
75 {
76  acceptedValue = (json_object *)action->data().value<void *>();
77 }
78 
84 {
85  json_object *includeValues = NULL;
86  json_object_object_get_ex(details, "includeValues", &includeValues);
87  if (!includeValues)
88  return true;
89 
90  bool includeThisValue = false;
91  int includeValuesCount = json_object_array_length(includeValues);
92  const char *valueString = json_object_to_json_string(value);
93  for (int j = 0; j < includeValuesCount; ++j)
94  {
95  const char *includeValueString = json_object_to_json_string(json_object_array_get_idx(includeValues, j));
96  if (strcmp(valueString, includeValueString) == 0)
97  {
98  includeThisValue = true;
99  break;
100  }
101  }
102 
103  return includeThisValue;
104 }
105 
110 {
111  json_object *o;
112  if (json_object_object_get_ex(details, "isDark", &o))
113  return json_object_get_boolean(o);
114  return false;
115 }