BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SaveService Class Reference

Description

Provides save/autosave of ProjectDocument in a thread.

Definition at line 26 of file SaveService.h.

Inheritance diagram for SaveService:
[legend]
Collaboration diagram for SaveService:
[legend]

Public Slots

void onAutosaveRequest ()
 

Signals

void projectSaved ()
 

Public Member Functions

 SaveService (QObject *parent=nullptr)
 
bool isAutosaveEnabled () const
 
bool isSaving () const
 
void save (const QString &project_file_name)
 
void setAutosaveEnabled (bool value)
 
void setAutosaveTime (int timerInterval)
 Sets autosave time (in msec) More...
 
void setDocument (ProjectDocument *document)
 
void stopService ()
 

Private Slots

void onProjectSaved ()
 

Private Member Functions

void process_queue ()
 

Private Attributes

AutosaveControllerm_autosave
 
ProjectDocumentm_document
 
bool m_is_saving
 
QQueue< QString > m_save_queue
 

Constructor & Destructor Documentation

◆ SaveService()

SaveService::SaveService ( QObject *  parent = nullptr)
explicit

Definition at line 44 of file SaveService.cpp.

45  : QObject(parent)
46  , m_is_saving(false)
47  , m_autosave(nullptr)
48  , m_document(nullptr)
49 {
50 }
ProjectDocument * m_document
Definition: SaveService.h:61
AutosaveController * m_autosave
Definition: SaveService.h:60
bool m_is_saving
Definition: SaveService.h:59

Member Function Documentation

◆ isAutosaveEnabled()

bool SaveService::isAutosaveEnabled ( ) const

Definition at line 84 of file SaveService.cpp.

85 {
86  return m_autosave;
87 }

References m_autosave.

Referenced by ProjectManager::isAutosaveEnabled().

◆ isSaving()

bool SaveService::isSaving ( ) const

Definition at line 97 of file SaveService.cpp.

98 {
99  return m_is_saving;
100 }

References m_is_saving.

Referenced by stopService().

◆ onAutosaveRequest

void SaveService::onAutosaveRequest ( )
slot

Definition at line 126 of file SaveService.cpp.

127 {
129 }
QString autosaveName() const
void save(const QString &project_file_name)
Definition: SaveService.cpp:62

References AutosaveController::autosaveName(), m_autosave, and save().

Referenced by setAutosaveEnabled().

Here is the call graph for this function:

◆ onProjectSaved

void SaveService::onProjectSaved ( )
privateslot

Definition at line 131 of file SaveService.cpp.

132 {
133  m_is_saving = false;
134  emit projectSaved();
135 
136  // processing possible consequent saves
137  process_queue();
138 }
void projectSaved()
void process_queue()

References m_is_saving, process_queue(), and projectSaved().

Referenced by process_queue().

Here is the call graph for this function:

◆ process_queue()

void SaveService::process_queue ( )
private

Definition at line 140 of file SaveService.cpp.

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 }
void saveProjectFile(const QString &project_file_name, bool autoSave=false)
bool hasData() const
QQueue< QString > m_save_queue
Definition: SaveService.h:58
void onProjectSaved()
Performs saving of heavy intensity data in a thread.
Definition: SaveThread.h:25
void saveReady()
QString autosaveSubdir()
Returns fixed name for autosave sub-directory.

References GUI::Project::Utils::autosaveSubdir(), ProjectDocument::hasData(), m_document, m_is_saving, m_save_queue, onProjectSaved(), ProjectDocument::saveProjectFile(), and SaveThread::saveReady().

Referenced by onProjectSaved(), and save().

Here is the call graph for this function:

◆ projectSaved

void SaveService::projectSaved ( )
signal

Referenced by onProjectSaved().

◆ save()

void SaveService::save ( const QString &  project_file_name)

Definition at line 62 of file SaveService.cpp.

63 {
64  ASSERT(m_document);
65 
66  m_save_queue.enqueue(project_file_name);
67  process_queue();
68 }

References m_document, m_save_queue, and process_queue().

Referenced by onAutosaveRequest(), and ProjectManager::saveProject().

Here is the call graph for this function:

◆ setAutosaveEnabled()

void SaveService::setAutosaveEnabled ( bool  value)

Definition at line 70 of file SaveService.cpp.

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 }
Triggers autosave request after some accumulated ammount of document changes.
void setDocument(ProjectDocument *document)
void onAutosaveRequest()

References AutosaveController::autosaveRequest(), m_autosave, m_document, onAutosaveRequest(), and AutosaveController::setDocument().

Referenced by ProjectManager::setAutosaveEnabled(), and setAutosaveTime().

Here is the call graph for this function:

◆ setAutosaveTime()

void SaveService::setAutosaveTime ( int  timerInterval)

Sets autosave time (in msec)

Definition at line 89 of file SaveService.cpp.

90 {
91  if (!m_autosave)
92  setAutosaveEnabled(true);
93 
94  m_autosave->setAutosaveTime(timerInterval);
95 }
void setAutosaveTime(int timerInterval)
Sets autosave time (in msec)
void setAutosaveEnabled(bool value)
Definition: SaveService.cpp:70

References m_autosave, setAutosaveEnabled(), and AutosaveController::setAutosaveTime().

Here is the call graph for this function:

◆ setDocument()

void SaveService::setDocument ( ProjectDocument document)

Definition at line 52 of file SaveService.cpp.

53 {
54  m_document = document;
55 
56  if (m_autosave)
57  m_autosave->setDocument(document);
58 
59  m_save_queue.clear();
60 }

References m_autosave, m_document, m_save_queue, and AutosaveController::setDocument().

Referenced by ProjectManager::createNewProject(), and stopService().

Here is the call graph for this function:

◆ stopService()

void SaveService::stopService ( )

Definition at line 104 of file SaveService.cpp.

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 }
void removeAutosaveDir() const
remove auto save directory for given project and all its content
bool isSaving() const
Definition: SaveService.cpp:97
void setDocument(ProjectDocument *document)
Definition: SaveService.cpp:52

References Error, isSaving(), m_autosave, AutosaveController::removeAutosaveDir(), and setDocument().

Referenced by ProjectManager::deleteCurrentProject().

Here is the call graph for this function:

Member Data Documentation

◆ m_autosave

AutosaveController* SaveService::m_autosave
private

◆ m_document

ProjectDocument* SaveService::m_document
private

Definition at line 61 of file SaveService.h.

Referenced by process_queue(), save(), setAutosaveEnabled(), and setDocument().

◆ m_is_saving

bool SaveService::m_is_saving
private

Definition at line 59 of file SaveService.h.

Referenced by isSaving(), onProjectSaved(), and process_queue().

◆ m_save_queue

QQueue<QString> SaveService::m_save_queue
private

Definition at line 58 of file SaveService.h.

Referenced by process_queue(), save(), and setDocument().


The documentation for this class was generated from the following files: