summaryrefslogtreecommitdiffstats
path: root/src/itemdocument.h
blob: 1194ee3d0a4851970772c0744b587e0691084a23 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/***************************************************************************
 *   Copyright (C) 2005 by David Saxton                                    *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#ifndef ITEMDOCUMENT_H
#define ITEMDOCUMENT_H

#include <document.h>
#include <tqcanvas.h>
#include <tqptrstack.h>
#include <tqvaluevector.h>

class Canvas;
class CanvasTip;
class Connector;
class CMManager;
class ECNode;
class Item;
class ItemDocumentData;
class ItemGroup;
class KTechlab;
class Operation;

class TDEActionMenu;
class TQCanvasItem;

typedef TQPtrStack<ItemDocumentData> IDDStack;
typedef TQGuardedPtr<Item> GuardedItem;
typedef TQMap< int, GuardedItem > IntItemMap;
typedef TQValueList<GuardedItem> ItemList;
typedef TQValueList<TQPoint> TQPointList;

/**
@author David Saxton
*/
class ItemDocument : public Document
{
	Q_OBJECT
  
	public:
		ItemDocument( const TQString &caption, KTechlab *ktechlab, const char *name = 0 );
		~ItemDocument();

		class Z
		{
			public:
				enum
				{
					Select				= 10000000,
					Connector			= 20000000,
					Item				= 30000000,
					RaisedItem			= 40000000,
					ResizeHandle 		= 50000000,
					Tip					= 60000000,
					ConnectorCreateLine	= 70000000,
					
					// How much "Z" separates items stacked on each other
					DeltaItem			= 10000
				};
		};

		class RTTI
		{
			public:
				enum
				{
					None				= 1000,
					CNItem				= 1001,
					Node				= 1002,
					Connector			= 1003,
					Pin					= 1004,
					Widget				= 1005,
					MechanicsItem		= 1006,
					ResizeHandle		= 1007,
					DrawPart			= 1008,
					ConnectorLine		= 1009
				};
		};
		
		/**
		 * Some things (such as the canvas getting resized, connectors being
		 * invalidated, need to be done after editing operations have finished,
		 * etc, and they also need to be done in the order given in the
		 * enumeration below.
		 */
		class ItemDocumentEvent
		{
			public: enum type
			{
				ResizeCanvasToItems				= 1 << 0,
				UpdateNodeGroups				= 1 << 1,
				RerouteInvalidatedConnectors	= 1 << 2,
				UpdateZOrdering					= 1 << 3,
			};
		};
	
