From b93aa77a1c67c2990c7dd76a8752b5ed6d48a847 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 20 Oct 2021 21:57:51 +0200 Subject: [PATCH] Gui: move handling of failed document saving to a function to reduce code duplication --- src/Gui/Document.cpp | 90 +++++++++++++++++++++++--------------------- src/Gui/Document.h | 2 + 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/Gui/Document.cpp b/src/Gui/Document.cpp index c4fdf1a943..589deecede 100644 --- a/src/Gui/Document.cpp +++ b/src/Gui/Document.cpp @@ -405,13 +405,16 @@ bool Document::setEdit(Gui::ViewProvider* p, int ModNum, const char *subname) d->_editViewProviderParent = vp; d->_editSubElement.clear(); d->_editSubname.clear(); - if(subname) { + + if (subname) { const char *element = Data::ComplexGeoData::findElementName(subname); - if(element) { + if (element) { d->_editSubname = std::string(subname,element-subname); d->_editSubElement = element; - }else + } + else { d->_editSubname = subname; + } } auto sobjs = obj->getSubObjectList(subname); @@ -1096,6 +1099,31 @@ static bool checkCanonicalPath(const std::map &docs) return ret == QMessageBox::Yes; } +bool Document::askIfSavingFailed(const QString& error) +{ + int ret = QMessageBox::question( + getMainWindow(), + QObject::tr("Could not save document"), + QObject::tr("There was an issue trying to save the file. " + "This may be because some of the parent folders do not exist, " + "or you do not have sufficient permissions, " + "or for other reasons. Error details:\n\n\"%1\"\n\n" + "Would you like to save the file with a different name?") + .arg(error), + QMessageBox::Yes, QMessageBox::No); + + if (ret == QMessageBox::No) { + // TODO: Understand what exactly is supposed to be returned here + getMainWindow()->showMessage(QObject::tr("Saving aborted"), 2000); + return false; + } + else if (ret == QMessageBox::Yes) { + return saveAs(); + } + + return false; +} + /// Save the document bool Document::save(void) { @@ -1105,7 +1133,7 @@ bool Document::save(void) std::map dmap; try { docs = getDocument()->getDependentDocuments(); - for(auto it=docs.begin(); it!=docs.end();) { + for (auto it=docs.begin(); it!=docs.end();) { App::Document *doc = *it; if (doc == getDocument()) { dmap[doc] = doc->mustExecute(); @@ -1123,18 +1151,21 @@ bool Document::save(void) dmap[doc] = doc->mustExecute(); ++it; } - }catch(const Base::RuntimeError &e) { + } + catch (const Base::RuntimeError &e) { FC_ERR(e.what()); docs = {getDocument()}; dmap.clear(); dmap[getDocument()] = getDocument()->mustExecute(); } - if(docs.size()>1) { + + if (docs.size()>1) { int ret = QMessageBox::question(getMainWindow(), QObject::tr("Save dependent files"), QObject::tr("The file contains external dependencies. " "Do you want to save the dependent files, too?"), QMessageBox::Yes,QMessageBox::No); + if (ret != QMessageBox::Yes) { docs = {getDocument()}; dmap.clear(); @@ -1147,35 +1178,22 @@ bool Document::save(void) Gui::WaitCursor wc; // save all documents - for(auto doc : docs) { + for (auto doc : docs) { // Changed 'mustExecute' status may be triggered by saving external document - if(!dmap[doc] && doc->mustExecute()) { + if (!dmap[doc] && doc->mustExecute()) { App::AutoTransaction trans("Recompute"); Command::doCommand(Command::Doc,"App.getDocument(\"%s\").recompute()",doc->getName()); } + Command::doCommand(Command::Doc,"App.getDocument(\"%s\").save()",doc->getName()); auto gdoc = Application::Instance->getDocument(doc); - if(gdoc) gdoc->setModified(false); + if (gdoc) + gdoc->setModified(false); } } catch (const Base::FileException& e) { - int ret = QMessageBox::question( - getMainWindow(), - QObject::tr("Could not save document"), - QObject::tr("There was an issue trying to save the file. " - "This may be because some of the parent folders do not exist, " - "or you do not have sufficient permissions, " - "or for other reasons. Error details:\n\n\"%1\"\n\n" - "Would you like to save the file with a different name?") - .arg(QString::fromUtf8(e.what())), - QMessageBox::Yes, QMessageBox::No); - if (ret == QMessageBox::No) { - // TODO: Understand what exactly is supposed to be returned here - getMainWindow()->showMessage(QObject::tr("Saving aborted"), 2000); - return false; - } else if (ret == QMessageBox::Yes) { - return saveAs(); - } + e.ReportException(); + return askIfSavingFailed(QString::fromUtf8(e.what())); } catch (const Base::Exception& e) { QMessageBox::critical(getMainWindow(), QObject::tr("Saving document failed"), @@ -1198,6 +1216,7 @@ bool Document::saveAs(void) QString fn = FileDialog::getSaveFileName(getMainWindow(), QObject::tr("Save %1 Document").arg(exe), QString::fromUtf8(getDocument()->FileName.getValue()), QString::fromLatin1("%1 %2 (*.FCStd)").arg(exe).arg(QObject::tr("Document"))); + if (!fn.isEmpty()) { QFileInfo fi; fi.setFile(fn); @@ -1217,23 +1236,8 @@ bool Document::saveAs(void) getMainWindow()->appendRecentFile(fi.filePath()); } catch (const Base::FileException& e) { - int ret = QMessageBox::question( - getMainWindow(), - QObject::tr("Could not save document"), - QObject::tr("There was an issue trying to save the file. " - "This may be because some of the parent folders do not exist, " - "or you do not have sufficient permissions, " - "or for other reasons. Error details:\n\n\"%1\"\n\n" - "Would you like to save the file with a different name?") - .arg(QString::fromUtf8(e.what())), - QMessageBox::Yes, QMessageBox::No); - if (ret == QMessageBox::No) { - // TODO: Understand what exactly is supposed to be returned here - getMainWindow()->showMessage(QObject::tr("Saving aborted"), 2000); - return false; - } else if (ret == QMessageBox::Yes) { - return saveAs(); - } + e.ReportException(); + return askIfSavingFailed(QString::fromUtf8(e.what())); } catch (const Base::Exception& e) { QMessageBox::critical(getMainWindow(), QObject::tr("Saving document failed"), diff --git a/src/Gui/Document.h b/src/Gui/Document.h index b24887aaf8..a7884670ed 100644 --- a/src/Gui/Document.h +++ b/src/Gui/Document.h @@ -306,6 +306,8 @@ private: /// Check other documents for the same transaction ID bool checkTransactionID(bool undo, int iSteps); + /// Ask for user interaction if saving has failed + bool askIfSavingFailed(const QString&); struct DocumentP* d; static int _iDocCount;