1 /*
2 SPDX-License-Identifier: GPL-2.0-or-later
3 SPDX-FileCopyrightText: 2003-2020 Umbrello UML Modeller Authors <umbrello-devel@kde.org>
4 */
5
6 // own header
7 #include "datatypewidget.h"
8
9 // app includes
10 #include "classifier.h"
11 #include "classifierlistitem.h"
12 #include "debug_utils.h"
13 #include "operation.h"
14 #include "umldoc.h"
15 #include "umlscene.h"
16 #include "umlview.h"
17
18 // qt includes
19 #include <QPainter>
20 #include <QXmlStreamWriter>
21
DEBUG_REGISTER_DISABLED(DatatypeWidget)22 DEBUG_REGISTER_DISABLED(DatatypeWidget)
23
24 /**
25 * Constructs an DatatypeWidget.
26 *
27 * @param scene The parent of this DatatypeWidget.
28 * @param d The UMLClassifier this will be representing.
29 */
30 DatatypeWidget::DatatypeWidget(UMLScene *scene, UMLClassifier *d)
31 : UMLWidget(scene, WidgetBase::wt_Datatype, d)
32 {
33 setSize(100, 30);
34 }
35
36 /**
37 * Standard deconstructor.
38 */
~DatatypeWidget()39 DatatypeWidget::~DatatypeWidget()
40 {
41 }
42
43 /**
44 * Overrides standard method.
45 */
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget)46 void DatatypeWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
47 {
48 setPenFromSettings(painter);
49 if (UMLWidget::useFillColor()) {
50 painter->setBrush(UMLWidget::fillColor());
51 } else {
52 painter->setBrush(m_scene->backgroundColor());
53 }
54
55 int w = width();
56 int h = height();
57
58 QFontMetrics &fm = getFontMetrics(FT_NORMAL);
59 int fontHeight = fm.lineSpacing();
60
61 painter->drawRect(0, 0, w, h);
62 painter->setPen(textColor());
63
64 QFont font = UMLWidget::font();
65 font.setBold(true);
66 painter->setFont(font);
67 painter->drawText(DATATYPE_MARGIN, 0,
68 w - DATATYPE_MARGIN* 2, fontHeight,
69 Qt::AlignCenter, m_umlObject->stereotype(true));
70
71 font.setItalic(m_umlObject->isAbstract());
72 painter->setFont(font);
73 painter->drawText(DATATYPE_MARGIN, fontHeight,
74 w - DATATYPE_MARGIN * 2, fontHeight, Qt::AlignCenter, name());
75
76 UMLWidget::paint(painter, option, widget);
77 }
78
79 /**
80 * Loads from a "datatypewidget" XMI element.
81 */
loadFromXMI1(QDomElement & qElement)82 bool DatatypeWidget::loadFromXMI1(QDomElement & qElement)
83 {
84 return UMLWidget::loadFromXMI1(qElement);
85 }
86
87 /**
88 * Saves to the "datatypewidget" XMI element.
89 */
saveToXMI1(QXmlStreamWriter & writer)90 void DatatypeWidget::saveToXMI1(QXmlStreamWriter& writer)
91 {
92 writer.writeStartElement(QLatin1String("datatypewidget"));
93 UMLWidget::saveToXMI1(writer);
94 writer.writeEndElement();
95 }
96
97 /**
98 * Overrides method from UMLWidget.
99 */
minimumSize() const100 QSizeF DatatypeWidget::minimumSize() const
101 {
102 if (!m_umlObject) {
103 return UMLWidget::minimumSize();
104 }
105 int width, height;
106 const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
107 const int fontHeight = fm.lineSpacing();
108
109 int lines = 1;//always have one line - for name
110 lines++; //for the stereotype
111
112 height = width = 0;
113 height += lines * fontHeight;
114
115 //now set the width of the concept
116 //set width to name to start with
117 //set width to name to start with
118 width = getFontMetrics(FT_BOLD_ITALIC).boundingRect(m_umlObject->fullyQualifiedName()).width();
119 int w = getFontMetrics(FT_BOLD).boundingRect(m_umlObject->stereotype(true)).width();
120
121 width = w > width?w:width;
122
123 //allow for width margin
124 width += DATATYPE_MARGIN * 2;
125
126 return QSizeF(width, height);
127 }
128