BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsonmodelconverter.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/jsonmodelconverter.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 
17 #include "mvvm/model/sessionitem.h"
22 #include <QJsonArray>
23 #include <QJsonObject>
24 #include <stdexcept>
25 
26 using namespace ModelView;
27 
28 namespace {
29 std::unique_ptr<JsonItemConverterInterface> CreateConverter(const ItemFactoryInterface* factory,
30  ConverterMode mode)
31 {
32  if (mode == ConverterMode::clone)
33  return CreateItemCloneConverter(factory);
34  else if (mode == ConverterMode::copy)
35  return CreateItemCopyConverter(factory);
36  else if (mode == ConverterMode::project)
37  return CreateItemProjectConverter(factory);
38  else
39  throw std::runtime_error("Error in JsonModelConverter: unknown converter mode");
40 }
41 
42 } // namespace
43 
45 
47 
48 QJsonObject JsonModelConverter::to_json(const SessionModel& model) const
49 {
50  QJsonObject result;
51 
52  if (!model.rootItem())
53  throw std::runtime_error("JsonModel::to_json() -> Error. Model is not initialized.");
54 
55  result[JsonItemFormatAssistant::sessionModelKey] = QString::fromStdString(model.modelType());
56 
57  QJsonArray itemArray;
58 
59  auto itemConverter = CreateConverter(model.factory(), m_mode);
60 
61  for (auto item : model.rootItem()->children())
62  itemArray.append(itemConverter->to_json(item));
63 
64  result[JsonItemFormatAssistant::itemsKey] = itemArray;
65 
66  return result;
67 }
68 
69 void JsonModelConverter::from_json(const QJsonObject& json, SessionModel& model) const
70 {
71  if (!model.rootItem())
72  throw std::runtime_error("JsonModel::json_to_model() -> Error. Model is not initialized.");
73 
74  JsonItemFormatAssistant assistant;
75  if (!assistant.isSessionModel(json))
76  throw std::runtime_error("JsonModel::json_to_model() -> Error. Invalid json object.");
77 
79  != QString::fromStdString(model.modelType()))
80  throw std::runtime_error(
81  "JsonModel::json_to_model() -> Unexpected model type '" + model.modelType()
82  + "', json key '"
83  + json[JsonItemFormatAssistant::sessionModelKey].toString().toStdString() + "'");
84 
85  auto itemConverter = CreateConverter(model.factory(), m_mode);
86 
87  auto rebuild_root = [&json, &itemConverter](auto parent) {
88  for (const auto ref : json[JsonItemFormatAssistant::itemsKey].toArray()) {
89  auto item = itemConverter->from_json(ref.toObject());
90  parent->insertItem(item.release(), TagRow::append());
91  }
92  };
93  model.clear(rebuild_root);
94 }
Interface class for all factories capable of producing SessionItem's.
Utility class to determine, whether given JSON object can represent various parts of SessionModel.
bool isSessionModel(const QJsonObject &object) const
Returns true if given json object represents SessionModel.
JsonModelConverter(ConverterMode mode)
void from_json(const QJsonObject &json, SessionModel &model) const override
Reads json object and build the model.
QJsonObject to_json(const SessionModel &model) const override
Writes content of model into json.
std::vector< SessionItem * > children() const
Returns vector of children formed from all chidlren from all tags.
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
std::string modelType() const
Returns model type.
const ItemFactoryInterface * factory() const
Returns item factory which can generate all items supported by this model.
SessionItem * rootItem() const
Returns root item of the model.
void clear(std::function< void(SessionItem *)> callback={})
Removes all items from the model.
static TagRow append(const std::string &tag_name={})
Returns TagRow corresponding to the append to tag_name.
Definition: tagrow.cpp:36
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
std::unique_ptr< JsonItemConverterInterface > CreateItemProjectConverter(const ItemFactoryInterface *item_factory)
Creates JSON item converter intended for saving on disk.
std::unique_ptr< JsonItemConverterInterface > CreateItemCopyConverter(const ItemFactoryInterface *item_factory)
Creates JSON item converter intended for item copying.
ConverterMode
Flags to define converter behavior on the way from SessionItem to JSON and back.
@ copy
full deep copying with item identifiers regenerated
@ project
selective copying for saving/loading the project (tags and data created by item, updated from JSON)
@ clone
full deep copying with item identifiers preserved
std::unique_ptr< JsonItemConverterInterface > CreateItemCloneConverter(const ItemFactoryInterface *item_factory)
Creates JSON item converter intended for item cloning.
std::string toString(PyObject *obj)
Converts PyObject into string, if possible, or throws exception.
Definition: PyUtils.cpp:24
Defines class CLASS?
Defines class CLASS?