Vuo  2.4.0
VuoInputEditorAnchor.cc
Go to the documentation of this file.
1
10#include "VuoInputEditorIcon.hh"
11
12extern "C"
13{
14}
15
19QIcon* 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
40QIcon* 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
104int 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
114void 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 for(int r = 0; r < 3; r++)
133 {
134 for(int c = 0; c < 3; c++)
135 {
136 int index = r * 3 + c;
137
138 QIcon* icon = iconForAnchorIndex(index);
139 matrix[index] = new QPushButton(*icon, "");
140 delete icon;
141 matrix[index]->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
142 matrix[index]->setCheckable(true);
143 matrix[index]->setChecked(false);
144 matrix[index]->setAutoExclusive(true);
145 matrix[index]->setFocusPolicy(Qt::NoFocus);
146 matrix[index]->setDown(false);
147 matrix[index]->setFlat(true);
148 matrix[index]->setContentsMargins(0,0,0,0);
149 matrix[index]->setMaximumWidth(24);
150 matrix[index]->setMaximumHeight(24);
151 matrix[index]->setMinimumWidth(24);
152 matrix[index]->setMinimumHeight(24);
153 if(isDark)
154 matrix[index]->setStyleSheet( "QPushButton:checked { background: #8c8c8c; border: none; outline: none; } QPushButton:pressed { background: #9c9c9c; border: none; outline: none; }" );
155 else
156 matrix[index]->setStyleSheet( "QPushButton:checked { background: #c0c0c0; border: none; outline: none; } QPushButton:pressed { background: #b0b0b0; border: none; outline: none; }" );
157 connect(matrix[index], &QPushButton::released, this, &VuoInputEditorAnchor::onSetAnchor);
158 layout->addWidget(matrix[index], r, c);
159 }
160 }
161
162 matrix[curIndex]->setChecked(true);
163
164 dialog.setFocusPolicy(Qt::NoFocus);
165 dialog.setLayout(layout);
166}
167
168void VuoInputEditorAnchor::onSetAnchor()
169{
170 int anchor = 0;
171 for (int i = 0; i < 9; ++i)
172 if (matrix[i] == sender())
173 {
174 anchor = i;
175 break;
176 }
177
178 matrix[anchor]->setChecked(true);
179 currentValue = VuoAnchor_getJson(ANCHOR_MAP[anchor]);
180 char* sum = VuoAnchor_getSummary(ANCHOR_MAP[anchor]);
181 free(sum);
182 emit valueChanged(currentValue);
183}
184
189{
190 return currentValue;
191}