42 class K3NamedCommand::Private
70 class K3MacroCommand::Private
84 qDeleteAll( d->commands );
90 d->commands.append(command);
95 QListIterator<K3Command *> it( d->commands );
96 while ( it.hasNext() ) {
103 QListIterator<K3Command *> it( d->commands );
105 while ( it.hasPrevious() ) {
106 it.previous()->unexecute();
116 class K3CommandHistory::K3CommandHistoryPrivate {
118 K3CommandHistoryPrivate()
119 : m_undoLimit(50), m_redoLimit(30),
120 m_savedAt(-1), m_current(-1) {
122 ~K3CommandHistoryPrivate() {
123 qDeleteAll( m_commands );
127 int m_undoLimit, m_redoLimit;
150 d( new K3CommandHistoryPrivate )
156 d( new K3CommandHistoryPrivate )
181 qDeleteAll( d->m_commands );
182 d->m_commands.clear();
193 d->m_commands.insert( d->m_current, command );
195 int count = d->m_commands.count();
196 for (
int i = d->m_current + 1; i < count; ++i )
197 delete d->m_commands.takeLast();
200 if ( d->m_current < d->m_savedAt )
214 if ( d->m_current >= 0 )
215 return d->m_commands[ d->m_current ];
220 Q_ASSERT( d->m_current >= 0 );
222 K3Command* command = d->m_commands[ d->m_current ];
229 if ( d->m_current == d->m_savedAt )
236 K3Command* command = d->m_commands[ d->m_current + 1 ];
242 if ( d->m_current == d->m_savedAt )
249 d->m_savedAt = d->m_current;
253 if ( limit>0 && limit != d->m_undoLimit ) {
254 d->m_undoLimit = limit;
260 if ( limit>0 && limit != d->m_redoLimit ) {
261 d->m_redoLimit = limit;
266 void K3CommandHistory::clipCommands() {
267 int count = d->m_commands.count();
268 if ( count <= d->m_undoLimit && count <= d->m_redoLimit ) {
273 if ( d->m_current >= d->m_undoLimit ) {
274 const int toRemove = (d->m_current - d->m_undoLimit) + 1;
275 for (
int i = 0; i < toRemove; ++i ) {
276 delete d->m_commands.takeFirst();
280 Q_ASSERT( d->m_current >= -1 );
281 count = d->m_commands.count();
282 if ( d->m_savedAt < 0 )
286 if ( d->m_current + d->m_redoLimit + 1 < count ) {
287 if ( d->m_savedAt > (d->m_current + d->m_redoLimit) )
289 const int toRemove = count - (d->m_current + d->m_redoLimit + 1);
290 for (
int i = 0; i< toRemove; ++i )
291 delete d->m_commands.takeLast();
304 return d->m_current >= 0;
309 return d->m_current < d->m_commands.count() - 1;
315 for (
int i = d->m_current; i >= 0; --i ) {
316 lst.append( d->m_commands[i] );
317 if ( maxCommands > 0 && lst.count() == maxCommands )
326 for (
int i = d->m_current + 1; i < d->m_commands.count(); ++i )
328 lst.append( d->m_commands[i] );
329 if ( maxCommands > 0 && lst.count() == maxCommands )
337 return d->m_undoLimit;
342 return d->m_redoLimit;
345 class K3UndoRedoAction::Private
350 commandHistory( commandHistory )
364 d( new Private( type, commandHistory ) )
367 if ( d->type ==
Undo ) {
368 connect(
this, SIGNAL(
triggered(
bool)), d->commandHistory, SLOT(
undo()) );
370 connect(
this, SIGNAL(
triggered(
bool)), d->commandHistory, SLOT(
redo()) );
372 connect( this->menu(), SIGNAL(aboutToShow()),
this, SLOT(slotAboutToShow()) );
375 connect( d->commandHistory, SIGNAL(commandHistoryChanged()),
this, SLOT(slotCommandHistoryChanged()) );
376 slotCommandHistoryChanged();
381 void K3UndoRedoAction::slotAboutToShow()
385 const int maxCommands = 9;
386 if ( d->type ==
Undo ) {
388 for (
int i = 0; i < commands.count(); ++i) {
389 QAction *action = menu()->addAction(
i18n(
"Undo: %1", commands[i]->
name()) );
390 action->setData( i );
394 for (
int i = 0; i < commands.count(); ++i) {
395 QAction *action = menu()->addAction(
i18n(
"Redo: %1", commands[i]->
name()) );
396 action->setData( i );
401 void K3UndoRedoAction::slotActionTriggered(
QAction *action )
403 const int pos = action->data().toInt();
405 if ( d->type ==
Undo ) {
406 for (
int i = 0 ; i < pos+1; ++i ) {
407 d->commandHistory->undo();
410 for (
int i = 0 ; i < pos+1; ++i ) {
411 d->commandHistory->redo();
416 void K3UndoRedoAction::slotCommandHistoryChanged()
418 const bool isUndo = d->type ==
Undo;
419 const bool enabled = isUndo ? d->commandHistory->isUndoAvailable() : d->commandHistory->isRedoAvailable();
422 setText(isUndo ?
i18n(
"&Undo") :
i18n(
"&Redo"));
425 K3Command* presentCommand = d->commandHistory->presentCommand();
426 Q_ASSERT(presentCommand);
427 setText(
i18n(
"&Undo: %1", presentCommand->
name()));
429 K3Command* redoCommand = d->commandHistory->redoCommands(1).first();
430 setText(
i18n(
"&Redo: %1", redoCommand->
name()));
445 #include "k3command.moc" QString i18n(const char *text)
K3CommandHistory()
Creates a command history, to store commands.
virtual void virtual_hook(int id, void *data)
virtual void virtual_hook(int id, void *data)
This type of action is used to show undo or redo actions in the menu or in the toolbars.
QList< K3Command * > undoCommands(int maxCommands=0) const
KAction * undo(const QObject *recvr, const char *slot, QObject *parent)
void addCommand(K3Command *command)
Appends a command to this macro command.
The abstract base class for all Commands.
K3UndoRedoAction(Type type, KActionCollection *actionCollection, K3CommandHistory *commandHistory)
const KShortcut & shortcut(StandardShortcut id)
void setName(const QString &name)
Updates the name of this command.
virtual ~K3CommandHistory()
Destructs the command history object.
const QList< K3Command * > commands() const
const char * name(StandardAction id)
QAction * addAction(const QString &name, QAction *action)
void addCommand(K3Command *command, bool execute=true)
Adds a command to the history.
QList< K3Command * > redoCommands(int maxCommands=0) const
K3Command()
Creates a command.
virtual QString name() const =0
K3Command * presentCommand() const
K3NamedCommand(const QString &name)
Creates a command.
K3MacroCommand(const QString &name)
Creates a macro command.
virtual ~K3MacroCommand()
void commandExecuted(K3Command *command)
Emitted every time a command is executed (whether by addCommand, undo or redo).
void setUndoLimit(int limit)
Sets the maximum number of items in the undo history.
virtual void unexecute()=0
Unexecutes (undo) this command.
KAction * redo(const QObject *recvr, const char *slot, QObject *parent)
virtual void redo()
Redoes the last undone action.
virtual void undo()
Undoes the last action.
void setShortcut(const KShortcut &shortcut, ShortcutTypes type=ShortcutTypes(ActiveShortcut|DefaultShortcut))
A command which stores its name.
virtual void documentSaved()
Remembers when you saved the document.
virtual void execute()
Executes this command, i.e.
void updateActions()
Enable or disable the undo and redo actions.
void triggered(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
virtual ~K3NamedCommand()
void setRedoLimit(int limit)
Sets the maximum number of items in the redo history.
virtual void execute()=0
The main method: executes this command.
virtual void virtual_hook(int id, void *data)
virtual QString name() const
bool isUndoAvailable() const
void documentRestored()
Emitted every time we reach the index where you saved the document for the last time.
void clear()
Erases all the undo/redo history.
void commandHistoryChanged()
Emitted whenever the command history has changed, i.e.
virtual void unexecute()
Undoes the execution of this command, i.e.
The command history stores a (user) configurable amount of Commands.
bool isRedoAvailable() const