33 #include <QElapsedTimer>
34 #include <QStandardPaths>
35 #include <QXmlStreamReader>
40 const QString minimal_supported_version =
"1.6.0";
41 const QString BornAgainTag(
"BornAgain");
42 const QString BornAgainVersionAttribute(
"Version");
43 const QString InfoTag(
"DocumentInfo");
44 const QString InfoNameAttribute(
"ProjectName");
45 const QString SimulationOptionsTag(
"SimulationOptions");
46 const QString SamplesTag(
"Samples");
47 const QString InstrumentsTag(
"Instruments");
48 const QString FormatVersionTag(
"FormatVersion");
56 , m_singleInstrumentMode(false)
57 , m_singleSampleMode(false)
58 , m_functionalities(All)
59 , m_instrumentEditController(&m_instruments)
67 m_linkManager = std::make_unique<LinkInstrumentManager>(
this);
68 setObjectName(
"ProjectDocument");
110 return QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
172 QFile file(project_file_name);
173 if (!file.open(QFile::ReadWrite | QIODevice::Truncate | QFile::Text))
174 throw Error(
"ProjectDocument::save_project_file() -> Error. Can't open "
176 + project_file_name +
"' for writing.");
191 d->updateNonXMLDataFileNames();
205 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
206 QString message = QString(
"Open file error '%1'").arg(file.errorString());
207 messageService.
addError(
this, message);
221 if (!messageService.
warnings().empty())
224 }
catch (
const std::exception& ex) {
225 QString message = QString(
"Exception thrown '%1'").arg(QString(ex.what()));
226 messageService.
addError(
this, message);
260 if (result.isEmpty())
319 const int warningsBefore = messageService.
warnings().size();
321 QXmlStreamReader reader(device);
323 while (!reader.atEnd()) {
325 if (reader.isStartElement()) {
326 if (reader.name() == BornAgainTag) {
329 reader.attributes().value(BornAgainVersionAttribute).toString();
332 minimal_supported_version)) {
333 QString message = QString(
"Can't open document version '%1', "
334 "minimal supported version '%2'")
336 .arg(minimal_supported_version);
337 messageService.
addError(
this, message);
341 while (reader.readNextStartElement())
342 if (reader.name() == InfoTag)
343 reader.skipCurrentElement();
344 else if (reader.name() == SimulationOptionsTag) {
346 reader.skipCurrentElement();
348 }
else if (reader.name() == SamplesTag) {
352 if (reader.name() != SamplesTag) {
353 if (!reader.isEndElement())
354 reader.skipCurrentElement();
355 reader.skipCurrentElement();
358 ASSERT(reader.isEndElement());
361 }
else if (reader.name() == InstrumentsTag) {
365 if (reader.name() != InstrumentsTag) {
366 if (!reader.isEndElement())
367 reader.skipCurrentElement();
368 reader.skipCurrentElement();
371 ASSERT(reader.isEndElement());
381 reader.raiseError(ex.
text());
384 if (reader.hasError()) {
385 QString message = QString(
"Format error '%1'").arg(reader.errorString());
386 messageService.
addError(
this, message);
397 QXmlStreamWriter writer(device);
398 writer.setAutoFormatting(
true);
399 writer.writeStartDocument();
400 writer.writeStartElement(BornAgainTag);
402 writer.writeAttribute(BornAgainVersionAttribute, version_string);
405 writer.writeStartElement(InfoTag);
406 writer.writeAttribute(InfoNameAttribute,
projectName());
407 writer.writeEndElement();
409 writer.writeStartElement(SimulationOptionsTag);
411 writer.writeEndElement();
413 writer.writeStartElement(SamplesTag);
416 writer.writeEndElement();
418 writer.writeStartElement(InstrumentsTag);
421 writer.writeEndElement();
425 writer.writeEndElement();
426 writer.writeEndDocument();
Defines class DeserializationException.
Defines class DatafieldIOService.
Defines class InstrumentItem and all its children.
Defines abstract item with a material property.
Defines class LinkInstrumentManager.
Defines class MaterialItem.
Defines MessageService class.
Defines class MultiLayerItem.
Defines class Helpers functions.
Defines class ProjectDocument.
Defines namespace GUI::Project::Utils.
Defines class RealDataItem.
Defines class RealDataModel.
Defines class SimulationOptionsItem.
JobModel * jobModel() const
void readFrom(class QXmlStreamReader *reader, MessageService *messageService)
RealDataModel * realDataModel() const
void writeTo(class QXmlStreamWriter *writer)
Writes all model in file one by one.
Provide read/write of heavy data files in a separate thread.
void setApplicationModels(ApplicationModels *models)
QVector< SaveLoadInterface * > dataInterfaces() const
Returns all non-XML items available for save/load.
void save(const QString &projectDir)
void loadDataFiles(const QString &projectDir, MessageService *messageService=nullptr)
void serialize(Streamer &s)
Class to modify the instruments list or a single instrument and provide the necessary signaling withi...
void instrumentAddedOrRemoved()
Signals a change in the list of instruments.
void instrumentChanged(const InstrumentItem *instrument)
Signals any change in the settings of the given instrument.
The LinkInstrumentManager class provides communication between InstrumentCollection and RealDataModel...
The service to collect messages from different senders.
QStringList warnings(bool includeSenders=false) const
void addError(QObject *sender, const QString &description)
Main model to hold sample items.
void serialize(Streamer &s)
void singleSampleModeChanged()
Emitted when single sample mode has changed.
JobModel * jobModel() const
ApplicationModels m_applicationModels
RealDataModel * realDataModel() const
void setSingleInstrumentMode(bool b)
InstrumentCollection m_instruments
QString projectFileName() const
void setProjectDir(const QString &text)
void saveProjectData(const QString &project_file_name)
SimulationOptionsItem m_simulationOptionsItem
void singleInstrumentModeChanged()
Emitted when single instrument mode has changed.
QString documentVersion() const
void functionalitiesChanged()
Emitted when functionality has changed.
void setSingleSampleMode(bool b)
void saveProjectFile(const QString &project_file_name, bool autoSave=false)
QString projectName() const
InstrumentCollection * collectedItems() const
void setFunctionalities(const Functionalities &f)
void setProjectFileName(const QString &projectFileName)
bool hasValidNameAndPath()
MultiLayerItems m_sampleItems
LinkInstrumentManager * linkInstrumentManager()
ReadResult readProject(QIODevice *device, MessageService &messageService)
void setProjectName(const QString &text)
QString validProjectDir() const
ReadResult loadProjectFile(const QString &project_file_name, MessageService &messageService)
void writeTo(QIODevice *device)
bool m_singleInstrumentMode
Functionalities functionalities() const
MultiLayerItems * sampleItems()
InstrumentsEditController m_instrumentEditController
bool singleInstrumentMode() const
std::unique_ptr< LinkInstrumentManager > m_linkManager
DatafieldIOService * m_dataService
QString projectDir() const
void modified()
Emitted for any modifications in the document.
QString userExportDir() const
Returns directory name suitable for saving plots.
Functionalities m_functionalities
SimulationOptionsItem * simulationOptionsItem()
void save(const QString &project_file_name, bool autoSave=false)
InstrumentsEditController * instrumentsEditController()
The edit controller for the instruments in this project document.
bool singleSampleMode() const
The RealDataModel class is a model to store all imported RealDataItem's.
The SimulationOptionsItem class holds simulation status (run policy, number of threads,...
void writeContentTo(QXmlStreamWriter *writer) const
void readContentFrom(QXmlStreamReader *reader)
Supports serialization to or deserialization from QXmlStream.
void writeVersion(unsigned version) const
As writer, serializes the given version number. As reader, does nothing.
void assertVersion(unsigned expectedVersion) const
As reader, throws DeserializationException unless the expected version is read. As writer,...
constexpr const char * projectFileExtension
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.
QString projectName(const QString &projectFileName)
Returns project name deduced from project file name.
void assertExpectedTag(QXmlStreamReader *reader, const QString &tag)
bool isVersionMatchMinimal(const QString &version, const QString &minimal_version)
Returns true if current BornAgain version match minimal required version.
QString getBornAgainVersionString()