summaryrefslogtreecommitdiffstats
path: root/karbon/commands/vcommand.h
blob: e15e8cf82a9f167a9d6e7473e898d2e310069eba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* This file is part of the KDE project
   Copyright (C) 2001, The Karbon Developers
   Copyright (C) 2002, The Karbon Developers

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifndef __VCOMMAND_H__
#define __VCOMMAND_H__


//#include <assert.h>

#include <tqobject.h>
#include <tqptrlist.h>

#include "vvisitor.h"

class VDocument;
class KarbonPart;
class TDEAction;

/**
 * The base class for all karbon commands.
 *
 * It basically defines the common interface that all commands should implement.
 */
class VCommand : public VVisitor
{
public:
	/**
	 * Constructs a new command.
	 *
	 * @param doc the document the command should work on
	 * @param name the name of the command (appears in command history)
	 * @param icon the icon of the command (appears in command history)
	 */
	VCommand( VDocument* doc, const TQString& name, const TQString& icon = "14_action" )
			: m_document( doc ), m_name( name ), m_icon( icon )
	{
// A crash because of an assert() is not much better than an crash because of a null
// pointer. Allowing null pointers allows the usage of the vitors ascpect of a VCommand.
//		assert( doc );
	}

	/** Destroys the command */
	virtual ~VCommand() {}

	/**
	 * Executes the command.
	 *
	 * All the changes to the document are done here.
	 * All commands have to implement this function.
	 */
	virtual void execute() = 0;

	/**
	 * Unexecutes the command.
	 *
	 * All changes to the document have to be undone here.
	 */
	virtual void unexecute() {}

	/**
	 * Returns if the command changes the actual document selection.
	 *
	 * This flag is checked to determine if the document has to be redrawn.
	 *
	 * @return true if the selection is changed, else false
	 */
	virtual bool changesSelection() const { return false; }

	/**
	 * Returns the name of the command.
	 *
	 * @return the command name
	 */
	TQString name() const
	{
		return m_name;
	}

	/**
	 * Sets the name of the command.
	 *
	 * @param name the new command name
	 */ 
	void setName( const TQString& name )
	{
		m_name = name;
	}

	/**
	 * Returns the icon of the command.
	 *
	 * @return the command icon
	 */
	TQString icon() const
	{
		return m_icon;
	}

	/**
	 * Returns the document the command works on.
	 *
	 * @return the command's document
	 */
	VDocument* document() const
	{
		return m_document;
	}

private:
	VDocument* m_document;

	TQString m_name;
	TQString m_icon;
};

/**
 * Manages a set of commands.
 *
 * It keeps the commands in a list, commands higher in the list are older
 * than lower commands.
 * All commands in the list can be undone, beginning from the latest command
 * at the end of the list. Undone commands can be redone, beginning at the
 * oldest undone command. That makes it possible to go back and forth to a 
 * specific document state.
 */
class VCommandHistory : public TQObject
{
	Q_OBJECT
  

public:
	/**
	 * Constructs a command history.
	 *
	 * @param part the part the commands are managed for
	 */
	VCommandHistory( KarbonPart* part );
	
	/** Destroys the command history. */
	~VCommandHistory();

	/**
	 * Clears the command history by removing all commands.
	 *
	 * Emits the historyCleared signal
	 */
	void clear();

	/**
	 * Adds a new command to the history.
	 *
	 * @param command the new command to add
	 * @param execute controls if the new command should be executed
	 */
	void addCommand( VCommand* command, bool execute = true );


	// limits
	/**
	 * Returns the actual undo limit.
	 *
	 * @return the undo limit
	 */
	unsigned int undoLimit() const
	{
		return m_undoLimit;
	}

	/**
	 * Sets a new undo limit.
	 *
	 * The undo limit controls how many commands are stored in the history.
	 * If the new limit is lower than the actual history size, the oldest
	 * commands are removed unitl the size matches the undo limit.
	 * 
	 * @param limit the new undo limit
	 */
	void setUndoLimit( unsigned int limit );

	/**
	 * Returns the actual redo limit.
	 *
	 * @return the redo limit
	 */
	unsigned int redoLimit() const
	{
		return m_redoLimit;
	}

	/**
	 * Sets a new redo limit.
	 *
	 * The redo limit controls how many undone commands are stored in history.
	 * If the new limit is lower than the actual number of undone commands,
	 * the newest commands are removed until the number matches the redo limit.
	 *
	 * @param limit the new redo limit
	 */
	void setRedoLimit( unsigned int limit );

	/**
	 * Read only access to the command history list.
	 *
	 * @return pointer to the list of commands
	 */
	const TQPtrList<VCommand>* commands() const
	{
		return & m_commands;
	}

public slots:
	/** Undoes the last command not already undone. */
	void undo();
	
	/** Redoes the last command not already undone. */
	void redo();
	
	/**
	 * Undoes the specified command.
	 *
	 * @param command the command to undo
	 */
	void undo( VCommand* command );

	/**
	 * Redoes the specified command.
	 *
	 * @param command the command to redo
	 */
	void redo( VCommand* command );
	
	/**
	 * Undoes all command up to the specified command.
	 *
	 * @param command the command up to which all later commands should be undone
	 */
	void undoAllTo( VCommand* command );

	/**
	 * Redoes all command up to the specified command.
	 *
	 * @param command the command up to which all former commands should be redone
	 */
	void redoAllTo( VCommand* command );

	/**
	 * Marks the actual document state as saved.
	 *
	 * The position within the list corresponding to the actual document state is saved.
	 */
	void documentSaved();

signals:
	/** This signal is emitted when the command history gets cleared. */
	void historyCleared();

	/** 
	 * This signal is emitted when a command is executed.
	 *
	 * The executed command is given as the argument.
	 */
	void commandExecuted( VCommand* );

	/** This signal is emitted when a command is executed. */
	void commandExecuted();

	/** 
	 * This signal is emitted when a command is added to the history.
	 *
	 * The added command is given as the argument.
	 */
	void commandAdded( VCommand* );

	/** This signal is emitted when the first (oldest) command is removed. */
	void firstCommandRemoved();

	/** This signal is emitted when the last (latest) command is removed. */
	void lastCommandRemoved();

	/** 
	* This signal is emitted when the actual document state matches the last saved one.
	*
	* Use documentSaved to set the last saved document state.
	*/
	void documentRestored();

private:
	// helpers
	void clipCommands();
	void updateActions();

	KarbonPart *m_part;
	unsigned int m_undoLimit;
	unsigned int m_redoLimit;
	TDEAction *m_undo;
	TDEAction *m_redo;
	TQPtrList<VCommand> m_commands;
	int m_savedPos;
};

#endif