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