Vuo  2.0.1
VuoInputEditorWithDialog.cc
Go to the documentation of this file.
1 
12 
17  : VuoInputEditor()
18 {
19  dialogPointer = NULL;
20  firstWidgetInTabOrder = NULL;
21  lastWidgetInTabOrder = NULL;
22 }
23 
27 json_object * VuoInputEditorWithDialog::show(QPoint portLeftCenter, json_object *originalValue, json_object *details, map<QString, json_object *> portNamesAndValues)
28 {
29  json_object *o = NULL;
30 
31  isDark = false;
32  if (json_object_object_get_ex(details, "isDark", &o))
33  isDark = json_object_get_boolean(o);
34 
35  bool showArrow = true;
36  if (json_object_object_get_ex(details, "showArrow", &o))
37  showArrow = json_object_get_boolean(o);
38 
39  VuoDialogForInputEditor dialog(isDark, showArrow);
40  dialogPointer = &dialog;
41  setUpDialog(dialog, originalValue, details);
42 
43  // Move children to account for margins if no autolayout is used.
44  QMargins margin = dialog.getPopoverContentsMargins();
45 
46  if( dialog.layout() == 0 )
47  {
48  QPoint topLeftMargin = QPoint(margin.left(),margin.top());
49  foreach (QObject *widget, dialog.children())
50  static_cast<QWidget *>(widget)->move(static_cast<QWidget *>(widget)->pos() + topLeftMargin);
51  }
52 
53  // Resize dialog to enclose child widgets and margins.
54  dialog.adjustSize();
55 
56  // Position the right center of the dialog at the left center of the port, or wherever required
57  // so that the entire dialog fits within the available screenspace.
58  int availableSpaceLeftBoundary = QApplication::desktop()->availableGeometry(&dialog).left();
59  QPoint dialogTopLeft = portLeftCenter - QPoint(dialog.width() - (showArrow ? 0 : margin.right()), dialog.height()/2.);
60  bool screenSpaceLimitsPos = (availableSpaceLeftBoundary > dialogTopLeft.x());
61  if (screenSpaceLimitsPos)
62  {
63  dialogTopLeft = (screenSpaceLimitsPos? QPoint(availableSpaceLeftBoundary, dialogTopLeft.y()) :
64  dialogTopLeft);
65  showArrow = false;
66  }
67  dialog.setShowArrow(showArrow);
68  dialog.move(dialogTopLeft);
69 
70 
71  dialog.show(); // Needed to position the dialog. (https://bugreports.qt-project.org/browse/QTBUG-31406)
72  dialog.exec();
73 
74  dialogPointer = NULL;
75 
76  return (dialog.result() == QDialog::Accepted ? getAcceptedValue() : originalValue);
77 }
78 
83 {
84  return dialogPointer;
85 }
86 
93 {
94  if (firstWidgetInTabOrder)
95  firstWidgetInTabOrder->removeEventFilter(this);
96 
97  if (widget)
98  widget->installEventFilter(this);
99 
100  firstWidgetInTabOrder = widget;
101 }
102 
109 {
110  if (lastWidgetInTabOrder)
111  lastWidgetInTabOrder->removeEventFilter(this);
112 
113  if (widget)
114  widget->installEventFilter(this);
115 
116  lastWidgetInTabOrder = widget;
117 }
118 
122 bool VuoInputEditorWithDialog::eventFilter(QObject *object, QEvent *event)
123 {
124  // If the input editor's final tab cycle element has received a 'Tab' keypress,
125  // or its first tab cycle element has received a 'Shift'+'Tab' keypress,
126  // emit the appropriate signal and then treat the 'Tab' as an 'Return' to accept and
127  // close the input editor.
128  if (event->type() == QEvent::KeyPress)
129  {
130  QKeyEvent *keyEvent = (QKeyEvent *)(event);
131  bool tabPressed = (keyEvent->key() == Qt::Key_Tab);
132  bool shiftTabPressed = (keyEvent->key() == Qt::Key_Backtab);
133 
134  bool aboutToCompleteReverseTabCycle = ((object == firstWidgetInTabOrder) && shiftTabPressed);
135  bool aboutToCompleteTabCycle = ((object == lastWidgetInTabOrder) && tabPressed);
136 
137  if (aboutToCompleteReverseTabCycle || aboutToCompleteTabCycle)
138  {
139  QKeyEvent modifiedKeyEvent(event->type(), Qt::Key_Return, 0);
140  QApplication::sendEvent(object, &modifiedKeyEvent);
141 
142  if (aboutToCompleteReverseTabCycle)
143  {
146  }
147 
148  else // if (aboutToCompleteTabCycle)
149  {
151  emit tabbedPastLastWidget();
152  }
153 
154  return true;
155  }
156  }
157 
158  return VuoInputEditor::eventFilter(object, event);
159 }