BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsonitem_types.h
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/jsonitem_types.h
6 //! @brief Defines 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 
15 #ifndef BORNAGAIN_MVVM_MODEL_MVVM_SERIALIZATION_JSONITEM_TYPES_H
16 #define BORNAGAIN_MVVM_MODEL_MVVM_SERIALIZATION_JSONITEM_TYPES_H
17 
18 //! @file mvvm/model/mvvm/serialization/jsonitem_types.h
19 //! Collection of custom types involved into SessionItem and JSON mutual convertion.
20 
21 #include "mvvm/model_export.h"
22 #include <functional>
23 #include <memory>
24 #include <vector>
25 
26 class QJsonObject;
27 
28 namespace ModelView {
29 
30 class SessionItem;
31 class ItemFactoryInterface;
32 
33 //! Provides necessary callbacks to convert SessionItem to JSON and back.
34 
35 struct MVVM_MODEL_EXPORT ConverterCallbacks {
36  using create_json_t = std::function<QJsonObject(const SessionItem&)>;
37  using create_item_t = std::function<std::unique_ptr<SessionItem>(const QJsonObject&)>;
38  using update_item_t = std::function<void(const QJsonObject&, SessionItem*)>;
39 
40  create_json_t m_create_json; //! creates JSON object from session item
41  create_item_t m_create_item; //! creates new SessionItem from JSON object
42  update_item_t m_update_item; //! updates existing SessionItem from JSON object
43 };
44 
45 //! Flags to define converter behavior on the way from SessionItem to JSON and back.
46 
47 enum class ConverterMode {
48  none, //!< undefined converter mode
49  clone, //!< full deep copying with item identifiers preserved
50  copy, //!< full deep copying with item identifiers regenerated
51  project //!< selective copying for saving/loading the project (tags and data created by item,
52  //!< updated from JSON)
53 };
54 
55 //! Returns true if given mode requires ID regeneration instead of using the one stored in JSON.
57 {
58  return mode == ConverterMode::copy;
59 }
60 
61 //! Returns true if item content should be reconstructed from JSON
63 {
64  return mode != ConverterMode::project;
65 }
66 
67 //! Collection of input paramters for SessionItemConverter
68 
69 struct MVVM_MODEL_EXPORT ConverterContext {
70  const ItemFactoryInterface* m_factory{nullptr};
72 };
73 
74 } // namespace ModelView
75 
76 #endif // BORNAGAIN_MVVM_MODEL_MVVM_SERIALIZATION_JSONITEM_TYPES_H
Interface class for all factories capable of producing SessionItem's.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
materialitems.h Collection of materials to populate MaterialModel.
bool isRebuildItemDataAndTagFromJson(ConverterMode mode)
Returns true if item content should be reconstructed from JSON.
bool isRegenerateIdWhenBackFromJson(ConverterMode mode)
Returns true if given mode requires ID regeneration instead of using the one stored in JSON.
ConverterMode
Flags to define converter behavior on the way from SessionItem to JSON and back.
@ copy
full deep copying with item identifiers regenerated
@ none
undefined converter mode
@ 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
Provides necessary callbacks to convert SessionItem to JSON and back.
update_item_t m_update_item
creates new SessionItem from JSON object
std::function< std::unique_ptr< SessionItem >(const QJsonObject &)> create_item_t
std::function< QJsonObject(const SessionItem &)> create_json_t
create_item_t m_create_item
creates JSON object from session item
std::function< void(const QJsonObject &, SessionItem *)> update_item_t
Collection of input paramters for SessionItemConverter.