BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
commandservice.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/commands/commandservice.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 
21 #include "mvvm/model/sessionitem.h"
23 #include <stdexcept>
24 
25 using namespace ModelView;
26 
27 CommandService::CommandService(SessionModel* model) : m_model(model), m_pause_record(false) {}
28 
30 {
31  if (value)
32  m_commands = std::make_unique<UndoStack>();
33  else
34  m_commands.reset();
35 }
36 
38  const TagRow& tagrow)
39 {
40  if (!parent)
41  parent = m_model->rootItem();
42 
43  int actual_row = tagrow.row < 0 ? parent->itemCount(tagrow.tag) : tagrow.row;
44 
45  return std::get<SessionItem*>(
46  process_command<InsertNewItemCommand>(func, parent, TagRow{tagrow.tag, actual_row}));
47 }
48 
50  const TagRow& tagrow)
51 {
52  if (!item)
53  return nullptr;
54 
55  if (parent->model() != m_model)
56  throw std::runtime_error(
57  "CommandService::copyItem() -> Item doesn't belong to given model");
58 
59  int actual_row = tagrow.row < 0 ? parent->itemCount(tagrow.tag) : tagrow.row;
60 
61  return std::get<SessionItem*>(
62  process_command<CopyItemCommand>(item, parent, TagRow{tagrow.tag, actual_row}));
63 }
64 
65 bool CommandService::setData(SessionItem* item, const Variant& value, int role)
66 {
67  if (!item)
68  return false;
69 
70  return std::get<bool>(process_command<SetValueCommand>(item, value, role));
71 }
72 
73 void CommandService::removeItem(SessionItem* parent, const TagRow& tagrow)
74 {
75  if (parent->model() != m_model)
76  throw std::runtime_error(
77  "CommandService::removeRow() -> Item doesn't belong to given model");
78 
79  process_command<RemoveItemCommand>(parent, tagrow);
80 }
81 
82 void CommandService::moveItem(SessionItem* item, SessionItem* new_parent, const TagRow& tagrow)
83 {
84  if (item->model() != m_model)
85  throw std::runtime_error(
86  "CommandService::removeRow() -> Item doesn't belong to given model");
87 
88  if (new_parent->model() != m_model)
89  throw std::runtime_error(
90  "CommandService::removeRow() -> Parent doesn't belong to given model");
91 
92  int actual_row = tagrow.row < 0 ? new_parent->itemCount(tagrow.tag) : tagrow.row;
93 
94  process_command<MoveItemCommand>(item, new_parent, TagRow{tagrow.tag, actual_row});
95 }
96 
98 {
99  return m_commands.get();
100 }
101 
103 {
104  m_pause_record = value;
105 }
106 
108 {
109  return m_commands && !m_pause_record;
110 }
bool setData(SessionItem *item, const Variant &value, int role)
void moveItem(SessionItem *item, SessionItem *new_parent, const TagRow &tagrow)
SessionItem * copyItem(const SessionItem *item, SessionItem *parent, const TagRow &tagrow)
void setCommandRecordPause(bool value)
SessionItem * insertNewItem(const item_factory_func_t &func, SessionItem *parent, const TagRow &tagrow)
void removeItem(SessionItem *parent, const TagRow &tagrow)
void setUndoRedoEnabled(bool value)
CommandService(SessionModel *model)
UndoStackInterface * undoStack() const
std::unique_ptr< UndoStackInterface > m_commands
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.
SessionModel * model() const
Returns the model to which given item belongs to.
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
SessionItem * rootItem() const
Returns root item of the model.
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
std::string tag
Definition: tagrow.h:27
Interface class for undo/redo stack.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
std::function< std::unique_ptr< SessionItem >()> item_factory_func_t
Definition for item factory funciton.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
QVariant Variant
Definition: variant.h:23