~ubuntu-branches/ubuntu/intrepid/kdesdk/intrepid-updates

« back to all changes in this revision

Viewing changes to umbrello/umbrello/signalwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-28 10:11:43 UTC
  • mto: This revision was merged to the branch mainline in revision 37.
  • Revision ID: james.westby@ubuntu.com-20080528101143-gzc3styjz1b70zxu
Tags: upstream-4.0.80
ImportĀ upstreamĀ versionĀ 4.0.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                                                                         *
 
3
 *   This program is free software; you can redistribute it and/or modify  *
 
4
 *   it under the terms of the GNU General Public License as published by  *
 
5
 *   the Free Software Foundation; either version 2 of the License, or     *
 
6
 *   (at your option) any later version.                                   *
 
7
 *                                                                         *
 
8
 *   copyright (C) 2002-2008                                               *
 
9
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 
10
 ***************************************************************************/
 
11
 
 
12
// own header
 
13
#include "signalwidget.h"
 
14
 
 
15
// qt includes
 
16
#include <qevent.h>
 
17
#include <QPolygon>
 
18
 
 
19
// kde includes
 
20
#include <klocale.h>
 
21
#include <kdebug.h>
 
22
#include <kinputdialog.h>
 
23
 
 
24
// app includes
 
25
#include "uml.h"
 
26
#include "umlnamespace.h"
 
27
#include "umldoc.h"
 
28
#include "uniqueid.h"
 
29
#include "docwindow.h"
 
30
#include "umlwidget.h"
 
31
#include "umlview.h"
 
32
#include "floatingtextwidget.h"
 
33
#include "linkwidget.h"
 
34
 
 
35
// #include "dialogs/signaldialog.h"
 
36
#include "listpopupmenu.h"
 
37
 
 
38
SignalWidget::SignalWidget(UMLView * view, SignalType signalType, Uml::IDType id)
 
39
        : UMLWidget(view, id) {
 
40
    UMLWidget::setBaseType(Uml::wt_Signal);
 
41
    m_SignalType = signalType;
 
42
    updateComponentSize();
 
43
    m_pName = NULL;
 
44
    if (signalType == SignalWidget::Time) {
 
45
        m_pName = new FloatingTextWidget(view,Uml::tr_Floating,"");
 
46
        view->setupNewWidget(m_pName);
 
47
        m_pName->setX(0);
 
48
        m_pName->setY(0);
 
49
    }
 
50
}
 
51
 
 
52
SignalWidget::~SignalWidget() {}
 
