diff --git a/GUI/coregui/mainwindow/ProjectUtils.cpp b/GUI/coregui/mainwindow/ProjectUtils.cpp index 9850ef0f18a36b368db9a845921424989674c008..956097e2d647b3ead1961052d13dee6bbbccf021 100644 --- a/GUI/coregui/mainwindow/ProjectUtils.cpp +++ b/GUI/coregui/mainwindow/ProjectUtils.cpp @@ -16,6 +16,8 @@ #include "ProjectUtils.h" #include "projectdocument.h" #include <QFileInfo> +#include <QDateTime> +#include <QDebug> QString ProjectUtils::projectName(const QString& projectFileName) { @@ -49,9 +51,20 @@ QString ProjectUtils::autosaveName(const QString& projectFileName) ProjectUtils::projectName(projectFileName) + ProjectDocument::projectFileExtension(); } -bool ProjectUtils::hasAutosavedData(const QString& projectFileName) +bool ProjectUtils::exists(const QString& fileName) { - QFileInfo info(autosaveName(projectFileName)); + QFileInfo info(fileName); return info.exists(); } +bool ProjectUtils::hasAutosavedData(const QString& projectFileName) +{ + return exists(projectFileName) && exists(autosaveName(projectFileName)); +} + + +QString ProjectUtils::lastModified(const QString& fileName) +{ + QFileInfo info(fileName); + return info.lastModified().toString("hh:mm:ss, MMMM d, yyyy"); +} diff --git a/GUI/coregui/mainwindow/ProjectUtils.h b/GUI/coregui/mainwindow/ProjectUtils.h index b7668de6f179b2339a56630bc32cfc5c2e59e9e1..dcc96539c51f84b0168b0efaa61b3942d707116c 100644 --- a/GUI/coregui/mainwindow/ProjectUtils.h +++ b/GUI/coregui/mainwindow/ProjectUtils.h @@ -39,9 +39,15 @@ BA_CORE_API_ QString autosaveDir(const QString& projectFileName); //! Returns name of project for autoSave from given project file name. BA_CORE_API_ QString autosaveName(const QString& projectFileName); +//! Returns true if file exists. +BA_CORE_API_ bool exists(const QString& fileName); + //! Returns true if project with given projectFileName contains autosaved data. BA_CORE_API_ bool hasAutosavedData(const QString& projectFileName); +//! Returns a string representing modification time of given file +BA_CORE_API_ QString lastModified(const QString& fileName); + } #endif // PROJECTUTILS_H diff --git a/GUI/coregui/mainwindow/projectmanager.cpp b/GUI/coregui/mainwindow/projectmanager.cpp index 4e1914283bdfa5fe6f7373d0583d1c8dac0274a9..73e5d1a2b2e48a64acdf9774f659dbe121619714 100644 --- a/GUI/coregui/mainwindow/projectmanager.cpp +++ b/GUI/coregui/mainwindow/projectmanager.cpp @@ -25,10 +25,12 @@ #include "newprojectdialog.h" #include "projectdocument.h" #include "AutosaveService.h" +#include "ProjectUtils.h" #include <QDebug> #include <QFileDialog> #include <QMessageBox> #include <QSettings> +#include <QDateTime> #include <QStandardPaths> namespace { @@ -269,8 +271,7 @@ void ProjectManager::openProject(QString fileName) } createNewProject(); - - m_project_document->load(fileName); + loadProject(fileName); if (m_project_document->isReady()) { addToRecentProjects(); @@ -318,6 +319,18 @@ void ProjectManager::deleteCurrentProject() m_mainWindow->models()->resetModels(); } +//! Load project data from file name. If autosave info exists, opens dialog for project restore. + +void ProjectManager::loadProject(const QString& projectFileName) +{ + if(ProjectUtils::hasAutosavedData(projectFileName) && restoreProjectDialog(projectFileName)) { + m_project_document->load(ProjectUtils::autosaveName(projectFileName)); + m_project_document->setProjectFileName(projectFileName); + } else { + m_project_document->load(projectFileName); + } +} + //! Returns project file name from dialog. QString ProjectManager::acquireProjectFileName() @@ -388,3 +401,20 @@ void ProjectManager::riseProjectLoadWarningDialog() warningDialog->show(); warningDialog->raise(); } + +//! Rises dialog if the project should be restored from autosave. Returns true, if yes. + +bool ProjectManager::restoreProjectDialog(const QString& projectFileName) +{ + QString title("Recover project"); + + QString message = QString("Project '%1' contains autosaved data.\n\n" + "Project saved at %2\nAutosave from %3") + .arg(ProjectUtils::projectName(projectFileName)) + .arg(ProjectUtils::lastModified(projectFileName)) + .arg(ProjectUtils::lastModified(ProjectUtils::autosaveName(projectFileName))); + + return GUIHelpers::question(m_mainWindow, title, message, + "\nDo you want to restore from autosave?\n", "Yes, please restore.", + "No, keep loading original"); +} diff --git a/GUI/coregui/mainwindow/projectmanager.h b/GUI/coregui/mainwindow/projectmanager.h index 1336a026cb1d41f5e758235075217f8a2ce8d207..97174d7abcfc6dc6f6ed46e301b5723e89aac010 100644 --- a/GUI/coregui/mainwindow/projectmanager.h +++ b/GUI/coregui/mainwindow/projectmanager.h @@ -62,6 +62,7 @@ public slots: private: void createNewProject(); void deleteCurrentProject(); + void loadProject(const QString& projectFileName); QString acquireProjectFileName(); void addToRecentProjects(); @@ -70,6 +71,7 @@ private: void riseProjectLoadFailedDialog(); void riseProjectLoadWarningDialog(); + bool restoreProjectDialog(const QString& projectFileName); MainWindow* m_mainWindow; ProjectDocument* m_project_document; diff --git a/Tests/UnitTests/GUI/TestAutosave.h b/Tests/UnitTests/GUI/TestAutosave.h index 7d1c0148ad7794e903babc42d9ac16dbe740e730..f6c1642bcad0af0798a717615ef30abd80f83184 100644 --- a/Tests/UnitTests/GUI/TestAutosave.h +++ b/Tests/UnitTests/GUI/TestAutosave.h @@ -50,9 +50,6 @@ inline void TestAutosave::test_autoSave() QCOMPARE(autosave.autosaveDir(), QString("test_autoSave/autosave")); QCOMPARE(autosave.autosaveName(), QString("test_autoSave/autosave/document.pro")); - QDir autosaveDir(autosave.autosaveDir()); - QVERIFY(autosaveDir.exists()); - QSignalSpy spyAutosave(&autosave, SIGNAL(autosaved())); // modify document once and check @@ -61,6 +58,9 @@ inline void TestAutosave::test_autoSave() QVERIFY(spyAutosave.wait(autosave_time * 1.5)); QCOMPARE(spyAutosave.count(), 1); + QDir autosaveDir(autosave.autosaveDir()); + QVERIFY(autosaveDir.exists()); + // saving document and checking that autosave is not triggered document->save(projectFileName); QVERIFY(document->isModified() == false);