diff -r 29dc03eceb5f -r ce0c9f2e6b9c src/mainwindow.cpp --- a/src/mainwindow.cpp Thu Jan 11 11:41:40 2018 +0200 +++ b/src/mainwindow.cpp Thu Jan 11 15:09:44 2018 +0200 @@ -77,17 +77,11 @@ g_win = this; ui.setupUi(this); m_updatingTabs = false; - m_renderer = new GLRenderer(this); m_tabs = new QTabBar; m_tabs->setTabsClosable(true); ui.verticalLayout->insertWidget(0, m_tabs); createBlankDocument(); - m_renderer->setDocument(m_currentDocument); - - // Stuff the renderer into its frame - QVBoxLayout* rendererLayout = new QVBoxLayout(ui.rendererFrame); - rendererLayout->addWidget(renderer()); connect(ui.objectList, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChanged())); connect(ui.objectList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(objectListDoubleClicked(QListWidgetItem*))); @@ -117,7 +111,6 @@ updateTitle(); loadShortcuts(); setMinimumSize(300, 200); - connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(doLastSecondCleanup())); connect( ui.ringToolSegments, SIGNAL(valueChanged(int)), this, SLOT(circleToolSegmentsChanged()) @@ -186,6 +179,10 @@ MainWindow::~MainWindow() { + for (GLRenderer* renderer : m_renderers.values()) + delete renderer; + + delete &ui; g_win = nullptr; } @@ -222,14 +219,6 @@ // --------------------------------------------------------------------------------------------------------------------- // -void MainWindow::doLastSecondCleanup() -{ - delete m_renderer; - delete &ui; -} - -// --------------------------------------------------------------------------------------------------------------------- -// void MainWindow::updateRecentFilesMenu() { // First, clear any items in the recent files menu @@ -319,7 +308,7 @@ // Recompile all Bézier curves, the changing grid affects their precision. for (LDObjectIterator it(m_currentDocument); it.isValid(); ++it) - renderer()->compileObject(it); + currentRenderer()->compileObject(it); } // --------------------------------------------------------------------------------------------------------------------- @@ -457,7 +446,7 @@ case OBJ_Overlay: { LDOverlay* ovl = static_cast(obj); - descr = format("[%1] %2(%3, %4), %5 x %6", renderer()->cameraName((ECamera) ovl->camera()), + descr = format("[%1] %2(%3, %4), %5 x %6", currentRenderer()->cameraName((ECamera) ovl->camera()), Basename(ovl->fileName()), ovl->x(), ovl->y(), ovl->width(), ovl->height()); break; @@ -553,9 +542,9 @@ removeDuplicates(compound); for (LDObject* obj : compound) - renderer()->compileObject(obj); + currentRenderer()->compileObject(obj); - renderer()->update(); + currentRenderer()->update(); } // --------------------------------------------------------------------------------------------------------------------- @@ -591,7 +580,7 @@ continue; // uncolored object obj->setColor(color); - renderer()->compileObject(obj); + currentRenderer()->compileObject(obj); } endAction(); @@ -616,8 +605,8 @@ // void MainWindow::doFullRefresh() { - buildObjectList(); - m_renderer->hardRefresh(); + this->buildObjectList(); + this->currentRenderer()->hardRefresh(); } // --------------------------------------------------------------------------------------------------------------------- @@ -627,7 +616,7 @@ void MainWindow::refresh() { buildObjectList(); - m_renderer->update(); + currentRenderer()->update(); } // --------------------------------------------------------------------------------------------------------------------- @@ -782,7 +771,7 @@ contextMenu->addAction(ui.actionSubfileSelection); } - if (renderer()->camera() != EFreeCamera) + if (currentRenderer()->camera() != EFreeCamera) { contextMenu->addSeparator(); contextMenu->addAction(ui.actionSetDrawDepth); @@ -813,7 +802,7 @@ // void MainWindow::updateEditModeActions() { - const EditModeType mode = renderer()->currentEditModeType(); + const EditModeType mode = currentRenderer()->currentEditModeType(); ui.actionModeSelect->setChecked(mode == EditModeType::Select); ui.actionModeDraw->setChecked(mode == EditModeType::Draw); ui.actionModeRectangle->setChecked(mode == EditModeType::Rectangle); @@ -894,7 +883,7 @@ // Adds a message to the renderer's message manager. void MainWindow::addMessage(QString msg) { - m_renderer->messageLog()->addLine(msg); + this->currentRenderer()->messageLog()->addLine(msg); } // ============================================================================ @@ -1058,9 +1047,9 @@ // --------------------------------------------------------------------------------------------------------------------- // -GLRenderer* MainWindow::renderer() +GLRenderer* MainWindow::currentRenderer() { - return m_renderer; + return m_renderers[m_currentDocument]; } // --------------------------------------------------------------------------------------------------------------------- @@ -1231,9 +1220,17 @@ connect(document->history(), SIGNAL(undone()), this, SLOT(historyTraversed())); connect(document->history(), SIGNAL(redone()), this, SLOT(historyTraversed())); connect(document->history(), SIGNAL(stepAdded()), this, SLOT(updateActions())); + connect(document, SIGNAL(closed()), this, SLOT(documentClosed())); + + GLRenderer* renderer = new GLRenderer {document, this}; + m_renderers[document] = renderer; + ui.rendererStack->addWidget(renderer); if (not cache) + { + changeDocument(document); document->openForEditing(); + } return document; } @@ -1251,7 +1248,8 @@ // void MainWindow::changeDocument(LDDocument* document) { - // Implicit files were loaded for caching purposes and may never be switched to. + // Implicit files were loaded for caching purposes and may + // not be switched to. if (document and document->isCache()) return; @@ -1263,8 +1261,7 @@ updateDocumentListItem(document); buildObjectList(); updateTitle(); - m_renderer->setDocument(document); - m_renderer->compiler()->needMerge(); + ui.rendererStack->setCurrentWidget(m_renderers[document]); print("Changed document to %1", document->getDisplayName()); } } @@ -1295,24 +1292,38 @@ // --------------------------------------------------------------------------------------------------------------------- // -void MainWindow::currentDocumentClosed() +void MainWindow::documentClosed() { - LDDocument* old = currentDocument(); + LDDocument* document = qobject_cast(sender()); + + if (not document) + return; + + if (document == currentDocument()) + { + LDDocument* previousCurrentDocument = currentDocument(); - // Find a replacement document to use - for (LDDocument* doc : m_documents->allDocuments()) - { - if (doc != old and not doc->isCache()) + // Find a replacement document to use + for (LDDocument* document : m_documents->allDocuments()) { - changeDocument(doc); - break; + if (document != previousCurrentDocument and not document->isCache()) + { + changeDocument(document); + break; + } + } + + if (currentDocument() == previousCurrentDocument) + { + // Failed to change to a suitable document, open a new one. + createBlankDocument(); } } - if (currentDocument() == old) + if (m_renderers.contains(document)) { - // Failed to change to a suitable document, open a new one. - createBlankDocument(); + delete m_renderers[document]; + m_renderers.remove(document); } }