53
 
 
54
void SignalWidget::draw(QPainter & p, int offsetX, int offsetY) {
 
55
    setPenFromSettings(p);
 
56
    const int w = width();
 
57
    const int h = height();
 
58
    QPolygon a;
 
59
    switch (m_SignalType)
 
60
    {
 
61
    case Send :
 
62
        if(UMLWidget::getUseFillColour())
 
63
            p.setBrush(UMLWidget::getFillColour());
 
64
        {
 
65
 
 
66
            a.setPoints( 5, offsetX           ,offsetY,
 
67
                            offsetX + (w*2)/3 ,offsetY,
 
68
                            offsetX + w       ,(h/2)+offsetY,
 
69
                            offsetX + (w*2)/3 ,h+offsetY,
 
70
                            offsetX           ,h+offsetY );
 
71
            p.drawPolygon( a );
 
72
            const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
 
73
            const int fontHeight  = fm.lineSpacing();
 
74
            int textStartY = (h / 2) - (fontHeight / 2);
 
75
 
 
76
            p.setPen(Qt::black);
 
77
            QFont font = UMLWidget::getFont();
 
78
            font.setBold( false );
 
79
            p.setFont( font );
 
80
            p.drawText(offsetX + SIGNAL_MARGIN, offsetY + textStartY,
 
81
                           w - SIGNAL_MARGIN * 2, fontHeight,
 
82
                           Qt::AlignCenter, getName());
 
83
            setPenFromSettings(p);
 
84
        }
 
85
        break;
 
86
    case Accept :
 
87
        if(UMLWidget::getUseFillColour())
 
88
            p.setBrush(UMLWidget::getFillColour());
 
89
        {
 
90
            a.setPoints( 5, offsetX ,      offsetY,
 
91
                            offsetX + w/3, (h/2)+offsetY,
 
92
                            offsetX ,      h+offsetY,
 
93
                            offsetX + w,   h+offsetY,
 
94
                            offsetX + w,   offsetY );
 
95
 
 
96
            p.drawPolygon( a );
 
97
            const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
 
98
            const int fontHeight  = fm.lineSpacing();
 
99
            int textStartY = (h / 2) - (fontHeight / 2);
 
100
 
 
101
            p.setPen(Qt::black);
 
102
            QFont font = UMLWidget::getFont();
 
103
            font.setBold( false );
 
104
            p.setFont( font );
 
105
            p.drawText(offsetX + SIGNAL_MARGIN, offsetY + textStartY,
 
106
                           w - SIGNAL_MARGIN * 2 + (w/3), fontHeight,
 
107
                           Qt::AlignCenter, getName());
 
108
            setPenFromSettings(p);
 
109
        }
 
110
        break;
 
111
    case Time :
 
112
        if(UMLWidget::getUseFillColour())
 
113
            p.setBrush(UMLWidget::getFillColour());
 
114
        {
 
115
            a.setPoints( 4, offsetX ,    offsetY,
 
116
                            offsetX + w, offsetY+h,
 
117
                            offsetX ,    offsetY+h,
 
118
                            offsetX + w, offsetY);
 
119
 
 
120
            p.drawPolygon( a );
 
121
            //const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
 
122
            //const int fontHeight  = fm.lineSpacing();
 
123
            //int textStartY = (h / 2) - (fontHeight / 2);
 
124
            p.setPen(Qt::black);
 
125
            QFont font = UMLWidget::getFont();
 
126
            font.setBold( false );
 
127
            p.setFont( font );
 
128
 
 
129
            setPenFromSettings(p);
 
130
        }
 
131
        if (m_pName->getX() == 0 && m_pName->getY() == 0) {
 
132
            //the floating text has not been linked with the signal
 
133
            m_pName->setX(offsetX + w/2 - m_pName->getWidth()/2);
 
134
            m_pName->setY(offsetY + h);
 
135
        }
 
136
        m_pName->setVisible( ( m_pName->getText().length() > 0 ) );
 
137
        m_pName->updateComponentSize();
 
138
 
 
139
        break;
 
140
    default:
 
141
        uWarning() << "Unknown signal type:" << m_SignalType;
 
142
        break;
 
143
    }
 
144
    if(m_bSelected)
 
145
        drawSelected(&p, offsetX, offsetY);
 
146
}
 
147
 
 
148
 
 
149
void SignalWidget::setX(int newX) {
 
150
    m_oldX = getX();
 
151
    UMLWidget::setX(newX);
 
152
}
 
153
 
 
154
void SignalWidget::setY(int newY) {
 
155
    m_oldY = getY();
 
156
    UMLWidget::setY(newY);
 
157
}
 
158
 
 
159
QSize SignalWidget::calculateSize() {
 
160
        int width = SIGNAL_WIDTH, height = SIGNAL_HEIGHT;
 
161
        const QFontMetrics &fm = getFontMetrics(FT_BOLD);
 
162
        const int fontHeight  = fm.lineSpacing();
 
163
        int textWidth = fm.width(getName());
 
164
 
 
165
        if (m_SignalType == Accept)
 
166
             textWidth = int((float)textWidth * 1.3f);
 
167
        height  = fontHeight;
 
168
        if (m_SignalType != Time)
 
169
        {
 
170
              width   = textWidth > SIGNAL_WIDTH?textWidth:SIGNAL_WIDTH;
 
171
              height  = height > SIGNAL_HEIGHT?height:SIGNAL_HEIGHT;
 
172
        }
 
173
        width  += SIGNAL_MARGIN * 2;
 
174
        height += SIGNAL_MARGIN * 2;
 
175
 
 
176
    return QSize(width, height);
 
177
}
 
178
 
 
179
void SignalWidget::setName(const QString &strName) {
 
180
    m_Text = strName;
 
181
    updateComponentSize();
 
182
    if (getSignalType() == SignalWidget::Time) {
 
183
        m_pName->setText(m_Text);
 
184
    }
 
185
}
 
186
 
 
187
QString SignalWidget::getName() const {
 
188
    return m_Text;
 
189
}
 
190
 
 
191
SignalWidget::SignalType SignalWidget::getSignalType() const {
 
192
    return m_SignalType;
 
193
}
 
