src/addObjectDialog.cc

changeset 603
47e7773c7841
parent 600
209e3f1f7b2c
child 604
01bdac75994a
equal deleted inserted replaced
602:ac1744536b33 603:47e7773c7841
37 #include "moc_addObjectDialog.cpp" 37 #include "moc_addObjectDialog.cpp"
38 38
39 // ============================================================================= 39 // =============================================================================
40 // ----------------------------------------------------------------------------- 40 // -----------------------------------------------------------------------------
41 class SubfileListItem : public QTreeWidgetItem 41 class SubfileListItem : public QTreeWidgetItem
42 { PROPERTY (public, Primitive*, PrimitiveInfo, NO_OPS, STOCK_WRITE) 42 {
43 PROPERTY (public, Primitive*, PrimitiveInfo, NO_OPS, STOCK_WRITE)
43 44
44 public: 45 public:
45 SubfileListItem (QTreeWidgetItem* parent, Primitive* info) : 46 SubfileListItem (QTreeWidgetItem* parent, Primitive* info) :
46 QTreeWidgetItem (parent), 47 QTreeWidgetItem (parent),
47 m_PrimitiveInfo (info) {} 48 m_PrimitiveInfo (info) {}
53 54
54 // ============================================================================= 55 // =============================================================================
55 // ----------------------------------------------------------------------------- 56 // -----------------------------------------------------------------------------
56 AddObjectDialog::AddObjectDialog (const LDObject::Type type, LDObject* obj, QWidget* parent) : 57 AddObjectDialog::AddObjectDialog (const LDObject::Type type, LDObject* obj, QWidget* parent) :
57 QDialog (parent) 58 QDialog (parent)
58 { setlocale (LC_ALL, "C"); 59 {
60 setlocale (LC_ALL, "C");
59 61
60 int coordCount = 0; 62 int coordCount = 0;
61 str typeName = LDObject::typeName (type); 63 str typeName = LDObject::typeName (type);
62 64
63 switch (type) 65 switch (type)
64 { case LDObject::Comment: 66 {
65 { le_comment = new QLineEdit; 67 case LDObject::Comment:
68 {
69 le_comment = new QLineEdit;
66 70
67 if (obj) 71 if (obj)
68 le_comment->setText (static_cast<LDComment*> (obj)->text); 72 le_comment->setText (static_cast<LDComment*> (obj)->text);
69 73
70 le_comment->setMinimumWidth (384); 74 le_comment->setMinimumWidth (384);
71 } break; 75 } break;
72 76
73 case LDObject::Line: 77 case LDObject::Line:
74 { coordCount = 6; 78 {
79 coordCount = 6;
75 } break; 80 } break;
76 81
77 case LDObject::Triangle: 82 case LDObject::Triangle:
78 { coordCount = 9; 83 {
84 coordCount = 9;
79 } break; 85 } break;
80 86
81 case LDObject::Quad: 87 case LDObject::Quad:
82 case LDObject::CondLine: 88 case LDObject::CondLine:
83 { coordCount = 12; 89 {
90 coordCount = 12;
84 } break; 91 } break;
85 92
86 case LDObject::Vertex: 93 case LDObject::Vertex:
87 { coordCount = 3; 94 {
95 coordCount = 3;
88 } break; 96 } break;
89 97
90 case LDObject::BFC: 98 case LDObject::BFC:
91 { rb_bfcType = new RadioGroup ("Statement", {}, 0, Qt::Vertical); 99 {
100 rb_bfcType = new RadioGroup ("Statement", {}, 0, Qt::Vertical);
92 101
93 for (int i = 0; i < LDBFC::NumStatements; ++i) 102 for (int i = 0; i < LDBFC::NumStatements; ++i)
94 { // Separate these in two columns 103 {
104 // Separate these in two columns
95 if (i == LDBFC::NumStatements / 2) 105 if (i == LDBFC::NumStatements / 2)
96 rb_bfcType->rowBreak(); 106 rb_bfcType->rowBreak();
97 107
98 rb_bfcType->addButton (LDBFC::statements[i]); 108 rb_bfcType->addButton (LDBFC::statements[i]);
99 } 109 }
101 if (obj) 111 if (obj)
102 rb_bfcType->setValue ( (int) static_cast<LDBFC*> (obj)->type); 112 rb_bfcType->setValue ( (int) static_cast<LDBFC*> (obj)->type);
103 } break; 113 } break;
104 114
105 case LDObject::Subfile: 115 case LDObject::Subfile:
106 { coordCount = 3; 116 {
117 coordCount = 3;
107 tw_subfileList = new QTreeWidget(); 118 tw_subfileList = new QTreeWidget();
108 tw_subfileList->setHeaderLabel (tr ("Primitives")); 119 tw_subfileList->setHeaderLabel (tr ("Primitives"));
109 120
110 for (PrimitiveCategory* cat : g_PrimitiveCategories) 121 for (PrimitiveCategory* cat : g_PrimitiveCategories)
111 { SubfileListItem* parentItem = new SubfileListItem (tw_subfileList, null); 122 {
123 SubfileListItem* parentItem = new SubfileListItem (tw_subfileList, null);
112 parentItem->setText (0, cat->getName()); 124 parentItem->setText (0, cat->getName());
113 QList<QTreeWidgetItem*> subfileItems; 125 QList<QTreeWidgetItem*> subfileItems;
114 126
115 for (Primitive& prim : cat->prims) 127 for (Primitive& prim : cat->prims)
116 { SubfileListItem* item = new SubfileListItem (parentItem, &prim); 128 {
129 SubfileListItem* item = new SubfileListItem (parentItem, &prim);
117 item->setText (0, fmt ("%1 - %2", prim.name, prim.title)); 130 item->setText (0, fmt ("%1 - %2", prim.name, prim.title));
118 subfileItems << item; 131 subfileItems << item;
119 132
120 // If this primitive is the one the current object points to, 133 // If this primitive is the one the current object points to,
121 // select it by default 134 // select it by default
130 lb_subfileName = new QLabel ("File:"); 143 lb_subfileName = new QLabel ("File:");
131 le_subfileName = new QLineEdit; 144 le_subfileName = new QLineEdit;
132 le_subfileName->setFocus(); 145 le_subfileName->setFocus();
133 146
134 if (obj) 147 if (obj)
135 { LDSubfile* ref = static_cast<LDSubfile*> (obj); 148 {
149 LDSubfile* ref = static_cast<LDSubfile*> (obj);
136 le_subfileName->setText (ref->getFileInfo()->getName()); 150 le_subfileName->setText (ref->getFileInfo()->getName());
137 } 151 }
138 } break; 152 } break;
139 153
140 default: 154 default:
141 { critical (fmt ("Unhandled LDObject type %1 (%2) in AddObjectDialog", (int) type, typeName)); 155 {
156 critical (fmt ("Unhandled LDObject type %1 (%2) in AddObjectDialog", (int) type, typeName));
142 } return; 157 } return;
143 } 158 }
144 159
145 QPixmap icon = getIcon (fmt ("add-%1", typeName)); 160 QPixmap icon = getIcon (fmt ("add-%1", typeName));
146 LDObject* defaults = LDObject::getDefault (type); 161 LDObject* defaults = LDObject::getDefault (type);
148 lb_typeIcon = new QLabel; 163 lb_typeIcon = new QLabel;
149 lb_typeIcon->setPixmap (icon); 164 lb_typeIcon->setPixmap (icon);
150 165
151 // Show a color edit dialog for the types that actually use the color 166 // Show a color edit dialog for the types that actually use the color
152 if (defaults->isColored()) 167 if (defaults->isColored())
153 { if (obj != null) 168 {
169 if (obj != null)
154 colnum = obj->getColor(); 170 colnum = obj->getColor();
155 else 171 else
156 colnum = (type == LDObject::CondLine || type == LDObject::Line) ? edgecolor : maincolor; 172 colnum = (type == LDObject::CondLine || type == LDObject::Line) ? edgecolor : maincolor;
157 173
158 pb_color = new QPushButton; 174 pb_color = new QPushButton;
159 setButtonBackground (pb_color, colnum); 175 setButtonBackground (pb_color, colnum);
160 connect (pb_color, SIGNAL (clicked()), this, SLOT (slot_colorButtonClicked())); 176 connect (pb_color, SIGNAL (clicked()), this, SLOT (slot_colorButtonClicked()));
161 } 177 }
162 178
163 for (int i = 0; i < coordCount; ++i) 179 for (int i = 0; i < coordCount; ++i)
164 { dsb_coords[i] = new QDoubleSpinBox; 180 {
181 dsb_coords[i] = new QDoubleSpinBox;
165 dsb_coords[i]->setDecimals (5); 182 dsb_coords[i]->setDecimals (5);
166 dsb_coords[i]->setMinimum (-10000.0); 183 dsb_coords[i]->setMinimum (-10000.0);
167 dsb_coords[i]->setMaximum (10000.0); 184 dsb_coords[i]->setMaximum (10000.0);
168 } 185 }
169 186
170 QGridLayout* const layout = new QGridLayout; 187 QGridLayout* const layout = new QGridLayout;
171 layout->addWidget (lb_typeIcon, 0, 0); 188 layout->addWidget (lb_typeIcon, 0, 0);
172 189
173 switch (type) 190 switch (type)
174 { case LDObject::Line: 191 {
192 case LDObject::Line:
175 case LDObject::CondLine: 193 case LDObject::CondLine:
176 case LDObject::Triangle: 194 case LDObject::Triangle:
177 case LDObject::Quad: 195 case LDObject::Quad:
178 196
179 // Apply coordinates 197 // Apply coordinates
180 if (obj) 198 if (obj)
181 { for (int i = 0; i < coordCount / 3; ++i) 199 {
200 for (int i = 0; i < coordCount / 3; ++i)
182 for (int j = 0; j < 3; ++j) 201 for (int j = 0; j < 3; ++j)
183 dsb_coords[ (i * 3) + j]->setValue (obj->getVertex (i).coord (j)); 202 dsb_coords[ (i * 3) + j]->setValue (obj->getVertex (i).coord (j));
184 } 203 }
185 204
186 break; 205 break;
202 default: 221 default:
203 break; 222 break;
204 } 223 }
205 224
206 if (defaults->hasMatrix()) 225 if (defaults->hasMatrix())
207 { LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (obj); 226 {
227 LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (obj);
208 228
209 QLabel* lb_matrix = new QLabel ("Matrix:"); 229 QLabel* lb_matrix = new QLabel ("Matrix:");
210 le_matrix = new QLineEdit; 230 le_matrix = new QLineEdit;
211 // le_matrix->setValidator (new QDoubleValidator); 231 // le_matrix->setValidator (new QDoubleValidator);
212 matrix defaultMatrix = g_identity; 232 matrix defaultMatrix = g_identity;
213 233
214 if (mo) 234 if (mo)
215 { for_axes (ax) 235 {
236 for_axes (ax)
216 dsb_coords[ax]->setValue (mo->getPosition()[ax]); 237 dsb_coords[ax]->setValue (mo->getPosition()[ax]);
217 238
218 defaultMatrix = mo->getTransform(); 239 defaultMatrix = mo->getTransform();
219 } 240 }
220 241
225 246
226 if (defaults->isColored()) 247 if (defaults->isColored())
227 layout->addWidget (pb_color, 1, 0); 248 layout->addWidget (pb_color, 1, 0);
228 249
229 if (coordCount > 0) 250 if (coordCount > 0)
230 { QGridLayout* const qCoordLayout = new QGridLayout; 251 {
252 QGridLayout* const qCoordLayout = new QGridLayout;
231 253
232 for (int i = 0; i < coordCount; ++i) 254 for (int i = 0; i < coordCount; ++i)
233 qCoordLayout->addWidget (dsb_coords[i], (i / 3), (i % 3)); 255 qCoordLayout->addWidget (dsb_coords[i], (i / 3), (i % 3));
234 256
235 layout->addLayout (qCoordLayout, 0, 1, (coordCount / 3), 3); 257 layout->addLayout (qCoordLayout, 0, 1, (coordCount / 3), 3);
247 } 269 }
248 270
249 // ============================================================================= 271 // =============================================================================
250 // ----------------------------------------------------------------------------- 272 // -----------------------------------------------------------------------------
251 void AddObjectDialog::setButtonBackground (QPushButton* button, int colnum) 273 void AddObjectDialog::setButtonBackground (QPushButton* button, int colnum)
252 { LDColor* col = getColor (colnum); 274 {
275 LDColor* col = getColor (colnum);
253 276
254 button->setIcon (getIcon ("palette")); 277 button->setIcon (getIcon ("palette"));
255 button->setAutoFillBackground (true); 278 button->setAutoFillBackground (true);
256 279
257 if (col) 280 if (col)
259 } 282 }
260 283
261 // ============================================================================= 284 // =============================================================================
262 // ----------------------------------------------------------------------------- 285 // -----------------------------------------------------------------------------
263 str AddObjectDialog::currentSubfileName() 286 str AddObjectDialog::currentSubfileName()
264 { SubfileListItem* item = static_cast<SubfileListItem*> (tw_subfileList->currentItem()); 287 {
288 SubfileListItem* item = static_cast<SubfileListItem*> (tw_subfileList->currentItem());
265 289
266 if (item->getPrimitiveInfo() == null) 290 if (item->getPrimitiveInfo() == null)
267 return ""; // selected a heading 291 return ""; // selected a heading
268 292
269 return item->getPrimitiveInfo()->name; 293 return item->getPrimitiveInfo()->name;
270 } 294 }
271 295
272 // ============================================================================= 296 // =============================================================================
273 // ----------------------------------------------------------------------------- 297 // -----------------------------------------------------------------------------
274 void AddObjectDialog::slot_colorButtonClicked() 298 void AddObjectDialog::slot_colorButtonClicked()
275 { ColorSelector::selectColor (colnum, colnum, this); 299 {
300 ColorSelector::selectColor (colnum, colnum, this);
276 setButtonBackground (pb_color, colnum); 301 setButtonBackground (pb_color, colnum);
277 } 302 }
278 303
279 // ============================================================================= 304 // =============================================================================
280 // ----------------------------------------------------------------------------- 305 // -----------------------------------------------------------------------------
281 void AddObjectDialog::slot_subfileTypeChanged() 306 void AddObjectDialog::slot_subfileTypeChanged()
282 { str name = currentSubfileName(); 307 {
308 str name = currentSubfileName();
283 309
284 if (name.length() > 0) 310 if (name.length() > 0)
285 le_subfileName->setText (name); 311 le_subfileName->setText (name);
286 } 312 }
287 313
288 // ============================================================================= 314 // =============================================================================
289 // ----------------------------------------------------------------------------- 315 // -----------------------------------------------------------------------------
290 template<class T> static T* initObj (LDObject*& obj) 316 template<class T> static T* initObj (LDObject*& obj)
291 { if (obj == null) 317 {
318 if (obj == null)
292 obj = new T; 319 obj = new T;
293 320
294 return static_cast<T*> (obj); 321 return static_cast<T*> (obj);
295 } 322 }
296 323
297 // ============================================================================= 324 // =============================================================================
298 // ----------------------------------------------------------------------------- 325 // -----------------------------------------------------------------------------
299 void AddObjectDialog::staticDialog (const LDObject::Type type, LDObject* obj) 326 void AddObjectDialog::staticDialog (const LDObject::Type type, LDObject* obj)
300 { setlocale (LC_ALL, "C"); 327 {
328 setlocale (LC_ALL, "C");
301 329
302 // FIXME: Redirect to Edit Raw 330 // FIXME: Redirect to Edit Raw
303 if (obj && obj->getType() == LDObject::Error) 331 if (obj && obj->getType() == LDObject::Error)
304 return; 332 return;
305 333
314 342
315 if (dlg.exec() == false) 343 if (dlg.exec() == false)
316 return; 344 return;
317 345
318 if (type == LDObject::Subfile) 346 if (type == LDObject::Subfile)
319 { QStringList matrixstrvals = dlg.le_matrix->text().split (" ", QString::SkipEmptyParts); 347 {
348 QStringList matrixstrvals = dlg.le_matrix->text().split (" ", QString::SkipEmptyParts);
320 349
321 if (matrixstrvals.size() == 9) 350 if (matrixstrvals.size() == 9)
322 { double matrixvals[9]; 351 {
352 double matrixvals[9];
323 int i = 0; 353 int i = 0;
324 354
325 for (str val : matrixstrvals) 355 for (str val : matrixstrvals)
326 matrixvals[i++] = val.toFloat(); 356 matrixvals[i++] = val.toFloat();
327 357
328 transform = matrix (matrixvals); 358 transform = matrix (matrixvals);
329 } 359 }
330 } 360 }
331 361
332 switch (type) 362 switch (type)
333 { case LDObject::Comment: 363 {
334 { LDComment* comm = initObj<LDComment> (obj); 364 case LDObject::Comment:
365 {
366 LDComment* comm = initObj<LDComment> (obj);
335 comm->text = dlg.le_comment->text(); 367 comm->text = dlg.le_comment->text();
336 } 368 }
337 break; 369 break;
338 370
339 case LDObject::Line: 371 case LDObject::Line:
340 case LDObject::Triangle: 372 case LDObject::Triangle:
341 case LDObject::Quad: 373 case LDObject::Quad:
342 case LDObject::CondLine: 374 case LDObject::CondLine:
343 { if (!obj) 375 {
376 if (!obj)
344 obj = LDObject::getDefault (type); 377 obj = LDObject::getDefault (type);
345 378
346 for (int i = 0; i < obj->vertices(); ++i) 379 for (int i = 0; i < obj->vertices(); ++i)
347 { vertex v; 380 {
381 vertex v;
348 382
349 for_axes (ax) 383 for_axes (ax)
350 v[ax] = dlg.dsb_coords[ (i * 3) + ax]->value(); 384 v[ax] = dlg.dsb_coords[ (i * 3) + ax]->value();
351 385
352 obj->setVertex (i, v); 386 obj->setVertex (i, v);
353 } 387 }
354 } break; 388 } break;
355 389
356 case LDObject::BFC: 390 case LDObject::BFC:
357 { LDBFC* bfc = initObj<LDBFC> (obj); 391 {
392 LDBFC* bfc = initObj<LDBFC> (obj);
358 bfc->type = (LDBFC::Type) dlg.rb_bfcType->value(); 393 bfc->type = (LDBFC::Type) dlg.rb_bfcType->value();
359 } break; 394 } break;
360 395
361 case LDObject::Vertex: 396 case LDObject::Vertex:
362 { LDVertex* vert = initObj<LDVertex> (obj); 397 {
398 LDVertex* vert = initObj<LDVertex> (obj);
363 399
364 for_axes (ax) 400 for_axes (ax)
365 vert->pos[ax] = dlg.dsb_coords[ax]->value(); 401 vert->pos[ax] = dlg.dsb_coords[ax]->value();
366 } 402 }
367 break; 403 break;
368 404
369 case LDObject::Subfile: 405 case LDObject::Subfile:
370 { str name = dlg.le_subfileName->text(); 406 {
407 str name = dlg.le_subfileName->text();
371 408
372 if (name.length() == 0) 409 if (name.length() == 0)
373 return; // no subfile filename 410 return; // no subfile filename
374 411
375 LDDocument* file = getDocument (name); 412 LDDocument* file = getDocument (name);
376 413
377 if (!file) 414 if (!file)
378 { critical (fmt ("Couldn't open `%1': %2", name, strerror (errno))); 415 {
416 critical (fmt ("Couldn't open `%1': %2", name, strerror (errno)));
379 return; 417 return;
380 } 418 }
381 419
382 LDSubfile* ref = initObj<LDSubfile> (obj); 420 LDSubfile* ref = initObj<LDSubfile> (obj);
383 assert (ref); 421 assert (ref);
395 433
396 if (obj->isColored()) 434 if (obj->isColored())
397 obj->setColor (dlg.colnum); 435 obj->setColor (dlg.colnum);
398 436
399 if (newObject) 437 if (newObject)
400 { int idx = g_win->getInsertionPoint(); 438 {
439 int idx = g_win->getInsertionPoint();
401 getCurrentDocument()->insertObj (idx, obj); 440 getCurrentDocument()->insertObj (idx, obj);
402 } 441 }
403 442
404 g_win->refresh(); 443 g_win->refresh();
405 } 444 }

mercurial