1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 - 2015 Teemu 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 <QSettings> |
|
39 #include "main.h" |
|
40 #include "glRenderer.h" |
|
41 #include "mainWindow.h" |
|
42 #include "ldDocument.h" |
|
43 #include "configuration.h" |
|
44 #include "miscallenous.h" |
|
45 #include "colors.h" |
|
46 #include "editHistory.h" |
|
47 #include "radioGroup.h" |
|
48 #include "addObjectDialog.h" |
|
49 #include "messageLog.h" |
|
50 #include "configuration.h" |
|
51 #include "ui_ldforge.h" |
|
52 #include "primitives.h" |
|
53 #include "editmodes/abstractEditMode.h" |
|
54 |
|
55 static bool g_isSelectionLocked = false; |
|
56 static QMap<QAction*, QKeySequence> g_defaultShortcuts; |
|
57 |
|
58 CFGENTRY (Bool, ColorizeObjectsList, true) |
|
59 CFGENTRY (String, QuickColorToolbar, "4:25:14:27:2:3:11:1:22:|:0:72:71:15") |
|
60 CFGENTRY (Bool, ListImplicitFiles, false) |
|
61 CFGENTRY (List, HiddenToolbars, {}) |
|
62 EXTERN_CFGENTRY (List, RecentFiles) |
|
63 EXTERN_CFGENTRY (Bool, DrawAxes) |
|
64 EXTERN_CFGENTRY (String, MainColor) |
|
65 EXTERN_CFGENTRY (Float, MainColorAlpha) |
|
66 EXTERN_CFGENTRY (Bool, DrawWireframe) |
|
67 EXTERN_CFGENTRY (Bool, BFCRedGreenView) |
|
68 EXTERN_CFGENTRY (Bool, DrawAngles) |
|
69 EXTERN_CFGENTRY (Bool, RandomColors) |
|
70 EXTERN_CFGENTRY (Bool, DrawSurfaces) |
|
71 EXTERN_CFGENTRY (Bool, DrawEdgeLines) |
|
72 EXTERN_CFGENTRY (Bool, DrawConditionalLines) |
|
73 |
|
74 // ============================================================================= |
|
75 // |
|
76 MainWindow::MainWindow (QWidget* parent, Qt::WindowFlags flags) : |
|
77 QMainWindow (parent, flags) |
|
78 { |
|
79 g_win = this; |
|
80 ui = new Ui_LDForgeUI; |
|
81 ui->setupUi (this); |
|
82 m_isUpdatingTabs = false; |
|
83 m_renderer = new GLRenderer (this); |
|
84 m_tabBar = new QTabBar; |
|
85 m_tabBar->setTabsClosable (true); |
|
86 ui->verticalLayout->insertWidget (0, m_tabBar); |
|
87 |
|
88 // Stuff the renderer into its frame |
|
89 QVBoxLayout* rendererLayout = new QVBoxLayout (ui->rendererFrame); |
|
90 rendererLayout->addWidget (R()); |
|
91 |
|
92 connect (ui->objectList, SIGNAL (itemSelectionChanged()), this, SLOT (slot_selectionChanged())); |
|
93 connect (ui->objectList, SIGNAL (itemDoubleClicked (QListWidgetItem*)), this, SLOT (slot_editObject (QListWidgetItem*))); |
|
94 connect (m_tabBar, SIGNAL (currentChanged(int)), this, SLOT (changeCurrentFile())); |
|
95 connect (m_tabBar, SIGNAL (tabCloseRequested (int)), this, SLOT (closeTab (int))); |
|
96 |
|
97 if (ActivePrimitiveScanner() != null) |
|
98 connect (ActivePrimitiveScanner(), SIGNAL (workDone()), this, SLOT (updatePrimitives())); |
|
99 else |
|
100 updatePrimitives(); |
|
101 |
|
102 m_messageLog = new MessageManager; |
|
103 m_messageLog->setRenderer (R()); |
|
104 m_renderer->setMessageLog (m_messageLog); |
|
105 m_quickColors = LoadQuickColorList(); |
|
106 slot_selectionChanged(); |
|
107 setStatusBar (new QStatusBar); |
|
108 updateActions(); |
|
109 |
|
110 // Connect all actions and save default sequences |
|
111 applyToActions ([&](QAction* act) |
|
112 { |
|
113 connect (act, SIGNAL (triggered()), this, SLOT (actionTriggered())); |
|
114 g_defaultShortcuts[act] = act->shortcut(); |
|
115 }); |
|
116 |
|
117 updateGridToolBar(); |
|
118 updateEditModeActions(); |
|
119 updateRecentFilesMenu(); |
|
120 updateColorToolbar(); |
|
121 updateTitle(); |
|
122 loadShortcuts (Config::SettingsObject()); |
|
123 setMinimumSize (300, 200); |
|
124 connect (qApp, SIGNAL (aboutToQuit()), this, SLOT (slot_lastSecondCleanup())); |
|
125 connect (ui->ringToolHiRes, SIGNAL (clicked (bool)), this, SLOT (ringToolHiResClicked (bool))); |
|
126 connect (ui->ringToolSegments, SIGNAL (valueChanged (int)), |
|
127 this, SLOT (circleToolSegmentsChanged())); |
|
128 circleToolSegmentsChanged(); // invoke it manually for initial label text |
|
129 |
|
130 for (QVariant const& toolbarname : cfg::HiddenToolbars) |
|
131 { |
|
132 QToolBar* toolbar = findChild<QToolBar*> (toolbarname.toString()); |
|
133 |
|
134 if (toolbar != null) |
|
135 toolbar->hide(); |
|
136 } |
|
137 } |
|
138 |
|
139 MainWindow::~MainWindow() |
|
140 { |
|
141 g_win = null; |
|
142 } |
|
143 |
|
144 // ============================================================================= |
|
145 // |
|
146 void MainWindow::actionTriggered() |
|
147 { |
|
148 // Get the name of the sender object and use it to compose the slot name, |
|
149 // then invoke this slot to call the action. |
|
150 QMetaObject::invokeMethod (this, |
|
151 qPrintable (format ("slot_%1", sender()->objectName())), Qt::DirectConnection); |
|
152 endAction(); |
|
153 } |
|
154 |
|
155 // ============================================================================= |
|
156 // |
|
157 void MainWindow::endAction() |
|
158 { |
|
159 // Add a step in the history now. |
|
160 CurrentDocument()->addHistoryStep(); |
|
161 |
|
162 // Update the list item of the current file - we may need to draw an icon |
|
163 // now that marks it as having unsaved changes. |
|
164 updateDocumentListItem (CurrentDocument()); |
|
165 refresh(); |
|
166 } |
|
167 |
|
168 // ============================================================================= |
|
169 // |
|
170 void MainWindow::slot_lastSecondCleanup() |
|
171 { |
|
172 delete m_renderer; |
|
173 delete ui; |
|
174 } |
|
175 |
|
176 // ============================================================================= |
|
177 // |
|
178 void MainWindow::updateRecentFilesMenu() |
|
179 { |
|
180 // First, clear any items in the recent files menu |
|
181 for (QAction * recent : m_recentFiles) |
|
182 delete recent; |
|
183 |
|
184 m_recentFiles.clear(); |
|
185 |
|
186 QAction* first = null; |
|
187 |
|
188 for (const QVariant& it : cfg::RecentFiles) |
|
189 { |
|
190 QString file = it.toString(); |
|
191 QAction* recent = new QAction (GetIcon ("open-recent"), file, this); |
|
192 |
|
193 connect (recent, SIGNAL (triggered()), this, SLOT (slot_recentFile())); |
|
194 ui->menuOpenRecent->insertAction (first, recent); |
|
195 m_recentFiles << recent; |
|
196 first = recent; |
|
197 } |
|
198 } |
|
199 |
|
200 // ============================================================================= |
|
201 // |
|
202 QList<LDQuickColor> LoadQuickColorList() |
|
203 { |
|
204 QList<LDQuickColor> colors; |
|
205 |
|
206 for (QString colorname : cfg::QuickColorToolbar.split (":")) |
|
207 { |
|
208 if (colorname == "|") |
|
209 colors << LDQuickColor::getSeparator(); |
|
210 else |
|
211 { |
|
212 LDColor col = LDColor::fromIndex (colorname.toLong()); |
|
213 |
|
214 if (col != null) |
|
215 colors << LDQuickColor (col, null); |
|
216 } |
|
217 } |
|
218 |
|
219 return colors; |
|
220 } |
|
221 |
|
222 // ============================================================================= |
|
223 // |
|
224 void MainWindow::updateColorToolbar() |
|
225 { |
|
226 m_colorButtons.clear(); |
|
227 ui->toolBarColors->clear(); |
|
228 ui->toolBarColors->addAction (ui->actionUncolor); |
|
229 ui->toolBarColors->addSeparator(); |
|
230 |
|
231 for (LDQuickColor& entry : m_quickColors) |
|
232 { |
|
233 if (entry.isSeparator()) |
|
234 { |
|
235 ui->toolBarColors->addSeparator(); |
|
236 } |
|
237 else |
|
238 { |
|
239 QToolButton* colorButton = new QToolButton; |
|
240 colorButton->setIcon (MakeColorIcon (entry.color(), 16)); |
|
241 colorButton->setIconSize (QSize (16, 16)); |
|
242 colorButton->setToolTip (entry.color().name()); |
|
243 |
|
244 connect (colorButton, SIGNAL (clicked()), this, SLOT (slot_quickColor())); |
|
245 ui->toolBarColors->addWidget (colorButton); |
|
246 m_colorButtons << colorButton; |
|
247 |
|
248 entry.setToolButton (colorButton); |
|
249 } |
|
250 } |
|
251 |
|
252 updateGridToolBar(); |
|
253 } |
|
254 |
|
255 // ============================================================================= |
|
256 // |
|
257 void MainWindow::updateGridToolBar() |
|
258 { |
|
259 // Ensure that the current grid - and only the current grid - is selected. |
|
260 ui->actionGridCoarse->setChecked (cfg::Grid == Grid::Coarse); |
|
261 ui->actionGridMedium->setChecked (cfg::Grid == Grid::Medium); |
|
262 ui->actionGridFine->setChecked (cfg::Grid == Grid::Fine); |
|
263 } |
|
264 |
|
265 // ============================================================================= |
|
266 // |
|
267 void MainWindow::updateTitle() |
|
268 { |
|
269 QString title = format (APPNAME " %1", VersionString()); |
|
270 |
|
271 // Append our current file if we have one |
|
272 if (CurrentDocument()) |
|
273 { |
|
274 title += ": "; |
|
275 title += CurrentDocument()->getDisplayName(); |
|
276 |
|
277 if (CurrentDocument()->getObjectCount() > 0 and |
|
278 CurrentDocument()->getObject (0)->type() == OBJ_Comment) |
|
279 { |
|
280 // Append title |
|
281 LDCommentPtr comm = CurrentDocument()->getObject (0).staticCast<LDComment>(); |
|
282 title += format (": %1", comm->text()); |
|
283 } |
|
284 |
|
285 if (CurrentDocument()->hasUnsavedChanges()) |
|
286 title += '*'; |
|
287 } |
|
288 |
|
289 #ifdef DEBUG |
|
290 title += " [debug build]"; |
|
291 #elif BUILD_ID != BUILD_RELEASE |
|
292 title += " [pre-release build]"; |
|
293 #endif // DEBUG |
|
294 |
|
295 if (CommitTimeString()[0] != '\0') |
|
296 title += format (" (%1)", QString::fromUtf8 (CommitTimeString())); |
|
297 |
|
298 setWindowTitle (title); |
|
299 } |
|
300 |
|
301 // ============================================================================= |
|
302 // |
|
303 int MainWindow::deleteSelection() |
|
304 { |
|
305 if (Selection().isEmpty()) |
|
306 return 0; |
|
307 |
|
308 LDObjectList selCopy = Selection(); |
|
309 |
|
310 // Delete the objects that were being selected |
|
311 for (LDObjectPtr obj : selCopy) |
|
312 obj->destroy(); |
|
313 |
|
314 refresh(); |
|
315 return selCopy.size(); |
|
316 } |
|
317 |
|
318 // ============================================================================= |
|
319 // |
|
320 void MainWindow::buildObjList() |
|
321 { |
|
322 if (not CurrentDocument()) |
|
323 return; |
|
324 |
|
325 // Lock the selection while we do this so that refreshing the object list |
|
326 // doesn't trigger selection updating so that the selection doesn't get lost |
|
327 // while this is done. |
|
328 g_isSelectionLocked = true; |
|
329 |
|
330 for (int i = 0; i < ui->objectList->count(); ++i) |
|
331 delete ui->objectList->item (i); |
|
332 |
|
333 ui->objectList->clear(); |
|
334 |
|
335 for (LDObjectPtr obj : CurrentDocument()->objects()) |
|
336 { |
|
337 QString descr; |
|
338 |
|
339 switch (obj->type()) |
|
340 { |
|
341 case OBJ_Comment: |
|
342 { |
|
343 descr = obj.staticCast<LDComment>()->text(); |
|
344 |
|
345 // Remove leading whitespace |
|
346 while (descr[0] == ' ') |
|
347 descr.remove (0, 1); |
|
348 |
|
349 break; |
|
350 } |
|
351 |
|
352 case OBJ_Empty: |
|
353 break; // leave it empty |
|
354 |
|
355 case OBJ_Line: |
|
356 case OBJ_Triangle: |
|
357 case OBJ_Quad: |
|
358 case OBJ_CondLine: |
|
359 { |
|
360 for (int i = 0; i < obj->numVertices(); ++i) |
|
361 { |
|
362 if (i != 0) |
|
363 descr += ", "; |
|
364 |
|
365 descr += obj->vertex (i).toString (true); |
|
366 } |
|
367 break; |
|
368 } |
|
369 |
|
370 case OBJ_Error: |
|
371 { |
|
372 descr = format ("ERROR: %1", obj->asText()); |
|
373 break; |
|
374 } |
|
375 |
|
376 case OBJ_Subfile: |
|
377 { |
|
378 LDSubfilePtr ref = obj.staticCast<LDSubfile>(); |
|
379 |
|
380 descr = format ("%1 %2, (", ref->fileInfo()->getDisplayName(), ref->position().toString (true)); |
|
381 |
|
382 for (int i = 0; i < 9; ++i) |
|
383 descr += format ("%1%2", ref->transform()[i], (i != 8) ? " " : ""); |
|
384 |
|
385 descr += ')'; |
|
386 break; |
|
387 } |
|
388 |
|
389 case OBJ_BFC: |
|
390 { |
|
391 descr = LDBFC::StatementStrings[int (obj.staticCast<LDBFC>()->statement())]; |
|
392 break; |
|
393 } |
|
394 |
|
395 case OBJ_Overlay: |
|
396 { |
|
397 LDOverlayPtr ovl = obj.staticCast<LDOverlay>(); |
|
398 descr = format ("[%1] %2 (%3, %4), %5 x %6", g_CameraNames[ovl->camera()], |
|
399 Basename (ovl->fileName()), ovl->x(), ovl->y(), |
|
400 ovl->width(), ovl->height()); |
|
401 break; |
|
402 } |
|
403 |
|
404 default: |
|
405 { |
|
406 descr = obj->typeName(); |
|
407 break; |
|
408 } |
|
409 } |
|
410 |
|
411 QListWidgetItem* item = new QListWidgetItem (descr); |
|
412 item->setIcon (GetIcon (obj->typeName())); |
|
413 |
|
414 // Use italic font if hidden |
|
415 if (obj->isHidden()) |
|
416 { |
|
417 QFont font = item->font(); |
|
418 font.setItalic (true); |
|
419 item->setFont (font); |
|
420 } |
|
421 |
|
422 // Color gibberish orange on red so it stands out. |
|
423 if (obj->type() == OBJ_Error) |
|
424 { |
|
425 item->setBackground (QColor ("#AA0000")); |
|
426 item->setForeground (QColor ("#FFAA00")); |
|
427 } |
|
428 elif (cfg::ColorizeObjectsList and obj->isColored() and |
|
429 obj->color() != null and obj->color() != MainColor() and obj->color() != EdgeColor()) |
|
430 { |
|
431 // If the object isn't in the main or edge color, draw this |
|
432 // list entry in said color. |
|
433 item->setForeground (obj->color().faceColor()); |
|
434 } |
|
435 |
|
436 obj->qObjListEntry = item; |
|
437 ui->objectList->insertItem (ui->objectList->count(), item); |
|
438 } |
|
439 |
|
440 g_isSelectionLocked = false; |
|
441 updateSelection(); |
|
442 scrollToSelection(); |
|
443 } |
|
444 |
|
445 // ============================================================================= |
|
446 // |
|
447 void MainWindow::scrollToSelection() |
|
448 { |
|
449 if (Selection().isEmpty()) |
|
450 return; |
|
451 |
|
452 LDObjectPtr obj = Selection().last(); |
|
453 ui->objectList->scrollToItem (obj->qObjListEntry); |
|
454 } |
|
455 |
|
456 // ============================================================================= |
|
457 // |
|
458 void MainWindow::slot_selectionChanged() |
|
459 { |
|
460 if (g_isSelectionLocked == true or CurrentDocument() == null) |
|
461 return; |
|
462 |
|
463 LDObjectList priorSelection = Selection(); |
|
464 |
|
465 // Get the objects from the object list selection |
|
466 CurrentDocument()->clearSelection(); |
|
467 const QList<QListWidgetItem*> items = ui->objectList->selectedItems(); |
|
468 |
|
469 for (LDObjectPtr obj : CurrentDocument()->objects()) |
|
470 { |
|
471 for (QListWidgetItem* item : items) |
|
472 { |
|
473 if (item == obj->qObjListEntry) |
|
474 { |
|
475 obj->select(); |
|
476 break; |
|
477 } |
|
478 } |
|
479 } |
|
480 |
|
481 // The select() method calls may have selected additional items (i.e. invertnexts) |
|
482 // Update it all now. |
|
483 updateSelection(); |
|
484 |
|
485 // Update the GL renderer |
|
486 LDObjectList compound = priorSelection + Selection(); |
|
487 RemoveDuplicates (compound); |
|
488 |
|
489 for (LDObjectPtr obj : compound) |
|
490 R()->compileObject (obj); |
|
491 |
|
492 R()->update(); |
|
493 } |
|
494 |
|
495 // ============================================================================= |
|
496 // |
|
497 void MainWindow::slot_recentFile() |
|
498 { |
|
499 QAction* qAct = static_cast<QAction*> (sender()); |
|
500 OpenMainModel (qAct->text()); |
|
501 } |
|
502 |
|
503 // ============================================================================= |
|
504 // |
|
505 void MainWindow::slot_quickColor() |
|
506 { |
|
507 QToolButton* button = static_cast<QToolButton*> (sender()); |
|
508 LDColor col = null; |
|
509 |
|
510 for (const LDQuickColor& entry : m_quickColors) |
|
511 { |
|
512 if (entry.toolButton() == button) |
|
513 { |
|
514 col = entry.color(); |
|
515 break; |
|
516 } |
|
517 } |
|
518 |
|
519 if (col == null) |
|
520 return; |
|
521 |
|
522 for (LDObjectPtr obj : Selection()) |
|
523 { |
|
524 if (not obj->isColored()) |
|
525 continue; // uncolored object |
|
526 |
|
527 obj->setColor (col); |
|
528 R()->compileObject (obj); |
|
529 } |
|
530 |
|
531 endAction(); |
|
532 refresh(); |
|
533 } |
|
534 |
|
535 // ============================================================================= |
|
536 // |
|
537 int MainWindow::getInsertionPoint() |
|
538 { |
|
539 // If we have a selection, put the item after it. |
|
540 if (not Selection().isEmpty()) |
|
541 return Selection().last()->lineNumber() + 1; |
|
542 |
|
543 // Otherwise place the object at the end. |
|
544 return CurrentDocument()->getObjectCount(); |
|
545 } |
|
546 |
|
547 // ============================================================================= |
|
548 // |
|
549 void MainWindow::doFullRefresh() |
|
550 { |
|
551 buildObjList(); |
|
552 m_renderer->hardRefresh(); |
|
553 } |
|
554 |
|
555 // ============================================================================= |
|
556 // |
|
557 void MainWindow::refresh() |
|
558 { |
|
559 buildObjList(); |
|
560 m_renderer->update(); |
|
561 } |
|
562 |
|
563 // ============================================================================= |
|
564 // |
|
565 void MainWindow::updateSelection() |
|
566 { |
|
567 g_isSelectionLocked = true; |
|
568 QItemSelection itemselect; |
|
569 int top = -1; |
|
570 int bottom = -1; |
|
571 |
|
572 for (LDObjectPtr obj : Selection()) |
|
573 { |
|
574 if (obj->qObjListEntry == null) |
|
575 continue; |
|
576 |
|
577 int row = ui->objectList->row (obj->qObjListEntry); |
|
578 |
|
579 if (top == -1) |
|
580 { |
|
581 top = bottom = row; |
|
582 } |
|
583 else |
|
584 { |
|
585 if (row != bottom + 1) |
|
586 { |
|
587 itemselect.select (ui->objectList->model()->index (top, 0), |
|
588 ui->objectList->model()->index (bottom, 0)); |
|
589 top = -1; |
|
590 } |
|
591 |
|
592 bottom = row; |
|
593 } |
|
594 } |
|
595 |
|
596 if (top != -1) |
|
597 { |
|
598 itemselect.select (ui->objectList->model()->index (top, 0), |
|
599 ui->objectList->model()->index (bottom, 0)); |
|
600 } |
|
601 |
|
602 ui->objectList->selectionModel()->select (itemselect, QItemSelectionModel::ClearAndSelect); |
|
603 g_isSelectionLocked = false; |
|
604 } |
|
605 |
|
606 // ============================================================================= |
|
607 // |
|
608 LDColor MainWindow::getSelectedColor() |
|
609 { |
|
610 LDColor result; |
|
611 |
|
612 for (LDObjectPtr obj : Selection()) |
|
613 { |
|
614 if (not obj->isColored()) |
|
615 continue; // doesn't use color |
|
616 |
|
617 if (result != null and obj->color() != result) |
|
618 return null; // No consensus in object color |
|
619 |
|
620 if (result == null) |
|
621 result = obj->color(); |
|
622 } |
|
623 |
|
624 return result; |
|
625 } |
|
626 |
|
627 // ============================================================================= |
|
628 // |
|
629 void MainWindow::closeEvent (QCloseEvent* ev) |
|
630 { |
|
631 // Check whether it's safe to close all files. |
|
632 if (not IsSafeToCloseAll()) |
|
633 { |
|
634 ev->ignore(); |
|
635 return; |
|
636 } |
|
637 |
|
638 // Save the toolbar set |
|
639 cfg::HiddenToolbars.clear(); |
|
640 |
|
641 for (QToolBar* toolbar : findChildren<QToolBar*>()) |
|
642 { |
|
643 if (toolbar->isHidden()) |
|
644 cfg::HiddenToolbars << toolbar->objectName(); |
|
645 } |
|
646 |
|
647 // Save the configuration before leaving. |
|
648 Config::Save(); |
|
649 ev->accept(); |
|
650 } |
|
651 |
|
652 // ============================================================================= |
|
653 // |
|
654 void MainWindow::spawnContextMenu (const QPoint pos) |
|
655 { |
|
656 const bool single = (Selection().size() == 1); |
|
657 LDObjectPtr singleObj = single ? Selection().first() : LDObjectPtr(); |
|
658 |
|
659 bool hasSubfiles = false; |
|
660 |
|
661 for (LDObjectPtr obj : Selection()) |
|
662 { |
|
663 if (obj->type() == OBJ_Subfile) |
|
664 { |
|
665 hasSubfiles = true; |
|
666 break; |
|
667 } |
|
668 } |
|
669 |
|
670 QMenu* contextMenu = new QMenu; |
|
671 |
|
672 if (single and singleObj->type() != OBJ_Empty) |
|
673 { |
|
674 contextMenu->addAction (ui->actionEdit); |
|
675 contextMenu->addSeparator(); |
|
676 } |
|
677 |
|
678 contextMenu->addAction (ui->actionCut); |
|
679 contextMenu->addAction (ui->actionCopy); |
|
680 contextMenu->addAction (ui->actionPaste); |
|
681 contextMenu->addAction (ui->actionDelete); |
|
682 contextMenu->addSeparator(); |
|
683 contextMenu->addAction (ui->actionSetColor); |
|
684 |
|
685 if (single) |
|
686 contextMenu->addAction (ui->actionEditRaw); |
|
687 |
|
688 contextMenu->addAction (ui->actionBorders); |
|
689 contextMenu->addAction (ui->actionSetOverlay); |
|
690 contextMenu->addAction (ui->actionClearOverlay); |
|
691 |
|
692 if (hasSubfiles) |
|
693 { |
|
694 contextMenu->addSeparator(); |
|
695 contextMenu->addAction (ui->actionOpenSubfiles); |
|
696 } |
|
697 |
|
698 contextMenu->addSeparator(); |
|
699 contextMenu->addAction (ui->actionModeSelect); |
|
700 contextMenu->addAction (ui->actionModeDraw); |
|
701 contextMenu->addAction (ui->actionModeCircle); |
|
702 |
|
703 if (not Selection().isEmpty()) |
|
704 { |
|
705 contextMenu->addSeparator(); |
|
706 contextMenu->addAction (ui->actionSubfileSelection); |
|
707 } |
|
708 |
|
709 if (R()->camera() != EFreeCamera) |
|
710 { |
|
711 contextMenu->addSeparator(); |
|
712 contextMenu->addAction (ui->actionSetDrawDepth); |
|
713 } |
|
714 |
|
715 contextMenu->exec (pos); |
|
716 } |
|
717 |
|
718 // ============================================================================= |
|
719 // |
|
720 void MainWindow::deleteByColor (LDColor color) |
|
721 { |
|
722 LDObjectList objs; |
|
723 |
|
724 for (LDObjectPtr obj : CurrentDocument()->objects()) |
|
725 { |
|
726 if (not obj->isColored() or obj->color() != color) |
|
727 continue; |
|
728 |
|
729 objs << obj; |
|
730 } |
|
731 |
|
732 for (LDObjectPtr obj : objs) |
|
733 obj->destroy(); |
|
734 } |
|
735 |
|
736 // ============================================================================= |
|
737 // |
|
738 void MainWindow::updateEditModeActions() |
|
739 { |
|
740 const EditModeType mode = R()->currentEditModeType(); |
|
741 ui->actionModeSelect->setChecked (mode == EditModeType::Select); |
|
742 ui->actionModeDraw->setChecked (mode == EditModeType::Draw); |
|
743 ui->actionModeRectangle->setChecked (mode == EditModeType::Rectangle); |
|
744 ui->actionModeCircle->setChecked (mode == EditModeType::Circle); |
|
745 ui->actionModeMagicWand->setChecked (mode == EditModeType::MagicWand); |
|
746 ui->actionModeLinePath->setChecked (mode == EditModeType::LinePath); |
|
747 } |
|
748 |
|
749 // ============================================================================= |
|
750 // |
|
751 void MainWindow::slot_editObject (QListWidgetItem* listitem) |
|
752 { |
|
753 for (LDObjectPtr it : CurrentDocument()->objects()) |
|
754 { |
|
755 if (it->qObjListEntry == listitem) |
|
756 { |
|
757 AddObjectDialog::staticDialog (it->type(), it); |
|
758 break; |
|
759 } |
|
760 } |
|
761 } |
|
762 |
|
763 // ============================================================================= |
|
764 // |
|
765 bool MainWindow::save (LDDocumentPtr doc, bool saveAs) |
|
766 { |
|
767 QString path = doc->fullPath(); |
|
768 int64 savesize; |
|
769 |
|
770 if (saveAs or path.isEmpty()) |
|
771 { |
|
772 QString name = doc->defaultName(); |
|
773 |
|
774 if (not doc->fullPath().isEmpty()) |
|
775 name = doc->fullPath(); |
|
776 elif (not doc->name().isEmpty()) |
|
777 name = doc->name(); |
|
778 |
|
779 name.replace ("\\", "/"); |
|
780 path = QFileDialog::getSaveFileName (g_win, tr ("Save As"), |
|
781 name, tr ("LDraw files (*.dat *.ldr)")); |
|
782 |
|
783 if (path.isEmpty()) |
|
784 { |
|
785 // User didn't give a file name, abort. |
|
786 return false; |
|
787 } |
|
788 } |
|
789 |
|
790 if (doc->save (path, &savesize)) |
|
791 { |
|
792 if (doc == CurrentDocument()) |
|
793 updateTitle(); |
|
794 |
|
795 print ("Saved to %1 (%2)", path, MakePrettyFileSize (savesize)); |
|
796 |
|
797 // Add it to recent files |
|
798 AddRecentFile (path); |
|
799 return true; |
|
800 } |
|
801 |
|
802 QString message = format (tr ("Failed to save to %1: %2"), path, strerror (errno)); |
|
803 |
|
804 // Tell the user the save failed, and give the option for saving as with it. |
|
805 QMessageBox dlg (QMessageBox::Critical, tr ("Save Failure"), message, QMessageBox::Close, g_win); |
|
806 |
|
807 // Add a save-as button |
|
808 QPushButton* saveAsBtn = new QPushButton (tr ("Save As")); |
|
809 saveAsBtn->setIcon (GetIcon ("file-save-as")); |
|
810 dlg.addButton (saveAsBtn, QMessageBox::ActionRole); |
|
811 dlg.setDefaultButton (QMessageBox::Close); |
|
812 dlg.exec(); |
|
813 |
|
814 if (dlg.clickedButton() == saveAsBtn) |
|
815 return save (doc, true); // yay recursion! |
|
816 |
|
817 return false; |
|
818 } |
|
819 |
|
820 void MainWindow::addMessage (QString msg) |
|
821 { |
|
822 m_messageLog->addLine (msg); |
|
823 } |
|
824 |
|
825 // ============================================================================ |
|
826 void ObjectList::contextMenuEvent (QContextMenuEvent* ev) |
|
827 { |
|
828 g_win->spawnContextMenu (ev->globalPos()); |
|
829 } |
|
830 |
|
831 // ============================================================================= |
|
832 // |
|
833 QPixmap GetIcon (QString iconName) |
|
834 { |
|
835 return (QPixmap (format (":/icons/%1.png", iconName))); |
|
836 } |
|
837 |
|
838 // ============================================================================= |
|
839 // |
|
840 bool Confirm (const QString& message) |
|
841 { |
|
842 return Confirm (MainWindow::tr ("Confirm"), message); |
|
843 } |
|
844 |
|
845 // ============================================================================= |
|
846 // |
|
847 bool Confirm (const QString& title, const QString& message) |
|
848 { |
|
849 return QMessageBox::question (g_win, title, message, |
|
850 (QMessageBox::Yes | QMessageBox::No), QMessageBox::No) == QMessageBox::Yes; |
|
851 } |
|
852 |
|
853 // ============================================================================= |
|
854 // |
|
855 void Critical (const QString& message) |
|
856 { |
|
857 QMessageBox::critical (g_win, MainWindow::tr ("Error"), message, |
|
858 (QMessageBox::Close), QMessageBox::Close); |
|
859 } |
|
860 |
|
861 // ============================================================================= |
|
862 // |
|
863 QIcon MakeColorIcon (LDColor colinfo, const int size) |
|
864 { |
|
865 // Create an image object and link a painter to it. |
|
866 QImage img (size, size, QImage::Format_ARGB32); |
|
867 QPainter paint (&img); |
|
868 QColor col = colinfo.faceColor(); |
|
869 |
|
870 if (colinfo == MainColor()) |
|
871 { |
|
872 // Use the user preferences for main color here |
|
873 col = cfg::MainColor; |
|
874 col.setAlphaF (cfg::MainColorAlpha); |
|
875 } |
|
876 |
|
877 // Paint the icon border |
|
878 paint.fillRect (QRect (0, 0, size, size), colinfo.edgeColor()); |
|
879 |
|
880 // Paint the checkerboard background, visible with translucent icons |
|
881 paint.drawPixmap (QRect (1, 1, size - 2, size - 2), GetIcon ("checkerboard"), QRect (0, 0, 8, 8)); |
|
882 |
|
883 // Paint the color above the checkerboard |
|
884 paint.fillRect (QRect (1, 1, size - 2, size - 2), col); |
|
885 return QIcon (QPixmap::fromImage (img)); |
|
886 } |
|
887 |
|
888 // ============================================================================= |
|
889 // |
|
890 void MakeColorComboBox (QComboBox* box) |
|
891 { |
|
892 std::map<LDColor, int> counts; |
|
893 |
|
894 for (LDObjectPtr obj : CurrentDocument()->objects()) |
|
895 { |
|
896 if (not obj->isColored() or obj->color() == null) |
|
897 continue; |
|
898 |
|
899 if (counts.find (obj->color()) == counts.end()) |
|
900 counts[obj->color()] = 1; |
|
901 else |
|
902 counts[obj->color()]++; |
|
903 } |
|
904 |
|
905 box->clear(); |
|
906 int row = 0; |
|
907 |
|
908 for (const auto& pair : counts) |
|
909 { |
|
910 QIcon ico = MakeColorIcon (pair.first, 16); |
|
911 box->addItem (ico, format ("[%1] %2 (%3 object%4)", |
|
912 pair.first, pair.first.name(), pair.second, Plural (pair.second))); |
|
913 box->setItemData (row, pair.first.index()); |
|
914 |
|
915 ++row; |
|
916 } |
|
917 } |
|
918 |
|
919 // ============================================================================= |
|
920 // |
|
921 void MainWindow::updateDocumentList() |
|
922 { |
|
923 m_isUpdatingTabs = true; |
|
924 |
|
925 while (m_tabBar->count() > 0) |
|
926 m_tabBar->removeTab (0); |
|
927 |
|
928 for (LDDocumentPtr f : LDDocument::explicitDocuments()) |
|
929 { |
|
930 // Add an item to the list for this file and store the tab index |
|
931 // in the document so we can find documents by tab index. |
|
932 f->setTabIndex (m_tabBar->addTab ("")); |
|
933 updateDocumentListItem (f); |
|
934 } |
|
935 |
|
936 m_isUpdatingTabs = false; |
|
937 } |
|
938 |
|
939 // ============================================================================= |
|
940 // |
|
941 void MainWindow::updateDocumentListItem (LDDocumentPtr doc) |
|
942 { |
|
943 bool oldUpdatingTabs = m_isUpdatingTabs; |
|
944 m_isUpdatingTabs = true; |
|
945 |
|
946 if (doc->tabIndex() == -1) |
|
947 { |
|
948 // We don't have a list item for this file, so the list either doesn't |
|
949 // exist yet or is out of date. Build the list now. |
|
950 updateDocumentList(); |
|
951 return; |
|
952 } |
|
953 |
|
954 // If this is the current file, it also needs to be the selected item on |
|
955 // the list. |
|
956 if (doc == CurrentDocument()) |
|
957 m_tabBar->setCurrentIndex (doc->tabIndex()); |
|
958 |
|
959 m_tabBar->setTabText (doc->tabIndex(), doc->getDisplayName()); |
|
960 |
|
961 // If the document.has unsaved changes, draw a little icon next to it to mark that. |
|
962 m_tabBar->setTabIcon (doc->tabIndex(), doc->hasUnsavedChanges() ? GetIcon ("file-save") : QIcon()); |
|
963 m_tabBar->setTabData (doc->tabIndex(), doc->name()); |
|
964 m_isUpdatingTabs = oldUpdatingTabs; |
|
965 } |
|
966 |
|
967 // ============================================================================= |
|
968 // |
|
969 // A file is selected from the list of files on the left of the screen. Find out |
|
970 // which file was picked and change to it. |
|
971 // |
|
972 void MainWindow::changeCurrentFile() |
|
973 { |
|
974 if (m_isUpdatingTabs) |
|
975 return; |
|
976 |
|
977 LDDocumentPtr f; |
|
978 int tabIndex = m_tabBar->currentIndex(); |
|
979 |
|
980 // Find the file pointer of the item that was selected. |
|
981 for (LDDocumentPtr it : LDDocument::explicitDocuments()) |
|
982 { |
|
983 if (it->tabIndex() == tabIndex) |
|
984 { |
|
985 f = it; |
|
986 break; |
|
987 } |
|
988 } |
|
989 |
|
990 // If we picked the same file we're currently on, we don't need to do |
|
991 // anything. |
|
992 if (f == null or f == CurrentDocument()) |
|
993 return; |
|
994 |
|
995 LDDocument::setCurrent (f); |
|
996 } |
|
997 |
|
998 // ============================================================================= |
|
999 // |
|
1000 void MainWindow::refreshObjectList() |
|
1001 { |
|
1002 #if 0 |
|
1003 ui->objectList->clear(); |
|
1004 LDDocumentPtr f = getCurrentDocument(); |
|
1005 |
|
1006 for (LDObjectPtr obj : *f) |
|
1007 ui->objectList->addItem (obj->qObjListEntry); |
|
1008 |
|
1009 #endif |
|
1010 |
|
1011 buildObjList(); |
|
1012 } |
|
1013 |
|
1014 // ============================================================================= |
|
1015 // |
|
1016 void MainWindow::updateActions() |
|
1017 { |
|
1018 if (CurrentDocument() != null and CurrentDocument()->history() != null) |
|
1019 { |
|
1020 History* his = CurrentDocument()->history(); |
|
1021 int pos = his->position(); |
|
1022 ui->actionUndo->setEnabled (pos != -1); |
|
1023 ui->actionRedo->setEnabled (pos < (long) his->getSize() - 1); |
|
1024 } |
|
1025 |
|
1026 ui->actionWireframe->setChecked (cfg::DrawWireframe); |
|
1027 ui->actionAxes->setChecked (cfg::DrawAxes); |
|
1028 ui->actionBFCView->setChecked (cfg::BFCRedGreenView); |
|
1029 ui->actionRandomColors->setChecked (cfg::RandomColors); |
|
1030 ui->actionDrawAngles->setChecked (cfg::DrawAngles); |
|
1031 ui->actionDrawSurfaces->setChecked (cfg::DrawSurfaces); |
|
1032 ui->actionDrawEdgeLines->setChecked (cfg::DrawEdgeLines); |
|
1033 ui->actionDrawConditionalLines->setChecked (cfg::DrawConditionalLines); |
|
1034 } |
|
1035 |
|
1036 // ============================================================================= |
|
1037 // |
|
1038 void MainWindow::updatePrimitives() |
|
1039 { |
|
1040 PopulatePrimitives (ui->primitives); |
|
1041 } |
|
1042 |
|
1043 // ============================================================================= |
|
1044 // |
|
1045 void MainWindow::closeTab (int tabindex) |
|
1046 { |
|
1047 LDDocumentPtr doc = FindDocument (m_tabBar->tabData (tabindex).toString()); |
|
1048 |
|
1049 if (doc == null) |
|
1050 return; |
|
1051 |
|
1052 doc->dismiss(); |
|
1053 } |
|
1054 |
|
1055 // ============================================================================= |
|
1056 // |
|
1057 void MainWindow::loadShortcuts (QSettings const* settings) |
|
1058 { |
|
1059 for (QAction* act : findChildren<QAction*>()) |
|
1060 { |
|
1061 QKeySequence seq = settings->value ("shortcut_" + act->objectName(), act->shortcut()).value<QKeySequence>(); |
|
1062 act->setShortcut (seq); |
|
1063 } |
|
1064 } |
|
1065 |
|
1066 // ============================================================================= |
|
1067 // |
|
1068 void MainWindow::saveShortcuts (QSettings* settings) |
|
1069 { |
|
1070 applyToActions ([&](QAction* act) |
|
1071 { |
|
1072 QString const key = "shortcut_" + act->objectName(); |
|
1073 |
|
1074 if (g_defaultShortcuts[act] != act->shortcut()) |
|
1075 settings->setValue (key, act->shortcut()); |
|
1076 else |
|
1077 settings->remove (key); |
|
1078 }); |
|
1079 } |
|
1080 |
|
1081 // ============================================================================= |
|
1082 // |
|
1083 void MainWindow::applyToActions (std::function<void(QAction*)> function) |
|
1084 { |
|
1085 for (QAction* act : findChildren<QAction*>()) |
|
1086 { |
|
1087 if (not act->objectName().isEmpty()) |
|
1088 function (act); |
|
1089 } |
|
1090 } |
|
1091 |
|
1092 // ============================================================================= |
|
1093 // |
|
1094 QKeySequence MainWindow::defaultShortcut (QAction* act) // [static] |
|
1095 { |
|
1096 return g_defaultShortcuts[act]; |
|
1097 } |
|
1098 |
|
1099 // ============================================================================= |
|
1100 // |
|
1101 bool MainWindow::ringToolHiRes() const |
|
1102 { |
|
1103 return ui->ringToolHiRes->isChecked(); |
|
1104 } |
|
1105 |
|
1106 // ============================================================================= |
|
1107 // |
|
1108 int MainWindow::ringToolSegments() const |
|
1109 { |
|
1110 return ui->ringToolSegments->value(); |
|
1111 } |
|
1112 |
|
1113 // ============================================================================= |
|
1114 // |
|
1115 void MainWindow::ringToolHiResClicked (bool checked) |
|
1116 { |
|
1117 if (checked) |
|
1118 { |
|
1119 ui->ringToolSegments->setMaximum (HighResolution); |
|
1120 ui->ringToolSegments->setValue (ui->ringToolSegments->value() * 3); |
|
1121 } |
|
1122 else |
|
1123 { |
|
1124 ui->ringToolSegments->setValue (ui->ringToolSegments->value() / 3); |
|
1125 ui->ringToolSegments->setMaximum (LowResolution); |
|
1126 } |
|
1127 } |
|
1128 |
|
1129 // ============================================================================= |
|
1130 // |
|
1131 void MainWindow::circleToolSegmentsChanged() |
|
1132 { |
|
1133 int numerator (ui->ringToolSegments->value()); |
|
1134 int denominator (ui->ringToolHiRes->isChecked() ? HighResolution : LowResolution); |
|
1135 Simplify (numerator, denominator); |
|
1136 ui->ringToolSegmentsLabel->setText (format ("%1 / %2", numerator, denominator)); |
|
1137 } |
|
1138 |
|
1139 // ============================================================================= |
|
1140 // |
|
1141 QImage GetImageFromScreencap (uchar* data, int w, int h) |
|
1142 { |
|
1143 // GL and Qt formats have R and B swapped. Also, GL flips Y - correct it as well. |
|
1144 return QImage (data, w, h, QImage::Format_ARGB32).rgbSwapped().mirrored(); |
|
1145 } |
|
1146 |
|
1147 // ============================================================================= |
|
1148 // |
|
1149 LDQuickColor::LDQuickColor (LDColor color, QToolButton* toolButton) : |
|
1150 m_color (color), |
|
1151 m_toolButton (toolButton) {} |
|
1152 |
|
1153 // ============================================================================= |
|
1154 // |
|
1155 LDQuickColor LDQuickColor::getSeparator() |
|
1156 { |
|
1157 return LDQuickColor (null, null); |
|
1158 } |
|
1159 |
|
1160 // ============================================================================= |
|
1161 // |
|
1162 bool LDQuickColor::isSeparator() const |
|
1163 { |
|
1164 return color() == null; |
|
1165 } |
|
1166 |
|
1167 void PopulatePrimitives (QTreeWidget* tw, QString const& selectByDefault) |
|
1168 { |
|
1169 tw->clear(); |
|
1170 |
|
1171 for (PrimitiveCategory* cat : g_PrimitiveCategories) |
|
1172 { |
|
1173 SubfileListItem* parentItem = new SubfileListItem (tw, null); |
|
1174 parentItem->setText (0, cat->name()); |
|
1175 QList<QTreeWidgetItem*> subfileItems; |
|
1176 |
|
1177 for (Primitive& prim : cat->prims) |
|
1178 { |
|
1179 SubfileListItem* item = new SubfileListItem (parentItem, &prim); |
|
1180 item->setText (0, format ("%1 - %2", prim.name, prim.title)); |
|
1181 subfileItems << item; |
|
1182 |
|
1183 // If this primitive is the one the current object points to, |
|
1184 // select it by default |
|
1185 if (selectByDefault == prim.name) |
|
1186 tw->setCurrentItem (item); |
|
1187 } |
|
1188 |
|
1189 tw->addTopLevelItem (parentItem); |
|
1190 } |
|
1191 } |
|