diff --git a/src/document.cpp b/src/document.cpp index 4b84508..f596065 100755 --- a/src/document.cpp +++ b/src/document.cpp @@ -295,7 +295,7 @@ bool Document::closeDocument() { if (isModified()) { // Document has unsaved changes, so prompt the user QMessageBox::StandardButton reply; - reply = QMessageBox::warning(this, tr("Unsaved Changes"), + reply = QMessageBox::warning(this, tr("Unsaved Changes for: ") + m_fileName, tr("The document has unsaved changes. Do you want to save your changes?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 23360e5..ae773ce 100755 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -238,13 +238,29 @@ void MainWindow::closeAllDocuments() { int tabCount = ui->documentsTab->count(); qDebug() << "Attempting to close all tabs. Current tab count:" << tabCount; - for (int i = tabCount - 1; i >= 0; --i) { - on_documentsTab_tabCloseRequested(i); + QList docsToClose; + for (int i = 0; i < tabCount; ++i) { + Document* doc = qobject_cast(ui->documentsTab->widget(i)); + if (doc && doc->closeDocument()) { // Check if the document can be closed + docsToClose.append(doc); + } else { + qDebug() << "Skipped closing tab due to unsaved changes."; + } + } + + // Batch remove the collected tabs + for (Document* doc : docsToClose) { + int index = ui->documentsTab->indexOf(doc); + if (index != -1) { + ui->documentsTab->removeTab(index); + doc->deleteLater(); // Safely delete the document + } } - qDebug() << "Remaining tabs after close all:" << ui->documentsTab->count(); + qDebug() << "All tabs have been closed."; } + void MainWindow::on_actionC_3_triggered() { Document *doc = qobject_cast(ui->documentsTab->currentWidget()); if (doc) { @@ -524,3 +540,43 @@ void MainWindow::on_documentsTab_tabCloseRequested(int index) { closingInProgress = false; }); } + +void MainWindow::on_actionClose_all_BUT_current_document_triggered() { + int activeTabIndex = ui->documentsTab->currentIndex(); // Get the index of the active tab + int tabCount = ui->documentsTab->count(); + + if (activeTabIndex == -1 || tabCount <= 1) { + qDebug() << "No tabs to close or only one active tab."; + return; + } + + qDebug() << "Closing all tabs except the active tab at index:" << activeTabIndex; + + // Collect documents to close except the active one + QList docsToClose; + for (int i = 0; i < tabCount; ++i) { + if (i != activeTabIndex) { + Document* doc = qobject_cast(ui->documentsTab->widget(i)); + if (doc && doc->closeDocument()) { // Check if the document can be closed + docsToClose.append(doc); + } else { + qDebug() << "Skipped closing tab due to unsaved changes."; + } + } + } + + // Batch remove the collected tabs + for (Document* doc : docsToClose) { + int index = ui->documentsTab->indexOf(doc); + if (index != -1) { + ui->documentsTab->removeTab(index); + doc->deleteLater(); // Safely delete the document + } + } + + qDebug() << "All tabs except the active one have been closed."; +} + + + + diff --git a/src/mainwindow.h b/src/mainwindow.h index c23a686..974fae4 100755 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -65,6 +65,8 @@ private slots: void on_actionSa_ve_a_copy_as_triggered(); + void on_actionClose_all_BUT_current_document_triggered(); + private: Ui::MainWindow *ui; void initialize();