• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
kcommand.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2000 Werner Trobin <trobin@kde.org>
3  Copyright (C) 2000 David Faure <faure@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kcommand.h"
22 #include <tdeaction.h>
23 #include <tdestdaccel.h>
24 #include <kstdaction.h>
25 #include <kdebug.h>
26 #include <tdelocale.h>
27 #include <tdepopupmenu.h>
28 
29 KCommand::~KCommand()
30 {
31 }
32 
33 KMacroCommand::KMacroCommand( const TQString & name ) : KNamedCommand(name)
34 {
35  m_commands.setAutoDelete(true);
36 }
37 
38 void KMacroCommand::addCommand(KCommand *command)
39 {
40  m_commands.append(command);
41 }
42 
43 void KMacroCommand::execute()
44 {
45  TQPtrListIterator<KCommand> it(m_commands);
46  for ( ; it.current() ; ++it )
47  it.current()->execute();
48 }
49 
50 void KMacroCommand::unexecute()
51 {
52  TQPtrListIterator<KCommand> it(m_commands);
53  it.toLast();
54  for ( ; it.current() ; --it )
55  it.current()->unexecute();
56 }
57 
58 
59 class KCommandHistory::KCommandHistoryPrivate {
60 public:
61  KCommandHistoryPrivate() {
62  m_savedAt=-1;
63  m_present=0;
64  }
65  ~KCommandHistoryPrivate() {}
66  int m_savedAt;
67  KCommand *m_present;
68 };
69 
71 
72 KCommandHistory::KCommandHistory() :
73  m_undo(0), m_redo(0), m_undoLimit(50), m_redoLimit(30), m_first(false)
74 {
75  d=new KCommandHistoryPrivate();
76  m_commands.setAutoDelete(true);
77  clear();
78 }
79 
80 KCommandHistory::KCommandHistory(TDEActionCollection * actionCollection, bool withMenus) :
81  m_undoLimit(50), m_redoLimit(30), m_first(false)
82 {
83  d=new KCommandHistoryPrivate();
84  if (withMenus)
85  {
86  TDEToolBarPopupAction * undo = new TDEToolBarPopupAction( i18n("&Undo"), "edit-undo",
87  TDEStdAccel::shortcut(TDEStdAccel::Undo), this, TQT_SLOT( undo() ),
88  actionCollection, KStdAction::stdName( KStdAction::Undo ) );
89  connect( undo->popupMenu(), TQT_SIGNAL( aboutToShow() ), this, TQT_SLOT( slotUndoAboutToShow() ) );
90  connect( undo->popupMenu(), TQT_SIGNAL( activated( int ) ), this, TQT_SLOT( slotUndoActivated( int ) ) );
91  m_undo = undo;
92  m_undoPopup = undo->popupMenu();
93 
94  TDEToolBarPopupAction * redo = new TDEToolBarPopupAction( i18n("&Redo"), "edit-redo",
95  TDEStdAccel::shortcut(TDEStdAccel::Redo), this, TQT_SLOT( redo() ),
96  actionCollection, KStdAction::stdName( KStdAction::Redo ) );
97  connect( redo->popupMenu(), TQT_SIGNAL( aboutToShow() ), this, TQT_SLOT( slotRedoAboutToShow() ) );
98  connect( redo->popupMenu(), TQT_SIGNAL( activated( int ) ), this, TQT_SLOT( slotRedoActivated( int ) ) );
99  m_redo = redo;
100  m_redoPopup = redo->popupMenu();
101  }
102  else
103  {
104  m_undo = KStdAction::undo( this, TQT_SLOT( undo() ), actionCollection );
105  m_redo = KStdAction::redo( this, TQT_SLOT( redo() ), actionCollection );
106  m_undoPopup = 0L;
107  m_redoPopup = 0L;
108  }
109  m_commands.setAutoDelete(true);
110  clear();
111 }
112 
113 KCommandHistory::~KCommandHistory() {
114  delete d;
115 }
116 
117 void KCommandHistory::clear() {
118  if (m_undo) {
119  m_undo->setEnabled(false);
120  m_undo->setText(i18n("&Undo"));
121  }
122  if (m_redo) {
123  m_redo->setEnabled(false);
124  m_redo->setText(i18n("&Redo"));
125  }
126  d->m_present = 0L;
127  d->m_savedAt=-42;
128 }
129 
130 void KCommandHistory::addCommand(KCommand *command, bool execute) {
131 
132  if(!command)
133  return;
134 
135  int index;
136  if(d->m_present && (index=m_commands.findRef(d->m_present))!=-1) {
137  if (m_first)
138  --index;
139  m_commands.insert(index+1, command);
140  // truncate history
141  unsigned int count=m_commands.count();
142  for(unsigned int i=index+2; i<count; ++i)
143  m_commands.removeLast();
144  // check whether we still can reach savedAt
145  if(index<d->m_savedAt)
146  d->m_savedAt=-1;
147  d->m_present=command;
148  m_first=false;
149  if (m_undo) {
150  m_undo->setEnabled(true);
151  m_undo->setText(i18n("&Undo: %1").arg(d->m_present->name()));
152  }
153  if((m_redo) && m_redo->isEnabled()) {
154  m_redo->setEnabled(false);
155  m_redo->setText(i18n("&Redo"));
156  }
157  clipCommands();
158  }
159  else { // either this is the first time we add a Command or something has gone wrong
160  kdDebug(230) << "Initializing the Command History" << endl;
161  m_commands.clear();
162  m_commands.append(command);
163  d->m_present=command;
164  if (m_undo) {
165  m_undo->setEnabled(true);
166  m_undo->setText(i18n("&Undo: %1").arg(d->m_present->name()));
167  }
168  if (m_redo) {
169  m_redo->setEnabled(false);
170  m_redo->setText(i18n("&Redo"));
171  }
172  m_first=false; // Michael B: yes, that *is* correct :-)
173  }
174  if ( execute )
175  {
176  command->execute();
177  emit commandExecuted();
178  emit commandExecuted(command);
179  }
180 }
181 
182 void KCommandHistory::undo() {
183 
184  if (m_first || !d->m_present)
185  return;
186 
187  d->m_present->unexecute();
188  emit commandExecuted();
189  emit commandExecuted(d->m_present);
190  if (m_redo) {
191  m_redo->setEnabled(true);
192  m_redo->setText(i18n("&Redo: %1").arg(d->m_present->name()));
193  }
194  int index;
195  if((index=m_commands.findRef(d->m_present))!=-1 && m_commands.prev()) {
196  d->m_present=m_commands.current();
197  if (m_undo) {
198  m_undo->setEnabled(true);
199  m_undo->setText(i18n("&Undo: %1").arg(d->m_present->name()));
200  }
201  --index;
202  if(index==d->m_savedAt)
203  emit documentRestored();
204  }
205  else {
206  if (m_undo) {
207  m_undo->setEnabled(false);
208  m_undo->setText(i18n("&Undo"));
209  }
210  if(d->m_savedAt==-42)
211  emit documentRestored();
212  m_first=true;
213  }
214  clipCommands(); // only needed here and in addCommand, NOT in redo
215 }
216 
217 void KCommandHistory::redo() {
218 
219  int index;
220  if(m_first) {
221  d->m_present->execute();
222  emit commandExecuted();
223  emit commandExecuted(d->m_present);
224  m_first=false;
225  m_commands.first();
226  if(!d->m_savedAt)
227  emit documentRestored();
228  }
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();
232  emit commandExecuted();
233  emit commandExecuted(d->m_present);
234  ++index;
235  if(index==d->m_savedAt)
236  emit documentRestored();
237  }
238 
239  if (m_undo) {
240  m_undo->setEnabled(true);
241  m_undo->setText(i18n("&Undo: %1").arg(d->m_present->name()));
242  }
243 
244  if(m_commands.next()) {
245  if (m_redo) {
246  m_redo->setEnabled(true);
247  m_redo->setText(i18n("&Redo: %1").arg(m_commands.current()->name()));
248  }
249  }
250  else {
251  if((m_redo) && m_redo->isEnabled()) {
252  m_redo->setEnabled(false);
253  m_redo->setText(i18n("&Redo"));
254  }
255  }
256 }
257 
258 void KCommandHistory::documentSaved() {
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)
262  d->m_savedAt=-42; // this value signals that the document has
263  // been saved with an empty history.
264  else if(m_first)
265  d->m_savedAt=-42;
266 }
267 
268 void KCommandHistory::setUndoLimit(int limit) {
269 
270  if(limit>0 && limit!=m_undoLimit) {
271  m_undoLimit=limit;
272  clipCommands();
273  }
274 }
275 
276 void KCommandHistory::setRedoLimit(int limit) {
277 
278  if(limit>0 && limit!=m_redoLimit) {
279  m_redoLimit=limit;
280  clipCommands();
281  }
282 }
283 
284 void KCommandHistory::clipCommands() {
285 
286  int count=m_commands.count();
287  if(count<=m_undoLimit && count<=m_redoLimit)
288  return;
289 
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();
294  --d->m_savedAt;
295  if(d->m_savedAt==-1)
296  d->m_savedAt=-42;
297  }
298  index=m_commands.findRef(d->m_present); // calculate the new
299  count=m_commands.count(); // values (for the redo-branch :)
300  // make it easier for us... d->m_savedAt==-1 -> invalid
301  if(d->m_savedAt!=-42 && d->m_savedAt<-1)
302  d->m_savedAt=-1;
303  }
304  // adjust the index if it's the first command
305  if(m_first)
306  index=-1;
307  if((index+m_redoLimit+1)<count) {
308  if(d->m_savedAt>(index+m_redoLimit))
309  d->m_savedAt=-1;
310  for(int i=0; i<(count-(index+m_redoLimit+1)); ++i)
311  m_commands.removeLast();
312  }
313 }
314 
315 void KCommandHistory::slotUndoAboutToShow()
316 {
317  m_undoPopup->clear();
318  int i = 0;
319  if (m_commands.findRef(d->m_present)!=-1)
320  while ( m_commands.current() && i<10 ) // TODO make number of items configurable ?
321  {
322  m_undoPopup->insertItem( i18n("Undo: %1").arg(m_commands.current()->name()), i++ );
323  m_commands.prev();
324  }
325 }
326 
327 void KCommandHistory::slotUndoActivated( int pos )
328 {
329  kdDebug(230) << "KCommandHistory::slotUndoActivated " << pos << endl;
330  for ( int i = 0 ; i < pos+1; ++i )
331  undo();
332 }
333 
334 void KCommandHistory::slotRedoAboutToShow()
335 {
336  m_redoPopup->clear();
337  int i = 0;
338  if (m_first)
339  {
340  d->m_present = m_commands.first();
341  m_redoPopup->insertItem( i18n("Redo: %1").arg(d->m_present->name()), i++ );
342  }
343  if (m_commands.findRef(d->m_present)!=-1 && m_commands.next())
344  while ( m_commands.current() && i<10 ) // TODO make number of items configurable ?
345  {
346  m_redoPopup->insertItem( i18n("Redo: %1").arg(m_commands.current()->name()), i++ );
347  m_commands.next();
348  }
349 }
350 
351 void KCommandHistory::slotRedoActivated( int pos )
352 {
353  kdDebug(230) << "KCommandHistory::slotRedoActivated " << pos << endl;
354  for ( int i = 0 ; i < pos+1; ++i )
355  redo();
356 }
357 
358 void KCommandHistory::updateActions()
359 {
360  if ( m_undo && m_redo )
361  {
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()));
364  }
365 }
366 
367 void KCommand::virtual_hook( int, void* )
368 { /*BASE::virtual_hook( id, data );*/ }
369 
370 void KNamedCommand::virtual_hook( int id, void* data )
371 { KCommand::virtual_hook( id, data ); }
372 
373 void KMacroCommand::virtual_hook( int id, void* data )
374 { KNamedCommand::virtual_hook( id, data ); }
375 
376 void KCommandHistory::virtual_hook( int, void* )
377 { /*BASE::virtual_hook( id, data );*/ }
378 
379 #include "kcommand.moc"
KCommandHistory::commandExecuted
void commandExecuted()
Emitted every time a command is executed (whether by addCommand, undo or redo).
TDEToolBarPopupAction
This action is a normal action everywhere, except in a toolbar where it also has a popupmenu (optionn...
Definition: tdeactionclasses.h:1094
KCommandHistory::documentRestored
void documentRestored()
Emitted every time we reach the index where you saved the document for the last time.
KCommand::execute
virtual void execute()=0
The main method: executes this command.
KMacroCommand::execute
virtual void execute()
Executes this command, i.e.
Definition: kcommand.cpp:43
TDEToolBarPopupAction::popupMenu
TDEPopupMenu * popupMenu() const
The popup menu that is shown when clicking (some time) on the toolbar button.
Definition: tdeactionclasses.cpp:1989
TDEStdAccel::shortcut
const TDEShortcut & shortcut(StdAccel id)
kdDebug
kdbgstream kdDebug(int area=0)
KCommandHistory::redo
virtual void redo()
Redoes the last undone action.
Definition: kcommand.cpp:217
TDEActionCollection
A managed set of TDEAction objects.
Definition: tdeactioncollection.h:78
KMacroCommand::KMacroCommand
KMacroCommand(const TQString &name)
Creates a macro command.
Definition: kcommand.cpp:33
KCommandHistory::documentSaved
virtual void documentSaved()
Remembers when you saved the document.
Definition: kcommand.cpp:258
KMacroCommand::addCommand
void addCommand(KCommand *command)
Appends a command to this macro command.
Definition: kcommand.cpp:38
KCommandHistory::updateActions
void updateActions()
Enable or disable the undo and redo actions.
Definition: kcommand.cpp:358
tdelocale.h
KCommandHistory::undo
virtual void undo()
Undoes the last action.
Definition: kcommand.cpp:182
KStdAction::redo
TDEAction * redo(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name)
Redo the last operation.
Definition: kstdaction.cpp:166
KMacroCommand::unexecute
virtual void unexecute()
Undoes the execution of this command, i.e.
Definition: kcommand.cpp:50
TDEAction::setText
virtual void setText(const TQString &text)
Sets the text associated with this action.
Definition: tdeaction.cpp:879
KCommandHistory::clear
void clear()
Erases all the undo/redo history.
Definition: kcommand.cpp:117
KCommand
The abstract base class for all Commands.
Definition: kcommand.h:37
TDEAction::setEnabled
virtual void setEnabled(bool enable)
Enables or disables this action.
Definition: tdeaction.cpp:832
KCommandHistory::setUndoLimit
void setUndoLimit(int limit)
Sets the maximum number of items in the undo history.
Definition: kcommand.cpp:268
KCommandHistory::~KCommandHistory
virtual ~KCommandHistory()
Destructs the command history object.
Definition: kcommand.cpp:113
KCommandHistory::KCommandHistory
KCommandHistory()
Creates a command history, to store commands.
Definition: kcommand.cpp:72
KStdAction::undo
TDEAction * undo(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name)
Undo the last operation.
Definition: kstdaction.cpp:164
KNamedCommand
A command which stores its name.
Definition: kcommand.h:78
KStdAction::stdName
const char * stdName(StdAction act_enum)
Definition: kstdaction.h:192
TDEAction::isEnabled
virtual bool isEnabled() const
Returns true if this action is enabled.
Definition: tdeaction.cpp:596
KCommandHistory::setRedoLimit
void setRedoLimit(int limit)
Sets the maximum number of items in the redo history.
Definition: kcommand.cpp:276
endl
kndbgstream & endl(kndbgstream &s)
KCommandHistory::addCommand
void addCommand(KCommand *command, bool execute=true)
Adds a command to the history.
Definition: kcommand.cpp:130

tdeui

Skip menu "tdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.8.8
This website is maintained by Timothy Pearson.