src/mainwindow.cpp

changeset 1231
ce0c9f2e6b9c
parent 1230
29dc03eceb5f
child 1232
7eb8b59577d0
equal deleted inserted replaced
1230:29dc03eceb5f 1231:ce0c9f2e6b9c
75 m_isSelectionLocked(false) 75 m_isSelectionLocked(false)
76 { 76 {
77 g_win = this; 77 g_win = this;
78 ui.setupUi(this); 78 ui.setupUi(this);
79 m_updatingTabs = false; 79 m_updatingTabs = false;
80 m_renderer = new GLRenderer(this);
81 m_tabs = new QTabBar; 80 m_tabs = new QTabBar;
82 m_tabs->setTabsClosable(true); 81 m_tabs->setTabsClosable(true);
83 ui.verticalLayout->insertWidget(0, m_tabs); 82 ui.verticalLayout->insertWidget(0, m_tabs);
84 83
85 createBlankDocument(); 84 createBlankDocument();
86 m_renderer->setDocument(m_currentDocument);
87
88 // Stuff the renderer into its frame
89 QVBoxLayout* rendererLayout = new QVBoxLayout(ui.rendererFrame);
90 rendererLayout->addWidget(renderer());
91 85
92 connect(ui.objectList, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChanged())); 86 connect(ui.objectList, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChanged()));
93 connect(ui.objectList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(objectListDoubleClicked(QListWidgetItem*))); 87 connect(ui.objectList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(objectListDoubleClicked(QListWidgetItem*)));
94 connect(m_tabs, SIGNAL(currentChanged(int)), this, SLOT(tabSelected())); 88 connect(m_tabs, SIGNAL(currentChanged(int)), this, SLOT(tabSelected()));
95 connect(m_tabs, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int))); 89 connect(m_tabs, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
115 updateRecentFilesMenu(); 109 updateRecentFilesMenu();
116 updateColorToolbar(); 110 updateColorToolbar();
117 updateTitle(); 111 updateTitle();
118 loadShortcuts(); 112 loadShortcuts();
119 setMinimumSize(300, 200); 113 setMinimumSize(300, 200);
120 connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(doLastSecondCleanup()));
121 connect( 114 connect(
122 ui.ringToolSegments, SIGNAL(valueChanged(int)), 115 ui.ringToolSegments, SIGNAL(valueChanged(int)),
123 this, SLOT(circleToolSegmentsChanged()) 116 this, SLOT(circleToolSegmentsChanged())
124 ); 117 );
125 connect( 118 connect(
184 } 177 }
185 } 178 }
186 179
187 MainWindow::~MainWindow() 180 MainWindow::~MainWindow()
188 { 181 {
182 for (GLRenderer* renderer : m_renderers.values())
183 delete renderer;
184
185 delete &ui;
189 g_win = nullptr; 186 g_win = nullptr;
190 } 187 }
191 188
192 // --------------------------------------------------------------------------------------------------------------------- 189 // ---------------------------------------------------------------------------------------------------------------------
193 // 190 //
216 void MainWindow::endAction() 213 void MainWindow::endAction()
217 { 214 {
218 m_currentDocument->addHistoryStep(); 215 m_currentDocument->addHistoryStep();
219 updateDocumentListItem(m_currentDocument); 216 updateDocumentListItem(m_currentDocument);
220 refresh(); 217 refresh();
221 }
222
223 // ---------------------------------------------------------------------------------------------------------------------
224 //
225 void MainWindow::doLastSecondCleanup()
226 {
227 delete m_renderer;
228 delete &ui;
229 } 218 }
230 219
231 // --------------------------------------------------------------------------------------------------------------------- 220 // ---------------------------------------------------------------------------------------------------------------------
232 // 221 //
233 void MainWindow::updateRecentFilesMenu() 222 void MainWindow::updateRecentFilesMenu()
317 ui.actionGridMedium->setChecked(grid == Grid::Medium); 306 ui.actionGridMedium->setChecked(grid == Grid::Medium);
318 ui.actionGridFine->setChecked(grid == Grid::Fine); 307 ui.actionGridFine->setChecked(grid == Grid::Fine);
319 308
320 // Recompile all Bézier curves, the changing grid affects their precision. 309 // Recompile all Bézier curves, the changing grid affects their precision.
321 for (LDObjectIterator<LDBezierCurve> it(m_currentDocument); it.isValid(); ++it) 310 for (LDObjectIterator<LDBezierCurve> it(m_currentDocument); it.isValid(); ++it)
322 renderer()->compileObject(it); 311 currentRenderer()->compileObject(it);
323 } 312 }
324 313
325 // --------------------------------------------------------------------------------------------------------------------- 314 // ---------------------------------------------------------------------------------------------------------------------
326 // 315 //
327 void MainWindow::updateTitle() 316 void MainWindow::updateTitle()
455 } 444 }
456 445
457 case OBJ_Overlay: 446 case OBJ_Overlay:
458 { 447 {
459 LDOverlay* ovl = static_cast<LDOverlay*>(obj); 448 LDOverlay* ovl = static_cast<LDOverlay*>(obj);
460 descr = format("[%1] %2(%3, %4), %5 x %6", renderer()->cameraName((ECamera) ovl->camera()), 449 descr = format("[%1] %2(%3, %4), %5 x %6", currentRenderer()->cameraName((ECamera) ovl->camera()),
461 Basename(ovl->fileName()), ovl->x(), ovl->y(), 450 Basename(ovl->fileName()), ovl->x(), ovl->y(),
462 ovl->width(), ovl->height()); 451 ovl->width(), ovl->height());
463 break; 452 break;
464 } 453 }
465 454
551 // Update the GL renderer 540 // Update the GL renderer
552 LDObjectList compound = priorSelection + selectedObjects(); 541 LDObjectList compound = priorSelection + selectedObjects();
553 removeDuplicates(compound); 542 removeDuplicates(compound);
554 543
555 for (LDObject* obj : compound) 544 for (LDObject* obj : compound)
556 renderer()->compileObject(obj); 545 currentRenderer()->compileObject(obj);
557 546
558 renderer()->update(); 547 currentRenderer()->update();
559 } 548 }
560 549
561 // --------------------------------------------------------------------------------------------------------------------- 550 // ---------------------------------------------------------------------------------------------------------------------
562 // 551 //
563 void MainWindow::recentFileClicked() 552 void MainWindow::recentFileClicked()
589 { 578 {
590 if (not obj->isColored()) 579 if (not obj->isColored())
591 continue; // uncolored object 580 continue; // uncolored object
592 581
593 obj->setColor(color); 582 obj->setColor(color);
594 renderer()->compileObject(obj); 583 currentRenderer()->compileObject(obj);
595 } 584 }
596 585
597 endAction(); 586 endAction();
598 refresh(); 587 refresh();
599 } 588 }
614 603
615 // --------------------------------------------------------------------------------------------------------------------- 604 // ---------------------------------------------------------------------------------------------------------------------
616 // 605 //
617 void MainWindow::doFullRefresh() 606 void MainWindow::doFullRefresh()
618 { 607 {
608 this->buildObjectList();
609 this->currentRenderer()->hardRefresh();
610 }
611
612 // ---------------------------------------------------------------------------------------------------------------------
613 //
614 // Builds the object list and tells the GL renderer to do a soft update.
615 //
616 void MainWindow::refresh()
617 {
619 buildObjectList(); 618 buildObjectList();
620 m_renderer->hardRefresh(); 619 currentRenderer()->update();
621 }
622
623 // ---------------------------------------------------------------------------------------------------------------------
624 //
625 // Builds the object list and tells the GL renderer to do a soft update.
626 //
627 void MainWindow::refresh()
628 {
629 buildObjectList();
630 m_renderer->update();
631 } 620 }
632 621
633 // --------------------------------------------------------------------------------------------------------------------- 622 // ---------------------------------------------------------------------------------------------------------------------
634 // 623 //
635 void MainWindow::updateSelection() 624 void MainWindow::updateSelection()
780 { 769 {
781 contextMenu->addSeparator(); 770 contextMenu->addSeparator();
782 contextMenu->addAction(ui.actionSubfileSelection); 771 contextMenu->addAction(ui.actionSubfileSelection);
783 } 772 }
784 773
785 if (renderer()->camera() != EFreeCamera) 774 if (currentRenderer()->camera() != EFreeCamera)
786 { 775 {
787 contextMenu->addSeparator(); 776 contextMenu->addSeparator();
788 contextMenu->addAction(ui.actionSetDrawDepth); 777 contextMenu->addAction(ui.actionSetDrawDepth);
789 } 778 }
790 779
811 800
812 // --------------------------------------------------------------------------------------------------------------------- 801 // ---------------------------------------------------------------------------------------------------------------------
813 // 802 //
814 void MainWindow::updateEditModeActions() 803 void MainWindow::updateEditModeActions()
815 { 804 {
816 const EditModeType mode = renderer()->currentEditModeType(); 805 const EditModeType mode = currentRenderer()->currentEditModeType();
817 ui.actionModeSelect->setChecked(mode == EditModeType::Select); 806 ui.actionModeSelect->setChecked(mode == EditModeType::Select);
818 ui.actionModeDraw->setChecked(mode == EditModeType::Draw); 807 ui.actionModeDraw->setChecked(mode == EditModeType::Draw);
819 ui.actionModeRectangle->setChecked(mode == EditModeType::Rectangle); 808 ui.actionModeRectangle->setChecked(mode == EditModeType::Rectangle);
820 ui.actionModeCircle->setChecked(mode == EditModeType::Circle); 809 ui.actionModeCircle->setChecked(mode == EditModeType::Circle);
821 ui.actionModeMagicWand->setChecked(mode == EditModeType::MagicWand); 810 ui.actionModeMagicWand->setChecked(mode == EditModeType::MagicWand);
892 } 881 }
893 882
894 // Adds a message to the renderer's message manager. 883 // Adds a message to the renderer's message manager.
895 void MainWindow::addMessage(QString msg) 884 void MainWindow::addMessage(QString msg)
896 { 885 {
897 m_renderer->messageLog()->addLine(msg); 886 this->currentRenderer()->messageLog()->addLine(msg);
898 } 887 }
899 888
900 // ============================================================================ 889 // ============================================================================
901 void ObjectList::contextMenuEvent(QContextMenuEvent* ev) 890 void ObjectList::contextMenuEvent(QContextMenuEvent* ev)
902 { 891 {
1056 ui.actionDrawConditionalLines->setChecked(config->drawConditionalLines()); 1045 ui.actionDrawConditionalLines->setChecked(config->drawConditionalLines());
1057 } 1046 }
1058 1047
1059 // --------------------------------------------------------------------------------------------------------------------- 1048 // ---------------------------------------------------------------------------------------------------------------------
1060 // 1049 //
1061 GLRenderer* MainWindow::renderer() 1050 GLRenderer* MainWindow::currentRenderer()
1062 { 1051 {
1063 return m_renderer; 1052 return m_renderers[m_currentDocument];
1064 } 1053 }
1065 1054
1066 // --------------------------------------------------------------------------------------------------------------------- 1055 // ---------------------------------------------------------------------------------------------------------------------
1067 // 1056 //
1068 void MainWindow::setQuickColors(const QList<ColorToolbarItem>& colors) 1057 void MainWindow::setQuickColors(const QList<ColorToolbarItem>& colors)
1229 { 1218 {
1230 LDDocument* document = m_documents->createNew(); 1219 LDDocument* document = m_documents->createNew();
1231 connect(document->history(), SIGNAL(undone()), this, SLOT(historyTraversed())); 1220 connect(document->history(), SIGNAL(undone()), this, SLOT(historyTraversed()));
1232 connect(document->history(), SIGNAL(redone()), this, SLOT(historyTraversed())); 1221 connect(document->history(), SIGNAL(redone()), this, SLOT(historyTraversed()));
1233 connect(document->history(), SIGNAL(stepAdded()), this, SLOT(updateActions())); 1222 connect(document->history(), SIGNAL(stepAdded()), this, SLOT(updateActions()));
1223 connect(document, SIGNAL(closed()), this, SLOT(documentClosed()));
1224
1225 GLRenderer* renderer = new GLRenderer {document, this};
1226 m_renderers[document] = renderer;
1227 ui.rendererStack->addWidget(renderer);
1234 1228
1235 if (not cache) 1229 if (not cache)
1230 {
1231 changeDocument(document);
1236 document->openForEditing(); 1232 document->openForEditing();
1233 }
1237 1234
1238 return document; 1235 return document;
1239 } 1236 }
1240 1237
1241 // --------------------------------------------------------------------------------------------------------------------- 1238 // ---------------------------------------------------------------------------------------------------------------------
1249 // 1246 //
1250 // TODO: document may be null, this shouldn't be the case 1247 // TODO: document may be null, this shouldn't be the case
1251 // 1248 //
1252 void MainWindow::changeDocument(LDDocument* document) 1249 void MainWindow::changeDocument(LDDocument* document)
1253 { 1250 {
1254 // Implicit files were loaded for caching purposes and may never be switched to. 1251 // Implicit files were loaded for caching purposes and may
1252 // not be switched to.
1255 if (document and document->isCache()) 1253 if (document and document->isCache())
1256 return; 1254 return;
1257 1255
1258 m_currentDocument = document; 1256 m_currentDocument = document;
1259 1257
1261 { 1259 {
1262 // A ton of stuff needs to be updated 1260 // A ton of stuff needs to be updated
1263 updateDocumentListItem(document); 1261 updateDocumentListItem(document);
1264 buildObjectList(); 1262 buildObjectList();
1265 updateTitle(); 1263 updateTitle();
1266 m_renderer->setDocument(document); 1264 ui.rendererStack->setCurrentWidget(m_renderers[document]);
1267 m_renderer->compiler()->needMerge();
1268 print("Changed document to %1", document->getDisplayName()); 1265 print("Changed document to %1", document->getDisplayName());
1269 } 1266 }
1270 } 1267 }
1271 1268
1272 // --------------------------------------------------------------------------------------------------------------------- 1269 // ---------------------------------------------------------------------------------------------------------------------
1293 return m_currentDocument->getSelection(); 1290 return m_currentDocument->getSelection();
1294 } 1291 }
1295 1292
1296 // --------------------------------------------------------------------------------------------------------------------- 1293 // ---------------------------------------------------------------------------------------------------------------------
1297 // 1294 //
1298 void MainWindow::currentDocumentClosed() 1295 void MainWindow::documentClosed()
1299 { 1296 {
1300 LDDocument* old = currentDocument(); 1297 LDDocument* document = qobject_cast<LDDocument*>(sender());
1301 1298
1302 // Find a replacement document to use 1299 if (not document)
1303 for (LDDocument* doc : m_documents->allDocuments()) 1300 return;
1304 { 1301
1305 if (doc != old and not doc->isCache()) 1302 if (document == currentDocument())
1306 { 1303 {
1307 changeDocument(doc); 1304 LDDocument* previousCurrentDocument = currentDocument();
1308 break; 1305
1309 } 1306 // Find a replacement document to use
1310 } 1307 for (LDDocument* document : m_documents->allDocuments())
1311 1308 {
1312 if (currentDocument() == old) 1309 if (document != previousCurrentDocument and not document->isCache())
1313 { 1310 {
1314 // Failed to change to a suitable document, open a new one. 1311 changeDocument(document);
1315 createBlankDocument(); 1312 break;
1313 }
1314 }
1315
1316 if (currentDocument() == previousCurrentDocument)
1317 {
1318 // Failed to change to a suitable document, open a new one.
1319 createBlankDocument();
1320 }
1321 }
1322
1323 if (m_renderers.contains(document))
1324 {
1325 delete m_renderers[document];
1326 m_renderers.remove(document);
1316 } 1327 }
1317 } 1328 }
1318 1329
1319 ExtProgramToolset* MainWindow::externalPrograms() 1330 ExtProgramToolset* MainWindow::externalPrograms()
1320 { 1331 {

mercurial