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
This commit is contained in:
xtemp09
2025-05-09 04:59:11 +07:00
committed by GitHub
parent fcd454c1da
commit 30498b7b48
3 changed files with 39 additions and 13 deletions

View File

@@ -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)

View File

@@ -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 <QWidget *> listOfMDIs = this->windows();
for (QWidget *widget : listOfMDIs){
auto mdiView = qobject_cast <MDIView *> (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:

View File

@@ -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.
*/