BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ProjectDocument Class Reference

Project document class handles all data related to the opened project (sample, jobModel, project specific windows settings) More...

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

Public Slots

void onModelChanged ()
 

Signals

void modified ()
 

Public Member Functions

 ProjectDocument (const QString &projectFileName="")
 
ProjectFlags::DocumentStatus documentStatus () const
 
QString documentVersion () const
 
bool hasData () const
 
bool hasErrors () const
 
bool hasValidNameAndPath ()
 
bool hasWarnings () const
 
bool isModified ()
 
bool isReady () const
 
void load (const QString &project_file_name)
 
QString projectDir () const
 
QString projectFileName () const
 
QString projectName () const
 
void save (const QString &project_file_name, bool autoSave=false)
 
void save_project_data (const QString &project_file_name)
 
void save_project_file (const QString &project_file_name, bool autoSave=false)
 
void setApplicationModels (ApplicationModels *applicationModels)
 
void setLogger (MessageService *messageService)
 
void setModified (bool flag)
 
void setProjectDir (const QString &text)
 
void setProjectFileName (const QString &text)
 
void setProjectName (const QString &text)
 

Static Public Member Functions

static QString projectFileExtension ()
 

Private Member Functions

void connectModels ()
 
void disconnectModels ()
 
void readFrom (QIODevice *device)
 
void writeTo (QIODevice *device)
 

Private Attributes

ApplicationModelsm_applicationModels
 
QString m_currentVersion
 
OutputDataIOServicem_dataService
 
ProjectFlags::DocumentStatus m_documentStatus
 
MessageServicem_messageService
 
bool m_modified
 
QString m_project_dir
 
QString m_project_name
 

Detailed Description

Project document class handles all data related to the opened project (sample, jobModel, project specific windows settings)

e.g. if project file is /home/users/development/Untitled/Untitled.pro projectName() - 'Untitled' projectDir() - '/home/users/development/Untitled projectFileName() - '/home/users/development/Untitled/Untitled.pro'

Definition at line 40 of file projectdocument.h.

Constructor & Destructor Documentation

◆ ProjectDocument()

ProjectDocument::ProjectDocument ( const QString &  projectFileName = "")

Definition at line 30 of file projectdocument.cpp.

31  : m_applicationModels(nullptr)
32  , m_modified(false)
34  , m_messageService(nullptr)
36 {
37  setObjectName("ProjectDocument");
38  if (!projectFileName.isEmpty())
40 }
Provide read/write of heavy data files in a separate thread.
OutputDataIOService * m_dataService
QString projectFileName() const
MessageService * m_messageService
ApplicationModels * m_applicationModels
ProjectFlags::DocumentStatus m_documentStatus
void setProjectFileName(const QString &text)

References projectFileName(), and setProjectFileName().

Here is the call graph for this function:

Member Function Documentation

◆ connectModels()

void ProjectDocument::connectModels ( )
private

Definition at line 295 of file projectdocument.cpp.

296 {
299  &ProjectDocument::onModelChanged, Qt::UniqueConnection);
300 }

References m_applicationModels, ApplicationModels::modelChanged(), and onModelChanged().

Referenced by load(), and setApplicationModels().

Here is the call graph for this function:

◆ disconnectModels()

void ProjectDocument::disconnectModels ( )
private

Definition at line 288 of file projectdocument.cpp.

References m_applicationModels, ApplicationModels::modelChanged(), and onModelChanged().

Referenced by load(), and setApplicationModels().

Here is the call graph for this function:

◆ documentStatus()

ProjectFlags::DocumentStatus ProjectDocument::documentStatus ( ) const

Definition at line 186 of file projectdocument.cpp.

187 {
188  return m_documentStatus;
189 }

References m_documentStatus.

◆ documentVersion()

QString ProjectDocument::documentVersion ( ) const

Definition at line 211 of file projectdocument.cpp.

212 {
213  QString result(m_currentVersion);
214  if (result.isEmpty())
216  return result;
217 }
QString m_currentVersion
QString getBornAgainVersionString()
Definition: GUIHelpers.cpp:130

References GUIHelpers::getBornAgainVersionString(), and m_currentVersion.

Referenced by ProjectManager::riseProjectLoadWarningDialog().

Here is the call graph for this function:

◆ hasData()

bool ProjectDocument::hasData ( ) const

Definition at line 206 of file projectdocument.cpp.

207 {
208  return !m_dataService->nonXMLItems().isEmpty();
209 }
QVector< SaveLoadInterface * > nonXMLItems() const
Returns all non-XML items available for save/load.

References m_dataService, and OutputDataIOService::nonXMLItems().

