|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013, 2014 Santeri Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #include <QGridLayout> |
|
20 #include <QMessageBox> |
|
21 #include <QEvent> |
|
22 #include <QContextMenuEvent> |
|
23 #include <QMenuBar> |
|
24 #include <QStatusBar> |
|
25 #include <QSplitter> |
|
26 #include <QListWidget> |
|
27 #include <QToolButton> |
|
28 #include <QComboBox> |
|
29 #include <QDialogButtonBox> |
|
30 #include <QToolBar> |
|
31 #include <QProgressBar> |
|
32 #include <QLabel> |
|
33 #include <QFileDialog> |
|
34 #include <QPushButton> |
|
35 #include <QCoreApplication> |
|
36 #include <QTimer> |
|
37 #include <QMetaMethod> |
|
38 #include "Main.h" |
|
39 #include "GLRenderer.h" |
|
40 #include "MainWindow.h" |
|
41 #include "Document.h" |
|
42 #include "Configuration.h" |
|
43 #include "Misc.h" |
|
44 #include "Colors.h" |
|
45 #include "EditHistory.h" |
|
46 #include "Widgets.h" |
|
47 #include "AddObjectDialog.h" |
|
48 #include "MessageLog.h" |
|
49 #include "Configuration.h" |
|
50 #include "ui_ldforge.h" |
|
51 #include "moc_MainWindow.cpp" |
|
52 |
|
53 static bool g_isSelectionLocked = false; |
|
54 |
|
55 cfg (Bool, lv_colorize, true); |
|
56 cfg (String, gui_colortoolbar, "16:24:|:4:25:14:27:2:3:11:1:22:|:0:72:71:15"); |
|
57 cfg (Bool, gui_implicitfiles, false); |
|
58 extern_cfg (List, io_recentfiles); |
|
59 extern_cfg (Bool, gl_axes); |
|
60 extern_cfg (String, gl_maincolor); |
|
61 extern_cfg (Float, gl_maincolor_alpha); |
|
62 extern_cfg (Bool, gl_wireframe); |
|
63 extern_cfg (Bool, gl_colorbfc); |
|
64 extern_cfg (Bool, gl_drawangles); |
|
65 |
|
66 // ============================================================================= |
|
67 // ----------------------------------------------------------------------------- |
|
68 MainWindow::MainWindow() |
|
69 { |
|
70 g_win = this; |
|
71 m_renderer = new GLRenderer; |
|
72 |
|
73 ui = new Ui_LDForgeUI; |
|
74 ui->setupUi (this); |
|
75 |
|
76 // Stuff the renderer into its frame |
|
77 QVBoxLayout* rendererLayout = new QVBoxLayout (ui->rendererFrame); |
|
78 rendererLayout->addWidget (R()); |
|
79 |
|
80 connect (ui->objectList, SIGNAL (itemSelectionChanged()), this, SLOT (slot_selectionChanged())); |
|
81 connect (ui->objectList, SIGNAL (itemDoubleClicked (QListWidgetItem*)), this, SLOT (slot_editObject (QListWidgetItem*))); |
|
82 connect (ui->fileList, SIGNAL (currentItemChanged (QListWidgetItem*, QListWidgetItem*)), this, SLOT (changeCurrentFile())); |
|
83 |
|
84 // Init message log manager |
|
85 m_msglog = new MessageManager; |
|
86 m_msglog->setRenderer (R()); |
|
87 m_renderer->setMessageLog (m_msglog); |
|
88 m_quickColors = quickColorsFromConfig(); |
|
89 slot_selectionChanged(); |
|
90 setStatusBar (new QStatusBar); |
|
91 |
|
92 // Make certain actions checkable |
|
93 ui->actionAxes->setChecked (gl_axes); |
|
94 ui->actionWireframe->setChecked (gl_wireframe); |
|
95 ui->actionBFCView->setChecked (gl_colorbfc); |
|
96 updateGridToolBar(); |
|
97 updateEditModeActions(); |
|
98 updateRecentFilesMenu(); |
|
99 updateToolBars(); |
|
100 updateTitle(); |
|
101 updateActionShortcuts(); |
|
102 |
|
103 setMinimumSize (300, 200); |
|
104 |
|
105 connect (qApp, SIGNAL (aboutToQuit()), this, SLOT (slot_lastSecondCleanup())); |
|
106 |
|
107 // Connect all actions |
|
108 for (QAction* act : findChildren<QAction*>()) |
|
109 if (!act->objectName().isEmpty()) |
|
110 connect (act, SIGNAL (triggered()), this, SLOT (slot_action())); |
|
111 } |
|
112 |
|
113 // ============================================================================= |
|
114 // ----------------------------------------------------------------------------- |
|
115 KeySequenceConfig* MainWindow::shortcutForAction (QAction* act) |
|
116 { |
|
117 QString keycfgname = fmt ("key_%1", act->objectName()); |
|
118 return KeySequenceConfig::getByName (keycfgname); |
|
119 } |
|
120 |
|
121 // ============================================================================= |
|
122 // ----------------------------------------------------------------------------- |
|
123 void MainWindow::updateActionShortcuts() |
|
124 { |
|
125 for (QAction* act : findChildren<QAction*>()) |
|
126 { |
|
127 KeySequenceConfig* cfg = shortcutForAction (act); |
|
128 |
|
129 if (cfg) |
|
130 act->setShortcut (cfg->getValue()); |
|
131 } |
|
132 } |
|
133 |
|
134 // ============================================================================= |
|
135 // ----------------------------------------------------------------------------- |
|
136 void MainWindow::slot_action() |
|
137 { |
|
138 // Get the name of the sender object and use it to compose the slot name. |
|
139 QString methodName = fmt ("slot_%1", sender()->objectName()); |
|
140 |
|
141 #ifdef DEBUG |
|
142 log ("Action %1 triggered", sender()->objectName()); |
|
143 #endif |
|
144 |
|
145 // Now invoke this slot to call the action. |
|
146 QMetaObject::invokeMethod (this, methodName.toAscii().constData(), Qt::DirectConnection); |
|
147 endAction(); |
|
148 } |
|
149 |
|
150 // ============================================================================= |
|
151 // ----------------------------------------------------------------------------- |
|
152 void MainWindow::endAction() |
|
153 { |
|
154 // Add a step in the history now. |
|
155 getCurrentDocument()->addHistoryStep(); |
|
156 |
|
157 // Update the list item of the current file - we may need to draw an icon |
|
158 // now that marks it as having unsaved changes. |
|
159 updateDocumentListItem (getCurrentDocument()); |
|
160 } |
|
161 |
|
162 // ============================================================================= |
|
163 // ----------------------------------------------------------------------------- |
|
164 void MainWindow::slot_lastSecondCleanup() |
|
165 { |
|
166 delete m_renderer; |
|
167 delete ui; |
|
168 } |
|
169 |
|
170 // ============================================================================= |
|
171 // ----------------------------------------------------------------------------- |
|
172 void MainWindow::updateRecentFilesMenu() |
|
173 { |
|
174 // First, clear any items in the recent files menu |
|
175 for (QAction * recent : m_recentFiles) |
|
176 delete recent; |
|
177 |
|
178 m_recentFiles.clear(); |
|
179 |
|
180 QAction* first = null; |
|
181 |
|
182 for (const QVariant& it : io_recentfiles) |
|
183 { |
|
184 QString file = it.toString(); |
|
185 QAction* recent = new QAction (getIcon ("open-recent"), file, this); |
|
186 |
|
187 connect (recent, SIGNAL (triggered()), this, SLOT (slot_recentFile())); |
|
188 ui->menuOpenRecent->insertAction (first, recent); |
|
189 m_recentFiles << recent; |
|
190 first = recent; |
|
191 } |
|
192 } |
|
193 |
|
194 // ============================================================================= |
|
195 // ----------------------------------------------------------------------------- |
|
196 QList<LDQuickColor> quickColorsFromConfig() |
|
197 { |
|
198 QList<LDQuickColor> colors; |
|
199 |
|
200 for (QString colorname : gui_colortoolbar.split (":")) |
|
201 { |
|
202 if (colorname == "|") |
|
203 colors << LDQuickColor::getSeparator(); |
|
204 else |
|
205 { |
|
206 LDColor* col = getColor (colorname.toLong()); |
|
207 |
|
208 if (col != null) |
|
209 colors << LDQuickColor (col, null); |
|
210 } |
|
211 } |
|
212 |
|
213 return colors; |
|
214 } |
|
215 |
|
216 // ============================================================================= |
|
217 // ----------------------------------------------------------------------------- |
|
218 void MainWindow::updateToolBars() |
|
219 { |
|
220 m_colorButtons.clear(); |
|
221 ui->colorToolbar->clear(); |
|
222 |
|
223 for (LDQuickColor& entry : m_quickColors) |
|
224 { |
|
225 if (entry.isSeparator()) |
|
226 ui->colorToolbar->addSeparator(); |
|
227 else |
|
228 { |
|
229 QToolButton* colorButton = new QToolButton; |
|
230 colorButton->setIcon (makeColorIcon (entry.getColor(), 22)); |
|
231 colorButton->setIconSize (QSize (22, 22)); |
|
232 colorButton->setToolTip (entry.getColor()->name); |
|
233 |
|
234 connect (colorButton, SIGNAL (clicked()), this, SLOT (slot_quickColor())); |
|
235 ui->colorToolbar->addWidget (colorButton); |
|
236 m_colorButtons << colorButton; |
|
237 |
|
238 entry.setToolButton (colorButton); |
|
239 } |
|
240 } |
|
241 |
|
242 updateGridToolBar(); |
|
243 } |
|
244 |
|
245 // ============================================================================= |
|
246 // ----------------------------------------------------------------------------- |
|
247 void MainWindow::updateGridToolBar() |
|
248 { |
|
249 // Ensure that the current grid - and only the current grid - is selected. |
|
250 ui->actionGridCoarse->setChecked (grid == Grid::Coarse); |
|
251 ui->actionGridMedium->setChecked (grid == Grid::Medium); |
|
252 ui->actionGridFine->setChecked (grid == Grid::Fine); |
|
253 } |
|
254 |
|
255 // ============================================================================= |
|
256 // ----------------------------------------------------------------------------- |
|
257 void MainWindow::updateTitle() |
|
258 { |
|
259 QString title = fmt (APPNAME " %1", fullVersionString()); |
|
260 |
|
261 // Append our current file if we have one |
|
262 if (getCurrentDocument()) |
|
263 { |
|
264 if (getCurrentDocument()->getName().length() > 0) |
|
265 title += fmt (": %1", basename (getCurrentDocument()->getName())); |
|
266 else |
|
267 title += fmt (": <anonymous>"); |
|
268 |
|
269 if (getCurrentDocument()->getObjectCount() > 0 && |
|
270 getCurrentDocument()->getObject (0)->getType() == LDObject::EComment) |
|
271 { |
|
272 // Append title |
|
273 LDComment* comm = static_cast<LDComment*> (getCurrentDocument()->getObject (0)); |
|
274 title += fmt (": %1", comm->text); |
|
275 } |
|
276 |
|
277 if (getCurrentDocument()->getHistory()->getPosition() != getCurrentDocument()->getSavePosition()) |
|
278 title += '*'; |
|
279 } |
|
280 |
|
281 #ifdef DEBUG |
|
282 title += " [debug build]"; |
|
283 #elif BUILD_ID != BUILD_RELEASE |
|
284 title += " [pre-release build]"; |
|
285 #endif // DEBUG |
|
286 |
|
287 #ifdef COMPILE_DATE |
|
288 title += " (built " COMPILE_DATE ")"; |
|
289 #endif // COMPILE_DATE |
|
290 |
|
291 setWindowTitle (title); |
|
292 } |
|
293 |
|
294 // ============================================================================= |
|
295 // ----------------------------------------------------------------------------- |
|
296 int MainWindow::deleteSelection() |
|
297 { |
|
298 if (selection().isEmpty()) |
|
299 return 0; |
|
300 |
|
301 LDObjectList selCopy = selection(); |
|
302 |
|
303 // Delete the objects that were being selected |
|
304 for (LDObject* obj : selCopy) |
|
305 obj->deleteSelf(); |
|
306 |
|
307 refresh(); |
|
308 return selCopy.size(); |
|
309 } |
|
310 |
|
311 // ============================================================================= |
|
312 // ----------------------------------------------------------------------------- |
|
313 void MainWindow::buildObjList() |
|
314 { |
|
315 if (!getCurrentDocument()) |
|
316 return; |
|
317 |
|
318 // Lock the selection while we do this so that refreshing the object list |
|
319 // doesn't trigger selection updating so that the selection doesn't get lost |
|
320 // while this is done. |
|
321 g_isSelectionLocked = true; |
|
322 |
|
323 for (int i = 0; i < ui->objectList->count(); ++i) |
|
324 delete ui->objectList->item (i); |
|
325 |
|
326 ui->objectList->clear(); |
|
327 |
|
328 for (LDObject* obj : getCurrentDocument()->getObjects()) |
|
329 { |
|
330 QString descr; |
|
331 |
|
332 switch (obj->getType()) |
|
333 { |
|
334 case LDObject::EComment: |
|
335 { |
|
336 descr = static_cast<LDComment*> (obj)->text; |
|
337 |
|
338 // Remove leading whitespace |
|
339 while (descr[0] == ' ') |
|
340 descr.remove (0, 1); |
|
341 } break; |
|
342 |
|
343 case LDObject::EEmpty: |
|
344 break; // leave it empty |
|
345 |
|
346 case LDObject::ELine: |
|
347 case LDObject::ETriangle: |
|
348 case LDObject::EQuad: |
|
349 case LDObject::ECondLine: |
|
350 { |
|
351 for (int i = 0; i < obj->vertices(); ++i) |
|
352 { |
|
353 if (i != 0) |
|
354 descr += ", "; |
|
355 |
|
356 descr += obj->getVertex (i).toString (true); |
|
357 } |
|
358 } break; |
|
359 |
|
360 case LDObject::EError: |
|
361 { |
|
362 descr = fmt ("ERROR: %1", obj->raw()); |
|
363 } break; |
|
364 |
|
365 case LDObject::EVertex: |
|
366 { |
|
367 descr = static_cast<LDVertex*> (obj)->pos.toString (true); |
|
368 } break; |
|
369 |
|
370 case LDObject::ESubfile: |
|
371 { |
|
372 LDSubfile* ref = static_cast<LDSubfile*> (obj); |
|
373 |
|
374 descr = fmt ("%1 %2, (", ref->getFileInfo()->getDisplayName(), ref->getPosition().toString (true)); |
|
375 |
|
376 for (int i = 0; i < 9; ++i) |
|
377 descr += fmt ("%1%2", ref->getTransform()[i], (i != 8) ? " " : ""); |
|
378 |
|
379 descr += ')'; |
|
380 } break; |
|
381 |
|
382 case LDObject::EBFC: |
|
383 { |
|
384 descr = LDBFC::statements[static_cast<LDBFC*> (obj)->type]; |
|
385 } break; |
|
386 |
|
387 case LDObject::EOverlay: |
|
388 { |
|
389 LDOverlay* ovl = static_cast<LDOverlay*> (obj); |
|
390 descr = fmt ("[%1] %2 (%3, %4), %5 x %6", g_CameraNames[ovl->getCamera()], |
|
391 basename (ovl->getFileName()), ovl->getX(), ovl->getY(), |
|
392 ovl->getWidth(), ovl->getHeight()); |
|
393 } |
|
394 break; |
|
395 |
|
396 default: |
|
397 { |
|
398 descr = obj->getTypeName(); |
|
399 } break; |
|
400 } |
|
401 |
|
402 QListWidgetItem* item = new QListWidgetItem (descr); |
|
403 item->setIcon (getIcon (obj->getTypeName())); |
|
404 |
|
405 // Use italic font if hidden |
|
406 if (obj->isHidden()) |
|
407 { |
|
408 QFont font = item->font(); |
|
409 font.setItalic (true); |
|
410 item->setFont (font); |
|
411 } |
|
412 |
|
413 // Color gibberish orange on red so it stands out. |
|
414 if (obj->getType() == LDObject::EError) |
|
415 { |
|
416 item->setBackground (QColor ("#AA0000")); |
|
417 item->setForeground (QColor ("#FFAA00")); |
|
418 } |
|
419 elif (lv_colorize && obj->isColored() && obj->getColor() != maincolor && obj->getColor() != edgecolor) |
|
420 { |
|
421 // If the object isn't in the main or edge color, draw this |
|
422 // list entry in said color. |
|
423 LDColor* col = getColor (obj->getColor()); |
|
424 |
|
425 if (col) |
|
426 item->setForeground (col->faceColor); |
|
427 } |
|
428 |
|
429 obj->qObjListEntry = item; |
|
430 ui->objectList->insertItem (ui->objectList->count(), item); |
|
431 } |
|
432 |
|
433 g_isSelectionLocked = false; |
|
434 updateSelection(); |
|
435 scrollToSelection(); |
|
436 } |
|
437 |
|
438 // ============================================================================= |
|
439 // ----------------------------------------------------------------------------- |
|
440 void MainWindow::scrollToSelection() |
|
441 { |
|
442 if (selection().isEmpty()) |
|
443 return; |
|
444 |
|
445 LDObject* obj = selection().last(); |
|
446 ui->objectList->scrollToItem (obj->qObjListEntry); |
|
447 } |
|
448 |
|
449 // ============================================================================= |
|
450 // ----------------------------------------------------------------------------- |
|
451 void MainWindow::slot_selectionChanged() |
|
452 { |
|
453 if (g_isSelectionLocked == true || getCurrentDocument() == null) |
|
454 return; |
|
455 |
|
456 // Update the shared selection array, though don't do this if this was |
|
457 // called during GL picking, in which case the GL renderer takes care |
|
458 // of the selection. |
|
459 if (m_renderer->isPicking()) |
|
460 return; |
|
461 |
|
462 LDObjectList priorSelection = selection(); |
|
463 |
|
464 // Get the objects from the object list selection |
|
465 getCurrentDocument()->clearSelection(); |
|
466 const QList<QListWidgetItem*> items = ui->objectList->selectedItems(); |
|
467 |
|
468 for (LDObject* obj : getCurrentDocument()->getObjects()) |
|
469 { |
|
470 for (QListWidgetItem* item : items) |
|
471 { |
|
472 if (item == obj->qObjListEntry) |
|
473 { |
|
474 obj->select(); |
|
475 break; |
|
476 } |
|
477 } |
|
478 } |
|
479 |
|
480 // Update the GL renderer |
|
481 LDObjectList compound = priorSelection + selection(); |
|
482 removeDuplicates (compound); |
|
483 |
|
484 for (LDObject* obj : compound) |
|
485 m_renderer->compileObject (obj); |
|
486 |
|
487 m_renderer->update(); |
|
488 } |
|
489 |
|
490 // ============================================================================= |
|
491 // ----------------------------------------------------------------------------- |
|
492 void MainWindow::slot_recentFile() |
|
493 { |
|
494 QAction* qAct = static_cast<QAction*> (sender()); |
|
495 openMainFile (qAct->text()); |
|
496 } |
|
497 |
|
498 // ============================================================================= |
|
499 // ----------------------------------------------------------------------------- |
|
500 void MainWindow::slot_quickColor() |
|
501 { |
|
502 QToolButton* button = static_cast<QToolButton*> (sender()); |
|
503 LDColor* col = null; |
|
504 |
|
505 for (const LDQuickColor& entry : m_quickColors) |
|
506 { |
|
507 if (entry.getToolButton() == button) |
|
508 { |
|
509 col = entry.getColor(); |
|
510 break; |
|
511 } |
|
512 } |
|
513 |
|
514 if (col == null) |
|
515 return; |
|
516 |
|
517 int newColor = col->index; |
|
518 |
|
519 for (LDObject* obj : selection()) |
|
520 { |
|
521 if (obj->isColored() == false) |
|
522 continue; // uncolored object |
|
523 |
|
524 obj->setColor (newColor); |
|
525 R()->compileObject (obj); |
|
526 } |
|
527 |
|
528 endAction(); |
|
529 refresh(); |
|
530 } |
|
531 |
|
532 // ============================================================================= |
|
533 // ----------------------------------------------------------------------------- |
|
534 int MainWindow::getInsertionPoint() |
|
535 { |
|
536 // If we have a selection, put the item after it. |
|
537 if (!selection().isEmpty()) |
|
538 return selection().last()->getIndex() + 1; |
|
539 |
|
540 // Otherwise place the object at the end. |
|
541 return getCurrentDocument()->getObjectCount(); |
|
542 } |
|
543 |
|
544 // ============================================================================= |
|
545 // ----------------------------------------------------------------------------- |
|
546 void MainWindow::doFullRefresh() |
|
547 { |
|
548 buildObjList(); |
|
549 m_renderer->hardRefresh(); |
|
550 } |
|
551 |
|
552 // ============================================================================= |
|
553 // ----------------------------------------------------------------------------- |
|
554 void MainWindow::refresh() |
|
555 { |
|
556 buildObjList(); |
|
557 m_renderer->update(); |
|
558 } |
|
559 |
|
560 // ============================================================================= |
|
561 // ----------------------------------------------------------------------------- |
|
562 void MainWindow::updateSelection() |
|
563 { |
|
564 g_isSelectionLocked = true; |
|
565 |
|
566 for (LDObject* obj : getCurrentDocument()->getObjects()) |
|
567 obj->setSelected (false); |
|
568 |
|
569 ui->objectList->clearSelection(); |
|
570 |
|
571 for (LDObject* obj : selection()) |
|
572 { |
|
573 if (obj->qObjListEntry == null) |
|
574 continue; |
|
575 |
|
576 obj->qObjListEntry->setSelected (true); |
|
577 obj->setSelected (true); |
|
578 } |
|
579 |
|
580 g_isSelectionLocked = false; |
|
581 slot_selectionChanged(); |
|
582 } |
|
583 |
|
584 // ============================================================================= |
|
585 // ----------------------------------------------------------------------------- |
|
586 int MainWindow::getSelectedColor() |
|
587 { |
|
588 int result = -1; |
|
589 |
|
590 for (LDObject* obj : selection()) |
|
591 { |
|
592 if (obj->isColored() == false) |
|
593 continue; // doesn't use color |
|
594 |
|
595 if (result != -1 && obj->getColor() != result) |
|
596 return -1; // No consensus in object color |
|
597 |
|
598 if (result == -1) |
|
599 result = obj->getColor(); |
|
600 } |
|
601 |
|
602 return result; |
|
603 } |
|
604 |
|
605 // ============================================================================= |
|
606 // ----------------------------------------------------------------------------- |
|
607 LDObject::Type MainWindow::getUniformSelectedType() |
|
608 { |
|
609 LDObject::Type result = LDObject::EUnidentified; |
|
610 |
|
611 for (LDObject* obj : selection()) |
|
612 { |
|
613 if (result != LDObject::EUnidentified && obj->getColor() != result) |
|
614 return LDObject::EUnidentified; |
|
615 |
|
616 if (result == LDObject::EUnidentified) |
|
617 result = obj->getType(); |
|
618 } |
|
619 |
|
620 return result; |
|
621 } |
|
622 |
|
623 // ============================================================================= |
|
624 // ----------------------------------------------------------------------------- |
|
625 void MainWindow::closeEvent (QCloseEvent* ev) |
|
626 { |
|
627 // Check whether it's safe to close all files. |
|
628 if (!safeToCloseAll()) |
|
629 { |
|
630 ev->ignore(); |
|
631 return; |
|
632 } |
|
633 |
|
634 // Save the configuration before leaving so that, for instance, grid choice |
|
635 // is preserved across instances. |
|
636 Config::save(); |
|
637 |
|
638 ev->accept(); |
|
639 } |
|
640 |
|
641 // ============================================================================= |
|
642 // ----------------------------------------------------------------------------- |
|
643 void MainWindow::spawnContextMenu (const QPoint pos) |
|
644 { |
|
645 const bool single = (selection().size() == 1); |
|
646 LDObject* singleObj = (single) ? selection()[0] : null; |
|
647 |
|
648 QMenu* contextMenu = new QMenu; |
|
649 |
|
650 if (single && singleObj->getType() != LDObject::EEmpty) |
|
651 { |
|
652 contextMenu->addAction (ui->actionEdit); |
|
653 contextMenu->addSeparator(); |
|
654 } |
|
655 |
|
656 contextMenu->addAction (ui->actionCut); |
|
657 contextMenu->addAction (ui->actionCopy); |
|
658 contextMenu->addAction (ui->actionPaste); |
|
659 contextMenu->addAction (ui->actionDelete); |
|
660 contextMenu->addSeparator(); |
|
661 contextMenu->addAction (ui->actionSetColor); |
|
662 |
|
663 if (single) |
|
664 contextMenu->addAction (ui->actionEditRaw); |
|
665 |
|
666 contextMenu->addAction (ui->actionBorders); |
|
667 contextMenu->addAction (ui->actionSetOverlay); |
|
668 contextMenu->addAction (ui->actionClearOverlay); |
|
669 contextMenu->addAction (ui->actionModeSelect); |
|
670 contextMenu->addAction (ui->actionModeDraw); |
|
671 contextMenu->addAction (ui->actionModeCircle); |
|
672 |
|
673 if (selection().size() > 0) |
|
674 { |
|
675 contextMenu->addSeparator(); |
|
676 contextMenu->addAction (ui->actionSubfileSelection); |
|
677 } |
|
678 |
|
679 if (R()->camera() != GL::EFreeCamera) |
|
680 { |
|
681 contextMenu->addSeparator(); |
|
682 contextMenu->addAction (ui->actionSetDrawDepth); |
|
683 } |
|
684 |
|
685 contextMenu->exec (pos); |
|
686 } |
|
687 |
|
688 // ============================================================================= |
|
689 // TODO: what the heh? |
|
690 // ----------------------------------------------------------------------------- |
|
691 void MainWindow::deleteObjects (LDObjectList objs) |
|
692 { |
|
693 for (LDObject* obj : objs) |
|
694 obj->deleteSelf(); |
|
695 } |
|
696 |
|
697 // ============================================================================= |
|
698 // ----------------------------------------------------------------------------- |
|
699 void MainWindow::deleteByColor (const int colnum) |
|
700 { |
|
701 LDObjectList objs; |
|
702 |
|
703 for (LDObject* obj : getCurrentDocument()->getObjects()) |
|
704 { |
|
705 if (!obj->isColored() || obj->getColor() != colnum) |
|
706 continue; |
|
707 |
|
708 objs << obj; |
|
709 } |
|
710 |
|
711 deleteObjects (objs); |
|
712 } |
|
713 |
|
714 // ============================================================================= |
|
715 // ----------------------------------------------------------------------------- |
|
716 void MainWindow::updateEditModeActions() |
|
717 { |
|
718 const EditMode mode = R()->getEditMode(); |
|
719 ui->actionModeSelect->setChecked (mode == ESelectMode); |
|
720 ui->actionModeDraw->setChecked (mode == EDrawMode); |
|
721 ui->actionModeCircle->setChecked (mode == ECircleMode); |
|
722 } |
|
723 |
|
724 // ============================================================================= |
|
725 // ----------------------------------------------------------------------------- |
|
726 void MainWindow::slot_editObject (QListWidgetItem* listitem) |
|
727 { |
|
728 LDObject* obj = null; |
|
729 |
|
730 for (LDObject* it : getCurrentDocument()->getObjects()) |
|
731 { |
|
732 if (it->qObjListEntry == listitem) |
|
733 { |
|
734 obj = it; |
|
735 break; |
|
736 } |
|
737 } |
|
738 |
|
739 AddObjectDialog::staticDialog (obj->getType(), obj); |
|
740 } |
|
741 |
|
742 // ============================================================================= |
|
743 // ----------------------------------------------------------------------------- |
|
744 bool MainWindow::save (LDDocument* f, bool saveAs) |
|
745 { |
|
746 QString path = f->getFullPath(); |
|
747 |
|
748 if (saveAs || path.isEmpty()) |
|
749 { |
|
750 QString name = f->getDefaultName(); |
|
751 |
|
752 if (!f->getFullPath().isEmpty()) |
|
753 name = f->getFullPath(); |
|
754 elif (!f->getName().isEmpty()) |
|
755 name = f->getName(); |
|
756 |
|
757 name.replace ("\\", "/"); |
|
758 path = QFileDialog::getSaveFileName (g_win, tr ("Save As"), |
|
759 name, tr ("LDraw files (*.dat *.ldr)")); |
|
760 |
|
761 if (path.isEmpty()) |
|
762 { |
|
763 // User didn't give a file name, abort. |
|
764 return false; |
|
765 } |
|
766 } |
|
767 |
|
768 if (f->save (path)) |
|
769 { |
|
770 if (f == getCurrentDocument()) |
|
771 updateTitle(); |
|
772 |
|
773 log ("Saved to %1.", path); |
|
774 |
|
775 // Add it to recent files |
|
776 addRecentFile (path); |
|
777 return true; |
|
778 } |
|
779 |
|
780 QString message = fmt (tr ("Failed to save to %1: %2"), path, strerror (errno)); |
|
781 |
|
782 // Tell the user the save failed, and give the option for saving as with it. |
|
783 QMessageBox dlg (QMessageBox::Critical, tr ("Save Failure"), message, QMessageBox::Close, g_win); |
|
784 |
|
785 // Add a save-as button |
|
786 QPushButton* saveAsBtn = new QPushButton (tr ("Save As")); |
|
787 saveAsBtn->setIcon (getIcon ("file-save-as")); |
|
788 dlg.addButton (saveAsBtn, QMessageBox::ActionRole); |
|
789 dlg.setDefaultButton (QMessageBox::Close); |
|
790 dlg.exec(); |
|
791 |
|
792 if (dlg.clickedButton() == saveAsBtn) |
|
793 return save (f, true); // yay recursion! |
|
794 |
|
795 return false; |
|
796 } |
|
797 |
|
798 void MainWindow::addMessage (QString msg) |
|
799 { |
|
800 m_msglog->addLine (msg); |
|
801 } |
|
802 |
|
803 // ============================================================================ |
|
804 void ObjectList::contextMenuEvent (QContextMenuEvent* ev) |
|
805 { |
|
806 g_win->spawnContextMenu (ev->globalPos()); |
|
807 } |
|
808 |
|
809 // ============================================================================= |
|
810 // ----------------------------------------------------------------------------- |
|
811 QPixmap getIcon (QString iconName) |
|
812 { |
|
813 return (QPixmap (fmt (":/icons/%1.png", iconName))); |
|
814 } |
|
815 |
|
816 // ============================================================================= |
|
817 bool confirm (QString msg) |
|
818 { |
|
819 return confirm (MainWindow::tr ("Confirm"), msg); |
|
820 } |
|
821 |
|
822 bool confirm (QString title, QString msg) |
|
823 { |
|
824 return QMessageBox::question (g_win, title, msg, |
|
825 (QMessageBox::Yes | QMessageBox::No), QMessageBox::No) == QMessageBox::Yes; |
|
826 } |
|
827 |
|
828 // ============================================================================= |
|
829 void critical (QString msg) |
|
830 { |
|
831 QMessageBox::critical (g_win, MainWindow::tr ("Error"), msg, |
|
832 (QMessageBox::Close), QMessageBox::Close); |
|
833 } |
|
834 |
|
835 // ============================================================================= |
|
836 QIcon makeColorIcon (LDColor* colinfo, const int size) |
|
837 { |
|
838 // Create an image object and link a painter to it. |
|
839 QImage img (size, size, QImage::Format_ARGB32); |
|
840 QPainter paint (&img); |
|
841 QColor col = colinfo->faceColor; |
|
842 |
|
843 if (colinfo->index == maincolor) |
|
844 { |
|
845 // Use the user preferences for main color here |
|
846 col = gl_maincolor; |
|
847 col.setAlphaF (gl_maincolor_alpha); |
|
848 } |
|
849 |
|
850 // Paint the icon border |
|
851 paint.fillRect (QRect (0, 0, size, size), colinfo->edgeColor); |
|
852 |
|
853 // Paint the checkerboard background, visible with translucent icons |
|
854 paint.drawPixmap (QRect (1, 1, size - 2, size - 2), getIcon ("checkerboard"), QRect (0, 0, 8, 8)); |
|
855 |
|
856 // Paint the color above the checkerboard |
|
857 paint.fillRect (QRect (1, 1, size - 2, size - 2), col); |
|
858 return QIcon (QPixmap::fromImage (img)); |
|
859 } |
|
860 |
|
861 // ============================================================================= |
|
862 void makeColorComboBox (QComboBox* box) |
|
863 { |
|
864 std::map<int, int> counts; |
|
865 |
|
866 for (LDObject* obj : getCurrentDocument()->getObjects()) |
|
867 { |
|
868 if (!obj->isColored()) |
|
869 continue; |
|
870 |
|
871 if (counts.find (obj->getColor()) == counts.end()) |
|
872 counts[obj->getColor()] = 1; |
|
873 else |
|
874 counts[obj->getColor()]++; |
|
875 } |
|
876 |
|
877 box->clear(); |
|
878 int row = 0; |
|
879 |
|
880 for (const auto& pair : counts) |
|
881 { |
|
882 LDColor* col = getColor (pair.first); |
|
883 assert (col != null); |
|
884 |
|
885 QIcon ico = makeColorIcon (col, 16); |
|
886 box->addItem (ico, fmt ("[%1] %2 (%3 object%4)", |
|
887 pair.first, col->name, pair.second, plural (pair.second))); |
|
888 box->setItemData (row, pair.first); |
|
889 |
|
890 ++row; |
|
891 } |
|
892 } |
|
893 |
|
894 void MainWindow::updateDocumentList() |
|
895 { |
|
896 ui->fileList->clear(); |
|
897 |
|
898 for (LDDocument* f : g_loadedFiles) |
|
899 { |
|
900 // Don't list implicit files unless explicitly desired. |
|
901 if (f->isImplicit() && !gui_implicitfiles) |
|
902 continue; |
|
903 |
|
904 // Add an item to the list for this file and store a pointer to it in |
|
905 // the file, so we can find files by the list item. |
|
906 ui->fileList->addItem (""); |
|
907 QListWidgetItem* item = ui->fileList->item (ui->fileList->count() - 1); |
|
908 f->setListItem (item); |
|
909 updateDocumentListItem (f); |
|
910 } |
|
911 } |
|
912 |
|
913 void MainWindow::updateDocumentListItem (LDDocument* f) |
|
914 { |
|
915 if (f->getListItem() == null) |
|
916 { |
|
917 // We don't have a list item for this file, so the list either doesn't |
|
918 // exist yet or is out of date. Build the list now. |
|
919 updateDocumentList(); |
|
920 return; |
|
921 } |
|
922 |
|
923 // If this is the current file, it also needs to be the selected item on |
|
924 // the list. |
|
925 if (f == getCurrentDocument()) |
|
926 ui->fileList->setCurrentItem (f->getListItem()); |
|
927 |
|
928 // If we list implicit files, draw them with a shade of gray to make them |
|
929 // distinct. |
|
930 if (f->isImplicit()) |
|
931 f->getListItem()->setForeground (QColor (96, 96, 96)); |
|
932 |
|
933 f->getListItem()->setText (f->getDisplayName()); |
|
934 |
|
935 // If the Document.has unsaved changes, draw a little icon next to it to mark that. |
|
936 f->getListItem()->setIcon (f->hasUnsavedChanges() ? getIcon ("file-save") : QIcon()); |
|
937 } |
|
938 |
|
939 // ============================================================================= |
|
940 // A file is selected from the list of files on the left of the screen. Find out |
|
941 // which file was picked and change to it. |
|
942 void MainWindow::changeCurrentFile() |
|
943 { |
|
944 LDDocument* f = null; |
|
945 QListWidgetItem* item = ui->fileList->currentItem(); |
|
946 |
|
947 // Find the file pointer of the item that was selected. |
|
948 for (LDDocument* it : g_loadedFiles) |
|
949 { |
|
950 if (it->getListItem() == item) |
|
951 { |
|
952 f = it; |
|
953 break; |
|
954 } |
|
955 } |
|
956 |
|
957 // If we picked the same file we're currently on, we don't need to do |
|
958 // anything. |
|
959 if (!f || f == getCurrentDocument()) |
|
960 return; |
|
961 |
|
962 LDDocument::setCurrent (f); |
|
963 } |
|
964 |
|
965 void MainWindow::refreshObjectList() |
|
966 { |
|
967 #if 0 |
|
968 ui->objectList->clear(); |
|
969 LDDocument* f = getCurrentDocument(); |
|
970 |
|
971 for (LDObject* obj : *f) |
|
972 ui->objectList->addItem (obj->qObjListEntry); |
|
973 |
|
974 #endif |
|
975 |
|
976 buildObjList(); |
|
977 } |
|
978 |
|
979 void MainWindow::updateActions() |
|
980 { |
|
981 History* his = getCurrentDocument()->getHistory(); |
|
982 int pos = his->getPosition(); |
|
983 ui->actionUndo->setEnabled (pos != -1); |
|
984 ui->actionRedo->setEnabled (pos < (long) his->getSize() - 1); |
|
985 ui->actionAxes->setChecked (gl_axes); |
|
986 ui->actionBFCView->setChecked (gl_colorbfc); |
|
987 ui->actionDrawAngles->setChecked (gl_drawangles); |
|
988 } |
|
989 |
|
990 QImage imageFromScreencap (uchar* data, int w, int h) |
|
991 { |
|
992 // GL and Qt formats have R and B swapped. Also, GL flips Y - correct it as well. |
|
993 return QImage (data, w, h, QImage::Format_ARGB32).rgbSwapped().mirrored(); |
|
994 } |
|
995 |
|
996 // ============================================================================= |
|
997 // ----------------------------------------------------------------------------- |
|
998 LDQuickColor::LDQuickColor (LDColor* color, QToolButton* toolButton) : |
|
999 m_Color (color), |
|
1000 m_ToolButton (toolButton) {} |
|
1001 |
|
1002 LDQuickColor LDQuickColor::getSeparator() |
|
1003 { |
|
1004 return LDQuickColor (null, null); |
|
1005 } |
|
1006 |
|
1007 bool LDQuickColor::isSeparator() const |
|
1008 { |
|
1009 return getColor() == null; |
|
1010 } |