CommandHistory.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 /*
16  This is a modified version of a source file from the Rosegarden
17  MIDI and audio sequencer and notation editor, copyright 2000-2006
18  Chris Cannam, distributed under the GNU General Public License.
19 
20  This file contains traces of the KCommandHistory class from the KDE
21  project, copyright 2000 Werner Trobin and David Faure and
22  distributed under the GNU Lesser General Public License.
23 */
24 
25 #ifndef SV_MULTI_VIEW_COMMAND_HISTORY_H
26 #define SV_MULTI_VIEW_COMMAND_HISTORY_H
27 
28 #include <QObject>
29 #include <QString>
30 
31 #include <stack>
32 #include <set>
33 #include <map>
34 
35 class Command;
36 class MacroCommand;
37 class QAction;
38 class QMenu;
39 class QToolBar;
40 class QTimer;
41 
52 class CommandHistory : public QObject
53 {
54  Q_OBJECT
55 
56 public:
57  virtual ~CommandHistory();
58 
59  static CommandHistory *getInstance();
60 
61  void clear();
62 
63  void registerMenu(QMenu *menu);
64  void registerToolbar(QToolBar *toolbar);
65 
74  void addCommand(Command *command);
75 
98  void addCommand(Command *command, bool execute, bool bundle = false);
99 
101  int getUndoLimit() const { return m_undoLimit; }
102 
104  void setUndoLimit(int limit);
105 
107  int getRedoLimit() const { return m_redoLimit; }
108 
110  void setRedoLimit(int limit);
111 
113  int getMenuLimit() const { return m_menuLimit; }
114 
116  void setMenuLimit(int limit);
117 
119  int getBundleTimeout() const { return m_bundleTimeout; }
120 
122  void setBundleTimeout(int msec);
123 
125  void startCompoundOperation(QString name, bool execute);
126 
128  void endCompoundOperation();
129 
130 public slots:
137  virtual void documentSaved();
138 
143  void addExecutedCommand(Command *);
144 
149  void addCommandAndExecute(Command *);
150 
151  void undo();
152  void redo();
153 
154 protected slots:
155  void undoActivated(QAction *);
156  void redoActivated(QAction *);
157  void bundleTimerTimeout();
158 
159 signals:
164  void commandExecuted();
165 
170  void commandExecuted(Command *);
171 
176  void commandUnexecuted(Command *);
177 
182  void documentRestored();
183 
187  void activity(QString);
188 
189 protected:
190  CommandHistory();
192 
193  QAction *m_undoAction;
194  QAction *m_redoAction;
197  QMenu *m_undoMenu;
198  QMenu *m_redoMenu;
199 
200  std::map<QAction *, int> m_actionCounts;
201 
202  typedef std::stack<Command *> CommandStack;
203  CommandStack m_undoStack;
204  CommandStack m_redoStack;
205 
210 
211  MacroCommand *m_currentCompound;
213  void addToCompound(Command *command, bool execute);
214 
215  MacroCommand *m_currentBundle;
218  QTimer *m_bundleTimer;
220  void addToBundle(Command *command, bool execute);
221  void closeBundle();
222 
223  void updateActions();
224 
225  void clipCommands();
226 
227  void clipStack(CommandStack &stack, int limit);
228  void clearStack(CommandStack &stack);
229 };
230 
231 
232 #endif
void addToCompound(Command *command, bool execute)
void setBundleTimeout(int msec)
Set the time after which a bundle will be closed if nothing is added.
void addExecutedCommand(Command *)
Add a command to the history that has already been executed, without executing it again...
QAction * m_redoAction
void addCommandAndExecute(Command *)
Add a command to the history and also execute it.
CommandStack m_undoStack
void startCompoundOperation(QString name, bool execute)
Start recording commands to batch up into a single compound command.
void endCompoundOperation()
Finish recording commands and store the compound command.
std::stack< Command * > CommandStack
void addCommand(Command *command)
Add a command to the command history.
void setUndoLimit(int limit)
Set the maximum number of items in the undo history.
void activity(QString)
Emitted when some activity happened (for activity logging).
void setMenuLimit(int limit)
Set the maximum number of items in the menus.
QAction * m_redoMenuAction
QAction * m_undoMenuAction
void documentRestored()
Emitted when the undo/redo stack has reached the same state at which the documentSaved slot was last ...
void registerToolbar(QToolBar *toolbar)
void commandUnexecuted(Command *)
Emitted whenever a command has just been unexecuted, whether by addCommand or undo.
QTimer * m_bundleTimer
void redoActivated(QAction *)
CommandStack m_redoStack
std::map< QAction *, int > m_actionCounts
int getUndoLimit() const
Return the maximum number of items in the undo history.
int getRedoLimit() const
Return the maximum number of items in the redo history.
void undoActivated(QAction *)
The CommandHistory class stores a list of executed commands and maintains Undo and Redo actions synch...
static CommandHistory * getInstance()
QAction * m_undoAction
int getMenuLimit() const
Return the maximum number of items visible in undo and redo menus.
void registerMenu(QMenu *menu)
static CommandHistory * m_instance
int getBundleTimeout() const
Return the time after which a bundle will be closed if nothing is added.
void setRedoLimit(int limit)
Set the maximum number of items in the redo history.
void commandExecuted()
Emitted whenever a command has just been executed or unexecuted, whether by addCommand, undo, or redo.
virtual ~CommandHistory()
void clipStack(CommandStack &stack, int limit)
void addToBundle(Command *command, bool execute)
MacroCommand * m_currentBundle
virtual void documentSaved()
Checkpoint function that should be called when the document is saved.
MacroCommand * m_currentCompound
QString m_currentBundleName
void clearStack(CommandStack &stack)