Referenced by SaveService::process_queue().

Here is the call graph for this function:

◆ hasErrors()

bool ProjectDocument::hasErrors ( ) const

Definition at line 201 of file projectdocument.cpp.

202 {
204 }

References m_documentStatus, and ProjectFlags::STATUS_FAILED.

Referenced by ProjectManager::openProject().

◆ hasValidNameAndPath()

◆ hasWarnings()

bool ProjectDocument::hasWarnings ( ) const

Definition at line 196 of file projectdocument.cpp.

197 {
199 }

References m_documentStatus, and ProjectFlags::STATUS_WARNING.

Referenced by ProjectManager::openProject().

◆ isModified()

bool ProjectDocument::isModified ( )

◆ isReady()

bool ProjectDocument::isReady ( ) const

Definition at line 191 of file projectdocument.cpp.

192 {
194 }

References m_documentStatus, and ProjectFlags::STATUS_OK.

Referenced by ProjectManager::openProject().

◆ load()

void ProjectDocument::load ( const QString &  project_file_name)

Definition at line 129 of file projectdocument.cpp.

130 {
131  QElapsedTimer timer1, timer2;
132  timer1.start();
133 
135  setProjectFileName(project_file_name);
136 
137  QFile file(projectFileName());
138  if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
139  QString message = QString("Open file error '%1'").arg(file.errorString());
140  m_messageService->send_error(this, message);
142  return;
143  }
144 
145  try {
147  readFrom(&file);
148  file.close();
149 
150  timer2.start();
152  connectModels();
153 
156 
157  } catch (const std::exception& ex) {
158  QString message = QString("Exception thrown '%1'").arg(QString(ex.what()));
159  m_messageService->send_error(this, message);
161  }
162 }
void send_error(QObject *sender, const QString &description)
int warningCount(const QObject *sender=0) const
Returns number of warnings for given sender.
void load(const QString &projectDir, MessageService *messageService=nullptr)
void readFrom(QIODevice *device)
QString projectDir() const
static void setFlag(ProjectFlags::DocumentStatus &flags, EDocumentStatus status)
Definition: ProjectFlags.h:30

References connectModels(), disconnectModels(), OutputDataIOService::load(), m_dataService, m_documentStatus, m_messageService, projectDir(), projectFileName(), readFrom(), MessageService::send_error(), ProjectFlags::setFlag(), setProjectFileName(), ProjectFlags::STATUS_FAILED, ProjectFlags::STATUS_OK, ProjectFlags::STATUS_WARNING, and MessageService::warningCount().

Referenced by ProjectManager::loadProject().

Here is the call graph for this function:

◆ modified

◆ onModelChanged

void ProjectDocument::onModelChanged ( )
slot

Definition at line 219 of file projectdocument.cpp.

220 {
221  m_modified = true;
222  emit modified();
223 }

References m_modified, and modified().

Referenced by connectModels(), and disconnectModels().

◆ projectDir()

QString ProjectDocument::projectDir ( ) const

Definition at line 55 of file projectdocument.cpp.

56 {
57  return m_project_dir;
58 }

References m_project_dir.

Referenced by AutosaveController::assureAutoSaveDirExists(), load(), ProjectManager::projectDir(), and projectFileName().

◆ projectFileExtension()

QString ProjectDocument::projectFileExtension ( )
static

Definition at line 79 of file projectdocument.cpp.

80 {
81  return ".pro";
82 }

Referenced by ProjectUtils::autosaveName(), NewProjectDialog::getProjectFileName(), and projectFileName().

◆ projectFileName()

QString ProjectDocument::projectFileName ( ) const

Definition at line 65 of file projectdocument.cpp.

66 {
67  if (!projectName().isEmpty())
68  return projectDir() + "/" + projectName() + projectFileExtension();
69  else
70  return "";
71 }
static QString projectFileExtension()
QString projectName() const

References projectDir(), projectFileExtension(), and projectName().

Referenced by ProjectDocument(), ProjectManager::addToRecentProjects(), AutosaveController::autosaveDir(), AutosaveController::autosaveName(), load(), ProjectManager::riseProjectLoadFailedDialog(), ProjectManager::saveProject(), and setProjectFileName().

Here is the call graph for this function:

◆ projectName()

QString ProjectDocument::projectName ( ) const

Definition at line 42 of file projectdocument.cpp.

43 {
44  return m_project_name;
45 }

References m_project_name.

Referenced by ProjectManager::onDocumentModified(), projectFileName(), and writeTo().

◆ readFrom()

void ProjectDocument::readFrom ( QIODevice *  device)
private

