src/gui.cc

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

mercurial