From ab828a1824d5e81ca5cef6cdfbfa1ce77a890abc Mon Sep 17 00:00:00 2001 From: Ilia Ryabokon Date: Fri, 29 Nov 2013 10:47:06 +0300 Subject: [PATCH] Document autosave facility --- src/board/UBBoardController.cpp | 54 +++++++++++++++++++++++++++++++++ src/board/UBBoardController.h | 17 +++++++++++ src/core/UBApplication.cpp | 3 +- src/core/UBDownloadThread.cpp | 3 -- src/core/UBSettings.cpp | 1 + src/core/UBSettings.h | 2 ++ 6 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 6c3facf3..19716757 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -108,6 +108,7 @@ UBBoardController::UBBoardController(UBMainWindow* mainWindow) , mMovingSceneIndex(-1) , mActionGroupText(tr("Group")) , mActionUngroupText(tr("Ungroup")) + , mAutosaveTimer(0) { mZoomFactor = UBSettings::settings()->boardZoomFactor->get().toDouble(); @@ -395,6 +396,21 @@ void UBBoardController::stopScript() freezeW3CWidgets(true); } +void UBBoardController::saveData(SaveFlags fls) +{ + bool verbose = fls | sf_showProgress; + + if (verbose) { + UBApplication::showMessage("Saving document..."); + } + if (mActiveScene && mActiveScene->isModified()) { + persistCurrentScene(); + } + if (verbose) { + UBApplication::showMessage("Document has just been saved..."); + } +} + void UBBoardController::initToolbarTexts() { QList allToolbarActions; @@ -1647,6 +1663,14 @@ void UBBoardController::adjustDisplayViews() } +int UBBoardController::autosaveTimeoutFromSettings() +{ + int value = UBSettings::settings()->timerInterval->get().toInt(); + int minute = 60 * 1000; + + return value * minute; +} + void UBBoardController::changeBackground(bool isDark, bool isCrossed) { bool currentIsDark = mActiveScene->isDarkBackground(); @@ -1768,6 +1792,36 @@ void UBBoardController::documentSceneChanged(UBDocumentProxy* pDocumentProxy, in } } +void UBBoardController::autosaveTimeout() +{ + if (UBApplication::applicationController->displayMode() != UBApplicationController::Board) { + //perform autosave only in board mode + return; + } + + saveData(sf_showProgress); +} + +void UBBoardController::appMainModeChanged(UBApplicationController::MainMode md) +{ +// int timerInterval = autosaveTimeoutFromSettings(); + int timerInterval = 50000; + if (!timerInterval) { + return; + } + + if (!mAutosaveTimer) { + mAutosaveTimer = new QTimer(this); + connect(mAutosaveTimer, SIGNAL(timeout()), this, SLOT(autosaveTimeout())); + } + + if (md == UBApplicationController::Board) { + mAutosaveTimer->start(timerInterval); + } else if (mAutosaveTimer->isActive()) { + mAutosaveTimer->stop(); + } +} + void UBBoardController::closing() { mIsClosing = true; diff --git a/src/board/UBBoardController.h b/src/board/UBBoardController.h index b1236784..38a04b67 100644 --- a/src/board/UBBoardController.h +++ b/src/board/UBBoardController.h @@ -32,6 +32,7 @@ #include #include "document/UBDocumentContainer.h" +#include "core/UBApplicationController.h" class UBMainWindow; class UBApplication; @@ -59,6 +60,13 @@ class UBBoardController : public UBDocumentContainer { Q_OBJECT + public: + enum SaveFlag { + sf_none = 0x0, + sf_showProgress = 0x1 + }; + Q_DECLARE_FLAGS(SaveFlags, SaveFlag) + public: UBBoardController(UBMainWindow *mainWindow); virtual ~UBBoardController(); @@ -240,6 +248,8 @@ class UBBoardController : public UBDocumentContainer void startScript(); void stopScript(); + void saveData(SaveFlags fls = sf_none); + signals: void newPageAdded(); void activeSceneChanged(); @@ -267,10 +277,15 @@ class UBBoardController : public UBDocumentContainer void undoRedoStateChange(bool canUndo); void documentSceneChanged(UBDocumentProxy* proxy, int pIndex); + private slots: + void autosaveTimeout(); + void appMainModeChanged(UBApplicationController::MainMode); + private: void updatePageSizeState(); void saveViewState(); void adjustDisplayViews(); + int autosaveTimeoutFromSettings(); UBMainWindow *mMainWindow; UBGraphicsScene* mActiveScene; @@ -298,6 +313,8 @@ class UBBoardController : public UBDocumentContainer QString mActionGroupText; QString mActionUngroupText; + QTimer *mAutosaveTimer; + private slots: void stylusToolDoubleClicked(int tool); void boardViewResized(QResizeEvent* event); diff --git a/src/core/UBApplication.cpp b/src/core/UBApplication.cpp index b6b8b06b..25368c7b 100644 --- a/src/core/UBApplication.cpp +++ b/src/core/UBApplication.cpp @@ -339,7 +339,8 @@ int UBApplication::exec(const QString& pFileToImport) connect(applicationController, SIGNAL(desktopMode(bool)), boardController->paletteManager(), SLOT(slot_changeDesktopMode(bool))); - + connect(applicationController, SIGNAL(mainModeChanged(UBApplicationController::MainMode)) + , boardController, SLOT(appMainModeChanged(UBApplicationController::MainMode))); connect(mainWindow->actionDesktop, SIGNAL(triggered(bool)), applicationController, SLOT(showDesktop(bool))); connect(mainWindow->actionDesktop, SIGNAL(triggered(bool)), this, SLOT(stopScript())); diff --git a/src/core/UBDownloadThread.cpp b/src/core/UBDownloadThread.cpp index 9448a0b4..5be1d73d 100644 --- a/src/core/UBDownloadThread.cpp +++ b/src/core/UBDownloadThread.cpp @@ -22,9 +22,6 @@ * along with OpenBoard. If not, see . */ - - - #include #include #include diff --git a/src/core/UBSettings.cpp b/src/core/UBSettings.cpp index c84895ba..19d0fcfb 100644 --- a/src/core/UBSettings.cpp +++ b/src/core/UBSettings.cpp @@ -352,6 +352,7 @@ void UBSettings::init() boardShowToolsPalette = new UBSetting(this, "Board", "ShowToolsPalette", "false"); magnifierDrawingMode = new UBSetting(this, "Board", "MagnifierDrawingMode", "0"); + timerInterval = new UBSetting(this, "Board", "Timer interval", "5"); svgViewBoxMargin = new UBSetting(this, "SVG", "ViewBoxMargin", "50"); diff --git a/src/core/UBSettings.h b/src/core/UBSettings.h index 91a91558..9de4c801 100644 --- a/src/core/UBSettings.h +++ b/src/core/UBSettings.h @@ -373,6 +373,8 @@ class UBSettings : public QObject UBSetting* libIconSize; UBSetting* magnifierDrawingMode; + UBSetting* timerInterval; + public slots: void setPenWidthIndex(int index);