src/gui.cc

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

mercurial