Vuo  2.0.1
VuoInputEditorAnchor.cc
Go to the documentation of this file.
1 
10 #include "VuoInputEditorIcon.hh"
11 
12 extern "C"
13 {
14 }
15 
19 QIcon* VuoInputEditorAnchor::renderIconWithLineSegments(const float* points, const unsigned int length)
20 {
21  // needs a minimum of 2 points
22  if (length % 4 != 0)
23  return NULL;
24 
26  ^(QPainter &p)
27  {
28  QPainterPath path;
29 
30  for(int i = 0; i < length; i += 4)
31  {
32  path.moveTo(points[i+0], points[i+1]);
33  path.lineTo(points[i+2], points[i+3]);
34  }
35 
36  p.strokePath(path, QPen(Qt::black, 1));
37  });
38 }
39 
40 QIcon* VuoInputEditorAnchor::iconForAnchorIndex(const int anchor)
41 {
42  const float left_top[8] = { 1.f, 1.f, 1.f, 15.f, 1.f, 1.f, 15.f, 1.f };
43  const float center_top[4] = { 1.f, 1.f, 15.f, 1.f };
44  const float right_top[8] = { 1.f, 1.f, 15.f, 1.f, 15.f, 1.f, 15.f, 15.f};
45 
46  const float left_center[4] = { 1.f, 1.f, 1.f, 15.f };
47  const float center_center[8] = { 8.f, 1.f, 8.f, 15.f, 1.f, 8.f, 15.f, 8.f };
48  const float right_center[4] = { 15.f, 1.f, 15.f, 15.f };
49 
50  const float left_bottom[8] = { 1.f, 15.f, 1.f, 1.f, 1.f, 15.f, 15.f, 15.f };
51  const float center_bottom[4] = { 1.f, 15.f, 15.f, 15.f };
52  const float right_bottom[8] = { 1.f, 15.f, 15.f, 15.f, 15.f, 15.f, 15.f, 1.f };
53 
54  switch(anchor)
55  {
56  // VuoHorizontalAlignment_Left, VuoVerticalAlignment_Top
57  case 0:
58  return renderIconWithLineSegments( left_top, 8 );
59 
60  // VuoHorizontalAlignment_Center, VuoVerticalAlignment_Top
61  case 1:
62  return renderIconWithLineSegments( center_top, 4 );
63 
64  // VuoHorizontalAlignment_Right, VuoVerticalAlignment_Top
65  case 2:
66  return renderIconWithLineSegments( right_top, 8 );
67 
68  // VuoHorizontalAlignment_Left, VuoVerticalAlignment_Center
69  case 3:
70  return renderIconWithLineSegments( left_center, 4 );
71 
72  // VuoHorizontalAlignment_Center, VuoVerticalAlignment_Center
73  case 4:
74  return renderIconWithLineSegments( center_center, 8 );
75 
76  // VuoHorizontalAlignment_Right, VuoVerticalAlignment_Center
77  case 5:
78  return renderIconWithLineSegments( right_center, 4 );
79 
80  // VuoHorizontalAlignment_Left, VuoVerticalAlignment_Bottom
81  case 6:
82  return renderIconWithLineSegments( left_bottom, 8 );
83  // VuoHorizontalAlignment_Center, VuoVerticalAlignment_Bottom
84  case 7:
85  return renderIconWithLineSegments( center_bottom, 8 );
86 
87  // VuoHorizontalAlignment_Right, VuoVerticalAlignment_Bottom
88  case 8:
89  return renderIconWithLineSegments( right_bottom, 8 );
90 
91  default:
92  return renderIconWithLineSegments( left_top, 8 );
93  }
94 }
95 
100 {
101  return new VuoInputEditorAnchor();
102 }
103 
104 int VuoInputEditorAnchor::VuoAnchorToIndex(const VuoAnchor anchor)
105 {
106  int r = VuoAnchor_getVertical(anchor) == VuoVerticalAlignment_Top ? 0 : (VuoAnchor_getVertical(anchor) == VuoVerticalAlignment_Center ? 1 : 2);
107  int c = VuoAnchor_getHorizontal(anchor) == VuoHorizontalAlignment_Left ? 0 : (VuoAnchor_getHorizontal(anchor) == VuoHorizontalAlignment_Center ? 1 : 2);
108  return r * 3 + c;
109 }
110 
114 void VuoInputEditorAnchor::setUpDialog(QDialog &dialog, json_object *originalValue, json_object *details)
115 {
116  VuoAnchor curAnchor = originalValue == NULL ? VuoAnchor_make(VuoHorizontalAlignment_Center, VuoVerticalAlignment_Center) : VuoAnchor_makeFromJson(originalValue);
117  currentValue = VuoAnchor_getJson(curAnchor);
118  int curIndex = VuoAnchorToIndex(curAnchor);
119 
120  // layout dialog
121  QGridLayout* layout = new QGridLayout;
122  layout->setContentsMargins(4, 4, 12, 4);
123  layout->setSpacing(0);
124 
125  QSettings* settings = new QSettings();
126  bool isDark = settings->value("darkInterface").toBool();
127 
128  // left top center top right top
129  // left center center center right center
130  // left bottom center bottom right center
131 
132  signalMapper = new QSignalMapper(this);
133 
134  for(int r = 0; r < 3; r++)
135  {
136  for(int c = 0; c < 3; c++)
137  {
138  int index = r * 3 + c;
139 
140  QIcon* icon = iconForAnchorIndex(index);
141  matrix[index] = new QPushButton(*icon, "");
142  delete icon;
143  matrix[index]->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
144  matrix[index]->setCheckable(true);
145  matrix[index]->setChecked(false);
146  matrix[index]->setAutoExclusive(true);
147  matrix[index]->setFocusPolicy(Qt::NoFocus);
148  matrix[index]->setDown(false);
149  matrix[index]->setFlat(true);
150  matrix[index]->setContentsMargins(0,0,0,0);
151  matrix[index]->setMaximumWidth(24);
152  matrix[index]->setMaximumHeight(24);
153  matrix[index]->setMinimumWidth(24);
154  matrix[index]->setMinimumHeight(24);
155  if(isDark)
156  matrix[index]->setStyleSheet( "QPushButton:checked { background:#8B8C8C; border:none; outline:none; }" );
157  else
158  matrix[index]->setStyleSheet( "QPushButton:checked { background:#909090; border:none; outline:none; }" );
159  connect(matrix[index], &QPushButton::released, signalMapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
160  signalMapper->setMapping(matrix[index], index) ;
161  layout->addWidget(matrix[index], r, c);
162  }
163  }
164 
165  matrix[curIndex]->setChecked(true);
166 
167  connect(signalMapper, static_cast<void (QSignalMapper::*)(int)>(&QSignalMapper::mapped), this, &VuoInputEditorAnchor::onSetAnchor);
168 
169  dialog.setFocusPolicy(Qt::NoFocus);
170  dialog.setLayout(layout);
171 }
172 
173 void VuoInputEditorAnchor::onSetAnchor(int anchor)
174 {
175  matrix[anchor]->setChecked(true);
176  currentValue = VuoAnchor_getJson(ANCHOR_MAP[anchor]);
177  char* sum = VuoAnchor_getSummary(ANCHOR_MAP[anchor]);
178  free(sum);
179  emit valueChanged(currentValue);
180 }
181 
186 {
187  return currentValue;
188 }