src/mainwindow.cpp

changeset 148
e1ced2523cad
parent 147
37f936073cac
child 160
536efae7137e
--- a/src/mainwindow.cpp	Sun Oct 24 11:33:32 2021 +0300
+++ b/src/mainwindow.cpp	Tue Nov 02 15:43:57 2021 +0200
@@ -120,10 +120,14 @@
 {
 	QString errorString;
 	QTextStream errorStream{&errorString};
-	QString modelName = this->documents.openModel(path, errorStream);
-	if (not modelName.isEmpty())
+	std::optional<ModelId> modelIdOpt = this->documents.openModel(
+		path,
+		errorStream,
+		DocumentManager::OpenType::ManuallyOpened);
+	if (modelIdOpt.has_value())
 	{
-		this->documents.loadDependenciesForModel(modelName, path, this->libraries, errorStream);
+		const ModelId modelId = modelIdOpt.value();
+		this->documents.loadDependenciesForModel(modelId, path, this->libraries, errorStream);
 		if (not errorString.isEmpty())
 		{
 			QMessageBox::warning(
@@ -131,7 +135,7 @@
 				tr("Problem loading references"),
 				errorString);
 		}
-		this->openModelForEditing(modelName);
+		this->openModelForEditing(modelId);
 		this->addRecentlyOpenedFile(path);
 	}
 	else
@@ -181,15 +185,16 @@
 	this->updateRecentlyOpenedDocumentsMenu();
 }
 
-void MainWindow::openModelForEditing(const QString& modelName)
+void MainWindow::openModelForEditing(const ModelId modelId)
 {
-	Document* document = new Document{this->documents.findModelByName(modelName), &this->documents, this->colorTable};
+	Document* document = new Document{this->documents.getModelById(modelId), &this->documents, this->colorTable};
 	document->setRenderPreferences(this->renderPreferences);
 	connect(document, &Document::newStatusText, [&](const QString& newStatusText)
 	{
 		this->statusBar()->showMessage(newStatusText);
 	});
-	this->ui->tabs->addTab(document, modelName);
+	const QFileInfo fileInfo{*this->documents.modelPath(modelId)};
+	this->ui->tabs->addTab(document, fileInfo.baseName());
 	this->ui->tabs->setCurrentWidget(document);
 	document->restoreSplitterState(this->documentSplitterState);
 }
@@ -209,6 +214,7 @@
 	return qobject_cast<Document*>(this->ui->tabs->currentWidget());
 }
 
+/*
 void MainWindow::closeDocument(Document *document)
 {
 	const int tabIndex = this->ui->tabs->indexOf(document);
@@ -228,6 +234,7 @@
 		}
 	}
 }
+*/
 
 void MainWindow::handleDocumentSplitterChange()
 {
@@ -283,7 +290,9 @@
 {
 	if (this->currentDocument() != nullptr)
 	{
-		if (this->currentDocument()->modelPath().isEmpty())
+		const ModelId modelId = {0};
+		const QString* path = this->documents.modelPath(modelId);
+		if (path == nullptr or path->isEmpty())
 		{
 			this->actionSaveAs();
 		}
@@ -291,14 +300,14 @@
 		{
 			QString error;
 			QTextStream errorStream{&error};
-			const bool succeeded = this->currentDocument()->save(errorStream);
+			const bool succeeded = this->documents.saveModel(modelId, errorStream);
 			if (not succeeded)
 			{
 				QMessageBox::critical(this, tr("Save error"), error);
 			}
 			else
 			{
-				this->addRecentlyOpenedFile(this->currentDocument()->modelPath());
+				this->addRecentlyOpenedFile(*path);
 			}
 		}
 	}
@@ -311,16 +320,20 @@
 {
 	if (this->currentDocument() != nullptr)
 	{
-		const QString dir = QFileInfo{this->currentDocument()->modelPath()}.absoluteDir().path();
+		const ModelId modelId = {0};
+		const QString* pathPtr = this->documents.modelPath(modelId);
+		QString defaultPath = (pathPtr != nullptr) ? *pathPtr : "";
 		const QString newPath = QFileDialog::getSaveFileName(
 			this,
 			tr("Save as…"),
-			dir, 
+			QFileInfo{defaultPath}.absoluteDir().path(), 
 			tr("LDraw files (*.ldr *dat);;All files (*)")
 		);
 		if (not newPath.isEmpty())
 		{
-			this->currentDocument()->setModelPath(newPath);
+			QString error;
+			QTextStream errorStream{&error};
+			this->documents.setModelPath(modelId, newPath, this->libraries, errorStream);
 			this->ui->tabs->setTabText(this->ui->tabs->currentIndex(), QFileInfo{newPath}.fileName());
 			this->actionSave();
 		}

mercurial