summaryrefslogtreecommitdiffstats
path: root/src/resizeoverlay.h
blob: a2a2f129353bfb9961b54cea36c35720b8c17737 (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
/***************************************************************************
 *   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 RESIZEOVERLAY_H
#define RESIZEOVERLAY_H

// This file contains class definitions for different types of resizing and rotating

#include <tqcanvas.h>
#include <tqguardedptr.h>
#include <tqmap.h>
#include <tqobject.h>
#include <tqvaluelist.h>

class MechanicsItem;
class ResizeHandle;
class ResizeOverlay;
class TQEvent;

typedef TQMap< int, TQGuardedPtr<ResizeHandle> > ResizeHandleMap;

/**
@author David Saxton
*/
class ResizeHandle : public TQObject, public TQCanvasRectangle
{
	Q_OBJECT
  
public:
	/**
	 * Convenience enumeration for resize handle positioning. Note: this class
	 * does not make use of the values in this enumeration - it is just
	 * provided here for use by other classes.
	 */
	enum ResizeHandlePosition
	{
		rhp_none,
		rhp_topLeft,
		rhp_topMiddle,
		rhp_topRight,
		rhp_middleRight,
		rhp_bottomRight,
		rhp_bottomMiddle,
		rhp_bottomLeft,
		rhp_middleLeft,
		rhp_center,
		rhp_start,
		rhp_end
	};
	
	enum DrawType
	{
		// Draws a simple rectangle
		dt_point_rect,
		
		// Crosshair
		dt_point_crosshair,
		
		// Straight arrows in various directions
		dt_resize_forwardsDiagonal,
		dt_resize_backwardsDiagonal,
		dt_resize_vertical,
		dt_resize_horizontal,
		
		// Arrows as part of an arc
		dt_rotate_topLeft,
		dt_rotate_topRight,
		dt_rotate_bottomRight,
		dt_rotate_bottomLeft
	};

	ResizeHandle( ResizeOverlay *resizeOverlay, int id, DrawType drawType, int xsnap, int ysnap );
	~ResizeHandle();
	
	int id() const { return m_id; }
	int rtti() const;
	
	void setDrawType( DrawType drawType );
	void moveRH( double x, double y );
	void setHover( bool hover );
	
	static const TQPixmap& handlePixmap( DrawType drawType, bool hover );
	
	virtual TQPointArray areaPoints () const;
	
public slots:
	void slotMoveByX( double dx ) { moveBy( dx, 0 ); }
	void slotMoveByY( double dy ) { moveBy( 0, dy ); }
	
signals:
	void rhMovedBy( int id, double dx, double dy );
	void rhMovedByX( double dx );
	void rhMovedByY( double dy );
	
protected:
	virtual void drawShape( TQPainter &p );
	DrawType m_drawType;
	bool b_hover; // If true, then paint resize handle for mouse hovering over
	int m_id;
	int m_xsnap;
	int m_ysnap;
	ResizeOverlay *p_resizeOverlay;
	
};
typedef TQValueList<ResizeHandle*> ResizeHandleList;

/**
@author David Saxton
*/
class ResizeOverlay : public TQObject
{
	Q_OBJECT
  
public:
	ResizeOverlay( Item *parent );
	~ResizeOverlay();
	
	Item *parentItem() const { return p_item; }
	
	/**
	 * Shows / hides the resize handles. They are hidden by default.
	 */
	void showResizeHandles( bool show );
	/**
	 * Sets the visibility. Visibility is true by default.
	 */
	void setVisible( bool visible );
	/**
	 * Reinherit this function to determine whether the X coordinate of the spot
	 * that the resize handle has moved into is valid or not
	 */
	virtual bool isValidXPos( ResizeHandle *rh ) { Q_UNUSED(rh); return true; }
	/**
	 * Reinherit this function to determine whether the Y coordinate of the spot
	 * that the resize handle has moved into is valid or not
	 */
	virtual bool isValidYPos( ResizeHandle *rh ) { Q_UNUSED(rh); return true; }
	
public slots:
	void slotMoveAllResizeHandles( double dx, double dy );
	
protected slots:
	virtual void slotResizeHandleMoved( int id, double dx, double dy ) = 0;
	
protected:
	/**
	 * Connects up the given resize handles so that they are always kept at the
	 * same horizontal coordinate
	 */
	void syncX( ResizeHandle *rh1, ResizeHandle *rh2 );
	void syncX( ResizeHandle *rh1, ResizeHandle *rh2, ResizeHandle *rh3 );
	/**
	 * Connects up the given resize handles so that they are always kept at the
	 * same vertical coordinate
	 */
	void syncY( ResizeHandle *rh1, ResizeHandle *rh2 );
	void syncY( ResizeHandle *rh1, ResizeHandle *rh2, ResizeHandle *rh3 );
	/**
	 * Returns a pointer to the ResizeHandle with the given id, or 0 if no such
	 * handle exists
	 */
	ResizeHandle *resizeHandle( int id );
	/**
	 * Creates and attaches the resize handle with the given DrawType. If a
	 * ResizeHandle with the given id exists, will return a pointer to that
	 * instead
	 */
	ResizeHandle *createResizeHandle( int id, ResizeHandle::DrawType drawType, int xsnap = 1, int ysnap = 1 );
	/**
	 * Removes the resize handle with the given id
	 */
	void removeResizeHandle( int id );
	
	Item *p_item;
	ResizeHandleMap m_resizeHandleMap;
	bool b_showResizeHandles;
	bool b_visible;
};


/**
@author David Saxton
*/
class MechanicsItemOverlay : public ResizeOverlay
{
Q_OBJECT
  
public:
	MechanicsItemOverlay( MechanicsItem *parent );
	~MechanicsItemOverlay();
	
public slots:
	void slotUpdateResizeHandles();
	
protected slots:
	virtual void slotResizeHandleMoved( int id, double dx, double dy );
	
protected:
	ResizeHandle *m_tl;
	ResizeHandle *m_tm;
	ResizeHandle *m_tr;
	ResizeHandle *m_mr;
	ResizeHandle *m_br;
	ResizeHandle *m_bm;
	ResizeHandle *m_bl;
	ResizeHandle *m_ml;
	ResizeHandle *m_mm;
	MechanicsItem *p_mechanicsItem;
};


/**
@author David Saxton
*/
class RectangularOverlay : public ResizeOverlay
{
Q_OBJECT
  
public:
	RectangularOverlay( Item *item, int xsnap = 1, int ysnap = 1 );
	void removeTopMiddle();
	void removeBotMiddle();
	/**
	 * Get the size rectangle from the position of the handles. If the size
	 * is invalid (e.g. the parent Item does not consider it a valid size,
	 * then *ok is set to false; otherwise to true.
	 * @returns the sizerect, regardless of whether or not it is valid
	 */
	TQRect getSizeRect( bool *ok = 0l, bool *widthOk = 0l, bool *heightOk = 0l ) const;
	virtual bool isValidXPos( ResizeHandle *rh );
	virtual bool isValidYPos( ResizeHandle *rh );
	
public slots:
	void slotUpdateResizeHandles();
	
protected slots:
	virtual void slotResizeHandleMoved( int id, double dx, double dy );
	
protected:
	ResizeHandle *m_tl;
	ResizeHandle *m_tm;
	ResizeHandle *m_tr;
	ResizeHandle *m_mr;
	ResizeHandle *m_br;
	ResizeHandle *m_bm;
	ResizeHandle *m_bl;
	ResizeHandle *m_ml;
};


/**
@author David Saxton
*/
class LineOverlay : public ResizeOverlay
{
	Q_OBJECT
  
	public:
		LineOverlay( Item * parent );
		TQPoint startPoint() const;
		TQPoint endPoint() const;
	
	public slots:
		void slotUpdateResizeHandles();
		
	protected slots:
		virtual void slotResizeHandleMoved( int id, double dx, double dy );
		
	protected:
		ResizeHandle * m_pStart;
		ResizeHandle * m_pEnd;
};

#endif