summaryrefslogtreecommitdiffstats
path: root/src/canvasmanipulator.h
blob: 8ddc253dc81e728c7c489dd1e38016b14027b904 (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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/***************************************************************************
 *   Copyright (C) 2004-2005 by David Saxton                               *
 *   [email protected]                                                    *
 *                                                                         *
 *   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 CANVAsmANIPULATOR_H
#define CANVAsmANIPULATOR_H

#include "eventinfo.h"

#include <tqcanvas.h>
#include <tqguardedptr.h>

class CanvasManipulator;
class Connector;
class CMManager;
class CNItem;
class CNItemGroup;
class EventInfo;
class FlowContainer;
class ICNDocument;
class Item;
class ItemDocument;
class ItemGroup;
class ItemView;
class ManipulatorInfo;
class ManualConnectorDraw;
class MechanicsItem;
class MechanicsGroup;
class MechanicsDocument;
class Node;
class NodeGroup;
class ResizeHandle;

class TQCanvas;
class TQCanvasItem;
class TQCanvasLine;
class TQCanvasRectangle;
class TQMouseEvent;
class TQTimer;
class TQWheelEvent;

typedef CanvasManipulator*(*CreateManipulatorPtr)( ItemDocument *, CMManager * );
typedef bool(*AcceptManipulationPtr)( uint eventState, uint cmState, uint itemType, uint cnItemType );
typedef TQValueList<NodeGroup*> NodeGroupList;
typedef TQValueList<TQGuardedPtr<Connector> > ConnectorList;
typedef TQValueList<TQPoint> TQPointList;


class ManipulatorInfo
{
public:
	ManipulatorInfo();
	AcceptManipulationPtr m_acceptManipulationPtr;
	CreateManipulatorPtr m_createManipulatorPtr;
};
typedef TQValueList<ManipulatorInfo*> ManipulatorInfoList;


/**
Handles canvas manipulation, such as moving an item or resizing the canvas
@author David Saxton
*/
class CMManager : public TQObject
{
Q_OBJECT
  TQ_OBJECT
public:
	enum EventState
	{
		es_right_click =	1 << 0,
		es_ctrl_pressed =	1 << 1
	};
	enum CMState
	{
		cms_repeated_add =	1 << 0,
		cms_manual_route =	1 << 1,
		cms_draw =			1 << 2
	};
	enum ItemType
	{
		it_none =			1 << 0,
		it_node =			1 << 1,
		it_connector =		1 << 2,
		it_pin =			1 << 3,
		it_canvas_item =	1 << 4,
		it_mechanics_item =	1 << 5,
		it_resize_handle =	1 << 6,
		it_drawpart =		1 << 7
	};
	enum ItemStateInfo
	{
		isi_isMovable = 0x2,
	};
	CMManager( ItemDocument *itemDocument );
	~CMManager();
	/**
	 * Called when the user single-clicks the mouse
	 */
	void mousePressEvent( EventInfo eventInfo );
	/**
	 * Called when the user releases the mouse
	 */
	void mouseReleaseEvent( const EventInfo &eventInfo );
	/**
	 * Called when the user double clicks the mouse
	 */
	void mouseDoubleClickEvent( const EventInfo &eventInfo );
	/**
	 * Called when the user moves the mouse
	 */
	void mouseMoveEvent( const EventInfo &eventInfo );
	/**
	 * Called when the user scrolls the mouse
	 */
	void wheelEvent( const EventInfo &eventInfo );
	/**
	 * Set a current CMState to true or false
	 */
	void setCMState( CMState type, bool state );
	/**
	 * Cancels the current manipulation (if there is one)
	 */
	void cancelCurrentManipulation();
	CanvasManipulator * currentManipulator() const { return m_canvasManipulator; }
	void setRepeatedAddId( const TQString & repeatedId = TQString() );
	uint cmState() const { return m_cmState; }
	void addManipulatorInfo( ManipulatorInfo *info );
	TQString repeatedItemId() const { return m_repeatedItemId; }
	void setDrawAction( int drawAction );
	int drawAction() const { return m_drawAction; }
	
public slots:
	void slotSetManualRoute( bool manualRoute );
	
signals:
	void manualRoutingChanged( bool manualRouting );
	
protected:
	/**
	 * Called when the mouse is moved or released, with the ResizeHandle that
	 * the mouse is currently over (which can be null). Updates which handle is
	 * selected, etc.
	 */
	void updateCurrentResizeHandle( ResizeHandle * mouseOver );
	CanvasManipulator *m_canvasManipulator;
	uint m_cmState;
	TQString m_repeatedItemId;
	ItemDocument *p_itemDocument;
	ManipulatorInfoList m_manipulatorInfoList;
	TQGuardedPtr<Item> p_lastMouseOverItem; // Pointer to the item where the mouse was last over - this is used to determine when mouse
	TQGuardedPtr<ResizeHandle> p_lastMouseOverResizeHandle;
	TQGuardedPtr<Item> p_lastItemClicked;
	TQTimer *m_allowItemScrollTmr; // When a user scrolls on the canvas, we don't want to stop scrolling when the user gets to (e.g.) a scrollable widget. So this timer prevents scrolling a widget for a few hundred milliseconds after a scroll event if it was initiated over the canvas
	bool b_allowItemScroll; // See above.
	int m_drawAction;
	
private slots:
	void slotAllowItemScroll() { b_allowItemScroll = true; }
};


/**
Abstract class for a "editing operation" on the ICNDocument, such as moving an item or resizing the canvas
@author David Saxton
*/
class CanvasManipulator
{
public:
	CanvasManipulator( ItemDocument *itemDocument, CMManager *cmManager );
	virtual ~CanvasManipulator();
	enum Type
	{
		RepeatedItemAdd,
		RightClick,
		AutoConnector,
		ManualConnector,
		ItemMove,
		ItemResize,
		MechItemMove,
		Select,
		CanvasResize,
		ItemDrag,
		Draw
	};
	virtual Type type() const = 0;
	/**
	 * Called when the user single-clicks the mouse
	 * @returns true if the manipulation operation has finished
	 */
	virtual bool mousePressedInitial( const EventInfo &/*info*/ ) { return false; }
	/**
	 * Called when the user single-clicks the mouse after the first time (only
	 * applicable for those operations who are not oneClick
	 * @returns true if the manipulation operation has finished
	 */
	virtual bool mousePressedRepeat( const EventInfo &/*info*/ ) { return false; };
	/**
	 * Called when the user moves the mouse
	 * @returns true if the manipulation operation has finished
	 */
	virtual bool mouseMoved( const EventInfo &/*info*/ ) { return false; };
	/**
	 * Called when the user releases the mouse
	 * @returns true if the manipulation operation has finished
	 */
	virtual bool mouseReleased( const EventInfo &/*info*/ ) { return true; }
	
protected:
	Type m_type;
	EventInfo m_eventInfo;
	TQPoint m_prevPos;
	ItemDocument *p_itemDocument;
	ICNDocument *p_icnDocument;
	MechanicsDocument *p_mechanicsDocument;
	TQCanvas *p_canvas;
	ItemGroup *p_selectList;
	CNItemGroup *p_cnItemSelectList;
	MechanicsGroup *p_mechItemSelectList;
	CNItem *p_cnItemClickedOn;
	MechanicsItem *p_mechanicsItemClickedOn;
	CMManager *p_cmManager;
};


/**
@author David Saxton
*/
class CMRepeatedItemAdd : public CanvasManipulator
{
public:
	CMRepeatedItemAdd( ItemDocument *itemDocument, CMManager *cmManager );
	virtual ~CMRepeatedItemAdd();
	virtual Type type() const { return RepeatedItemAdd; }
	
	static CanvasManipulator* construct( ItemDocument *itemDocument, CMManager *cmManager );
	static ManipulatorInfo *manipulatorInfo();
	static bool acceptManipulation( uint eventState, uint cmState, uint itemType, uint cnItemType );
	
	virtual bool mousePressedInitial( const EventInfo &info );
	virtual bool mousePressedRepeat( const EventInfo &info );
	virtual bool mouseMoved( const EventInfo &info );
	virtual bool mouseReleased( const EventInfo &info );
	
protected:
};



/**
@author David Saxton
*/
class CMRightClick : public CanvasManipulator
{
public:
	CMRightClick( ItemDocument *itemDocument, CMManager *cmManager );
	virtual ~CMRightClick();
	virtual Type type() const { return RightClick; }
	
	static CanvasManipulator* construct( ItemDocument *itemDocument, CMManager *cmManager );
	static ManipulatorInfo *manipulatorInfo();
	static bool acceptManipulation( uint eventState, uint cmState, uint itemType, uint cnItemType );
	
	virtual bool mousePressedInitial( const EventInfo &info );
	virtual bool mouseMoved( const EventInfo &info );
	virtual bool mouseReleased( const EventInfo &info );
	
protected:
};


/**
@author David Saxton
*/
class ConnectorDraw : public CanvasManipulator
{
	public:
		ConnectorDraw( ItemDocument *itemDocument, CMManager *cmManager );
		virtual ~ConnectorDraw();
		
		/**
		 * Returns the colour used to indicate that the current connection
		 * being drawn is valid. Invalid colour is black.
		 */
		static TQColor validConnectionColor();
		
	protected:
		/**
		 * If the node has more than 2 connections, return one of the
		 * connectors
		*/
		Connector * toConnector( Node * node );
		/**
		 * Converts the given qcanvasitem to an appropriate node or connector.
		 * @param posIsExact if true, then only gets an appropriate node or
		 * connector when the to-be end-point of the new connector will coincide
		 * with pos (i.e. auto-connector will call this with posIsExact = false,
		 * and manual-connector will call this with posIsExact = true).
		 */
		void grabEndStuff( TQCanvasItem * endItem, const TQPoint & pos, bool posIsExact );
		/**
		 * Returns the closest point to the clickPos that is on the given
		 * connector.
		 */
		TQPoint toValidPos( const TQPoint & clickPos, Connector * clickedConnector ) const;
		
		TQGuardedPtr<Node> p_startNode;
		TQGuardedPtr<Connector> p_startConnector;
		Node * p_endNode;
		Connector * p_endConnector;
		TQPoint startConnectorPoint;
};


/**
@author David Saxton
*/
class CMAutoConnector : public ConnectorDraw
{
public:
	CMAutoConnector( ItemDocument *itemDocument, CMManager *cmManager );
	virtual ~CMAutoConnector();
	virtual Type type() const { return AutoConnector; }
	
	static CanvasManipulator* construct( ItemDocument *itemDocument, CMManager *cmManager );
	static ManipulatorInfo *manipulatorInfo();
	static bool acceptManipulation( uint eventState, uint cmState, uint itemType, uint cnItemType );
	
	virtual bool mousePressedInitial( const EventInfo &info );
	virtual bool mouseMoved( const EventInfo &info );
	virtual bool mouseReleased( const EventInfo &info );
	
protected:
	TQCanvasLine *m_connectorLine;
};


/**
@author David Saxton
*/
class CMManualConnector : public ConnectorDraw
{
public:
	CMManualConnector( ItemDocument *itemDocument, CMManager *cmManager );
	virtual ~CMManualConnector();
	virtual Type type() const { return ManualConnector; }
	
	static CanvasManipulator* construct( ItemDocument *itemDocument, CMManager *cmManager );
	static ManipulatorInfo *manipulatorInfo();
	static bool acceptManipulation( uint eventState, uint cmState, uint itemType, uint cnItemType );
	
	virtual bool mousePressedInitial( const EventInfo &info );
	virtual bool mousePressedRepeat( const EventInfo &info );
	virtual bool mouseMoved( const EventInfo &info );
	virtual bool mouseReleased( const EventInfo &info );
	
protected:
	ConnectorList m_fixedRouteConnectors;
	ManualConnectorDraw *m_manualConnectorDraw;
};


/**
@author David Saxton
*/
class CMItemMove : public CanvasManipulator
{
public:
	CMItemMove( ItemDocument *itemDocument, CMManager *cmManager );
	virtual ~CMItemMove();
	virtual Type type() const { return ItemMove; }
	
	static CanvasManipulator* construct( ItemDocument *itemDocument, CMManager *cmManager );
	static ManipulatorInfo *manipulatorInfo();
	static bool acceptManipulation( uint eventState, uint cmState, uint itemType, uint cnItemType );
	
	virtual bool mousePressedInitial( const EventInfo &info );
	virtual bool mouseMoved( const EventInfo &info );
	virtual bool mouseReleased( const EventInfo &info );
	
protected:
	ConnectorList m_translatableConnectors;
	NodeGroupList m_translatableNodeGroups;
	FlowContainer *p_flowContainerCandidate;
};


/**
@author David Saxton
*/
class CMItemResize : public CanvasManipulator
{
public:
	CMItemResize( ItemDocument *itemDocument, CMManager *cmManager );
	virtual ~CMItemResize();
	virtual Type type() const { return ItemResize; }
	
	static CanvasManipulator* construct( ItemDocument *itemDocument, CMManager *cmManager );
	static ManipulatorInfo *manipulatorInfo();
	static bool acceptManipulation( uint eventState, uint cmState, uint itemType, uint cnItemType );
	
	virtual bool mousePressedInitial( const EventInfo &info );
	virtual bool mouseMoved( const EventInfo &info );
	virtual bool mouseReleased( const EventInfo &info );
	
protected:
	ResizeHandle *p_resizeHandle;
	double m_rh_dx;
	double m_rh_dy;
};


/**
@author David Saxton
*/
class CMMechItemMove : public CanvasManipulator
{
public:
	CMMechItemMove( ItemDocument *itemDocument, CMManager *cmManager );
	virtual ~CMMechItemMove();
	virtual Type type() const { return MechItemMove; }
	
	static CanvasManipulator* construct( ItemDocument *itemDocument, CMManager *cmManager );
	static ManipulatorInfo *manipulatorInfo();
	static bool acceptManipulation( uint eventState, uint cmState, uint itemType, uint cnItemType );
	
	virtual bool mousePressedInitial( const EventInfo &info );
	virtual bool mouseMoved( const EventInfo &info );
	virtual bool mouseReleased( const EventInfo &info );
	
protected:
	uint m_prevClickedOnSM; // Previous select mode of the item that was clicked on
};


/**
@author David Saxton
*/
class SelectRectangle
{
	public:
		SelectRectangle( int x, int y, int w, int h, TQCanvas *qcanvas );
		~SelectRectangle();
		
		void setSize( int w, int h );
		TQCanvasItemList collisions();
		
	protected:
		TQCanvasLine *m_topLine;
		TQCanvasLine *m_rightLine;
		TQCanvasLine *m_bottomLine;
		TQCanvasLine *m_leftLine;
		const int m_x;
		const int m_y;
		int m_w;
		int m_h;
		int m_prevCollisions_w;
		int m_prevCollisions_h;
		TQCanvasItemList m_prevCollisions;
};


/**
@author David Saxton
*/
class CMSelect : public CanvasManipulator
{
public:
	CMSelect( ItemDocument *itemDocument, CMManager *cmManager );
	virtual ~CMSelect();
	virtual Type type() const { return Select; }
	
	static CanvasManipulator* construct( ItemDocument *itemDocument, CMManager *cmManager );
	static ManipulatorInfo *manipulatorInfo();
	static bool acceptManipulation( uint eventState, uint cmState, uint itemType, uint cnItemType );
	
	virtual bool mousePressedInitial( const EventInfo &info );
	virtual bool mouseMoved( const EventInfo &info );
	virtual bool mouseReleased( const EventInfo &info );
	
protected:
	SelectRectangle *m_selectRectangle;
};


/**
@author David Saxton
*/
class CMItemDrag : public CanvasManipulator
{
public:
	CMItemDrag( ItemDocument *itemDocument, CMManager *cmManager );
	virtual ~CMItemDrag();
	virtual Type type() const { return ItemDrag; }
	
	static CanvasManipulator* construct( ItemDocument *itemDocument, CMManager *cmManager );
	static ManipulatorInfo *manipulatorInfo();
	static bool acceptManipulation( uint eventState, uint cmState, uint itemType, uint cnItemType );
	
	virtual bool mousePressedInitial( const EventInfo &info );
	virtual bool mouseMoved( const EventInfo &info );
	virtual bool mouseReleased( const EventInfo &info );
	
protected:
	bool b_dragged;
};



/**
@author David Saxton
A TQCanvasEllipse that uses a pen (not a brush) to paint
*/
class CanvasEllipseDraw : public TQCanvasEllipse
{
	public:
		CanvasEllipseDraw( int x, int y, TQCanvas * canvas );
		
	protected:
		virtual void drawShape( TQPainter & p );
};


/**
@author David Saxton
*/
class CMDraw : public CanvasManipulator
{
	public:
		CMDraw( ItemDocument *itemDocument, CMManager *cmManager );
		virtual ~CMDraw();
		virtual Type type() const { return Draw; }
	
		static CanvasManipulator* construct( ItemDocument *itemDocument, CMManager *cmManager );
		static ManipulatorInfo *manipulatorInfo();
		static bool acceptManipulation( uint eventState, uint cmState, uint itemType, uint cnItemType );
	
		virtual bool mousePressedInitial( const EventInfo &info );
		virtual bool mouseMoved( const EventInfo &info );
		virtual bool mouseReleased( const EventInfo &info );
	
	protected:
		TQCanvasRectangle * m_pDrawRectangle;
		CanvasEllipseDraw * m_pDrawEllipse;
		TQCanvasLine * m_pDrawLine;
};


/**
@author David Saxton
*/
class ManualConnectorDraw
{
	public:
		ManualConnectorDraw( ICNDocument *_icnDocument, const TQPoint &initialPos );
		virtual ~ManualConnectorDraw();
		
		/**
		 * Called when the mouse is moved.
		 * Normally will do something like updating the connector route
		 */
		void mouseMoved( const TQPoint &pos );
		/**
		 * Called when the user clicks the mouse. If the connector finishes on a
		 * valid TQCanvasItem (Node or Connetor), then this is returned. Otherwise,
		 * null is returned.
		 */
		TQCanvasItem * mouseClicked( const TQPoint &pos );
		/**
		 * Returns the list of points that define the manual connection route
		 */
		TQPointList pointList();
		/**
		 * Sets the colour used to draw the connection lines.
		 */
		void setColor( const TQColor & color );

	protected:
		void updateConnectorEnds();
	
		TQValueList<TQCanvasLine*> m_connectorLines;
		ICNDocument *icnDocument;
	
		bool b_currentVertical;
		bool b_orientationDefined;
	
		TQPoint m_initialPos;
		TQPoint m_previousPos;
		TQPoint m_currentPos;
	
		TQCanvasLine *m_currentCon;
		TQCanvasLine *m_previousCon;
	
		// The first item that we clicked on
		TQCanvasItem *p_initialItem;
	
		TQColor m_color;
};


#endif