From 9253572afeb1cfd98e711d638e55fed23f8c3aa4 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 4 Sep 2015 00:48:26 +0200 Subject: [PATCH] + implement GUI to enable/disable auto-save and its timeout --- src/Gui/Application.cpp | 6 ++- src/Gui/AutoSaver.cpp | 57 ++++++++++++++++------- src/Gui/AutoSaver.h | 6 +-- src/Gui/DlgSettingsDocument.ui | 73 +++++++++++++++++++++++++++--- src/Gui/DlgSettingsDocumentImp.cpp | 13 ++++++ 5 files changed, 128 insertions(+), 27 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index 36552fdea..da4e7772c 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -1696,7 +1696,11 @@ void Application::runApplication(void) MainWindow mw; mw.setWindowTitle(mainApp.applicationName()); - AutoSaver::instance()->setTimeout(3); + ParameterGrp::handle hDocGrp = WindowParameter::getDefaultParameter()->GetGroup("Document"); + int timeout = hDocGrp->GetInt("AutoSaveTimeout", 15); // 15 min + if (!hDocGrp->GetBool("AutoSaveEnabled", true)) + timeout = 0; + AutoSaver::instance()->setTimeout(timeout * 60000); // set toolbar icon size ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General"); diff --git a/src/Gui/AutoSaver.cpp b/src/Gui/AutoSaver.cpp index 9a4512699..266dc3fa7 100644 --- a/src/Gui/AutoSaver.cpp +++ b/src/Gui/AutoSaver.cpp @@ -37,13 +37,14 @@ #include #include "WaitCursor.h" +#include "Widgets.h" using namespace Gui; AutoSaver* AutoSaver::self = 0; AutoSaver::AutoSaver(QObject* parent) - : QObject(parent), timeout(5) + : QObject(parent), timeout(900000) { App::GetApplication().signalNewDocument.connect(boost::bind(&AutoSaver::slotCreateDocument, this, _1)); App::GetApplication().signalDeleteDocument.connect(boost::bind(&AutoSaver::slotDeleteDocument, this, _1)); @@ -60,18 +61,24 @@ AutoSaver* AutoSaver::instance() return self; } -void AutoSaver::setTimeout(int s) +void AutoSaver::setTimeout(int ms) { - timeout = Base::clamp(s, 0, 30); + timeout = Base::clamp(ms, 0, 3600000); // between 0 and 60 min + + // go through the attached documents and apply the new timeout + for (std::map::iterator it = timerMap.begin(); it != timerMap.end(); ++it) { + if (it->second > 0) + killTimer(it->second); + int id = timeout > 0 ? startTimer(timeout) : 0; + it->second = id; + } } void AutoSaver::slotCreateDocument(const App::Document& Doc) { std::string name = Doc.getName(); - if (timeout > 0) { - int id = startTimer(timeout * 60000); - timerMap[name] = id; - } + int id = timeout > 0 ? startTimer(timeout) : 0; + timerMap[name] = id; } void AutoSaver::slotDeleteDocument(const App::Document& Doc) @@ -79,7 +86,8 @@ void AutoSaver::slotDeleteDocument(const App::Document& Doc) std::string name = Doc.getName(); std::map::iterator it = timerMap.find(name); if (it != timerMap.end()) { - killTimer(it->second); + if (it->second > 0) + killTimer(it->second); timerMap.erase(it); } } @@ -100,23 +108,38 @@ void AutoSaver::saveDocument(const std::string& name) bool save = hGrp->GetBool("SaveThumbnail",false); hGrp->SetBool("SaveThumbnail",false); + Gui::StatusWidget* sw = new Gui::StatusWidget(qApp->activeWindow()); + sw->setStatusText(tr("Please wait until the AutoRecovery file has been saved...")); + sw->show(); + qApp->processEvents(); + // open extra scope to close ZipWriter properly + Base::StopWatch watch; + watch.start(); { Base::ofstream file(tmp, std::ios::out | std::ios::binary); - Base::ZipWriter writer(file); + if (file.is_open()) { + Base::ZipWriter writer(file); - writer.setComment("FreeCAD Document"); - writer.setLevel(0); - writer.putNextEntry("Document.xml"); + writer.setComment(doc->Label.getValue()); // store the document's current label + writer.setLevel(1); // apparently the fastest compression + writer.putNextEntry("Document.xml"); - doc->Save(writer); + doc->Save(writer); - // Special handling for Gui document. - doc->signalSaveDocument(writer); + // Special handling for Gui document. + doc->signalSaveDocument(writer); - // write additional files - writer.writeFiles(); + // write additional files + writer.writeFiles(); + } } + + sw->hide(); + sw->deleteLater(); + + std::string str = watch.toString(watch.elapsed()); + Base::Console().Log("Save AutoRecovery file: %s\n", str.c_str()); hGrp->SetBool("SaveThumbnail",save); } } diff --git a/src/Gui/AutoSaver.h b/src/Gui/AutoSaver.h index adf9e944e..6aa41c3cb 100644 --- a/src/Gui/AutoSaver.h +++ b/src/Gui/AutoSaver.h @@ -50,9 +50,9 @@ private: public: static AutoSaver* instance(); /*! - Sets the timeout in minutes. A value of 0 means that no timer is used. + Sets the timeout in milliseconds. A value of 0 means that no timer is used. */ - void setTimeout(int s); + void setTimeout(int ms); protected: void slotCreateDocument(const App::Document& Doc); @@ -61,7 +61,7 @@ protected: void saveDocument(const std::string&); private: - int timeout; /*!< Timeout in minutes */ + int timeout; /*!< Timeout in milliseconds */ std::map timerMap; }; diff --git a/src/Gui/DlgSettingsDocument.ui b/src/Gui/DlgSettingsDocument.ui index 322fb4eb0..4d3b7fa28 100644 --- a/src/Gui/DlgSettingsDocument.ui +++ b/src/Gui/DlgSettingsDocument.ui @@ -200,6 +200,51 @@ + + + + + Save AutoRecovery information every + + + true + + + AutoSaveEnabled + + + Document + + + + + + + 15 min + + + min + + + 1 + + + 60 + + + 15 + + + AutoSaveTimeout + + + Document + + + + + + QFrame::HLine @@ -212,7 +257,7 @@ - + Save thumbnail into project file when saving document @@ -225,7 +270,7 @@ - + 6 @@ -515,12 +560,28 @@ setEnabled(bool) - 86 - 295 + 106 + 325 - 395 - 286 + 479 + 326 + + + + + prefAutoSaveEnabled + toggled(bool) + prefAutoSaveTimeout + setEnabled(bool) + + + 196 + 253 + + + 275 + 254 diff --git a/src/Gui/DlgSettingsDocumentImp.cpp b/src/Gui/DlgSettingsDocumentImp.cpp index 991bcab00..e97839516 100644 --- a/src/Gui/DlgSettingsDocumentImp.cpp +++ b/src/Gui/DlgSettingsDocumentImp.cpp @@ -27,6 +27,7 @@ #include "DlgSettingsDocumentImp.h" #include "PrefWidgets.h" +#include "AutoSaver.h" using namespace Gui::Dialog; @@ -40,6 +41,9 @@ DlgSettingsDocumentImp::DlgSettingsDocumentImp( QWidget* parent ) : PreferencePage( parent ) { this->setupUi(this); + prefSaveTransaction->hide(); + prefDiscardTransaction->hide(); + prefCountBackupFiles->setMaximum(INT_MAX); prefCompression->setMinimum(Z_NO_COMPRESSION); prefCompression->setMaximum(Z_BEST_COMPRESSION); @@ -73,6 +77,13 @@ void DlgSettingsDocumentImp::saveSettings() prefAuthor->onSave(); prefSetAuthorOnSave->onSave(); prefCompany->onSave(); + prefAutoSaveEnabled->onSave(); + prefAutoSaveTimeout->onSave(); + + int timeout = prefAutoSaveTimeout->value(); + if (!prefAutoSaveEnabled->isChecked()) + timeout = 0; + AutoSaver::instance()->setTimeout(timeout * 60000); } void DlgSettingsDocumentImp::loadSettings() @@ -93,6 +104,8 @@ void DlgSettingsDocumentImp::loadSettings() prefAuthor->onRestore(); prefSetAuthorOnSave->onRestore(); prefCompany->onRestore(); + prefAutoSaveEnabled->onRestore(); + prefAutoSaveTimeout->onRestore(); } /**