--- a/src/mainwindow.cpp Sat Mar 05 12:47:10 2022 +0200 +++ b/src/mainwindow.cpp Sat Mar 05 13:18:28 2022 +0200 @@ -76,6 +76,8 @@ this, &MainWindow::actionSave); connect(this->ui->actionSaveAs, &QAction::triggered, this, &MainWindow::actionSaveAs); + connect(this->ui->actionClose, &QAction::triggered, this, &MainWindow::actionClose); + connect(this->ui->tabs, &QTabWidget::tabCloseRequested, this, &MainWindow::handleTabCloseButton); for (auto data : ::renderStyleButtons) { QAction* action = data.memberInstance(this->ui.get()); @@ -214,28 +216,6 @@ return qobject_cast<Document*>(this->ui->tabs->currentWidget()); } -/* -void MainWindow::closeDocument(Document *document) -{ - const int tabIndex = this->ui->tabs->indexOf(document); - if (tabIndex != -1) - { - this->ui->tabs->removeTab(tabIndex); - const QString closedDocumentName = pathToName(document->modelPath()); - delete document; - } - QSet<QString> openedDocumentNames; - for (int i = 0; i < this->ui->tabs->count(); i += 1) - { - Document* document = qobject_cast<Document*>(this->ui->tabs->widget(i)); - if (document != nullptr) - { - openedDocumentNames.insert(pathToName(document->modelPath())); - } - } -} -*/ - void MainWindow::handleDocumentSplitterChange() { Document* currentDocument = this->currentDocument(); @@ -340,6 +320,47 @@ } } +/** + * @brief Handles the "Close" (Ctrl+W) action + */ +void MainWindow::actionClose() +{ + if (this->currentDocument() != nullptr) + { + this->closeDocument(this->currentDocument()); + } +} + +/** + * @brief Removes the document at the specified tab index + * @param index + */ +void MainWindow::handleTabCloseButton(int tabIndex) +{ + if (tabIndex >= 0 and tabIndex < this->ui->tabs->count()) + { + Document* document = qobject_cast<Document*>(this->ui->tabs->widget(tabIndex)); + if (document != nullptr) + { + this->closeDocument(document); + } + } +} + +/** + * @brief Closes the specified document + * @param document + */ +void MainWindow::closeDocument(Document *document) +{ + std::optional<ModelId> modelId = this->documents.findIdForModel(&document->getModel()); + if (modelId.has_value()) + { + this->documents.closeDocument(modelId.value()); + delete document; + } +} + void MainWindow::changeEvent(QEvent* event) { if (event != nullptr)