		virtual void fileSave();
		virtual void fileSaveAs();
		virtual void print();
		virtual bool openURL( const KURL &url );
		/**
		 * Attempt to register the item, returning true iff successful
		 */
		virtual bool registerItem( TQCanvasItem *qcanvasItem );
		/**
		 * Will attempt to create an item with the given id at position p. Some item
		 * (such as PIC/START) have restrictions, and can only have one instance of
		 * themselves on the canvas, and adds the operation to the undo list
		 */
		virtual Item* addItem( const TQString &id, const TQPoint &p, bool newItem ) = 0;
		/**
		 * @returns A pointer to the canvas
		 */
		Canvas *canvas() const { return m_canvas; }
		/**
		 * Attemtps to register a unique id for the canvas view of an item on the
		 * canvas. If the id does not already exist, will return true; otherwise
		 * the function will return false.
		 */
		bool registerUID( const TQString & uid );
		/**
		 * Generates a unique id based on a possibly unique component name.
		 */
		TQString generateUID( TQString name );
		/**
		 * Unlists the given id as one that is used.
		 * @see registerUID
		 */
		void unregisterUID( const TQString & uid );
		/**
		 * @return Whether or not the item is valid; i.e. is appropriate to the
		 * document being edited, and does not have other special restrictions
		 * on it (such as only allowing one instance of the Start part in
		 * FlowCode).
		 */
		virtual bool isValidItem( Item *item ) = 0;
		/**
		 * @return Whether or not the item is valid; i.e. is appropriate to the
		 * document being edited, and does not have other special restrictions
		 * on it (such as only allowing one instance of the Start part in
		 * FlowCode).
		 */
		virtual bool isValidItem( const TQString &itemId ) = 0;
		/**
		 * Increases the "height" of the given list of items by "one".
		 */
		void raiseZ( const ItemList & itemList );
		/**
		 * Decreases the "height" of the given list of items by "one".
		 */
		void lowerZ( const ItemList & itemList );
		/**
		 * @return ItemGroup that is used as the select list for this document.
		 */
		virtual ItemGroup *selectList() const = 0;
		/**
		 * Deselects any currently selected items
		 */
		void unselectAll();
		/**
		 * Select a list of TQCanvasItem's
		 */
		void select( const TQCanvasItemList & list );
		/**
		 * Select a TQCanvasItem
		 */
		void select( TQCanvasItem * item );
		/**
		 * Unselects the item
		 */
		void unselect( TQCanvasItem *qcanvasItem );
		/**
		 * Deletes anything waiting to be deleted.
		 */
		virtual void flushDeleteList() = 0;
		/**
		 * Returns a rubber-band rectangle that contains all of the items on the
		 * canvas, padded out by a small border.
		 */
		TQRect canvasBoundingRect() const;
		/**
		 * Returns a pointer to a Item on the canvas with the given id,
		 * or NULL if no such Item exists.
		 */
		Item* itemWithID( const TQString & );
		/**
		 * Returns true if the the user can perform an undo action
		 * (i.e. the undo stack is not empty)
		 */
		virtual bool isUndoAvailable() const;
		/**
		 * Returns true if the the user can perform an redo action
		 * (i.e. the redo stack is not empty)
		 */
		virtual bool isRedoAvailable() const;
		/**
		 * Returns the top item at point (x, y), or NULL if there is no item there
		 */
		TQCanvasItem* itemAtTop( const TQPoint &pos ) const;
		/**
		 * Called when the canvas is clicked on with the right mouse button.
		 * Popups up a menu for editing operations
		 */
		virtual void canvasRightClick( const TQPoint &pos, TQCanvasItem* item );
		/**
		 * List of items in the ItemDocument
		 */
		ItemList itemList() const { return m_itemList; }
		/**
		 * Set the given TQCanvasItem (which will attempt to be casted to known
		 * items to be deleted.
		 */
		virtual void appendDeleteList( TQCanvasItem * ) = 0;
		/**
		 * Save the current state of the document to the undo/redo history.
		 * @param actionTicket if this is non-negative, and the last state save
		 * also had the same actionTicket, then the next state save will
		 * overwrite the previous state save.
		 * @see getActionTicket
		 */
		void requestStateSave( int actionTicket = -1 );
		/**
		 * Returns a unique id, for use in requestStateSave
		 */
		int getActionTicket() const { return m_nextActionTicket++; }
		/**
		 * Clears the undo / redo history
		 */
		void clearHistory();
		/**
		 * Requests an event to be done after other stuff (editing, etc) is finished.
		 */
		void requestEvent( ItemDocumentEvent::type type );
		/**
		 * Called from Canvas (when TQCanvas::advance is called).
		 */
		virtual void update();

	public slots:
		virtual void undo();
		virtual void redo();
		virtual void cut();
		virtual void paste();
		/**
		 * Selects everything in the view.
		 */
		virtual void selectAll() = 0;
		/**
		 * Increases the "height" of the selected items.
		 */
		void raiseZ();
		/**
		 * Decreases the "height" of the selected items.
		 */
		void lowerZ();
		/**
		 * Brings up a file dialog requesting the location of the file to export
		 * to, and then exports an image of the canvas.
		 */
		void exportToImage();
		/**
		 * Deletes whatever is selected.
		 */
		virtual void deleteSelection() {};
		/**
		 * Called when the user presses Escape (or similar)
		 */
		void cancelCurrentOperation();
		/**
		 * Sets the y-positions of the selected items to the average of the
		 * initial y-positions.
		 */
		void alignHorizontally();
		/**
		 * Sets the x-positions of the selected items to the average of the
		 * initial x-positions.
		 */
		void alignVertically();
		/**
		 * Averages out the horizontal spacing between the selected items.
		 */
		void distributeHorizontally();
		/**
		 * Averages out the vertical spacing between the selected items.
		 */
		void distributeVertically();
		/**
		 * Adds an items not in the Z ordering to the ordering, and removes any
		 * items from the Z ordering if they have parents. Then, calls all items
		 * found in the ordering to tell them their Z position.
		 */
		void slotUpdateZOrdering();
		/**
		 * Call this with ItemDocument::DrawAction to start drawing the given thing
		 */
		void slotSetDrawAction( int da );
		/**
		 * Sets the editing mode to repeatedly creating a CNItem
		 * with the given id. Usually called when the user double-clicks on
		 * the component box.
		 */
		void slotSetRepeatedItemId( const TQString &id );
		/**
		 * Unsets the editing mode from repeatedly creating a CNItem
		 */
		void slotUnsetRepeatedItemId();
		/**
		 * Called when the user changes the configuration.
		 * This, for example, will tell the CNItems on the canvas to update
		 * their configuration.
		 */
		virtual void slotUpdateConfiguration();
		/**
		 * Enables / disables / selects various actions depending on
		 * what is selected or not.
		 */
		virtual void slotInitItemActions( Item *item = 0 );
		/**
		 * Process queued events (see ItemDocument::ItemDocumentEvent).
		 */
		void processItemDocumentEvents();
	
