+ implement GUI to enable/disable auto-save and its timeout

This commit is contained in:
wmayer 2015-09-04 00:48:26 +02:00
parent a811a3bf03
commit 9253572afe
5 changed files with 128 additions and 27 deletions

View File

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

View File

@ -37,13 +37,14 @@
#include <App/Document.h>
#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<int>(s, 0, 30);
timeout = Base::clamp<int>(ms, 0, 3600000); // between 0 and 60 min
// go through the attached documents and apply the new timeout
for (std::map<std::string, int>::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<std::string, int>::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);
}
}

View File

@ -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<std::string, int> timerMap;
};

View File

@ -200,6 +200,51 @@
</widget>
</item>
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="Gui::PrefCheckBox" name="prefAutoSaveEnabled">
<property name="text">
<string>Save AutoRecovery information every</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="prefEntry" stdset="0">
<cstring>AutoSaveEnabled</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>Document</cstring>
</property>
</widget>
</item>
<item>
<widget class="Gui::PrefSpinBox" name="prefAutoSaveTimeout">
<property name="text" stdset="0">
<string>15 min</string>
</property>
<property name="suffix">
<string notr="true"> min</string>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>60</number>
</property>
<property name="value">
<number>15</number>
</property>
<property name="prefEntry" stdset="0">
<cstring>AutoSaveTimeout</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>Document</cstring>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="0">
<widget class="Line" name="line1_2_3">
<property name="frameShape">
<enum>QFrame::HLine</enum>
@ -212,7 +257,7 @@
</property>
</widget>
</item>
<item row="3" column="0">
<item row="4" column="0">
<widget class="Gui::PrefCheckBox" name="prefSaveThumbnail">
<property name="text">
<string>Save thumbnail into project file when saving document</string>
@ -225,7 +270,7 @@
</property>
</widget>
</item>
<item row="4" column="0">
<item row="5" column="0">
<layout class="QHBoxLayout">
<property name="spacing">
<number>6</number>
@ -515,12 +560,28 @@
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>86</x>
<y>295</y>
<x>106</x>
<y>325</y>
</hint>
<hint type="destinationlabel">
<x>395</x>
<y>286</y>
<x>479</x>
<y>326</y>
</hint>
</hints>
</connection>
<connection>
<sender>prefAutoSaveEnabled</sender>
<signal>toggled(bool)</signal>
<receiver>prefAutoSaveTimeout</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>196</x>
<y>253</y>
</hint>
<hint type="destinationlabel">
<x>275</x>
<y>254</y>
</hint>
</hints>
</connection>

View File

@ -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();
}
/**