# HG changeset patch # User Teemu Piippo # Date 1646479108 -7200 # Node ID 536efae7137e3d1d81b57bb1ce24d819034db399 # Parent 1a04364d20b548047deeff993db71313e144050f Add document closing diff -r 1a04364d20b5 -r 536efae7137e icons/close-circle-outline.png Binary file icons/close-circle-outline.png has changed diff -r 1a04364d20b5 -r 536efae7137e icons_svg/close-circle-outline.svg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icons_svg/close-circle-outline.svg Sat Mar 05 13:18:28 2022 +0200 @@ -0,0 +1,70 @@ + + + + + + image/svg+xml + + + + + + + ionicons-v5-m + + + + diff -r 1a04364d20b5 -r 536efae7137e ldforge.qrc --- a/ldforge.qrc Sat Mar 05 12:47:10 2022 +0200 +++ b/ldforge.qrc Sat Mar 05 13:18:28 2022 +0200 @@ -25,5 +25,6 @@ icons/pencil-outline.png icons/warning-outline.png icons/linetype-conditionaledge.png + icons/close-circle-outline.png diff -r 1a04364d20b5 -r 536efae7137e src/mainwindow.cpp --- 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(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 openedDocumentNames; - for (int i = 0; i < this->ui->tabs->count(); i += 1) - { - Document* document = qobject_cast(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(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 = this->documents.findIdForModel(&document->getModel()); + if (modelId.has_value()) + { + this->documents.closeDocument(modelId.value()); + delete document; + } +} + void MainWindow::changeEvent(QEvent* event) { if (event != nullptr) diff -r 1a04364d20b5 -r 536efae7137e src/mainwindow.h --- a/src/mainwindow.h Sat Mar 05 12:47:10 2022 +0200 +++ b/src/mainwindow.h Sat Mar 05 13:18:28 2022 +0200 @@ -46,6 +46,8 @@ void setRenderStyle(gl::RenderStyle renderStyle); void actionSave(); void actionSaveAs(); + void actionClose(); + void handleTabCloseButton(int tabIndex); protected: void changeEvent(QEvent* event) override; void closeEvent(QCloseEvent* event) override; @@ -53,7 +55,6 @@ private: std::unique_ptr ui; DocumentManager documents; - QMap modelWidgets; QString currentLanguage = "en"; QTranslator translator; Configuration settings; @@ -75,4 +76,5 @@ static QString pathToTranslation(const QString& localeCode); void loadColors(); Document *currentDocument(); + void closeDocument(Document* document); }; diff -r 1a04364d20b5 -r 536efae7137e src/mainwindow.ui --- a/src/mainwindow.ui Sat Mar 05 12:47:10 2022 +0200 +++ b/src/mainwindow.ui Sat Mar 05 13:18:28 2022 +0200 @@ -16,7 +16,11 @@ - + + + true + + @@ -40,9 +44,10 @@ + - + @@ -210,6 +215,18 @@ Ctrl+Shift+S + + + + :/icons/close-circle-outline.png:/icons/close-circle-outline.png + + + Close + + + Ctrl+W + +