BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelView::JsonDocument Class Reference

Saves and restores list of SessionModel's to/from disk using json format. More...

Inheritance diagram for ModelView::JsonDocument:
[legend]
Collaboration diagram for ModelView::JsonDocument:
[legend]

Classes

struct  JsonDocumentImpl
 

Public Member Functions

 JsonDocument (const std::vector< SessionModel * > &models)
 
 ~JsonDocument () override
 
void load (const std::string &file_name) override
 Loads models from disk. If models have some data already, it will be rewritten. More...
 
void save (const std::string &file_name) const override
 Saves models on disk. More...
 

Private Attributes

std::unique_ptr< JsonDocumentImplp_impl
 

Detailed Description

Saves and restores list of SessionModel's to/from disk using json format.

Single JsonDocument corresponds to a single file on disk.

Definition at line 29 of file jsondocument.h.

Constructor & Destructor Documentation

◆ JsonDocument()

JsonDocument::JsonDocument ( const std::vector< SessionModel * > &  models)

Definition at line 32 of file jsondocument.cpp.

33  : p_impl(std::make_unique<JsonDocumentImpl>(models))
34 {
35 }
std::unique_ptr< JsonDocumentImpl > p_impl
Definition: jsondocument.h:38

◆ ~JsonDocument()

JsonDocument::~JsonDocument ( )
overridedefault

Member Function Documentation

◆ load()

void JsonDocument::load ( const std::string &  file_name)
overridevirtual

Loads models from disk. If models have some data already, it will be rewritten.

Implements ModelView::ModelDocumentInterface.

Definition at line 59 of file jsondocument.cpp.

60 {
61  QFile file(QString::fromStdString(file_name));
62  if (!file.open(QIODevice::ReadOnly))
63  throw std::runtime_error("Error in JsonDocument: can't read the file '" + file_name + "'");
64 
65  auto document = QJsonDocument::fromJson(file.readAll());
66  auto array = document.array();
67  if (array.size() != static_cast<int>(p_impl->models.size())) {
68  std::ostringstream ostr;
69  ostr << "Error in JsonDocument: number of application models " << p_impl->models.size()
70  << " and number of json models " << array.size() << " doesn't match";
71  throw std::runtime_error(ostr.str());
72  }
73 
74  auto converter = ModelView::CreateModelProjectConverter();
75  int index(0);
76  for (auto model : p_impl->models) {
77  converter->from_json(array.at(index).toObject(), *model);
78  ++index;
79  }
80 
81  file.close();
82 }
MVVM_MODEL_EXPORT std::unique_ptr< JsonModelConverterInterface > CreateModelProjectConverter()
Creates a JSON model converter intended for save/load of the project on disk.

References ModelView::CreateModelProjectConverter(), and p_impl.

Here is the call graph for this function:

◆ save()

void JsonDocument::save ( const std::string &  file_name) const
overridevirtual

Saves models on disk.

Implements ModelView::ModelDocumentInterface.

Definition at line 38 of file jsondocument.cpp.

39 {
40  auto converter = ModelView::CreateModelProjectConverter();
41  QJsonArray array;
42 
43  for (auto model : p_impl->models)
44  array.push_back(converter->to_json(*model));
45 
46  QJsonDocument document(array);
47  QFile file(QString::fromStdString(file_name));
48 
49  if (!file.open(QIODevice::WriteOnly))
50  throw std::runtime_error("Error in JsonDocument: can't save the file '" + file_name + "'");
51 
52  file.write(document.toJson());
53 
54  file.close();
55 }

References ModelView::CreateModelProjectConverter(), and p_impl.

Referenced by TEST_F().

Here is the call graph for this function:

Member Data Documentation

◆ p_impl

std::unique_ptr<JsonDocumentImpl> ModelView::JsonDocument::p_impl
private

Definition at line 39 of file jsondocument.h.

Referenced by load(), and save().


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