BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsonitemdataconverter.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/jsonitemdataconverter.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 
16 #include "mvvm/model/mvvm_types.h"
20 #include <QJsonArray>
21 #include <QJsonObject>
22 #include <set>
23 #include <stdexcept>
24 
25 using namespace ModelView;
26 
27 namespace {
28 QJsonValue keyValue(const QJsonValue& parent_value, const QString& key)
29 {
30  const QJsonObject& parent_object = parent_value.toObject();
31  return parent_object.value(key);
32 }
33 } // namespace
34 
36  accept_strategy_t from_json_accept)
37  : m_to_json_accept(to_json_accept)
38  , m_from_json_accept(from_json_accept)
39  , m_variant_converter(std::make_unique<JsonVariantConverter>())
40 {
41 }
42 
44 
46 {
47  QJsonArray result;
48 
49  for (const auto& x : data) {
50  QJsonObject object;
51  if (isRoleToJson(x.m_role)) {
52  object[JsonItemFormatAssistant::roleKey] = x.m_role;
53  object[JsonItemFormatAssistant::variantKey] = m_variant_converter->get_json(x.m_data);
54  result.append(object);
55  }
56  }
57 
58  return result;
59 }
60 
61 //! Updates existing data with JSON content.
62 
63 void JsonItemDataConverter::from_json(const QJsonArray& object, SessionItemData& data)
64 {
65  static JsonItemFormatAssistant assistant;
66  auto persistent_data = std::make_unique<SessionItemData>();
67 
68  for (const auto& x : object) {
69  if (!assistant.isSessionItemData(x.toObject()))
70  throw std::runtime_error("JsonItemData::get_data() -> Invalid json object.");
71  auto role = keyValue(x, JsonItemFormatAssistant::roleKey).toInt();
72  auto variant = m_variant_converter->get_variant(
73  keyValue(x, JsonItemFormatAssistant::variantKey).toObject());
74  if (isRoleFromJson(role))
75  persistent_data->setData(variant, role);
76  }
77 
78  auto runtime_roles = data.roles();
79  auto persistent_roles = persistent_data->roles();
80 
81  std::set<int> roles(runtime_roles.begin(), runtime_roles.end());
82  roles.insert(persistent_roles.begin(), persistent_roles.end());
83 
84  for (auto role : roles) {
85  // all roles existing in `persistent` will be taken from there
86  if (persistent_data->hasData(role))
87  data.setData(persistent_data->data(role), role);
88  }
89 }
90 
91 //! Creates JSON data converter intended for simple data copying. Nothing is filtered out.
92 
93 std::unique_ptr<JsonItemDataConverterInterface> JsonItemDataConverter::createCopyConverter()
94 {
95  return std::make_unique<JsonItemDataConverter>();
96 }
97 
98 //! Creates JSON data converter intended for project saving. Only IDENTIFIER and DATA gous to/from
99 //! JSON.
100 
101 std::unique_ptr<JsonItemDataConverterInterface> JsonItemDataConverter::createProjectConverter()
102 {
103  auto accept_roles = [](auto role) {
104  return role == ItemDataRole::IDENTIFIER || role == ItemDataRole::DATA;
105  };
106  return std::make_unique<JsonItemDataConverter>(accept_roles, accept_roles);
107 }
108 
109 //! Returns true if given role should be saved in json object.
110 
112 {
113  return m_to_json_accept ? m_to_json_accept(role) : true;
114 }
115 
116 //! Returns true if given role should be parsed from json object.
117 
119 {
120  return m_from_json_accept ? m_from_json_accept(role) : true;
121 }
bool isRoleFromJson(int role) const
Returns true if given role should be parsed from json object.
void from_json(const QJsonArray &object, SessionItemData &data) override
Updates existing data with JSON content.
JsonItemDataConverter(accept_strategy_t to_json_accept={}, accept_strategy_t from_json_accept={})
std::function< bool(int)> accept_strategy_t
bool isRoleToJson(int role) const
Returns true if given role should be saved in json object.
static std::unique_ptr< JsonItemDataConverterInterface > createProjectConverter()
Creates JSON data converter intended for project saving.
QJsonArray to_json(const SessionItemData &data) override
Converts SessionItemData to JSON;.
static std::unique_ptr< JsonItemDataConverterInterface > createCopyConverter()
Creates JSON data converter intended for simple data copying. Nothing is filtered out.
std::unique_ptr< JsonVariantConverterInterface > m_variant_converter
accept_strategy_t m_from_json_accept
callback to find whether to read role from json
accept_strategy_t m_to_json_accept
callback to find whether to write role to json
Utility class to determine, whether given JSON object can represent various parts of SessionModel.
bool isSessionItemData(const QJsonObject &json) const
Default converter between supported variants and json objects.
Handles data roles for SessionItem.
std::vector< int > roles() const
bool setData(const Variant &value, int role)
Sets the data for given role.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
const int DATA
main data role
Definition: mvvm_types.h:30
const int IDENTIFIER
unique identifier
Definition: mvvm_types.h:29
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
Defines class CLASS?