BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsonutils.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/jsonutils.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 "mvvm/utils/reallimits.h"
19 #include <QJsonDocument>
20 #include <QJsonObject>
21 #include <stdexcept>
22 
23 namespace {
24 const std::string text_limitless = "limitless";
25 const std::string text_positive = "positive";
26 const std::string text_nonnegative = "nonnegative";
27 const std::string text_lowerlimited = "lowerlimited";
28 const std::string text_upperlimited = "upperlimited";
29 const std::string text_limited = "limited";
30 const std::string separator = " ";
31 } // namespace
32 using namespace ModelView;
33 
35 {
36  auto converter = CreateModelCopyConverter();
37  QJsonObject json_source = converter->to_json(model);
38  QJsonDocument document(json_source);
39  return QString(document.toJson(QJsonDocument::Indented)).toStdString();
40 }
41 
42 std::string JsonUtils::ToString(const ModelView::RealLimits& limits)
43 {
44  if (limits.isLimitless())
45  return text_limitless;
46  else if (limits.isPositive())
47  return text_positive;
48  else if (limits.isNonnegative())
49  return text_nonnegative;
50  else if (limits.isLowerLimited())
51  return text_lowerlimited;
52  else if (limits.isUpperLimited())
53  return text_upperlimited;
54  else if (limits.isLimited())
55  return text_limited;
56  else
57  throw std::runtime_error("JsonUtils::ToString() -> Unknown type");
58 }
59 
60 RealLimits JsonUtils::CreateLimits(const std::string& text, double min, double max)
61 {
62  if (text == text_limitless)
63  return RealLimits();
64  else if (text == text_positive)
65  return RealLimits::positive();
66  else if (text == text_nonnegative)
67  return RealLimits::nonnegative();
68  else if (text == text_lowerlimited)
69  return RealLimits::lowerLimited(min);
70  else if (text == text_upperlimited)
71  return RealLimits::upperLimited(max);
72  else if (text == text_limited)
73  return RealLimits::limited(min, max);
74  else
75  throw std::runtime_error("JsonUtils::CreateLimits -> Unknown type");
76 }
Limits for double.
Definition: reallimits.h:25
bool isLimited() const
Definition: reallimits.cpp:151
static RealLimits upperLimited(double bound_value)
Creates an object bounded from the right.
Definition: reallimits.cpp:56
bool isLowerLimited() const
Definition: reallimits.cpp:141
bool isPositive() const
Definition: reallimits.cpp:130
static RealLimits lowerLimited(double bound_value)
Creates an object bounded from the left.
Definition: reallimits.cpp:41
static RealLimits positive()
Creates an object which can have only positive values (>0., zero is not included)
Definition: reallimits.cpp:46
bool isNonnegative() const
Definition: reallimits.cpp:136
bool isLimitless() const
Definition: reallimits.cpp:125
static RealLimits nonnegative()
Creates an object which can have only positive values with 0. included.
Definition: reallimits.cpp:51
bool isUpperLimited() const
Definition: reallimits.cpp:146
static RealLimits limited(double left_bound_value, double right_bound_value)
Creates an object bounded from the left and right.
Definition: reallimits.cpp:61
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
Defines class CLASS?
Defines class CLASS?
MVVM_MODEL_EXPORT RealLimits CreateLimits(const std::string &text, double min=0.0, double max=0.0)
Definition: jsonutils.cpp:60
MVVM_MODEL_EXPORT std::string ToString(const RealLimits &limits)
Returns string representation of RealLimits.
Definition: jsonutils.cpp:42
MVVM_MODEL_EXPORT std::string ModelToJsonString(const SessionModel &model)
Returns multiline string representing model content as json.
Definition: jsonutils.cpp:34
materialitems.h Collection of materials to populate MaterialModel.
MVVM_MODEL_EXPORT std::unique_ptr< JsonModelConverterInterface > CreateModelCopyConverter()
Creates a JSON model converter intended for model copying.
Defines class CLASS?
Defines class CLASS?