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 );
126 QList<K3Command *> 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;
314 QList<K3Command *> lst;
315 for (
int i = d->m_current; i >= 0; --i ) {
316 lst.append( d->m_commands[i] );
317 if ( maxCommands > 0 && lst.count() == maxCommands )
325 QList<K3Command *> lst;
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 ) {
387 const QList<K3Command *> commands = d->commandHistory->undoCommands( maxCommands );
388 for (
int i = 0; i < commands.count(); ++i) {
389 QAction *action = menu()->addAction(
i18n(
"Undo: %1", commands[i]->
name()) );
390 action->setData( i );
393 const QList<K3Command *> commands = d->commandHistory->redoCommands( maxCommands );
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"