fw4spl
SCommandHistory.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "ctrlHistory/SCommandHistory.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 #include <fwCom/Slot.hpp>
13 #include <fwCom/Slot.hxx>
14 #include <fwCom/Slots.hpp>
15 #include <fwCom/Slots.hxx>
16 
17 #include <fwData/mt/ObjectWriteLock.hpp>
18 
19 #include <fwDataCamp/exception/ObjectNotFound.hpp>
20 #include <fwDataCamp/getObject.hpp>
21 
22 #include <numeric>
23 
24 namespace ctrlHistory
25 {
26 
28 
29 static const ::fwCom::Signals::SignalKeyType s_CANUNDO_SIGNAL = "canUndo";
30 static const ::fwCom::Signals::SignalKeyType s_CANREDO_SIGNAL = "canRedo";
31 
32 static const ::fwCom::Slots::SlotKeyType s_ENQUEUE_SLOT = "enqueue";
33 static const ::fwCom::Slots::SlotKeyType s_UNDO_SLOT = "undo";
34 static const ::fwCom::Slots::SlotKeyType s_REDO_SLOT = "redo";
35 static const ::fwCom::Slots::SlotKeyType s_CLEAR_SLOT = "clear";
36 
37 //-----------------------------------------------------------------------------
38 
40 {
41  newSlot(s_ENQUEUE_SLOT, &SCommandHistory::enqueue, this);
42  newSlot(s_UNDO_SLOT, &SCommandHistory::undo, this);
43  newSlot(s_REDO_SLOT, &SCommandHistory::redo, this);
44  newSlot(s_CLEAR_SLOT, &SCommandHistory::clear, this);
45 
46  m_canUndoSig = newSignal< CanDoSignalType >( s_CANUNDO_SIGNAL );
47  m_canRedoSig = newSignal< CanDoSignalType >( s_CANREDO_SIGNAL );
48 }
49 
50 //-----------------------------------------------------------------------------
51 
53 {
54 }
55 
56 //-----------------------------------------------------------------------------
57 
59 {
60  ::fwServices::IService::ConfigType config = this->getConfigTree();
61 
62  auto maxCommands = config.get_optional< size_t >("maxCommands");
63  auto maxMemory = config.get_optional< size_t >("maxMemory");
64 
65  if(maxCommands.is_initialized())
66  {
67  m_undoRedoManager.setCommandCount(maxCommands.value());
68  }
69 
70  if(maxMemory.is_initialized())
71  {
72  m_undoRedoManager.setHistorySize(maxMemory.value());
73  }
74 
75 }
76 
77 //-----------------------------------------------------------------------------
78 
80 {
81  this->emitModifSig();
82 }
83 
84 //-----------------------------------------------------------------------------
85 
87 {
88  this->emitModifSig();
89 }
90 
91 //-----------------------------------------------------------------------------
92 
94 {
95  m_undoRedoManager.clear();
96 }
97 
98 //-----------------------------------------------------------------------------
99 
100 void SCommandHistory::enqueue(fwCommand::ICommand::sptr command)
101 {
102  m_undoRedoManager.enqueue(command);
103  this->emitModifSig();
104 }
105 
106 //-----------------------------------------------------------------------------
107 
108 void SCommandHistory::undo()
109 {
110  m_undoRedoManager.undo();
111  this->emitModifSig();
112 }
113 
114 //-----------------------------------------------------------------------------
115 
116 void SCommandHistory::redo()
117 {
118  m_undoRedoManager.redo();
119  this->emitModifSig();
120 }
121 
122 //-----------------------------------------------------------------------------
123 
124 void SCommandHistory::clear()
125 {
126  m_undoRedoManager.clear();
127  this->emitModifSig();
128 }
129 
130 //-----------------------------------------------------------------------------
131 
132 void SCommandHistory::emitModifSig() const
133 {
134  m_canUndoSig->asyncEmit(m_undoRedoManager.canUndo());
135  m_canRedoSig->asyncEmit(m_undoRedoManager.canRedo());
136 }
137 
138 //-----------------------------------------------------------------------------
139 
140 } // namespace ctrlHistory
Base class for all services.
Definition: IService.hpp:61
virtual CTRLHISTORY_API void configuring() override
Set memory and command boundaries.
virtual CTRLHISTORY_API ~SCommandHistory()
Destructor.
FWCOMMAND_API bool canUndo() const
Return true if we can undo.
FWCOMMAND_API bool enqueue(ICommand::sptr cmd)
Push a command to the history.
virtual CTRLHISTORY_API void updating() override
Notify if undo or redo are possible.
CTRLHISTORY_API SCommandHistory()
Constructor.
The namespace ctrlHistory contains services handling command histories.
FWCOMMAND_API void setCommandCount(size_t cmdCount)
Set the maximum number of enqueued commands.
FWCOMMAND_API bool undo()
Execute the previous command if any.
FWCOMMAND_API bool canRedo() const
Return true if we can redo.
Base class for each data object.
This service manages a command history. The history is modified when receiving "undo", "redo", "enqueue" or "clear" signal.
FWCOMMAND_API bool redo()
Execute the next command if any.
virtual CTRLHISTORY_API void stopping() override
Clears the history.
virtual CTRLHISTORY_API void starting() override
Notify if undo or redo are possible.
FWCOMMAND_API void clear()
Remove all commands in history.
FWCOMMAND_API void setHistorySize(size_t histSize)
Set the maximum amount of memory used by the history.
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247