src/mainwindow.cpp

changeset 141
185eb297dc1e
parent 128
7c834fe36b25
child 142
e085f36e4d9f
--- a/src/mainwindow.cpp	Mon Sep 27 21:04:45 2021 +0300
+++ b/src/mainwindow.cpp	Tue Sep 28 00:10:29 2021 +0300
@@ -72,6 +72,10 @@
 			this->currentDocument()->adjustGridToView();
 		}
 	});
+	connect(this->ui->actionSave, &QAction::triggered,
+		this, &MainWindow::actionSave);
+	connect(this->ui->actionSaveAs, &QAction::triggered,
+		this, &MainWindow::actionSaveAs);
 	for (auto data : ::renderStyleButtons)
 	{
 		QAction* action = data.memberInstance(this->ui.get());
@@ -252,6 +256,56 @@
 	this->updateRenderPreferences();
 }
 
+/**
+ * @brief Handles the "Save" (Ctrl+S) action
+ */
+void MainWindow::actionSave()
+{
+	if (this->currentDocument() != nullptr)
+	{
+		if (this->currentDocument()->modelPath().isEmpty())
+		{
+			this->actionSaveAs();
+		}
+		else
+		{
+			QString error;
+			QTextStream errorStream{&error};
+			const bool succeeded = this->currentDocument()->save(errorStream);
+			if (not succeeded)
+			{
+				QMessageBox::critical(this, tr("Save error"), error);
+			}
+			else
+			{
+				this->addRecentlyOpenedFile(this->currentDocument()->modelPath());
+			}
+		}
+	}
+}
+
+/**
+ * @brief Handles the "Save as…" (Ctrl+Shift+S) action
+ */
+void MainWindow::actionSaveAs()
+{
+	if (this->currentDocument() != nullptr)
+	{
+		const QString dir = QFileInfo{this->currentDocument()->modelPath()}.absoluteDir().path();
+		const QString newPath = QFileDialog::getSaveFileName(
+			this,
+			tr("Save as…"),
+			dir, 
+			tr("LDraw files (*.ldr *dat);;All files (*)")
+		);
+		if (not newPath.isEmpty())
+		{
+			this->currentDocument()->setModelPath(newPath);
+			this->actionSave();
+		}
+	}
+}
+
 void MainWindow::changeEvent(QEvent* event)
 {
 	if (event != nullptr)

mercurial