summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/messagewidget.h
blob: 05aa6162f39b698fe3584c2cd8b4ff4343ded767 (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
/***************************************************************************
 *                                                                         *
 *   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-2007                                               *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

#ifndef MESSAGEWIDGET_H
#define MESSAGEWIDGET_H

#include "umlwidget.h"
#include "linkwidget.h"

// forward declarations
class FloatingTextWidget;
class ObjectWidget;
class UMLOperation;
class MessageWidgetController;

/**
 * Used to display a message on a sequence diagram.  The message
 * could be between two objects or a message that calls itself on
 * an object.  This class will only display the line that is
 * required and the text will be setup by the @ref FloatingTextWidget
 * widget that is passed in the constructor.  A message can be
 * synchronous (calls a method and gains control back on return,
 * as happens in most programming languages) or asynchronous
 * (calls a method and gains back control immediately).
 *
 * @short Displays a message.
 * @author Paul Hensgen
 * @see UMLWidget
 * @see ObjectWidget
 * @see FloatingTextWidget
 * Bugs and comments to uml-devel@lists.sf.net or http://bugs.kde.org
 */
class MessageWidget : public UMLWidget, public LinkWidget {
    Q_OBJECT
  
public:
    friend class MessageWidgetController;

    /**
     * Constructs a MessageWidget.
     *
     * @param view      The parent to this class.
     * @param a The role A widget for this message.
     * @param b The role B widget for this message.
     * @param y The vertical position to display this message.
     * @param sequenceMessageType Whether synchronous or asynchronous
     * @param id        A unique id used for deleting this object cleanly.
     *              The default (-1) will prompt generation of a new ID.
     */
    MessageWidget(UMLView * view, ObjectWidget* a, ObjectWidget* b,
                  int y, Uml::Sequence_Message_Type sequenceMessageType,
                  Uml::IDType id = Uml::id_None);

    /**
     * Constructs a MessageWidget.
     *
     * @param view              The parent to this class.
     * @param sequenceMessageType The Uml::Sequence_Message_Type of this message widget
     * @param id                The ID to assign (-1 will prompt a new ID.)
     */
    MessageWidget(UMLView * view, Uml::Sequence_Message_Type sequenceMessageType, Uml::IDType id = Uml::id_None);

    /**
     * Initializes key variables of the class.
     */
    void init();

    /**
     * Standard deconstructor.
     */
    virtual ~MessageWidget();

    /**
     * Write property of TQString m_SequenceNumber.
     */
    void setSequenceNumber( const TQString &sequenceNumber );

    /**
     * Read property of TQString m_SequenceNumber.
     */
    TQString getSequenceNumber() const;

    /**
     * Returns whether the message is synchronous or asynchronous
     */
    Uml::Sequence_Message_Type getSequenceMessageType() const {
        return m_sequenceMessageType;
    }

    /**
     * Check to see if the given ObjectWidget is involved in the message.
     *
     * @param w The ObjectWidget to check for.
     * @return  true - if is contained, false - not contained.
     */
    bool contains(ObjectWidget * w);

    /**
     * Returns the related widget on the given side.
     *
     * @return  The ObjectWidget we are related to.
     */
    ObjectWidget* getWidget(Uml::Role_Type role);

    /**
     * Sets the related widget on the given side.
     *
     * @param ow        The ObjectWidget we are related to.
     * @param role      The Uml::Role_Type to be set for the ObjectWidget
     */
    void setWidget(ObjectWidget * ow, Uml::Role_Type role) ;

    /**
     * Returns the text widget it is related to.
     *
     * @return  The text widget we are related to.
     */
    FloatingTextWidget * getFloatingTextWidget() {
        return m_pFText;
    }

    /**
     * Sets the text widget it is related to.
     *
     * @param f The text widget we are related to.
     */
    void setFloatingTextWidget(FloatingTextWidget * f) {
        m_pFText = f;
    }

    /**
     * Implements operation from LinkWidget.
     * Required by FloatingTextWidget.
     */
    void lwSetFont (TQFont font);

    /**
     * Overrides operation from LinkWidget.
     * Required by FloatingTextWidget.
     * @todo Move to LinkWidget.
     */
    UMLClassifier *getOperationOwner();

    /**
     * Implements operation from LinkWidget.
     * Motivated by FloatingTextWidget.
     */
    UMLOperation *getOperation();

    /**
     * Implements operation from LinkWidget.
     * Motivated by FloatingTextWidget.
     */
    void setOperation(UMLOperation *op);

    /**
     * Overrides operation from LinkWidget.
     * Required by FloatingTextWidget.
     */
    TQString getCustomOpText();

    /**
     * Overrides operation from LinkWidget.
     * Required by FloatingTextWidget.
     */
    void setCustomOpText(const TQString &opText);

    /**
     * Overrides operation from LinkWidget.
     * Required by FloatingTextWidget.
     *
     * @param ft        The text widget which to update.
     */
    void setMessageText(FloatingTextWidget *ft);

    /**
     * Overrides operation from LinkWidget.
     * Required by FloatingTextWidget.
     *
     * @param ft        The text widget which to update.
     * @param newText   The new text to set.
     */
    void setText(FloatingTextWidget *ft, const TQString &newText);

    /**
     * Overrides operation from LinkWidget.
     * Required by FloatingTextWidget.
     *
     * @param seqNum    The new sequence number string to set.
     * @param op                The new operation string to set.
     */
    void setSeqNumAndOp(const TQString &seqNum, const TQString &op);

    /**
     * Overrides operation from LinkWidget.
     * Required by FloatingTextWidget.
     *
     * @param seqNum    Return this MessageWidget's sequence number string.
     * @param op                Return this MessageWidget's operation string.
     */
    UMLClassifier * getSeqNumAndOp(TQString& seqNum, TQString& op);

    /**
     * Calculate the geometry of the widget.
     */
    void calculateWidget();

    /**
     * Activates a MessageWidget.  Connects its m_pOw[] pointers
     * to UMLObjects and also send signals about its FloatingTextWidget.
     */
    bool activate(IDChangeLog * Log = 0);

    /**
     * Calculates the size of the widget by calling
     * calculateDimenstionsSynchronous(),
     * calculateDimenstionsAsynchronous(), or
     * calculateDimensionsCreation()
     */
    void calculateDimensions();

    /**
     * Calculates and sets the size of the widget for a synchronous message
     */
    void calculateDimensionsSynchronous();

    /**
     * Calculates and sets the size of the widget for an asynchronous message
     */
    void calculateDimensionsAsynchronous();

    /**
     * Calculates and sets the size of the widget for a creation message
     */
    void calculateDimensionsCreation();

    /**
     * Calls drawSynchronous() or drawAsynchronous()
     */
    void draw(TQPainter& p, int offsetX, int offsetY);

    /**
     * Draws the calling arrow with filled in arrowhead, the
     * timeline box and the returning arrow with a dashed line and
     * stick arrowhead.
     */
    void drawSynchronous(TQPainter& p, int offsetX, int offsetY);

    /**
     * Draws a solid arrow line and a stick arrow head.
     */
    void drawAsynchronous(TQPainter& p, int offsetX, int offsetY);

    /**
     * Draws a solid arrow line and a stick arrow head to the
     * edge of the target object widget instead of to the
     * sequence line.
     */
    void drawCreation(TQPainter& p, int offsetX, int offsetY);

    /**
     * Sets the text position relative to the sequence message.
     */
    void setTextPosition();

    /**
     * Constrains the FloatingTextWidget X and Y values supplied.
     * Overrides operation from LinkWidget.
     *
     * @param textX             Candidate X value (may be modified by the constraint.)
     * @param textY             Candidate Y value (may be modified by the constraint.)
     * @param textWidth Width of the text.
     * @param textHeight        Height of the text.
     * @param tr                Uml::Text_Role of the text.
     */
    void constrainTextPos(int &textX, int &textY, int textWidth, int textHeight,
                          Uml::Text_Role tr);

    /**
     * Used to cleanup any other widget it may need to delete.
     */
    void cleanup();

    /**
     * Sets the state of whether the widget is selected.
     *
     * @param _select   True if the widget is selected.
     */
    void setSelected(bool _select);

    /**
     * Returns the minimum height this widget should be set at on
     * a sequence diagrams.  Takes into account the widget positions
     * it is related to.
     */
    int getMinY();

    /**
     * Returns the maximum height this widget should be set at on
     * a sequence diagrams.  Takes into account the widget positions
     * it is related to.
     */
    int getMaxY();

    /**
     * Overrides operation from UMLWidget.
     *
     * @param p Point to be checked.
     *
     * @return Non-zero if the point is on a part of the MessageWidget.
     *         NB In case of a synchronous message, the empty space
     *         between call line and return line does not count, i.e. if
     *         the point is located in that space the function returns 0.
     */
    int onWidget(const TQPoint & p);

    /**
     * Saves to the "messagewidget" XMI element.
     */
    void saveToXMI( TQDomDocument & qDoc, TQDomElement & qElement );

    /**
     * Loads from the "messagewidget" XMI element.
     */
    bool loadFromXMI( TQDomElement & qElement );

protected:
    /**
     * Shortcut for calling m_pFText->setLink() followed by
     * this->setTextPosition().
     */
    void setLinkAndTextPos();

    /**
     * Returns the textX arg with constraints applied.
     * Auxiliary to setTextPosition() and constrainTextPos().
     */
    int constrainX(int textX, int textWidth, Uml::Text_Role tr);

    /**
     * Draw an arrow pointing in the given direction.
     * The arrow head is not solid, i.e. it is made up of two lines
     * like so:  --->
     * The direction can be either TQt::LeftArrow or TQt::RightArrow.
     */
    static void drawArrow( TQPainter& p, int x, int y, int w,
                           TQt::ArrowType direction, bool useDottedLine = false );

    /**
     * Draw a solid (triangular) arrowhead pointing in the given direction.
     * The direction can be either TQt::LeftArrow or TQt::RightArrow.
     */
    static void drawSolidArrowhead(TQPainter& p, int x, int y, TQt::ArrowType direction);

    /**
     * Update the UMLWidget::m_bResizable flag according to the
     * charactersitics of this message.
     */
    void updateResizability();

    // Data loaded/saved
    TQString m_SequenceNumber;
    TQString m_CustomOp;
    /**
     * Whether the message is synchronous or asynchronous
     */
    Uml::Sequence_Message_Type m_sequenceMessageType;

private:
    void moveEvent(TQMoveEvent */*m*/);
    void resizeEvent(TQResizeEvent */*re*/);

    ObjectWidget * m_pOw[2];
    FloatingTextWidget * m_pFText;
    int m_nY;
    /**
     * The following variables are used by loadFromXMI() as an intermediate
     * store. activate() resolves the IDs, i.e. after activate() the variables
     * m_pOw[] and m_pFText can be used.
     */
    Uml::IDType m_widgetAId, m_widgetBId, m_textId;

public slots:
    void slotWidgetMoved(Uml::IDType id);
    void slotMenuSelection(int sel);
signals:
    /**
     * emitted when the message widget is moved up or down
     * slots into ObjectWidget::slotMessageMoved()
     */
    void sigMessageMoved();
};

#endif