Definition at line 225 of file projectdocument.cpp.

226 {
228  QXmlStreamReader reader(device);
229 
230  while (!reader.atEnd()) {
231  reader.readNext();
232  if (reader.isStartElement()) {
233  if (reader.name() == ProjectDocumentXML::BornAgainTag) {
234  m_currentVersion = reader.attributes()
236  .toString();
237 
239  minimal_supported_version)) {
241  QString message = QString("Can't open document version '%1', "
242  "minimal supported version '%2'")
243  .arg(m_currentVersion)
244  .arg(minimal_supported_version);
245  m_messageService->send_error(this, message);
246  return;
247  }
248 
249  while (reader.readNextStartElement())
250  if (reader.name() == ProjectDocumentXML::InfoTag)
251  reader.skipCurrentElement();
252  else
254 
257  }
258  }
259  }
260 
261  if (reader.hasError()) {
263  QString message = QString("Format error '%1'").arg(reader.errorString());
264  m_messageService->send_error(this, message);
265  return;
266  }
267 }
#define ASSERT(condition)
Definition: Assert.h:31
void readFrom(class QXmlStreamReader *reader, MessageService *messageService)
int messageCount(const QObject *sender, const QString &message_type="") const
Reports number of messages of given type reported by the sender.
bool isVersionMatchMinimal(const QString &version, const QString &minimal_version)
returns true if current BornAgain version match minimal required version
Definition: GUIHelpers.cpp:187
const QString InfoTag("DocumentInfo")
const QString BornAgainVersionAttribute("Version")
const QString BornAgainTag("BornAgain")

References ASSERT, ProjectDocumentXML::BornAgainTag(), ProjectDocumentXML::BornAgainVersionAttribute(), ProjectDocumentXML::InfoTag(), GUIHelpers::isVersionMatchMinimal(), m_applicationModels, m_currentVersion, m_documentStatus, m_messageService, MessageService::messageCount(), ApplicationModels::readFrom(), MessageService::send_error(), ProjectFlags::setFlag(), ProjectFlags::STATUS_FAILED, and ProjectFlags::STATUS_WARNING.

Referenced by load().

Here is the call graph for this function:

◆ save()

void ProjectDocument::save ( const QString &  project_file_name,
bool  autoSave = false 
)

Definition at line 94 of file projectdocument.cpp.

95 {
96  save_project_data(project_file_name);
97  save_project_file(project_file_name, autoSave);
98 }
void save_project_data(const QString &project_file_name)
void save_project_file(const QString &project_file_name, bool autoSave=false)

References save_project_data(), and save_project_file().

Here is the call graph for this function:

◆ save_project_data()

void ProjectDocument::save_project_data ( const QString &  project_file_name)

Definition at line 121 of file projectdocument.cpp.

122 {
123  QElapsedTimer timer;
124  timer.start();
125 
126  m_dataService->save(ProjectUtils::projectDir(project_file_name));
127 }
void save(const QString &projectDir)
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.

References m_dataService, ProjectUtils::projectDir(), and OutputDataIOService::save().

Referenced by SaveThread::run(), and save().

Here is the call graph for this function:

◆ save_project_file()

void ProjectDocument::save_project_file ( const QString &  project_file_name,
bool  autoSave = false 
)

Definition at line 100 of file projectdocument.cpp.

101 {
102  QElapsedTimer timer;
103  timer.start();
104 
105  QFile file(project_file_name);
106  if (!file.open(QFile::ReadWrite | QIODevice::Truncate | QFile::Text))
107  throw GUIHelpers::Error("ProjectDocument::save_project_file() -> Error. Can't open "
108  "file '"
109  + project_file_name + "' for writing.");
110 
111  writeTo(&file);
112  file.close();
113 
114  if (!autoSave) {
115  setProjectFileName(project_file_name);
116  m_modified = false;
117  emit modified();
118  }
119 }
void writeTo(QIODevice *device)

References m_modified, modified(), setProjectFileName(), and writeTo().

Referenced by SaveService::process_queue(), and save().

Here is the call graph for this function:

◆ setApplicationModels()

void ProjectDocument::setApplicationModels ( ApplicationModels applicationModels)

Definition at line 84 of file projectdocument.cpp.

85 {
86  if (applicationModels != m_applicationModels) {
88  m_applicationModels = applicationModels;
90  connectModels();
91  }
92 }
void setApplicationModels(ApplicationModels *models)

References connectModels(), disconnectModels(), m_applicationModels, m_dataService, and OutputDataIOService::setApplicationModels().

Referenced by ProjectManager::createNewProject().

Here is the call graph for this function:

◆ setLogger()

