--- a/src/mainwindow.cpp Wed Mar 09 14:22:22 2022 +0200 +++ b/src/mainwindow.cpp Sun Mar 13 14:51:39 2022 +0200 @@ -224,6 +224,11 @@ return qobject_cast<Document*>(this->ui->tabs->currentWidget()); } +const Document* MainWindow::currentDocument() const +{ + return qobject_cast<const Document*>(this->ui->tabs->currentWidget()); +} + void MainWindow::handleDocumentSplitterChange() { Document* currentDocument = this->currentDocument(); @@ -285,24 +290,27 @@ { if (this->currentDocument() != nullptr) { - const ModelId modelId = {0}; - const QString* path = this->documents.modelPath(modelId); - if (path == nullptr or path->isEmpty()) + const std::optional<ModelId> modelId = this->findCurrentModelId(); + if (modelId.has_value()) { - this->actionSaveAs(); - } - else - { - QString error; - QTextStream errorStream{&error}; - const bool succeeded = this->documents.saveModel(modelId, errorStream); - if (not succeeded) + const QString* path = this->documents.modelPath(*modelId); + if (path == nullptr or path->isEmpty()) { - QMessageBox::critical(this, tr("Save error"), error); + this->actionSaveAs(); } else { - this->addRecentlyOpenedFile(*path); + QString error; + QTextStream errorStream{&error}; + const bool succeeded = this->documents.saveModel(*modelId, errorStream); + if (not succeeded) + { + QMessageBox::critical(this, tr("Save error"), error); + } + else + { + this->addRecentlyOpenedFile(*path); + } } } } @@ -315,22 +323,25 @@ { if (this->currentDocument() != nullptr) { - 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…"), - QFileInfo{defaultPath}.absoluteDir().path(), - tr("LDraw files (*.ldr *dat);;All files (*)") - ); - if (not newPath.isEmpty()) + const std::optional<ModelId> modelId = this->findCurrentModelId(); + if (modelId.has_value()) { - 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(); + const QString* pathPtr = this->documents.modelPath(*modelId); + QString defaultPath = (pathPtr != nullptr) ? *pathPtr : ""; + const QString newPath = QFileDialog::getSaveFileName( + this, + tr("Save as…"), + QFileInfo{defaultPath}.absoluteDir().path(), + tr("LDraw files (*.ldr *dat);;All files (*)") + ); + if (not newPath.isEmpty()) + { + 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(); + } } } } @@ -397,6 +408,19 @@ } } +std::optional<ModelId> MainWindow::findCurrentModelId() const +{ + const Document* document = this->currentDocument(); + if (document != nullptr) + { + return this->documents.findIdForModel(&document->getModel()); + } + else + { + return {}; + } +} + void MainWindow::changeEvent(QEvent* event) { if (event != nullptr)