BornAgain  1.19.0
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/coregui/mainwindow/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 const int update_every = 20000; // in msec
23 }
24 
26  : QObject(parent), m_document(0), m_timer(new UpdateTimer(update_every, this))
27 {
29 }
30 
32 {
33  if (document == m_document)
34  return;
35 
36  m_timer->reset();
37 
38  if (m_document)
39  setDocumentConnected(false);
40 
41  m_document = document;
42 
43  if (m_document)
45 
47 }
48 
49 void AutosaveController::setAutosaveTime(int timerInterval)
50 {
51  m_timer->reset();
52  m_timer->setWallclockTimer(timerInterval);
53 }
54 
55 //! Returns the name of autosave directory.
56 
58 {
61 
62  return "";
63 }
64 
66 {
69 
70  return "";
71 }
72 
74 {
75  if (autosaveDir().isEmpty())
76  return;
77 
78  QDir dir(autosaveDir());
79  dir.removeRecursively();
80 }
81 
83 {
84  if (m_document->isModified())
85  autosave();
86 }
87 
89 {
90  Q_UNUSED(object);
91  m_timer->reset();
92  m_document = 0;
93 }
94 
96 {
97  if (!m_document)
98  return;
99 
102 }
103 
105 {
107  const QDir projectDir = m_document->projectDir();
108  if (projectDir.exists() && !projectDir.exists(ProjectUtils::autosaveSubdir()))
110 
111  return QDir(autosaveDir()).exists();
112  }
113 
114  return false;
115 }
116 
118 {
119  try {
120  if (!autosaveName().isEmpty() && assureAutoSaveDirExists())
121  emit autosaveRequest();
122  } catch (...) {
123  // catch any exception - autosave itself never should cause a crash by an unhandled
124  // exception
125  }
126 }
127 
129 {
130  if (!m_document)
131  return;
132 
133  if (set_connected) {
134  connect(m_document, &ProjectDocument::destroyed, this,
135  &AutosaveController::onDocumentDestroyed, Qt::UniqueConnection);
136  connect(m_document, &ProjectDocument::modified, this,
137  &AutosaveController::onDocumentModified, Qt::UniqueConnection);
138  } else {
139  disconnect(m_document, &ProjectDocument::destroyed, this,
141  disconnect(m_document, &ProjectDocument::modified, this,
143  }
144 }
Defines class AutosaveController.
Defines ProjectUtils namespace.
Defines class UpdateTimer.
void setAutosaveTime(int timerInterval)
Sets autosave time (in msec)
void setDocumentConnected(bool set_connected)
void setDocument(ProjectDocument *document)
void onDocumentDestroyed(QObject *object)
void removeAutosaveDir()
remove auto save directory for given project and all its content
ProjectDocument * m_document
AutosaveController(QObject *parent=0)
QString autosaveName() const
bool assureAutoSaveDirExists() const
Tries to make sure that the directory for auto saving exists.
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, jobModel,...
QString projectFileName() const
QString projectDir() const
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 autosaveName(const QString &projectFileName)
Returns name of project for autoSave from given project file name.
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.
QString autosaveDir(const QString &projectFileName)
Returns name of autosave directory for project with given project file name.
QString autosaveSubdir()
Returns fixed name for autosave sub-directory.
Defines class ProjectDocument.