usecasewidget.cpp 3.52 KB
Newer Older
1
2
3
4
5
6
/***************************************************************************
 *   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.                                   *
 *                                                                         *
Oliver Kellogg's avatar
Oliver Kellogg committed
7
 *   copyright (C) 2002-2020                                               *
Ralf Habacker's avatar
Ralf Habacker committed
8
 *   Umbrello UML Modeller Authors <umbrello-devel@kde.org>                *
9
 ***************************************************************************/
10

11
// own header file
12
#include "usecasewidget.h"
13

14
// app includes
15
#include "debug_utils.h"
16
17
#include "usecase.h"
#include "umlview.h"
18

19
20
DEBUG_REGISTER_DISABLED(UseCaseWidget)

21
22
/**
 *  Creates a UseCase widget.
23
24
 *  @param  scene  The parent of the widget.
 *  @param  o      The UMLUseCase to represent.
25
 */
26
UseCaseWidget::UseCaseWidget(UMLScene * scene, UMLUseCase *o)
27
  : UMLWidget(scene, WidgetBase::wt_UseCase, o)
28
{
29
}
30

31
32
33
/**
 * Destructor.
 */
34
35
36
UseCaseWidget::~UseCaseWidget()
{
}
Luis De la Parra Blum's avatar
Luis De la Parra Blum committed
37

38
39
40
/**
 * Overrides the standard paint event.
 */
41
void UseCaseWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
42
{
43
44
45
46
    Q_UNUSED(option);
    Q_UNUSED(widget);

    setPenFromSettings(painter);
47
48
    if (UMLWidget::useFillColor())
        painter->setBrush(UMLWidget::fillColor());
49
    QFont font = UMLWidget::font();
Oliver Kellogg's avatar
Oliver Kellogg committed
50
51
    font.setUnderline(false);
    font.setBold(false);
52
53
    font.setItalic(m_umlObject->isAbstract());
    painter->setFont(font);
54
55
56
57
    const QFontMetricsF &fm = getFontMetrics(FT_NORMAL);
    const qreal fontHeight  = fm.lineSpacing();
    const qreal w = width();
    const qreal h = height();
58
    bool drawStereotype = umlObject() && !umlObject()->stereotype().isEmpty();
59
    painter->drawEllipse(QRectF(0, 0, w, h));
60
    painter->setPen(textColor());
61
62

    QString txt;
63
    if (drawStereotype)
64
65
66
67
68
69
70
    {
        // Prepend text of stereotype to other text:
        txt = umlObject()->stereotype(true);
    }
    if (!txt.isEmpty())
        txt.append(QLatin1String("\n"));
    QString name_txt = name();
71

72
73
74
75
76
77
78
79
80
    // Replace user-entered "\n" with real line breaks:
    name_txt.replace(QLatin1String("\\n"),QLatin1String("\n"));
    txt += name_txt;
    qreal dy = 0.0;
    if (drawStereotype)
        dy = fontHeight/2.0;

    QRectF rectangle(UC_MARGIN, UC_MARGIN - dy, w - UC_MARGIN*2, h - UC_MARGIN*2);
    painter->drawText(rectangle, Qt::AlignCenter | Qt::TextWordWrap, txt);
81
    setPenFromSettings(painter);
82
83

    UMLWidget::paint(painter, option, widget);
84
}
85

86
87
88
/**
 * Saves this UseCase to file.
 */
Ralf Habacker's avatar
Ralf Habacker committed
89
void UseCaseWidget::saveToXMI1(QDomDocument & qDoc, QDomElement & qElement)
90
{
Oliver Kellogg's avatar
Oliver Kellogg committed
91
    QDomElement usecaseElement = qDoc.createElement(QLatin1String("usecasewidget"));
Ralf Habacker's avatar
Ralf Habacker committed
92
    UMLWidget::saveToXMI1(qDoc, usecaseElement);
93
    qElement.appendChild(usecaseElement);
94
95
96
97
98
}

/**
 * Overrides method from UMLWidget
 */
99
QSizeF UseCaseWidget::minimumSize() const
100
{
101
    const UMLWidget::FontType ft = (m_umlObject->isAbstract() ? FT_BOLD_ITALIC : FT_BOLD);
102
103
    const QFontMetrics &fm = UMLWidget::getFontMetrics(ft);
    const int fontHeight = fm.lineSpacing();
104
    const int textWidth = fm.width(name());
105
    bool drawStereotype = umlObject() && !umlObject()->stereotype().isEmpty();
106
    int width = (textWidth / 3) > UC_WIDTH ? textWidth / 3 : UC_WIDTH;
107
    int height = UC_HEIGHT + (drawStereotype ? 2 * fontHeight : fontHeight) + UC_MARGIN;
Oliver Kellogg's avatar
Oliver Kellogg committed
108
    width += UC_MARGIN * 2;
109

110
    return QSizeF(width, height);
111
}