22 #include <tdeaction.h>
23 #include <tdestdaccel.h>
24 #include <kstdaction.h>
27 #include <tdepopupmenu.h>
35 m_commands.setAutoDelete(
true);
40 m_commands.append(command);
45 TQPtrListIterator<KCommand> it(m_commands);
46 for ( ; it.current() ; ++it )
47 it.current()->execute();
52 TQPtrListIterator<KCommand> it(m_commands);
54 for ( ; it.current() ; --it )
55 it.current()->unexecute();
59 class KCommandHistory::KCommandHistoryPrivate {
61 KCommandHistoryPrivate() {
65 ~KCommandHistoryPrivate() {}
73 m_undo(0), m_redo(0), m_undoLimit(50), m_redoLimit(30), m_first(false)
75 d=
new KCommandHistoryPrivate();
76 m_commands.setAutoDelete(
true);
81 m_undoLimit(50), m_redoLimit(30), m_first(false)
83 d=
new KCommandHistoryPrivate();
89 connect(
undo->popupMenu(), TQ_SIGNAL( aboutToShow() ),
this, TQ_SLOT( slotUndoAboutToShow() ) );
90 connect(
undo->popupMenu(), TQ_SIGNAL( activated(
int ) ),
this, TQ_SLOT( slotUndoActivated(
int ) ) );
92 m_undoPopup =
undo->popupMenu();
97 connect(
redo->popupMenu(), TQ_SIGNAL( aboutToShow() ),
this, TQ_SLOT( slotRedoAboutToShow() ) );
98 connect(
redo->popupMenu(), TQ_SIGNAL( activated(
int ) ),
this, TQ_SLOT( slotRedoActivated(
int ) ) );
100 m_redoPopup =
redo->popupMenu();
109 m_commands.setAutoDelete(
true);
120 m_undo->
setText(i18n(
"&Undo"));
124 m_redo->
setText(i18n(
"&Redo"));
136 if(d->m_present && (index=m_commands.findRef(d->m_present))!=-1) {
139 m_commands.insert(index+1, command);
141 unsigned int count=m_commands.count();
142 for(
unsigned int i=index+2; i<count; ++i)
143 m_commands.removeLast();
145 if(index<d->m_savedAt)
147 d->m_present=command;
151 m_undo->
setText(i18n(
"&Undo: %1").arg(d->m_present->name()));
155 m_redo->
setText(i18n(
"&Redo"));
160 kdDebug(230) <<
"Initializing the Command History" <<
endl;
162 m_commands.append(command);
163 d->m_present=command;
166 m_undo->
setText(i18n(
"&Undo: %1").arg(d->m_present->name()));
170 m_redo->
setText(i18n(
"&Redo"));
184 if (m_first || !d->m_present)
187 d->m_present->unexecute();
192 m_redo->
setText(i18n(
"&Redo: %1").arg(d->m_present->name()));
195 if((index=m_commands.findRef(d->m_present))!=-1 && m_commands.prev()) {
196 d->m_present=m_commands.current();
199 m_undo->
setText(i18n(
"&Undo: %1").arg(d->m_present->name()));
202 if(index==d->m_savedAt)
208 m_undo->
setText(i18n(
"&Undo"));
210 if(d->m_savedAt==-42)
221 d->m_present->execute();
229 else if((index=m_commands.findRef(d->m_present))!=-1 && m_commands.next()) {
230 d->m_present=m_commands.current();
231 d->m_present->execute();
235 if(index==d->m_savedAt)
241 m_undo->
setText(i18n(
"&Undo: %1").arg(d->m_present->name()));
244 if(m_commands.next()) {
247 m_redo->
setText(i18n(
"&Redo: %1").arg(m_commands.current()->name()));
253 m_redo->
setText(i18n(
"&Redo"));
259 if(d->m_present && !m_first)
260 d->m_savedAt=m_commands.findRef(d->m_present);
261 else if(!d->m_present && !m_first)
270 if(limit>0 && limit!=m_undoLimit) {
278 if(limit>0 && limit!=m_redoLimit) {
284 void KCommandHistory::clipCommands() {
286 int count=m_commands.count();
287 if(count<=m_undoLimit && count<=m_redoLimit)
290 int index=m_commands.findRef(d->m_present);
291 if(index>=m_undoLimit) {
292 for(
int i=0; i<=(index-m_undoLimit); ++i) {
293 m_commands.removeFirst();
298 index=m_commands.findRef(d->m_present);
299 count=m_commands.count();
301 if(d->m_savedAt!=-42 && d->m_savedAt<-1)
307 if((index+m_redoLimit+1)<count) {
308 if(d->m_savedAt>(index+m_redoLimit))
310 for(
int i=0; i<(count-(index+m_redoLimit+1)); ++i)
311 m_commands.removeLast();
315 void KCommandHistory::slotUndoAboutToShow()
317 m_undoPopup->clear();
319 if (m_commands.findRef(d->m_present)!=-1)
320 while ( m_commands.current() && i<10 )
322 m_undoPopup->insertItem( i18n(
"Undo: %1").arg(m_commands.current()->name()), i++ );
327 void KCommandHistory::slotUndoActivated(
int pos )
329 kdDebug(230) <<
"KCommandHistory::slotUndoActivated " << pos <<
endl;
330 for (
int i = 0 ; i < pos+1; ++i )
334 void KCommandHistory::slotRedoAboutToShow()
336 m_redoPopup->clear();
340 d->m_present = m_commands.first();
341 m_redoPopup->insertItem( i18n(
"Redo: %1").arg(d->m_present->name()), i++ );
343 if (m_commands.findRef(d->m_present)!=-1 && m_commands.next())
344 while ( m_commands.current() && i<10 )
346 m_redoPopup->insertItem( i18n(
"Redo: %1").arg(m_commands.current()->name()), i++ );
351 void KCommandHistory::slotRedoActivated(
int pos )
353 kdDebug(230) <<
"KCommandHistory::slotRedoActivated " << pos <<
endl;
354 for (
int i = 0 ; i < pos+1; ++i )
360 if ( m_undo && m_redo )
362 m_undo->
setEnabled( !m_first && ( d->m_present ) );
363 m_redo->
setEnabled(m_first || (m_commands.findRef(d->m_present)!=-1 && m_commands.next()));
367 void KCommand::virtual_hook(
int,
void* )
370 void KNamedCommand::virtual_hook(
int id,
void* data )
371 { KCommand::virtual_hook(
id, data ); }
373 void KMacroCommand::virtual_hook(
int id,
void* data )
374 { KNamedCommand::virtual_hook(
id, data ); }
376 void KCommandHistory::virtual_hook(
int,
void* )
379 #include "kcommand.moc"
void setRedoLimit(int limit)
Sets the maximum number of items in the redo history.
void setUndoLimit(int limit)
Sets the maximum number of items in the undo history.
void clear()
Erases all the undo/redo history.
virtual void undo()
Undoes the last action.
void addCommand(KCommand *command, bool execute=true)
Adds a command to the history.
void updateActions()
Enable or disable the undo and redo actions.
void documentRestored()
Emitted every time we reach the index where you saved the document for the last time.
virtual void redo()
Redoes the last undone action.
virtual void documentSaved()
Remembers when you saved the document.
void commandExecuted()
Emitted every time a command is executed (whether by addCommand, undo or redo).
KCommandHistory()
Creates a command history, to store commands.
virtual ~KCommandHistory()
Destructs the command history object.
The abstract base class for all Commands.
virtual void execute()=0
The main method: executes this command.
virtual void unexecute()
Undoes the execution of this command, i.e.
void addCommand(KCommand *command)
Appends a command to this macro command.
KMacroCommand(const TQString &name)
Creates a macro command.
virtual void execute()
Executes this command, i.e.
A command which stores its name.
A managed set of TDEAction objects.
virtual void setEnabled(bool enable)
Enables or disables this action.
virtual bool isEnabled() const
Returns true if this action is enabled.
virtual void setText(const TQString &text)
Sets the text associated with this action.
kndbgstream & endl(kndbgstream &s)
kdbgstream kdDebug(int area=0)
TDEAction * undo(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name)
Undo the last operation.
const char * stdName(StdAction act_enum)
TDEAction * redo(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name)
Redo the last operation.
const TDEShortcut & shortcut(StdAccel id)