29#include <tdepopupmenu.h>
30#include <tdeshortcutlist.h>
31#include <tdestdaccel.h>
32#include <kstdaction.h>
34#include "KoCommandHistory.h"
36KoListBox::KoListBox( TQWidget *parent ,
const char *name , WFlags f)
37 : TQListBox( parent,
name, f)
39 setVScrollBarMode( AlwaysOn );
42void KoListBox::contentsMouseMoveEvent ( TQMouseEvent * e)
44 TQListBoxItem *item_p = itemAt( contentsToViewport(e->pos()));
47 int itemIndex = index( item_p );
48 for (
int i = 0; i<=itemIndex;i++)
49 setSelected ( i,
true );
50 for (
int i = itemIndex+1; i<(int)count();i++)
51 setSelected ( i,
false );
52 emit changeNumberOfSelectedItem( itemIndex);
56TQSize KoListBox::sizeHint()
const
58 return TQSize(TQMIN(maxItemWidth() + verticalScrollBar()->width() + 4, 400),
59 TQMIN(count() * itemHeight() + horizontalScrollBar()->height() + 4,300));
62class KoCommandHistory::KoCommandHistoryPrivate {
64 KoCommandHistoryPrivate() {
68 ~KoCommandHistoryPrivate() {}
71 KoListBox *m_undoListBox;
72 KoListBox *m_redoListBox;
80 m_undo(0), m_redo(0), m_undoLimit(50), m_redoLimit(30), m_first(false)
82 d=
new KoCommandHistoryPrivate();
83 m_commands.setAutoDelete(
true);
88 m_undoLimit(50), m_redoLimit(30), m_first(false)
90 d=
new KoCommandHistoryPrivate();
94 TDEStdAccel::undo(),
this, TQ_SLOT(
undo() ),
95 actionCollection,
"koffice_undo" );
96 connect(
undo->popupMenu(), TQ_SIGNAL( aboutToShow() ),
this, TQ_SLOT( slotUndoAboutToShow() ) );
97 connect(
undo->popupMenu(), TQ_SIGNAL( activated(
int ) ),
this, TQ_SLOT( slotUndoActivated(
int ) ) );
99 m_undoPopup =
undo->popupMenu();
100 d->m_undoListBox =
new KoListBox( m_undoPopup );
101 d->m_undoListBox->setSelectionMode( TQListBox::Multi );
103 m_undoPopup->insertItem(d->m_undoListBox);
104 d->m_undoLabel =
new TQLabel( m_undoPopup);
105 m_undoPopup->insertItem(d->m_undoLabel);
107 connect( d->m_undoListBox, TQ_SIGNAL( selected(
int ) ),
this, TQ_SLOT( slotUndoActivated(
int ) ) );
108 connect( d->m_undoListBox, TQ_SIGNAL(clicked ( TQListBoxItem *)),
this, TQ_SLOT( slotUndoActivated( TQListBoxItem * ) ) );
110 connect( d->m_undoListBox, TQ_SIGNAL( changeNumberOfSelectedItem(
int )),
this, TQ_SLOT( slotChangeUndoNumberOfSelectedItem(
int )));
113 TDEStdAccel::redo(),
this, TQ_SLOT(
redo() ),
116 connect(
redo->popupMenu(), TQ_SIGNAL( aboutToShow() ),
this, TQ_SLOT( slotRedoAboutToShow() ) );
117 connect(
redo->popupMenu(), TQ_SIGNAL( activated(
int ) ),
this, TQ_SLOT( slotRedoActivated(
int ) ) );
119 m_redoPopup =
redo->popupMenu();
120 d->m_redoListBox =
new KoListBox( m_redoPopup );
121 d->m_redoListBox->setSelectionMode( TQListBox::Multi );
122 m_redoPopup->insertItem(d->m_redoListBox);
124 d->m_redoLabel =
new TQLabel( m_redoPopup);
125 m_redoPopup->insertItem(d->m_redoLabel);
128 connect( d->m_redoListBox, TQ_SIGNAL( selected(
int ) ),
this, TQ_SLOT( slotRedoActivated(
int ) ) );
129 connect( d->m_redoListBox, TQ_SIGNAL(clicked ( TQListBoxItem *)),
this, TQ_SLOT( slotRedoActivated( TQListBoxItem * ) ) );
130 connect( d->m_redoListBox, TQ_SIGNAL( changeNumberOfSelectedItem(
int )),
this, TQ_SLOT( slotChangeRedoNumberOfSelectedItem(
int )));
139 d->m_redoListBox = 0L;
140 d->m_undoListBox = 0L;
144 m_commands.setAutoDelete(
true);
153{
return d->m_present;
159 m_undo->setEnabled(
false);
160 m_undo->setText(i18n(
"&Undo"));
163 m_redo->setEnabled(
false);
164 m_redo->setText(i18n(
"&Redo"));
176 if(d->m_present!=0L && (index=m_commands.findRef(d->m_present))!=-1) {
179 m_commands.insert(index+1, command);
181 unsigned int count=m_commands.count();
182 for(
unsigned int i=index+2; i<count; ++i)
183 m_commands.removeLast();
185 if(index<d->m_savedAt)
187 d->m_present=command;
190 m_undo->setEnabled(
true);
191 m_undo->setText(i18n(
"&Undo: %1").arg(d->m_present->name()));
193 if((m_redo != 0) && m_redo->isEnabled()) {
194 m_redo->setEnabled(
false);
195 m_redo->setText(i18n(
"&Redo"));
200 kdDebug(230) <<
"KoCommandHistory: Initializing the Command History" << endl;
202 m_commands.append(command);
203 d->m_present=command;
205 m_undo->setEnabled(
true);
206 m_undo->setText(i18n(
"&Undo: %1").arg(d->m_present->name()));
209 m_redo->setEnabled(
false);
210 m_redo->setText(i18n(
"&Redo"));
224 if (m_first || (d->m_present == 0L))
228 KCommand *commandUndone = d->m_present;
231 m_redo->setEnabled(
true);
232 m_redo->setText(i18n(
"&Redo: %1").arg(d->m_present->name()));
235 if((index=m_commands.findRef(d->m_present))!=-1 && m_commands.prev()!=0) {
236 d->m_present=m_commands.current();
240 m_undo->setEnabled(
true);
241 m_undo->setText(i18n(
"&Undo: %1").arg(d->m_present->name()));
244 if(index==d->m_savedAt)
251 m_undo->setEnabled(
false);
252 m_undo->setText(i18n(
"&Undo"));
254 if(d->m_savedAt==-42)
265 d->m_present->execute();
273 else if((index=m_commands.findRef(d->m_present))!=-1 && m_commands.next()!=0) {
274 d->m_present=m_commands.current();
275 d->m_present->execute();
279 if(index==d->m_savedAt)
284 m_undo->setEnabled(
true);
285 m_undo->setText(i18n(
"&Undo: %1").arg(d->m_present->name()));
288 if(m_commands.next()!=0) {
290 m_redo->setEnabled(
true);
291 m_redo->setText(i18n(
"&Redo: %1").arg(m_commands.current()->name()));
295 if((m_redo != 0) && m_redo->isEnabled()) {
296 m_redo->setEnabled(
false);
297 m_redo->setText(i18n(
"&Redo"));
303 if(d->m_present!=0 && !m_first)
304 d->m_savedAt=m_commands.findRef(d->m_present);
305 else if(d->m_present==0 && !m_first)
314 if(limit>0 && limit!=m_undoLimit) {
322 if(limit>0 && limit!=m_redoLimit) {
328void KoCommandHistory::clipCommands() {
330 int count=m_commands.count();
331 if(count<=m_undoLimit && count<=m_redoLimit)
334 int index=m_commands.findRef(d->m_present);
335 if(index>=m_undoLimit) {
336 for(
int i=0; i<=(index-m_undoLimit); ++i) {
337 m_commands.removeFirst();
342 index=m_commands.findRef(d->m_present);
343 count=m_commands.count();
345 if(d->m_savedAt!=-42 && d->m_savedAt<-1)
351 if((index+m_redoLimit+1)<count) {
352 if(d->m_savedAt>(index+m_redoLimit))
354 for(
int i=0; i<(count-(index+m_redoLimit+1)); ++i)
355 m_commands.removeLast();
359void KoCommandHistory::slotUndoAboutToShow()
361 d->m_undoListBox->clear();
362 slotChangeUndoNumberOfSelectedItem( -1 );
365 if (m_commands.findRef(d->m_present)!=-1)
366 while ( m_commands.current() && i<10 )
368 lst.append(i18n(
"Undo: %1").arg(m_commands.current()->name()));
371 d->m_undoListBox->insertStringList( lst );
374void KoCommandHistory::slotUndoActivated(
int pos )
376 kdDebug(230) <<
"KoCommandHistory::slotUndoActivated " << pos << endl;
377 for (
int i = 0 ; i < pos+1; ++i )
382void KoCommandHistory::slotUndoActivated( TQListBoxItem * item)
385 slotUndoActivated( item->listBox()->index(item));
388void KoCommandHistory::slotRedoActivated( TQListBoxItem * item)
391 slotRedoActivated( item->listBox()->index(item));
394void KoCommandHistory::slotChangeUndoNumberOfSelectedItem(
int pos)
396 d->m_undoLabel->setText( i18n(
"Undo %n action",
"Undo %n actions", pos+1) );
399void KoCommandHistory::slotChangeRedoNumberOfSelectedItem(
int pos)
401 d->m_redoLabel->setText( i18n(
"Redo %n action",
"Redo %n actions", pos+1) );
405void KoCommandHistory::slotRedoAboutToShow()
407 d->m_redoListBox->clear();
408 slotChangeRedoNumberOfSelectedItem( -1 );
413 d->m_present = m_commands.first();
414 lst.append(i18n(
"Redo: %1").arg(d->m_present->name()));
416 if (m_commands.findRef(d->m_present)!=-1 && m_commands.next())
417 while ( m_commands.current() && i<10 )
419 lst.append(i18n(
"Redo: %1").arg(m_commands.current()->name()));
422 d->m_redoListBox->insertStringList( lst );
426void KoCommandHistory::slotRedoActivated(
int pos )
428 kdDebug(230) <<
"KoCommandHistory::slotRedoActivated " << pos << endl;
429 for (
int i = 0 ; i < pos+1; ++i )
436 if ( m_undo && m_redo )
438 m_undo->setEnabled( !m_first && ( d->m_present != 0L ) );
439 m_redo->setEnabled(m_first || (m_commands.findRef(d->m_present)!=-1 && m_commands.next()!=0));
443void KoCommandHistory::virtual_hook(
int,
void* )
446#include "KoCommandHistory.moc"
virtual void unexecute()=0
virtual void documentSaved()
Remembers when you saved the document.
void commandExecuted()
Emitted every time a command is executed (whether by addCommand, undo or redo).
void addCommand(KCommand *command, bool execute=true)
Adds a command to the history.
virtual void redo()
Redoes the last undone action.
void setRedoLimit(int limit)
Sets the maximum number of items in the redo history.
virtual void undo()
Undoes the last action.
void clear()
Erases all the undo/redo 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.
KoCommandHistory()
Creates a command history, to store commands.
void setUndoLimit(int limit)
Sets the maximum number of items in the undo history.
virtual ~KoCommandHistory()
Destructs the command history object.
KCommand * presentCommand()
TDEAction * undo(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
TDEAction * redo(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
const char * name(StdAction id)