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

Description

Triggers autosave request after some accumulated ammount of document changes.

Definition at line 25 of file AutosaveController.h.

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

Signals

void autosaveRequest ()
 

Public Member Functions

 AutosaveController (QObject *parent=nullptr)
 
QString autosaveDir () const
 The complete path to the autosave dir (e.g. '/projects/Untitled2/autosave'). More...
 
QString autosaveName () const
 
void removeAutosaveDir () const
 remove auto save directory for given project and all its content More...
 
void setAutosaveTime (int timerInterval)
 Sets autosave time (in msec) More...
 
void setDocument (ProjectDocument *document)
 

Private Slots

void onDocumentDestroyed (QObject *object)
 
void onDocumentModified ()
 
void onTimerTimeout ()
 

Private Member Functions

bool assureAutoSaveDirExists () const
 Tries to make sure that the directory for auto saving exists. Tries to create it if not existing so far. No creation, if project directory itself doesn't exist at all. Returns true, if the directory finally exists. More...
 
void autosave ()
 

Private Attributes

ProjectDocumentm_document
 
UpdateTimerm_timer
 

Constructor & Destructor Documentation

◆ AutosaveController()

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

Definition at line 26 of file AutosaveController.cpp.

27  : QObject(parent)
28  , m_document(nullptr)
29  , m_timer(new UpdateTimer(update_every, this))
30 {
32 }
ProjectDocument * m_document
The UpdateTimer class accumulates update requests during certain period of time, and at the end of th...
Definition: UpdateTimer.h:27
void timeToUpdate()

References m_timer, onTimerTimeout(), and UpdateTimer::timeToUpdate().

Here is the call graph for this function:

Member Function Documentation

◆ assureAutoSaveDirExists()

bool AutosaveController::assureAutoSaveDirExists ( ) const
private

Tries to make sure that the directory for auto saving exists. Tries to create it if not existing so far. No creation, if project directory itself doesn't exist at all. Returns true, if the directory finally exists.

Definition at line 111 of file AutosaveController.cpp.

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 }
QString autosaveDir() const
The complete path to the autosave dir (e.g. '/projects/Untitled2/autosave').
QString projectDir() const
QString autosaveSubdir()
Returns fixed name for autosave sub-directory.
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.

References autosaveDir(), GUI::Project::Utils::autosaveSubdir(), ProjectDocument::hasValidNameAndPath(), m_document, ProjectDocument::projectDir(), and GUI::Project::Utils::projectDir().

Referenced by autosave().

Here is the call graph for this function:

◆ autosave()

void AutosaveController::autosave ( )
private

Definition at line 124 of file AutosaveController.cpp.

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 }
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...

References assureAutoSaveDirExists(), autosaveName(), and autosaveRequest().

Referenced by onTimerTimeout().

Here is the call graph for this function:

◆ autosaveDir()

QString AutosaveController::autosaveDir ( ) const

The complete path to the autosave dir (e.g. '/projects/Untitled2/autosave').

Returns the name of autosave directory.

Definition at line 64 of file AutosaveController.cpp.

65 {
68 
69  return "";
70 }
QString projectFileName() const
QString autosaveDir(const QString &projectFileName)
Returns name of autosave directory for project with given project file name. E.g. from '/projects/Unt...

References GUI::Project::Utils::autosaveDir(), ProjectDocument::hasValidNameAndPath(), m_document, and ProjectDocument::projectFileName().

Referenced by assureAutoSaveDirExists(), and removeAutosaveDir().

Here is the call graph for this function:

◆ autosaveName()

QString AutosaveController::autosaveName ( ) const

Definition at line 72 of file AutosaveController.cpp.

73 {
76 
77  return "";
78 }
QString autosaveName(const QString &projectFileName)
Returns name of project for autoSave from given project file name. E.g. from '/projects/Untitled2/Unt...

References GUI::Project::Utils::autosaveName(), ProjectDocument::hasValidNameAndPath(), m_document, and ProjectDocument::projectFileName().

Referenced by autosave(), and SaveService::onAutosaveRequest().

Here is the call graph for this function:

◆ autosaveRequest

void AutosaveController::autosaveRequest ( )
signal

◆ onDocumentDestroyed

void AutosaveController::onDocumentDestroyed ( QObject *  object)
privateslot

Definition at line 95 of file AutosaveController.cpp.

96 {
97  Q_UNUSED(object);
98  m_timer->reset();
99  m_document = nullptr;
100 }
void reset()
Definition: UpdateTimer.cpp:30

References m_document, m_timer, and UpdateTimer::reset().

Referenced by setDocument().

Here is the call graph for this function:

◆ onDocumentModified

void AutosaveController::onDocumentModified ( )
privateslot

Definition at line 102 of file AutosaveController.cpp.

103 {
104  if (!m_document)
105  return;
106 
109 }
bool isModified() const
void scheduleUpdate()
Definition: UpdateTimer.cpp:42

References ProjectDocument::hasValidNameAndPath(), ProjectDocument::isModified(), m_document, m_timer, and UpdateTimer::scheduleUpdate().

Referenced by setDocument().

Here is the call graph for this function:

◆ onTimerTimeout

void AutosaveController::onTimerTimeout ( )
privateslot

Definition at line 89 of file AutosaveController.cpp.

90 {
91  if (m_document->isModified())
92  autosave();
93 }

References autosave(), ProjectDocument::isModified(), and m_document.

Referenced by AutosaveController().

Here is the call graph for this function:

◆ removeAutosaveDir()

void AutosaveController::removeAutosaveDir ( ) const

remove auto save directory for given project and all its content

Definition at line 80 of file AutosaveController.cpp.

81 {
82  if (autosaveDir().isEmpty())
83  return;
84 
85  QDir dir(autosaveDir());
86  dir.removeRecursively();
87 }

References autosaveDir().

Referenced by SaveService::stopService().

Here is the call graph for this function:

◆ setAutosaveTime()

void AutosaveController::setAutosaveTime ( int  timerInterval)

Sets autosave time (in msec)

Definition at line 56 of file AutosaveController.cpp.

57 {
58  m_timer->reset();
59  m_timer->setWallclockTimer(timerInterval);
60 }
void setWallclockTimer(int timerInterval)
Definition: UpdateTimer.cpp:37

References m_timer, UpdateTimer::reset(), and UpdateTimer::setWallclockTimer().

Referenced by SaveService::setAutosaveTime().

Here is the call graph for this function:

◆ setDocument()

void AutosaveController::setDocument ( ProjectDocument document)

Definition at line 34 of file AutosaveController.cpp.

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 }
void onDocumentDestroyed(QObject *object)
void modified()
Emitted for any modifications in the document.

References m_document, m_timer, ProjectDocument::modified(), onDocumentDestroyed(), onDocumentModified(), and UpdateTimer::reset().

Referenced by SaveService::setAutosaveEnabled(), and SaveService::setDocument().

Here is the call graph for this function:

Member Data Documentation

◆ m_document

◆ m_timer

UpdateTimer* AutosaveController::m_timer
private

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