BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SaveService.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Project/SaveService.cpp
6 //! @brief Implements class SaveService
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 
16 #include "Base/Util/Assert.h"
19 #include "GUI/Util/Error.h"
22 #include <QApplication>
23 #include <QCoreApplication>
24 #include <QTime>
25 
26 namespace {
27 
28 //! Class to reset flag in the destructor (to ensure resetting even if an exception occurs)
29 class AutoSetResetFlag {
30 public:
31  AutoSetResetFlag(bool* flag)
32  : m_flag(flag)
33  {
34  *flag = true;
35  }
36  ~AutoSetResetFlag() { *m_flag = false; }
37 
38 private:
39  bool* m_flag = nullptr;
40 };
41 
42 } // namespace
43 
44 SaveService::SaveService(QObject* parent)
45  : QObject(parent)
46  , m_is_saving(false)
47  , m_autosave(nullptr)
48  , m_document(nullptr)
49 {
50 }
51 
53 {
54  m_document = document;
55 
56  if (m_autosave)
57  m_autosave->setDocument(document);
58 
59  m_save_queue.clear();
60 }
61 
62 void SaveService::save(const QString& project_file_name)
63 {
64  ASSERT(m_document);
65 
66  m_save_queue.enqueue(project_file_name);
67  process_queue();
68 }
69 
71 {
72  if (value) {
73  delete m_autosave;
74  m_autosave = new AutosaveController(this);
78  } else {
79  delete m_autosave;
80  m_autosave = nullptr;
81  }
82 }
83 
85 {
86  return m_autosave;
87 }
88 
89 void SaveService::setAutosaveTime(int timerInterval)
90 {
91  if (!m_autosave)
92  setAutosaveEnabled(true);
93 
94  m_autosave->setAutosaveTime(timerInterval);
95 }
96 
98 {
99  return m_is_saving;
100 }
101 
102 //!
103 
105 {
106  QApplication::setOverrideCursor(Qt::WaitCursor);
107 
108  if (isSaving()) {
109  QTime dieTime = QTime::currentTime().addSecs(60);
110  while (QTime::currentTime() < dieTime) {
111  QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
112  if (!isSaving())
113  break;
114  }
115  if (isSaving())
116  throw Error("SaveService::stopService() -> Error. Can't stop service. ");
117  }
118 
119  if (m_autosave)
121 
122  setDocument(nullptr);
123  QApplication::restoreOverrideCursor();
124 }
125 
127 {
129 }
130 
132 {
133  m_is_saving = false;
134  emit projectSaved();
135 
136  // processing possible consequent saves
137  process_queue();
138 }
139 
141 {
142  if (m_is_saving)
143  return;
144 
145  if (!m_save_queue.isEmpty()) {
146  // use set/reset class to ensure correct resetting even in case of an exception
147  AutoSetResetFlag autoSetReset(&m_is_saving);
148 
149  QString project_file_name = m_save_queue.dequeue();
150 
151  // saving project file in a main thread
152  const bool isAutosave = project_file_name.contains(GUI::Project::Utils::autosaveSubdir());
153  m_document->saveProjectFile(project_file_name, isAutosave);
154 
155  if (m_document->hasData()) {
156  // saving heavy data in separate thread
157  auto* saveThread = new SaveThread(this);
158  saveThread->setSaveContext(m_document, project_file_name);
159 
160  connect(saveThread, &SaveThread::saveReady, this, &SaveService::onProjectSaved);
161  connect(saveThread, &SaveThread::finished, saveThread, &QObject::deleteLater);
162  saveThread->start();
163  } else {
164  onProjectSaved();
165  }
166  }
167 }
Defines class AutosaveController.
Defines error class.
Defines class ProjectDocument.
Defines namespace GUI::Project::Utils.
Defines class SaveService.
Defines SaveThread classes.
Triggers autosave request after some accumulated ammount of document changes.
void setAutosaveTime(int timerInterval)
Sets autosave time (in msec)
void setDocument(ProjectDocument *document)
QString autosaveName() const
void removeAutosaveDir() const
remove auto save directory for given project and all its content
Project document class handles all data related to the opened project (sample, job,...
void saveProjectFile(const QString &project_file_name, bool autoSave=false)
bool hasData() const
void projectSaved()
bool isSaving() const
Definition: SaveService.cpp:97
void setAutosaveTime(int timerInterval)
Sets autosave time (in msec)
Definition: SaveService.cpp:89
SaveService(QObject *parent=nullptr)
Definition: SaveService.cpp:44
void setAutosaveEnabled(bool value)
Definition: SaveService.cpp:70
void stopService()
ProjectDocument * m_document
Definition: SaveService.h:61
void save(const QString &project_file_name)
Definition: SaveService.cpp:62
void process_queue()
bool isAutosaveEnabled() const
Definition: SaveService.cpp:84
QQueue< QString > m_save_queue
Definition: SaveService.h:58
void setDocument(ProjectDocument *document)
Definition: SaveService.cpp:52
AutosaveController * m_autosave
Definition: SaveService.h:60
bool m_is_saving
Definition: SaveService.h:59
void onProjectSaved()
void onAutosaveRequest()
Performs saving of heavy intensity data in a thread.
Definition: SaveThread.h:25
void saveReady()
QString autosaveSubdir()
Returns fixed name for autosave sub-directory.