194
 
 
195
void SignalWidget::setSignalType( SignalType signalType ) {
 
196
    m_SignalType = signalType;
 
197
}
 
198
 
 
199
void SignalWidget::slotMenuSelection(QAction* action) {
 
200
    bool ok = false;
 
201
    QString name = m_Text;
 
202
 
 
203
    ListPopupMenu::Menu_Type sel = m_pMenu->getMenuType(action);
 
204
    switch( sel ) {
 
205
    case ListPopupMenu::mt_Rename:
 
206
        name = KInputDialog::getText( i18n("Enter signal name"), i18n("Enter the signal name :"), m_Text, &ok );
 
207
        if( ok && name.length() > 0 )
 
208
            setName(name);
 
209
        break;
 
210
 
 
211
    default:
 
212
        UMLWidget::slotMenuSelection(action);
 
213
    }
 
214
}
 
215
 
 
216
 
 
217
void SignalWidget::showProperties() {}
 
218
 
 
219
void SignalWidget::mouseMoveEvent(QMouseEvent* me) {
 
220
    UMLWidget::mouseMoveEvent(me);
 
221
    int diffX = m_oldX - getX();
 
222
    int diffY = m_oldY - getY();
 
223
    if (m_pName!=NULL) {
 
224
        m_pName->setX(m_pName->getX() - diffX);
 
225
        m_pName->setY(m_pName->getY() - diffY);
 
226
    }
 
227
}
 
228
 
 
229
void SignalWidget::saveToXMI( QDomDocument & qDoc, QDomElement & qElement ) {
 
230
    QDomElement signalElement = qDoc.createElement( "signalwidget" );
 
231
    UMLWidget::saveToXMI( qDoc, signalElement );
 
232
    signalElement.setAttribute( "signalname", m_Text );
 
233
    signalElement.setAttribute( "documentation", m_Doc );
 
234
    signalElement.setAttribute( "signaltype", m_SignalType );
 
235
    if (m_pName && !m_pName->getText().isEmpty()) {
 
236
        signalElement.setAttribute( "textid", ID2STR(m_pName->getID()) );
 
237
        m_pName -> saveToXMI( qDoc, signalElement );
 
238
    }
 
239
    qElement.appendChild( signalElement );
 
240
}
 
241
 
 
242
bool SignalWidget::loadFromXMI( QDomElement & qElement ) {
 
243
    if( !UMLWidget::loadFromXMI( qElement ) )
 
244
        return false;
 
245
    m_Text = qElement.attribute( "signalname", "" );
 
246
    m_Doc = qElement.attribute( "documentation", "" );
 
247
    QString type = qElement.attribute( "signaltype", "" );
 
248
    QString textid = qElement.attribute( "textid", "-1" );
 
249
    Uml::IDType textId = STR2ID(textid);
 
250
 
 
251
    setSignalType((SignalType)type.toInt());
 
252
    if (getSignalType() == Time) {
 
253
 
 
254
        if (textId != Uml::id_None) {
 
255
            UMLWidget *flotext = m_pView -> findWidget( textId );
 
256
            if (flotext != NULL) {
 
257
            // This only happens when loading files produced by
 
258
            // umbrello-1.3-beta2.
 
259
                m_pName = static_cast<FloatingTextWidget*>(flotext);
 
260
                return true;
 
261
            }
 
262
        } else {
 
263
            // no textid stored -> get unique new one
 
264
            textId = UniqueID::gen();
 
265
        }
 
266
    }
 
267
     //now load child elements
 
268
    QDomNode node = qElement.firstChild();
 
269
    QDomElement element = node.toElement();
 
270
    if ( !element.isNull() ) {
 
271
        QString tag = element.tagName();
 
272
        if (tag == "floatingtext") {
 
273
            m_pName = new FloatingTextWidget( m_pView,Uml::tr_Floating,m_Text, textId );
 
274
            if( ! m_pName->loadFromXMI(element) ) {
 
275
                // Most likely cause: The FloatingTextWidget is empty.
 
276
                delete m_pName;
 
277
                m_pName = NULL;
 
278
            }
 
279
        } else {
 
280
            uError() << "unknown tag " << tag << endl;
 
281
        }
 
282
    }
 
283
   return true;
 
284
}
 
285
 
 
286
 
 
287
#include "signalwidget.moc"
 
288