BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
undostack.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/undostack.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 
17 #include <QUndoStack>
18 
19 using namespace ModelView;
20 
22  std::unique_ptr<QUndoStack> m_undoStack;
23  UndoStackImpl() : m_undoStack(std::make_unique<QUndoStack>()) {}
24  QUndoStack* undoStack() { return m_undoStack.get(); }
25 };
26 
27 UndoStack::UndoStack() : p_impl(std::make_unique<UndoStackImpl>()) {}
28 
29 void UndoStack::execute(std::shared_ptr<AbstractItemCommand> command)
30 {
31  // Wrapping command for Qt. It will be executed by Qt after push.
32  auto adapter = new CommandAdapter(std::move(command));
33  p_impl->undoStack()->push(adapter);
34 }
35 
36 UndoStack::~UndoStack() = default;
37 
38 bool UndoStack::isActive() const
39 {
40  return p_impl->undoStack()->isActive();
41 }
42 
43 bool UndoStack::canUndo() const
44 {
45  return p_impl->undoStack()->canUndo();
46 }
47 
48 bool UndoStack::canRedo() const
49 {
50  return p_impl->undoStack()->canRedo();
51 }
52 
53 int UndoStack::index() const
54 {
55  return p_impl->undoStack()->index();
56 }
57 
58 int UndoStack::count() const
59 {
60  return p_impl->undoStack()->count();
61 }
62 
64 {
65  return p_impl->undoStack()->undo();
66 }
67 
69 {
70  return p_impl->undoStack()->redo();
71 }
72 
74 {
75  return p_impl->undoStack()->clear();
76 }
77 
78 void UndoStack::setUndoLimit(int limit)
79 {
80  return p_impl->undoStack()->setUndoLimit(limit);
81 }
82 
83 //! Returns underlying QUndoStack if given object can be casted to UndoStack instance.
84 //! This method is used to "convert" current instance to Qt implementation, and use it with other
85 //! Qt widgets, if necessary.
86 
87 QUndoStack* UndoStack::qtUndoStack(UndoStackInterface* stack_interface)
88 {
89  if (auto stack = dynamic_cast<UndoStack*>(stack_interface); stack)
90  return stack->p_impl->undoStack();
91  return nullptr;
92 }
93 
94 void UndoStack::beginMacro(const std::string& name)
95 {
96  p_impl->undoStack()->beginMacro(QString::fromStdString(name));
97 }
98 
100 {
101  p_impl->undoStack()->endMacro();
102 }
Adapter to execute our commands within Qt undo/redo framework.
Interface class for undo/redo stack.
Default undo stack implementation.
Definition: undostack.h:30
bool canRedo() const override
Definition: undostack.cpp:48
void beginMacro(const std::string &name) override
Definition: undostack.cpp:94
std::unique_ptr< UndoStackImpl > p_impl
Definition: undostack.h:54
void undo() override
Definition: undostack.cpp:63
int index() const override
Definition: undostack.cpp:53
void clear() override
Definition: undostack.cpp:73
void execute(std::shared_ptr< AbstractItemCommand > command) override
Executes the command, then pushes it in the stack for possible undo.
Definition: undostack.cpp:29
void setUndoLimit(int limit) override
Definition: undostack.cpp:78
void endMacro() override
Definition: undostack.cpp:99
static QUndoStack * qtUndoStack(UndoStackInterface *stack_interface)
Returns underlying QUndoStack if given object can be casted to UndoStack instance.
Definition: undostack.cpp:87
bool canUndo() const override
Definition: undostack.cpp:43
bool isActive() const override
Definition: undostack.cpp:38
int count() const override
Definition: undostack.cpp:58
void redo() override
Definition: undostack.cpp:68
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
QString const & name(EShape k)
Definition: particles.cpp:21
Definition: filesystem.h:81
std::unique_ptr< QUndoStack > m_undoStack
Definition: undostack.cpp:22
Defines class CLASS?