summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/linepath.h
blob: eb7c73b9e373f7b30cac65e914be7c6ace7b227f (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
/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   copyright (C) 2002-2006                                               *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

#ifndef LINEPATH_H
#define LINEPATH_H
#include <tqobject.h>
#include <tqptrlist.h>
#include <tqpoint.h>
#include <tqpointarray.h>
#include <tqcanvas.h>
#include <tqpainter.h>
#include "umlnamespace.h"

/* how many pixels a user could click around a point */
#define POINT_DELTA 5

class AssociationWidget;
class UMLView;

// TQt forward declarations
class TQDataStream;
class TQDomDocument;
class TQDomElement;

// typedefs
typedef TQPtrList<TQCanvasLine> LineList;
typedef TQPtrListIterator<TQCanvasLine> LineListIt;

typedef TQPtrList<TQCanvasRectangle> RectList;
typedef TQPtrListIterator<TQCanvasRectangle> RectListIt;


/**
 *@author Paul Hensgen
 * Bugs and comments to uml-devel@lists.sf.net or http://bugs.trinitydesktop.org
 */
class LinePath : public TQObject {
    Q_OBJECT
  
public:
    /**
    *   Constructor
    */
    LinePath();

    /**
    *   Deconstructor
    */
    ~LinePath();

    /**
    *   equal to (==) operator
    */
    bool operator==( LinePath & rhs );

    /**
    *   copy ( = ) operator
    */
    LinePath & operator=( LinePath & rhs );

    /**
    *   Enum to tell whether the line docks top/bottom or left/right
    */
    enum Region {
        TopBottom, LeftRight
    };

    /**
    *   Tell the line where the line docks
    */
    void setDockRegion( Region region );

    bool hasPoints ();
    void dumpPoints ();

    /**
    *   Returns the point at the point index.
    */
    TQPoint getPoint( int pointIndex );

    /**
    *   Sets the position of an already set point.
    */
    bool setPoint( int pointIndex, const TQPoint &point );

    /**
    *   Checks, if we are at an end of the segment or somewhere in the middle.
    *   We use the delta, because with the mouse it is hard to find the
    *   exactly point.
    */
    bool isPoint( int pointIndex, const TQPoint &point, unsigned short delta = 0 );

    /**
    *   Inserts a point at the given index.
    */
    bool insertPoint( int pointIndex, const TQPoint &point );

    /**
    *   Removes the point on the line given by the index, at the coordinates
    *   given by point with a fuzzy of delta
    */
    bool removePoint( int pointIndex, const TQPoint &point, unsigned short delta = 0 );

    /**
    *   Sets the start and end points.
    */
    bool setStartEndPoints( const TQPoint &start, const TQPoint &end );

    /**
    *   Returns the amount of POINTS on the line.
    *   Includes start and end points.
    */
    int count();

    /**
    *   Returns -1 if the given point is not on the line.
    *   else returns the line segment the point is on.
    *   Use the value to insert points at the point position.
    */
    int onLinePath( const TQPoint &position );

    /**
    *   Sets the canvas to be used.
    */
    void setCanvas( TQCanvas * canvas );

    /**
    *   Sets the Association type.
    */
    void setAssocType( Uml::Association_Type type );

    /**
    *   Calls a group of methods to update the line.  Used to save you calling multiple methods.
    */
    void update();

    /**
    *   This will setup the class ready to display the line correctly.
    *   This MUST be called before you can use this class.
    */
    void setAssociation( AssociationWidget * association );

    /**
    *   Returns the Association this class is linked to.
    */
    AssociationWidget * getAssociation() {
        return m_pAssociation;
    }

    /**
    *   Sets the status of whether the line is selected or not.
    */
    void setSelected( bool select );

    void saveToXMI( TQDomDocument & qDoc, TQDomElement & qElement );

    bool loadFromXMI( TQDomElement & qElement );

    /**
    *   Activates the line list.
    *   This is needed because the m_pAssociation does not yet
    *   exist at the time of the LinePath::loadFromXMI call.
    *   However, this means that the points in the m_LineList
    *   do not have a parent when they are loaded.
    *   They need to be reparented by calling LinePath::activate()
    *   once the m_pAssociation exists.
    */
    void activate();

    /**
    *   Removes and item created that are no longer needed.
    */
    void cleanup();

    /**
     * Returns the type of pen to use depending on the type of Association.
     */
    TQPen getPen();

    /**
    *   Sets the line color used by the line.
    */
    void setLineColor( const TQColor &color );
    /**
    *   Sets the line width used by the line.
    */
    void setLineWidth( uint width );

protected:

    /**
     * Draw a (hollow) circle.
     * We can't use TQCanvasEllipse directly for this because it doesn't
     * use the pen, i.e. TQCanvasEllipse only draws filled ellipses.
     */
    class Circle : public TQCanvasEllipse {
    public:
        explicit Circle(TQCanvas * canvas, int radius = 0);
        void setRadius(int radius);
        int getRadius() const;
        void setX(int x);
        void setY(int y);
        /**
         * The beef: Override method from TQCanvasEllipse.
         */
        void drawShape(TQPainter& p);
    };

    /**
    *   Returns the canvas being used.
    *   Will return zero if the Association hasn't been set.
    *
    *   This class doesn't hold this information but is a wrapper
    *   method to stop calls to undefined variable like m_pAssociation.
    */
    TQCanvas * getCanvas();

    /**
    *   Returns the Association type.
    *   Returns Uml::at_Association if association hasn't been set.
    *
    *   This class doesn't hold this information but is a wrapper
    *   method to stop calls to undefined variable like m_pAssociation.
    */
    Uml::Association_Type getAssocType();

    /**
    *   Returns the Line Color to use.
    *   Returns black if association not set.
    *
    *   This class doesn't hold this information but is a wrapper
    *   method to stop calls to undefined variable like m_pAssociation.
    */
    TQColor getLineColor();
    /**
    *   Returns the Line Width to use.
    *   Returns 0 if association not set.
    *
    *   This class doesn't hold this information but is a wrapper
    *   method to stop calls to undefined variable like m_pAssociation.
    */
    uint getLineWidth();

    /**
    *   Moves the selected canvas widgets.
    */
    void moveSelected( int pointIndex );

    /**
    *   Sets up the selected canvases needed.
    */
    void setupSelected();

    /**
    *   Calculates the head points.
    */
    void calculateHead();

    /**
    *   Creates the head lines to display the head.
    */
    void createHeadLines();

    /**
     * Create a number of new lines and append them to the given list.
     *
     * @param list  The list into which to append lines.
     * @param by    The number of lines to insert into the given list.
     */
    void growList(LineList &list, int by);

    /**
    *   Updates the head lines.  Call after calculating the new points.
    */
    void updateHead();

    /**
    *   Creates the line objects to display the parallel line.
    */
    void setupParallelLine();

    /**
    *   Calculates the position of the parallel line.
    */
    void calculateParallelLine();

    /**
    *   Updates the parallel line.
    *   Call after calculating the new position.
    */
    void updateParallelLine();

    /********Attributes*************/

    /**
    *   The association we are representing.
    */
    AssociationWidget * m_pAssociation;

    /**
    *   Contains all the lines of the association.
    */
    LineList m_LineList;

    /**
    *   Selected boxes list.
    */
    RectList m_RectList;

    /**
    *   Head lines.
    */
    LineList m_HeadList;

    /**
    *   The parallel line.
    */
    LineList m_ParallelList;

    /**
    *     Selected status.
    */
    bool  m_bSelected;

    /**
    *   Contains calculated points used to draw the line head.
    */
    TQPointArray m_PointArray;

    /**
    *   Contains calculated points used to draw the line head.
    */
    TQPoint m_ArrowPointA, m_ArrowPointB, m_MidPoint, m_EgdePoint;

    /**
    *   A polygon object to blank out any lines we don't want to see.
    */
    TQCanvasPolygon * m_pClearPoly;

    /**
     *  The transparent circle required by containment associations.
     */
    Circle * m_pCircle;

    /**
    *   Contains the calculated points for the parallel line
    *   on a collaboration message to use.
    */
    TQPointArray m_ParallelLines;

    /**
    *   Region where the line docks
    */
    Region m_DockRegion;

    bool m_bHeadCreated;

    bool m_bParallelLineCreated;

public slots:

    /**
    *   Sets the line color used by the line.
    *
    * @param viewID The id of the object behind the widget.
    */
    void slotLineColorChanged( Uml::IDType viewID );
    /**
    *   Sets the line width used by the line.
    *
    * @param viewID The id of the object behind the widget.
    */
    void slotLineWidthChanged( Uml::IDType viewID );
};

#endif