BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
modelutils.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/model/modelutils.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 
15 #include "mvvm/model/modelutils.h"
17 #include "mvvm/model/path.h"
18 #include <QJsonObject>
19 
20 using namespace ModelView;
21 
23 {
24  if (!item || !item->model())
25  return {};
26 
27  Path result;
28  const SessionItem* current(item);
29  while (current && current->parent()) {
30  result.prepend(Utils::IndexOfChild(current->parent(), current));
31  current = current->parent();
32  }
33  return result;
34 }
35 
36 SessionItem* Utils::ItemFromPath(const SessionModel& model, const Path& path)
37 {
38  SessionItem* result(model.rootItem());
39  for (const auto& x : path) {
40  result = Utils::ChildAt(result, x);
41  if (!result)
42  break;
43  }
44  return result;
45 }
46 
48  const SessionModel& source, SessionModel& target)
49 {
50  QJsonObject object = converter->to_json(source);
51  converter->from_json(object, target);
52 }
53 
55 {
56  auto model = item->model();
57  if (!model)
58  return;
59 
60  model->removeItem(item->parent(), item->tagRow());
61 }
62 
64 {
65  auto tagrow = item->tagRow();
66  if (tagrow.row == 0)
67  return; // item already at the top
68  item->model()->moveItem(item, item->parent(), tagrow.prev());
69 }
70 
72 {
73  auto tagrow = item->tagRow();
74  if (tagrow.row == item->parent()->itemCount(tagrow.tag) - 1)
75  return; // item already at the buttom
76  item->model()->moveItem(item, item->parent(), tagrow.next());
77 }
78 
80 {
81  if (auto stack = model.undoStack(); stack)
82  stack->undo();
83 }
84 
86 {
87  if (auto stack = model.undoStack(); stack)
88  stack->redo();
89 }
90 
91 void Utils::BeginMacros(const SessionItem* item, const std::string& macro_name)
92 {
93  if (!item->model())
94  return;
95 
96  if (auto stack = item->model()->undoStack(); stack)
97  stack->beginMacro(macro_name);
98 }
99 
100 void Utils::EndMacros(const SessionItem* item)
101 {
102  if (!item->model())
103  return;
104 
105  if (auto stack = item->model()->undoStack(); stack)
106  stack->endMacro();
107 }
Base class for all converters of SessionModel to/from json object.
virtual void from_json(const QJsonObject &, SessionModel &) const =0
virtual QJsonObject to_json(const SessionModel &) const =0
Supports navigation through SessionModel.
Definition: path.h:35
void prepend(PathElement element)
Definition: path.cpp:62
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
int itemCount(const std::string &tag) const
Returns number of items in given tag.
SessionItem * parent() const
Returns parent item. Will return nullptr if item doesn't have a parent.
SessionModel * model() const
Returns the model to which given item belongs to.
TagRow tagRow() const
Returns TagRow of this item under which it is accessible through its parent.
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
void moveItem(SessionItem *item, SessionItem *new_parent, const TagRow &tagrow)
Move item from it's current parent to a new parent under given tag and row.
SessionItem * rootItem() const
Returns root item of the model.
UndoStackInterface * undoStack() const
Returns command stack to perform undo/redo.
void removeItem(SessionItem *parent, const TagRow &tagrow)
Removes given row from parent.
Defines class CLASS?
MVVM_MODEL_EXPORT void EndMacros(const SessionItem *item)
Finishes undo/redo macros.
Definition: modelutils.cpp:100
MVVM_MODEL_EXPORT void MoveDown(SessionItem *item)
Moves item down (increments row of the item). Works on children belonging to single tag.
Definition: modelutils.cpp:71
MVVM_MODEL_EXPORT void BeginMacros(const SessionItem *item, const std::string &macro_name)
Begin undo/redo macros with given name.
Definition: modelutils.cpp:91
MVVM_MODEL_EXPORT int IndexOfChild(const SessionItem *parent, const SessionItem *child)
Returns index in children array corresponding to given child.
Definition: itemutils.cpp:81
MVVM_MODEL_EXPORT void MoveUp(SessionItem *item)
Moves item up (decrements row of the item). Works on children belonging to single tag.
Definition: modelutils.cpp:63
MVVM_MODEL_EXPORT SessionItem * ItemFromPath(const SessionModel &moodel, const Path &path)
Returns item found in the model following given Path.
Definition: modelutils.cpp:36
MVVM_MODEL_EXPORT void Undo(SessionModel &model)
Undo last model operation. If not undo/redo enabled, will do nothing.
Definition: modelutils.cpp:79
MVVM_MODEL_EXPORT Path PathFromItem(const SessionItem *item)
Constructs path to find given item. Item must belong to a model.
Definition: modelutils.cpp:22
MVVM_MODEL_EXPORT void Redo(SessionModel &model)
Redo model operation which was undone just before. If not undo/redo enabled, will do nothing.
Definition: modelutils.cpp:85
MVVM_MODEL_EXPORT SessionItem * ChildAt(const SessionItem *parent, int index)
Returns child at given index of parent.
Definition: itemutils.cpp:70
MVVM_MODEL_EXPORT void PopulateEmptyModel(const JsonModelConverterInterface *converter, const SessionModel &source, SessionModel &target)
Populate empty model with content of target model using provided converter.
Definition: modelutils.cpp:47
MVVM_MODEL_EXPORT void DeleteItemFromModel(SessionItem *item)
Removes and deletes item from its model.
Definition: modelutils.cpp:54
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
Defines class CLASS?