BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsondocument.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/serialization/jsondocument.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
18 #include <QFile>
19 #include <QJsonArray>
20 #include <QJsonDocument>
21 #include <QJsonObject>
22 #include <sstream>
23 #include <stdexcept>
24 
25 using namespace ModelView;
26 
28  std::vector<SessionModel*> models;
29  JsonDocumentImpl(const std::vector<SessionModel*>& models) : models(models) {}
30 };
31 
32 JsonDocument::JsonDocument(const std::vector<SessionModel*>& models)
33  : p_impl(std::make_unique<JsonDocumentImpl>(models))
34 {
35 }
36 
37 //! Saves models on disk.
38 void JsonDocument::save(const std::string& file_name) const
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 }
56 
57 //! Loads models from disk. If models have some data already, it will be rewritten.
58 
59 void JsonDocument::load(const std::string& file_name)
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 }
83 
84 JsonDocument::~JsonDocument() = default;
void save(const std::string &file_name) const override
Saves models on disk.
void load(const std::string &file_name) override
Loads models from disk. If models have some data already, it will be rewritten.
std::unique_ptr< JsonDocumentImpl > p_impl
Definition: jsondocument.h:38
JsonDocument(const std::vector< SessionModel * > &models)
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
MVVM_MODEL_EXPORT std::unique_ptr< JsonModelConverterInterface > CreateModelProjectConverter()
Creates a JSON model converter intended for save/load of the project on disk.
Definition: filesystem.h:81
Defines class CLASS?
JsonDocumentImpl(const std::vector< SessionModel * > &models)
std::vector< SessionModel * > models