	signals:
		/**
		 * Emitted when a Item is selected
		 */
		void itemSelected( Item *item );
		/**
		 * Emitted when a Item is unselected
		 */
		void itemUnselected( Item *item = 0 );
		
	protected:
		/**
		 * Called from registerItem when a new item is added.
		 */
		virtual void itemAdded( Item * item );
		virtual void handleNewView( View *view );
		/**
		 * Set to true to remove buttons and grid and so on from the canvas, set false to put them back
		 */
		void setSVGExport( bool svgExport );
		void writeFile();
		/**
		 * Reinherit this if you want to add any options to the right-click context
		 */
		virtual void fillContextMenu( const TQPoint & pos );
		/**
		 * Reads the background settings (grid-colour, underlying colour) from the Config settings,
		 * and generates the background pixmap from those settings
		 */
		void updateBackground();
		/**
		 * Sets the canvas size to both (a) containing all items present on the
		 * canvas, and (b) no smaller than the smallest view of the canvas. This
		 * function should only be called by processItemDocumentEvents - a resize
		 * request must be made with requestEvent.
		 */
		void resizeCanvasToItems();
	
		Canvas *m_canvas;
		KTechlab *p_ktechlab;
		TQStringList m_idList;
		static int m_nextActionTicket;
		uint m_nextIdNum;
		bool m_bIsLoading;
		TQSize m_oldCanvasSize;
		CMManager *m_cmManager;
		ItemList m_itemDeleteList;
		ItemList m_itemList;
		IDDStack m_undoStack;
		IDDStack m_redoStack;
		ItemDocumentData * m_currentState;
		int m_currentActionTicket;
		ItemDocumentData * m_savedState; // Pointer to the document data that holds the state when it saved
		TQString m_fileExtensionInfo; // For displaying in the save file dialog
		CanvasTip * m_canvasTip;
		IntItemMap m_zOrder;
		TDEActionMenu * m_pAlignmentAction;
		TQTimer * m_pEventTimer;
		unsigned m_queuedEvents; // OR'ed together list of ItemDocumentEvent::type
		
		friend class ICNView;
		friend class ItemView;
};


/**
@author David Saxton
*/
class Canvas : public TQCanvas
{
	Q_OBJECT
  
	public:
		Canvas( ItemDocument *itemDocument, const char * name = 0 );
		
		/**
		 * Sets a message to be displayed on the canvas for a brief period of
		 * time. If this is called with an empty message, then any existing
		 * message will be removed.
		 */
		void setMessage( const TQString & message );
		
		virtual void update();
		
	public slots:
		void slotSetAllChanged() { setAllChanged(); }
	
	protected:
		virtual void drawBackground ( TQPainter & painter, const TQRect & clip );
		virtual void drawForeground ( TQPainter & painter, const TQRect & clip );
	
		ItemDocument *p_itemDocument;
		
		TQString m_message;
		TQTimer * m_pMessageTimeout;
};


/**
@author David Saxton
*/
class CanvasTip : public TQCanvasText
{
public:
	CanvasTip( ItemDocument *itemDocument, TQCanvas *qcanvas );
	virtual ~CanvasTip();
	
	void displayVI( ECNode *node, const TQPoint &pos );
	void displayVI( Connector *connector, const TQPoint &pos );
	
protected:
	virtual void draw( TQPainter &p );
	bool updateVI();
	void display( const TQPoint &pos );
	TQString displayText( unsigned num ) const;
	
	TQValueVector<double> m_v;
	TQValueVector<double> m_i;
	ItemDocument *p_itemDocument;
};


#endif