From 54fdd62fc7e95c1783024a91a4ef74f3e5ac1138 Mon Sep 17 00:00:00 2001 From: xtemp09 Date: Fri, 9 May 2025 04:59:11 +0700 Subject: [PATCH] Gui: Add switching to the document when closing FreeCAD (#21135) * Add switching to the document when closing FreeCAD Closes #20997 * Some update of language * camelCase every variable name used --- src/Gui/Document.cpp | 12 +++++++----- src/Gui/MainWindow.cpp | 38 +++++++++++++++++++++++++++++++------- src/Gui/MainWindow.h | 2 +- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/Gui/Document.cpp b/src/Gui/Document.cpp index 41b1311c98..d85f3ddb5c 100644 --- a/src/Gui/Document.cpp +++ b/src/Gui/Document.cpp @@ -2256,8 +2256,7 @@ bool Document::canClose (bool checkModify, bool checkLink) bool ok = true; if (checkModify && isModified() && !getDocument()->testStatus(App::Document::PartialDoc)) { - const char *docName = getDocument()->Label.getValue(); - int res = getMainWindow()->confirmSave(docName, getActiveView()); + int res = getMainWindow()->confirmSave(getDocument(), getActiveView()); switch (res) { case MainWindow::ConfirmSaveResult::Cancel: @@ -2267,11 +2266,14 @@ bool Document::canClose (bool checkModify, bool checkLink) case MainWindow::ConfirmSaveResult::Save: ok = save(); if (!ok) { + const QString docName = QString::fromStdString(getDocument()->Label.getStrValue()); + const QString text = (!docName.isEmpty() + ? QObject::tr("Failed to save document '%1'. Would you like to cancel the closure?").arg(docName) + : QObject::tr("Document saving failed. Would you like to cancel the closure?")); int ret = QMessageBox::question( getActiveView(), - QObject::tr("Document not saved"), - QObject::tr("The document%1 could not be saved. Do you want to cancel closing it?") - .arg(docName?(QStringLiteral(" ")+QString::fromUtf8(docName)):QString()), + QObject::tr("Unable to save document"), + text, QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Discard); if (ret == QMessageBox::Discard) diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp index ed793818b0..c2a664d48f 100644 --- a/src/Gui/MainWindow.cpp +++ b/src/Gui/MainWindow.cpp @@ -786,17 +786,18 @@ void MainWindow::closeActiveWindow () d->mdiArea->closeActiveSubWindow(); } -int MainWindow::confirmSave(const char *docName, QWidget *parent, bool addCheckbox) { +int MainWindow::confirmSave(App::Document *doc, QWidget *parent, bool addCheckbox) { QMessageBox box(parent?parent:this); box.setObjectName(QStringLiteral("confirmSave")); box.setIcon(QMessageBox::Question); box.setWindowFlags(box.windowFlags() | Qt::WindowStaysOnTopHint); box.setWindowTitle(QObject::tr("Unsaved document")); - if(docName) - box.setText(QObject::tr("Do you want to save your changes to document '%1' before closing?") - .arg(QString::fromUtf8(docName))); - else - box.setText(QObject::tr("Do you want to save your changes to document before closing?")); + const QString docName = QString::fromStdString(doc->Label.getStrValue()); + const QString text = (!docName.isEmpty() + ? QObject::tr("Do you want to save your changes to document '%1' before closing?").arg(docName) + : QObject::tr("Do you want to save your changes to document before closing?")); + box.setText(text); + box.setInformativeText(QObject::tr("If you don't save, your changes will be lost.")); box.setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel | QMessageBox::Save); @@ -830,6 +831,19 @@ int MainWindow::confirmSave(const char *docName, QWidget *parent, bool addCheckb int res = ConfirmSaveResult::Cancel; box.adjustSize(); // Silence warnings from Qt on Windows + + // activates the last used MDI view of the closing document + MDIView *activeView = this->activeWindow(); + App::Document *activeDoc = (activeView ? activeView->getAppDocument() : nullptr); + if (activeDoc != doc){ + const QList listOfMDIs = this->windows(); + for (QWidget *widget : listOfMDIs){ + auto mdiView = qobject_cast (widget); + if (mdiView != nullptr && mdiView->getAppDocument() == doc) + this->setActiveWindow(mdiView); + } + } + switch (box.exec()) { case QMessageBox::Save: @@ -858,6 +872,16 @@ bool MainWindow::closeAllDocuments (bool close) bool saveAll = false; int failedSaves = 0; + // moves the active document to the front + MDIView *activeView = this->activeWindow(); + App::Document *activeDoc = (activeView ? activeView->getAppDocument() : nullptr); + if (activeDoc != nullptr) + for (auto it = ++docs.begin(); it != docs.end(); it++) + if (*it == activeDoc){ + docs.erase(it); + docs.insert(docs.begin(), activeDoc); + } + for (auto doc : docs) { auto gdoc = Application::Instance->getDocument(doc); if (!gdoc) @@ -870,7 +894,7 @@ bool MainWindow::closeAllDocuments (bool close) continue; bool save = saveAll; if (!save && checkModify) { - int res = confirmSave(doc->Label.getStrValue().c_str(), this, docs.size()>1); + int res = confirmSave(doc, this, docs.size() > 1); switch (res) { case ConfirmSaveResult::Cancel: diff --git a/src/Gui/MainWindow.h b/src/Gui/MainWindow.h index fafaea0560..ac49d55a2d 100644 --- a/src/Gui/MainWindow.h +++ b/src/Gui/MainWindow.h @@ -238,7 +238,7 @@ public Q_SLOTS: bool closeAllDocuments (bool close=true); /** Pop up a message box asking for saving document */ - int confirmSave(const char *docName, QWidget *parent=nullptr, bool addCheckBox=false); + int confirmSave(App::Document *doc, QWidget *parent = nullptr, bool addCheckBox = false); /** * Activates the next window in the child window chain. */