BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsontaginfoconverter.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/jsontaginfoconverter.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/taginfo.h"
17 #include <QJsonArray>
18 #include <QJsonObject>
19 #include <QStringList>
20 #include <stdexcept>
21 
22 using namespace ModelView;
23 
24 namespace {
25 QStringList expected_taginfo_keys()
26 {
27  QStringList result = QStringList()
30  std::sort(result.begin(), result.end());
31  return result;
32 }
33 } // namespace
34 
36 {
37  QJsonObject result;
38  result[nameKey] = QString::fromStdString(tag.name());
39  result[minKey] = tag.min();
40  result[maxKey] = tag.max();
41  QJsonArray str_array;
42  for (const auto& str : tag.modelTypes())
43  str_array.append(QString::fromStdString(str));
44  result[modelsKey] = str_array;
45 
46  return result;
47 }
48 
49 TagInfo JsonTagInfoConverter::from_json(const QJsonObject& object)
50 {
51  if (!isTagInfo(object))
52  throw std::runtime_error("JsonTagInfo::get_tags() -> Invalid json object.");
53 
54  auto name = object[nameKey].toString().toStdString();
55  auto min = object[minKey].toInt();
56  auto max = object[maxKey].toInt();
57  std::vector<std::string> models;
58  for (const auto ref : object[modelsKey].toArray())
59  models.push_back(ref.toString().toStdString());
60 
61  return TagInfo(name, min, max, models);
62 }
63 
64 //! Returns true if given json object represents TagInfo object.
65 
66 bool JsonTagInfoConverter::isTagInfo(const QJsonObject& object)
67 {
68  static const QStringList expected = expected_taginfo_keys();
69 
70  if (object.keys() != expected)
71  return false;
72 
73  if (!object[modelsKey].isArray())
74  return false;
75 
76  return true;
77 }
QJsonObject to_json(const TagInfo &tag) override
TagInfo from_json(const QJsonObject &object) override
bool isTagInfo(const QJsonObject &object)
Returns true if given json object represents TagInfo object.
Holds info about single tag for SessionItem.
Definition: taginfo.h:28
std::string name() const
Definition: taginfo.cpp:45
int max() const
Definition: taginfo.cpp:55
std::vector< std::string > modelTypes() const
Definition: taginfo.cpp:60
int min() const
Definition: taginfo.cpp:50
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
QString const & name(EShape k)
Definition: particles.cpp:21
Defines class CLASS?