BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
commandservice.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/commands/commandservice.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_COMMANDS_COMMANDSERVICE_H
16 #define BORNAGAIN_MVVM_MODEL_MVVM_COMMANDS_COMMANDSERVICE_H
17 
20 #include "mvvm/core/variant.h"
22 #include "mvvm/model_export.h"
23 #include <memory>
24 
25 class QUndoCommand;
26 
27 namespace ModelView {
28 
29 class SessionModel;
30 class SessionItem;
31 class TagRow;
32 
33 //! Provides undo/redo for all commands of SessionModel.
34 
35 class MVVM_MODEL_EXPORT CommandService {
36 public:
38 
39  void setUndoRedoEnabled(bool value);
40 
41  SessionItem* insertNewItem(const item_factory_func_t& func, SessionItem* parent,
42  const TagRow& tagrow);
43 
44  SessionItem* copyItem(const SessionItem* item, SessionItem* parent, const TagRow& tagrow);
45 
46  bool setData(SessionItem* item, const Variant& value, int role);
47 
48  void removeItem(SessionItem* parent, const TagRow& tagrow);
49 
50  void moveItem(SessionItem* item, SessionItem* new_parent, const TagRow& tagrow);
51 
52  UndoStackInterface* undoStack() const;
53 
54  void setCommandRecordPause(bool value);
55 
56 private:
57  template <typename C, typename... Args> CommandResult process_command(Args&&... args);
58 
59  bool provideUndo() const;
60 
62  std::unique_ptr<UndoStackInterface> m_commands;
64 };
65 
66 //! Creates and processes command of given type using given argument list.
67 
68 template <typename C, typename... Args>
70 {
71  if (provideUndo()) {
72  // making shared because underlying QUndoStack requires ownership
73  auto command = std::make_shared<C>(std::forward<Args>(args)...);
74  m_commands->execute(command);
75  return command->result();
76  } else {
77  C command(std::forward<Args>(args)...);
78  command.execute();
79  return command.result();
80  }
81 }
82 
83 } // namespace ModelView
84 
85 #endif // BORNAGAIN_MVVM_MODEL_MVVM_COMMANDS_COMMANDSERVICE_H
Provides undo/redo for all commands of SessionModel.
CommandResult process_command(Args &&... args)
Creates and processes command of given type using given argument list.
std::unique_ptr< UndoStackInterface > m_commands
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
Interface class for undo/redo stack.
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.
std::variant< bool, ModelView::SessionItem * > CommandResult
Results of command execution.
Definition: commandresult.h:25
Defines class CLASS?
Defines class CLASS?
QVariant Variant
Definition: variant.h:23