BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
abstractitemcommand.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/abstractitemcommand.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/modelutils.h"
17 #include "mvvm/model/path.h"
18 #include "mvvm/model/sessionitem.h"
20 #include <stdexcept>
21 
22 using namespace ModelView;
23 
25  enum class Status { initial, after_execute, after_undo };
26  bool m_isObsolete{false};
27  std::string m_text;
28  Status m_status{Status::initial};
29  SessionModel* m_model{nullptr};
33 
34  void set_after_execute() { m_status = Status::after_execute; }
35  void set_after_undo() { m_status = Status::after_undo; }
36  bool can_execute() const { return m_status != Status::after_execute; }
37  bool can_undo() const { return m_status == Status::after_execute && !m_self->isObsolete(); }
38 };
39 
41  : p_impl(std::make_unique<AbstractItemCommand::AbstractItemCommandImpl>(this))
42 {
43  if (!receiver)
44  throw std::runtime_error("Invalid item.");
45 
46  if (!receiver->model())
47  throw std::runtime_error("Item doesn't have a model");
48 
49  p_impl->m_model = receiver->model();
50 }
51 
53 
54 //! Execute command.
55 
57 {
58  if (!p_impl->can_execute())
59  throw std::runtime_error("Can't execute the command. Wrong order.");
60 
62 
63  p_impl->set_after_execute();
64 }
65 
66 //! Undo command as it was before execution.
67 
69 {
70  if (!p_impl->can_undo())
71  throw std::runtime_error("Can't undo the command. Wrong order.");
72 
73  undo_command();
74 
75  p_impl->set_after_undo();
76 }
77 
78 //! Returns whether the command is obsolete (which means that it shouldn't be kept in the stack).
79 
81 {
82  return p_impl->m_isObsolete;
83 }
84 
85 //! Returns command description.
86 
88 {
89  return p_impl->m_text;
90 }
91 
93 {
94  return p_impl->m_result;
95 }
96 
97 //! Sets command obsolete flag.
98 
100 {
101  p_impl->m_isObsolete = flag;
102 }
103 
104 //! Sets command description.
105 
106 void AbstractItemCommand::setDescription(const std::string& text)
107 {
108  p_impl->m_text = text;
109 }
110 
112 {
113  return Utils::PathFromItem(item);
114 }
115 
117 {
118  return Utils::ItemFromPath(*p_impl->m_model, path);
119 }
120 
122 {
123  return p_impl->m_model;
124 }
125 
127 {
128  p_impl->m_result = command_result;
129 }
Defines class CLASS?
Abstract command interface to manipulate SessionItem in model context.
virtual void execute_command()=0
SessionItem * itemFromPath(const Path &path) const
std::string description() const
Returns command description.
void setDescription(const std::string &text)
Sets command description.
AbstractItemCommand(SessionItem *receiver)
void undo()
Undo command as it was before execution.
std::unique_ptr< AbstractItemCommandImpl > p_impl
void setResult(const CommandResult &command_result)
Path pathFromItem(SessionItem *item) const
bool isObsolete() const
Returns whether the command is obsolete (which means that it shouldn't be kept in the stack).
void setObsolete(bool flag)
Sets command obsolete flag.
Supports navigation through SessionModel.
Definition: path.h:35
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
SessionModel * model() const
Returns the model to which given item belongs to.
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
Defines class CLASS?
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 Path PathFromItem(const SessionItem *item)
Constructs path to find given item. Item must belong to a model.
Definition: modelutils.cpp:22
materialitems.h Collection of materials to populate MaterialModel.
std::variant< bool, ModelView::SessionItem * > CommandResult
Results of command execution.
Definition: commandresult.h:25
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?