void ProjectDocument::setLogger ( MessageService messageService)

Definition at line 181 of file projectdocument.cpp.

182 {
183  m_messageService = messageService;
184 }

References m_messageService.

Referenced by ProjectManager::createNewProject().

◆ setModified()

void ProjectDocument::setModified ( bool  flag)

Definition at line 174 of file projectdocument.cpp.

175 {
176  m_modified = flag;
177  if (m_modified)
178  emit modified();
179 }

References m_modified, and modified().

Referenced by ProjectManager::loadProject().

◆ setProjectDir()

void ProjectDocument::setProjectDir ( const QString &  text)

Definition at line 60 of file projectdocument.cpp.

61 {
62  m_project_dir = text;
63 }

References m_project_dir.

Referenced by setProjectFileName().

◆ setProjectFileName()

void ProjectDocument::setProjectFileName ( const QString &  text)

Definition at line 73 of file projectdocument.cpp.

74 {
77 }
void setProjectDir(const QString &text)
void setProjectName(const QString &text)
QString projectName(const QString &projectFileName)
Returns project name deduced from project file name.

References ProjectUtils::projectDir(), projectFileName(), ProjectUtils::projectName(), setProjectDir(), and setProjectName().

Referenced by ProjectDocument(), load(), ProjectManager::loadProject(), and save_project_file().

Here is the call graph for this function:

◆ setProjectName()

void ProjectDocument::setProjectName ( const QString &  text)

Definition at line 47 of file projectdocument.cpp.

48 {
49  if (m_project_name != text) {
50  m_project_name = text;
51  emit modified();
52  }
53 }

References m_project_name, and modified().

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

◆ writeTo()

void ProjectDocument::writeTo ( QIODevice *  device)
private

Definition at line 269 of file projectdocument.cpp.

270 {
271  QXmlStreamWriter writer(device);
272  writer.setAutoFormatting(true);
273  writer.writeStartDocument();
274  writer.writeStartElement(ProjectDocumentXML::BornAgainTag);
275  QString version_string = GUIHelpers::getBornAgainVersionString();
276  writer.writeAttribute(ProjectDocumentXML::BornAgainVersionAttribute, version_string);
277 
278  writer.writeStartElement(ProjectDocumentXML::InfoTag);
279  writer.writeAttribute(ProjectDocumentXML::InfoNameAttribute, projectName());
280  writer.writeEndElement(); // InfoTag
281 
282  m_applicationModels->writeTo(&writer);
283 
284  writer.writeEndElement(); // BornAgain tag
285  writer.writeEndDocument();
286 }
void writeTo(class QXmlStreamWriter *writer)
Writes all model in file one by one.
const QString InfoNameAttribute("ProjectName")

References ProjectDocumentXML::BornAgainTag(), ProjectDocumentXML::BornAgainVersionAttribute(), GUIHelpers::getBornAgainVersionString(), ProjectDocumentXML::InfoNameAttribute(), ProjectDocumentXML::InfoTag(), m_applicationModels, projectName(), and ApplicationModels::writeTo().

Referenced by save_project_file().

Here is the call graph for this function:

Member Data Documentation

◆ m_applicationModels

ApplicationModels* ProjectDocument::m_applicationModels
private

◆ m_currentVersion

QString ProjectDocument::m_currentVersion
private

Definition at line 104 of file projectdocument.h.

Referenced by documentVersion(), and readFrom().

◆ m_dataService

OutputDataIOService* ProjectDocument::m_dataService
private

Definition at line 105 of file projectdocument.h.

Referenced by hasData(), load(), save_project_data(), and setApplicationModels().

◆ m_documentStatus

ProjectFlags::DocumentStatus ProjectDocument::m_documentStatus
private

Definition at line 102 of file projectdocument.h.

Referenced by documentStatus(), hasErrors(), hasWarnings(), isReady(), load(), and readFrom().

◆ m_messageService

MessageService* ProjectDocument::m_messageService
private

Definition at line 103 of file projectdocument.h.

Referenced by load(), readFrom(), and setLogger().

◆ m_modified

bool ProjectDocument::m_modified
private

Definition at line 101 of file projectdocument.h.

Referenced by isModified(), onModelChanged(), save_project_file(), and setModified().

◆ m_project_dir

QString ProjectDocument::m_project_dir
private

Definition at line 98 of file projectdocument.h.

Referenced by hasValidNameAndPath(), projectDir(), and setProjectDir().

◆ m_project_name

QString ProjectDocument::m_project_name
private

Definition at line 99 of file projectdocument.h.

Referenced by hasValidNameAndPath(), projectName(), and setProjectName().


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