Skip to content

Commit

Permalink
close all BUT current document
Browse files Browse the repository at this point in the history
  • Loading branch information
yousefvand committed Oct 19, 2024
1 parent ed7cc90 commit 8cd6b73
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
62 changes: 59 additions & 3 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Document*> docsToClose;
for (int i = 0; i < tabCount; ++i) {
Document* doc = qobject_cast<Document*>(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<Document *>(ui->documentsTab->currentWidget());
if (doc) {
Expand Down Expand Up @@ -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<Document*> docsToClose;
for (int i = 0; i < tabCount; ++i) {
if (i != activeTabIndex) {
Document* doc = qobject_cast<Document*>(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.";
}




2 changes: 2 additions & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 8cd6b73

Please sign in to comment.