BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
AutosaveController.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Project/AutosaveController.cpp
6 //! @brief Implements class AutosaveController
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
19 #include <QDir>
20 
21 namespace {
22 
23 const int update_every = 20000; // in msec
24 }
25 
27  : QObject(parent)
28  , m_document(nullptr)
29  , m_timer(new UpdateTimer(update_every, this))
30 {
32 }
33 
35 {
36  if (document == m_document)
37  return;
38 
39  m_timer->reset();
40 
41  if (m_document)
42  m_document->disconnect(this);
43 
44  m_document = document;
45 
46  if (m_document) {
47  connect(m_document, &ProjectDocument::destroyed, this,
48  &AutosaveController::onDocumentDestroyed, Qt::UniqueConnection);
49  connect(m_document, &ProjectDocument::modified, this,
50  &AutosaveController::onDocumentModified, Qt::UniqueConnection);
51  }
52 
54 }
55 
56 void AutosaveController::setAutosaveTime(int timerInterval)
57 {
58  m_timer->reset();
59  m_timer->setWallclockTimer(timerInterval);
60 }
61 
62 //! Returns the name of autosave directory.
63 
65 {
68 
69  return "";
70 }
71 
73 {
76 
77  return "";
78 }
79 
81 {
82  if (autosaveDir().isEmpty())
83  return;
84 
85  QDir dir(autosaveDir());
86  dir.removeRecursively();
87 }
88 
90 {
91  if (m_document->isModified())
92  autosave();
93 }
94 
96 {
97  Q_UNUSED(object);
98  m_timer->reset();
99  m_document = nullptr;
100 }
101 
103 {
104  if (!m_document)
105  return;
106 
109 }
110 
112 {
114  const QDir projectDir = m_document->projectDir();
115  if (projectDir.exists() && !projectDir.exists(GUI::Project::Utils::autosaveSubdir()))
117 
118  return QDir(autosaveDir()).exists();
119  }
120 
121  return false;
122 }
123 
125 {
126  try {
127  if (!autosaveName().isEmpty() && assureAutoSaveDirExists())
128  emit autosaveRequest();
129  } catch (...) {
130  // catch any exception - autosave itself never should cause a crash by an unhandled
131  // exception
132  }
133 }
Defines class AutosaveController.
Defines class ProjectDocument.
Defines namespace GUI::Project::Utils.
Defines class UpdateTimer.
void setAutosaveTime(int timerInterval)
Sets autosave time (in msec)
void setDocument(ProjectDocument *document)
void onDocumentDestroyed(QObject *object)
AutosaveController(QObject *parent=nullptr)
ProjectDocument * m_document
QString autosaveName() const
bool assureAutoSaveDirExists() const
Tries to make sure that the directory for auto saving exists. Tries to create it if not existing so f...
void removeAutosaveDir() const
remove auto save directory for given project and all its content
QString autosaveDir() const
The complete path to the autosave dir (e.g. '/projects/Untitled2/autosave').
Project document class handles all data related to the opened project (sample, job,...
QString projectFileName() const
bool isModified() const
QString projectDir() const
void modified()
Emitted for any modifications in the document.
The UpdateTimer class accumulates update requests during certain period of time, and at the end of th...
Definition: UpdateTimer.h:27
void scheduleUpdate()
Definition: UpdateTimer.cpp:42
void timeToUpdate()
void setWallclockTimer(int timerInterval)
Definition: UpdateTimer.cpp:37
void reset()
Definition: UpdateTimer.cpp:30
QString autosaveSubdir()
Returns fixed name for autosave sub-directory.
QString autosaveDir(const QString &projectFileName)
Returns name of autosave directory for project with given project file name. E.g. from '/projects/Unt...
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.
QString autosaveName(const QString &projectFileName)
Returns name of project for autoSave from given project file name. E.g. from '/projects/Untitled2/Unt...