aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/modelinglib/qmt/diagram_scene/items/classitem.cpp
blob: 23a9c524a2b973dd6fcfac775831be08824dd5ca (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
// Copyright (C) 2016 Jochen Becher
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "classitem.h"

#include "qmt/controller/namecontroller.h"
#include "qmt/diagram/dclass.h"
#include "qmt/diagram/dinheritance.h"
#include "qmt/diagram_controller/diagramcontroller.h"
#include "qmt/diagram_scene/diagramsceneconstants.h"
#include "qmt/diagram_scene/diagramscenemodel.h"
#include "qmt/diagram_scene/parts/contextlabelitem.h"
#include "qmt/diagram_scene/parts/customiconitem.h"
#include "qmt/diagram_scene/parts/editabletextitem.h"
#include "qmt/diagram_scene/parts/relationstarter.h"
#include "qmt/diagram_scene/parts/stereotypesitem.h"
#include "qmt/diagram_scene/parts/templateparameterbox.h"
#include "qmt/infrastructure/contextmenuaction.h"
#include "qmt/infrastructure/geometryutilities.h"
#include "qmt/infrastructure/qmtassert.h"
#include "qmt/model/mclass.h"
#include "qmt/model/mclassmember.h"
#include "qmt/model/massociation.h"
#include "qmt/model/minheritance.h"
#include "qmt/model_controller/modelcontroller.h"
#include "qmt/stereotype/stereotypecontroller.h"
#include "qmt/stereotype/stereotypeicon.h"
#include "qmt/style/stylecontroller.h"
#include "qmt/style/style.h"
#include "qmt/tasks/diagramscenecontroller.h"
#include "qmt/tasks/ielementtasks.h"

#include <utils/algorithm.h>

#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsSimpleTextItem>
#include <QGraphicsLineItem>
#include <QBrush>
#include <QPen>
#include <QFont>
#include <QMenu>

#include <algorithm>

#include <qmt/stereotype/customrelation.h>

#include "../../modelinglibtr.h"

namespace qmt {

static const char ASSOCIATION[] = "association";
static const char INHERITANCE[] = "inheritance";

static const qreal MINIMUM_AUTO_WIDTH = 80.0;
static const qreal MINIMUM_AUTO_HEIGHT = 60.0;
static const qreal BODY_VERT_BORDER = 4.0;
static const qreal BODY_HORIZ_BORDER = 4.0;
static const qreal BODY_HORIZ_DISTANCE = 4.0;

ClassItem::ClassItem(DClass *klass, DiagramSceneModel *diagramSceneModel, QGraphicsItem *parent)
    : ObjectItem("class", klass, diagramSceneModel, parent)
{
}

ClassItem::~ClassItem()
{
}

void ClassItem::update()
{
    prepareGeometryChange();

    updateStereotypeIconDisplay();

    auto diagramClass = dynamic_cast<DClass *>(object());
    QMT_ASSERT(diagramClass, return);

    const Style *style = adaptedStyle(stereotypeIconId());

    if (diagramClass->showAllMembers()) {
        updateMembers(style);
    } else {
        m_attributesText.clear();
        m_methodsText.clear();
    }

    updateCustomIcon(style);

    // shape
    if (!customIconItem()) {
        if (!m_shape)
            m_shape = new QGraphicsRectItem(this);
        m_shape->setBrush(style->fillBrush());
        m_shape->setPen(style->outerLinePen());
        m_shape->setZValue(SHAPE_ZVALUE);
    } else if (m_shape) {
        m_shape->scene()->removeItem(m_shape);
        delete m_shape;
        m_shape = nullptr;
    }

    // stereotypes
    updateStereotypes(stereotypeIconId(), stereotypeIconDisplay(), style);

    // base classes
    DiagramController *diagramController = diagramSceneModel()->diagramController();
    ModelController *modelController = diagramController->modelController();
    MClass *klass = modelController->findElement<MClass>(diagramClass->modelUid());
    if (klass) {
        // TODO cache relation's Uids and check for change
        QList<QString> baseClasses;
        for (const auto &handle : klass->relations()) {
            if (MInheritance *mInheritance = dynamic_cast<MInheritance *>(handle.target())) {
                if (mInheritance->base() != klass->uid()) {
                    DInheritance *dInheritance = diagramController->findDelegate<DInheritance>(mInheritance, diagramSceneModel()->diagram());
                    if (dInheritance) {
                        DClass *dBaseClass = diagramController->findElement<DClass>(dInheritance->base(), diagramSceneModel()->diagram());
                        if (dBaseClass)
                            continue;
                    }
                    MClass *mBaseClass = diagramController->modelController()->findElement<MClass>(mInheritance->base());
                    if (mBaseClass) {
                        QString baseClassName;
                        baseClassName += mBaseClass->name();
                        baseClasses.append(baseClassName);
                    }
                }
            }
        }
        if (!baseClasses.isEmpty()) {
            if (!m_baseClasses)
                m_baseClasses = new QGraphicsSimpleTextItem(this);
            QFont font = style->smallFont();
            font.setItalic(true);
            m_baseClasses->setFont(font);
            m_baseClasses->setBrush(style->textBrush());
            m_baseClasses->setText(baseClasses.join('\n'));
        } else if (m_baseClasses) {
            if (m_baseClasses->scene())
                m_baseClasses->scene()->removeItem(m_baseClasses);
            delete m_baseClasses;
            m_baseClasses = nullptr;
        }
    }

    // namespace
    if (!suppressTextDisplay() && !diagramClass->umlNamespace().isEmpty()) {
        if (!m_namespace)
            m_namespace = new QGraphicsSimpleTextItem(this);
        m_namespace->setFont(style->smallFont());
        m_namespace->setBrush(style->textBrush());
        m_namespace->setText(diagramClass->umlNamespace());
    } else if (m_namespace) {
        m_namespace->scene()->removeItem(m_namespace);
        delete m_namespace;
        m_namespace = nullptr;
    }

    // class name
    updateNameItem(style);

    // context
    if (!suppressTextDisplay() && showContext()) {
        if (!m_contextLabel)
            m_contextLabel = new ContextLabelItem(this);
        m_contextLabel->setFont(style->smallFont());
        m_contextLabel->setBrush(style->textBrush());
        m_contextLabel->setContext(object()->context());
    } else if (m_contextLabel) {
        m_contextLabel->scene()->removeItem(m_contextLabel);
        delete m_contextLabel;
        m_contextLabel = nullptr;
    }

    // attributes separator
    if (m_shape || (!suppressTextDisplay() && (!m_attributesText.isEmpty() || !m_methodsText.isEmpty()))) {
        if (!m_attributesSeparator)
            m_attributesSeparator = new QGraphicsLineItem(this);
        m_attributesSeparator->setPen(style->innerLinePen());
        m_attributesSeparator->setZValue(SHAPE_DETAILS_ZVALUE);
    } else if (m_attributesSeparator) {
        m_attributesSeparator->scene()->removeItem(m_attributesSeparator);
        delete m_attributesSeparator;
        m_attributesSeparator = nullptr;
    }

    // attributes
    if (!suppressTextDisplay() && !m_attributesText.isEmpty()) {
        if (!m_attributes)
            m_attributes = new QGraphicsTextItem(this);
        m_attributes->setFont(style->normalFont());
        //m_attributes->setBrush(style->textBrush());
        m_attributes->setDefaultTextColor(style->textBrush().color());
        m_attributes->setHtml(m_attributesText);
    } else if (m_attributes) {
        m_attributes->scene()->removeItem(m_attributes);
        delete m_attributes;
        m_attributes = nullptr;
    }

    // methods separator
    if (m_shape || (!suppressTextDisplay() && (!m_attributesText.isEmpty() || !m_methodsText.isEmpty()))) {
        if (!m_methodsSeparator)
            m_methodsSeparator = new QGraphicsLineItem(this);
        m_methodsSeparator->setPen(style->innerLinePen());
        m_methodsSeparator->setZValue(SHAPE_DETAILS_ZVALUE);
    } else if (m_methodsSeparator) {
        m_methodsSeparator->scene()->removeItem(m_methodsSeparator);
        delete m_methodsSeparator;
        m_methodsSeparator = nullptr;
    }

    // methods
    if (!suppressTextDisplay() && !m_methodsText.isEmpty()) {
        if (!m_methods)
            m_methods = new QGraphicsTextItem(this);
        m_methods->setFont(style->normalFont());
        //m_methods->setBrush(style->textBrush());
        m_methods->setDefaultTextColor(style->textBrush().color());
        m_methods->setHtml(m_methodsText);
    } else if (m_methods) {
        m_methods->scene()->removeItem(m_methods);
        delete m_methods;
        m_methods = nullptr;
    }

    // template parameters
    if (templateDisplay() == DClass::TemplateBox && !diagramClass->templateParameters().isEmpty()) {
        // TODO due to a bug in Qt the m_nameItem may get focus back when this item is newly created
        // 1. Select name item of class without template
        // 2. Click into template property (item name loses focus) and enter a letter
        // 3. Template box is created which gives surprisingly focus back to item name
        if (!m_templateParameterBox)
            m_templateParameterBox = new TemplateParameterBox(this);
        QPen pen = style->outerLinePen();
        pen.setStyle(Qt::DashLine);
        m_templateParameterBox->setPen(pen);
        m_templateParameterBox->setBrush(QBrush(Qt::white));
        m_templateParameterBox->setFont(style->smallFont());
        m_templateParameterBox->setTextBrush(style->textBrush());
        m_templateParameterBox->setTemplateParameters(diagramClass->templateParameters());
    } else if (m_templateParameterBox) {
        m_templateParameterBox->scene()->removeItem(m_templateParameterBox);
        delete m_templateParameterBox;
        m_templateParameterBox = nullptr;
    }

    updateSelectionMarker(customIconItem());
    updateRelationStarter();
    updateAlignmentButtons();
    updateGeometry();
}

bool ClassItem::intersectShapeWithLine(const QLineF &line, QPointF *intersectionPoint, QLineF *intersectionLine) const
{
    if (customIconItem()) {
        QList<QPolygonF> polygons = customIconItem()->outline();
        for (int i = 0; i < polygons.size(); ++i)
            polygons[i].translate(object()->pos() + object()->rect().topLeft());
        if (shapeIcon().textAlignment() == qmt::StereotypeIcon::TextalignBelow) {
            if (nameItem()) {
                QPolygonF polygon(nameItem()->boundingRect());
                polygon.translate(object()->pos() + nameItem()->pos());
                polygons.append(polygon);
            }
            if (m_contextLabel) {
                QPolygonF polygon(m_contextLabel->boundingRect());
                polygon.translate(object()->pos() + m_contextLabel->pos());
                polygons.append(polygon);
            }
        }
        return GeometryUtilities::intersect(polygons, line, nullptr, intersectionPoint, intersectionLine);
    }
    QPolygonF polygon;
    QRectF rect = object()->rect();
    rect.translate(object()->pos());
    polygon << rect.topLeft() << rect.topRight() << rect.bottomRight() << rect.bottomLeft() << rect.topLeft();
    return GeometryUtilities::intersect(polygon, line, intersectionPoint, intersectionLine);
}

QSizeF ClassItem::minimumSize() const
{
    return calcMinimumGeometry();
}

void ClassItem::relationDrawn(const QString &id, ObjectItem *targetItem, const QList<QPointF> &intermediatePoints)
{
    DiagramSceneController *diagramSceneController = diagramSceneModel()->diagramSceneController();
    if (id == INHERITANCE) {
        auto baseClass = dynamic_cast<DClass *>(targetItem->object());
        if (baseClass) {
            auto derivedClass = dynamic_cast<DClass *>(object());
            QMT_ASSERT(derivedClass, return);
            diagramSceneController->createInheritance(derivedClass, baseClass, intermediatePoints, diagramSceneModel()->diagram());
        }
        return;
    } else if (id == ASSOCIATION) {
        auto associatedClass = dynamic_cast<DClass *>(targetItem->object());
        if (associatedClass) {
            auto derivedClass = dynamic_cast<DClass *>(object());
            QMT_ASSERT(derivedClass, return);
            diagramSceneController->createAssociation(derivedClass, associatedClass, intermediatePoints, diagramSceneModel()->diagram());
        }
        return;
    } else {
        StereotypeController *stereotypeController = diagramSceneModel()->stereotypeController();
        CustomRelation customRelation = stereotypeController->findCustomRelation(id);
        if (!customRelation.isNull()) {
            switch (customRelation.element()) {
            case CustomRelation::Element::Inheritance:
            {
                auto baseClass = dynamic_cast<DClass *>(targetItem->object());
                if (baseClass) {
                    auto derivedClass = dynamic_cast<DClass *>(object());
                    QMT_ASSERT(derivedClass, return);
                    diagramSceneController->createInheritance(derivedClass, baseClass, intermediatePoints, diagramSceneModel()->diagram());
                }
                return;
            }
            case CustomRelation::Element::Association:
            {
                auto assoziatedClass = dynamic_cast<DClass *>(targetItem->object());
                if (assoziatedClass) {
                    auto derivedClass = dynamic_cast<DClass *>(object());
                    QMT_ASSERT(derivedClass, return);
                    diagramSceneController->createAssociation(
                                derivedClass, assoziatedClass, intermediatePoints, diagramSceneModel()->diagram(),
                                [=] (MAssociation *mAssociation, DAssociation *dAssociation) {
                        if (mAssociation && dAssociation) {
                            static const QHash<CustomRelation::Relationship, MAssociationEnd::Kind> relationship2KindMap = {
                                { CustomRelation::Relationship::Association, MAssociationEnd::Association },
                                { CustomRelation::Relationship::Aggregation, MAssociationEnd::Aggregation },
                                { CustomRelation::Relationship::Composition, MAssociationEnd::Composition } };
                            diagramSceneController->modelController()->startUpdateRelation(mAssociation);
                            mAssociation->setStereotypes(Utils::toList(customRelation.stereotypes()));
                            mAssociation->setName(customRelation.name());
                            MAssociationEnd endA;
                            endA.setCardinality(customRelation.endA().cardinality());
                            endA.setKind(relationship2KindMap.value(customRelation.endA().relationship()));
                            endA.setName(customRelation.endA().role());
                            endA.setNavigable(customRelation.endA().navigable());
                            mAssociation->setEndA(endA);
                            MAssociationEnd endB;
                            endB.setCardinality(customRelation.endB().cardinality());
                            endB.setKind(relationship2KindMap.value(customRelation.endB().relationship()));
                            endB.setName(customRelation.endB().role());
                            endB.setNavigable(customRelation.endB().navigable());
                            mAssociation->setEndB(endB);
                            diagramSceneController->modelController()->finishUpdateRelation(mAssociation, false);
                        }
                    });
                }
                return;
            }
            case CustomRelation::Element::Dependency:
            case CustomRelation::Element::Relation:
                // fall thru
                break;
            }
        }
    }
    ObjectItem::relationDrawn(id, targetItem, intermediatePoints);
}

bool ClassItem::extendContextMenu(QMenu *menu)
{
    bool extended = false;
    if (diagramSceneModel()->diagramSceneController()->elementTasks()->hasClassDefinition(object(), diagramSceneModel()->diagram())) {
        menu->addAction(new ContextMenuAction(Tr::tr("Show Definition"), "showDefinition", menu));
        extended = true;
    }
    return extended;
}

bool ClassItem::handleSelectedContextMenuAction(const QString &id)
{
    if (id == "showDefinition") {
        diagramSceneModel()->diagramSceneController()->elementTasks()->openClassDefinition(object(), diagramSceneModel()->diagram());
        return true;
    }
    return false;
}

QString ClassItem::buildDisplayName() const
{
    auto diagramClass = dynamic_cast<DClass *>(object());
    QMT_ASSERT(diagramClass, return QString());

    QString name;
    if (templateDisplay() == DClass::TemplateName && !diagramClass->templateParameters().isEmpty()) {
        name = object()->name();
        name += QLatin1Char('<');
        bool first = true;
        for (const QString &p : diagramClass->templateParameters()) {
            if (!first)
                name += QLatin1Char(',');
            name += p;
            first = false;
        }
        name += QLatin1Char('>');
    } else {
        name = object()->name();
    }
    return name;
}

void ClassItem::setFromDisplayName(const QString &displayName)
{
    if (templateDisplay() == DClass::TemplateName) {
        QString name;
        QStringList templateParameters;
        // NOTE namespace is ignored because it has its own edit field
        if (NameController::parseClassName(displayName, nullptr, &name, &templateParameters)) {
            auto diagramClass = dynamic_cast<DClass *>(object());
            QMT_ASSERT(diagramClass, return);
            ModelController *modelController = diagramSceneModel()->diagramSceneController()->modelController();
            MClass *mklass = modelController->findObject<MClass>(diagramClass->modelUid());
            if (mklass && (name != mklass->name() || templateParameters != mklass->templateParameters())) {
                modelController->startUpdateObject(mklass);
                mklass->setName(name);
                mklass->setTemplateParameters(templateParameters);
                modelController->finishUpdateObject(mklass, false);
            }
        }
    } else {
        ObjectItem::setFromDisplayName(displayName);
    }
}

void ClassItem::addRelationStarterTool(const QString &id)
{
    if (id == INHERITANCE)
        relationStarter()->addArrow(INHERITANCE, ArrowItem::ShaftSolid,
                                    ArrowItem::HeadNone, ArrowItem::HeadTriangle,
                                    Tr::tr("Inheritance"));
    else if (id == ASSOCIATION)
        relationStarter()->addArrow(ASSOCIATION, ArrowItem::ShaftSolid,
                                    ArrowItem::HeadNone, ArrowItem::HeadFilledTriangle,
                                    Tr::tr("Association"));
    else
        ObjectItem::addRelationStarterTool(id);
}

void ClassItem::addRelationStarterTool(const CustomRelation &customRelation)
{
    ArrowItem::Shaft shaft = ArrowItem::ShaftSolid;
    ArrowItem::Head headStart = ArrowItem::HeadNone;
    ArrowItem::Head headEnd = ArrowItem::HeadNone;
    switch (customRelation.element()) {
    case CustomRelation::Element::Inheritance:
        shaft = ArrowItem::ShaftSolid;
        headEnd = ArrowItem::HeadTriangle;
        break;
    case CustomRelation::Element::Association:
        switch (customRelation.endA().relationship()) {
        case CustomRelation::Relationship::Association:
            if (customRelation.endA().navigable() && customRelation.endB().navigable()) {
                headStart = ArrowItem::HeadNone;
                headEnd = ArrowItem::HeadNone;
            } else if (customRelation.endA().navigable()) {
                headStart = ArrowItem::HeadFilledTriangle;
            } else {
                headEnd = ArrowItem::HeadFilledTriangle;
            }
            break;
        case CustomRelation::Relationship::Aggregation:
            headStart = ArrowItem::HeadDiamond;
            break;
        case CustomRelation::Relationship::Composition:
            headStart = ArrowItem::HeadFilledDiamond;
            break;
        }
        break;
    case CustomRelation::Element::Dependency:
    case CustomRelation::Element::Relation:
        ObjectItem::addRelationStarterTool(customRelation);
        return;
    }
    relationStarter()->addArrow(customRelation.id(), shaft, headStart, headEnd,
                                customRelation.title());
}

void ClassItem::addStandardRelationStarterTools()
{
    ObjectItem::addStandardRelationStarterTools();
    addRelationStarterTool(INHERITANCE);
    addRelationStarterTool(ASSOCIATION);
}

DClass::TemplateDisplay ClassItem::templateDisplay() const
{
    auto diagramClass = dynamic_cast<DClass *>(object());
    QMT_ASSERT(diagramClass, return DClass::TemplateSmart);

    DClass::TemplateDisplay templateDisplay = diagramClass->templateDisplay();
    if (templateDisplay == DClass::TemplateSmart) {
        if (customIconItem())
            templateDisplay = DClass::TemplateName;
        else
            templateDisplay = DClass::TemplateBox;
    }
    return templateDisplay;
}

QSizeF ClassItem::calcMinimumGeometry() const
{
    double width = 0.0;
    double height = 0.0;

    if (customIconItem()) {
        QSizeF sz = customIconItemMinimumSize(customIconItem(),
                                              CUSTOM_ICON_MINIMUM_AUTO_WIDTH, CUSTOM_ICON_MINIMUM_AUTO_HEIGHT);
        if (shapeIcon().textAlignment() != qmt::StereotypeIcon::TextalignTop
                && shapeIcon().textAlignment() != qmt::StereotypeIcon::TextalignCenter)
            return sz;
        width = sz.width();
    }

    height += BODY_VERT_BORDER;
    double top_row_width = BODY_HORIZ_DISTANCE;
    double top_row_height = 0;
    if (CustomIconItem *stereotypeIconItem = this->stereotypeIconItem()) {
        top_row_width += stereotypeIconItem->boundingRect().width();
        top_row_height = stereotypeIconItem->boundingRect().height();
    }
    if (m_baseClasses) {
        top_row_width += m_baseClasses->boundingRect().width();
        top_row_height = std::max(top_row_height, m_baseClasses->boundingRect().height());
    }
    width = std::max(width, top_row_width);
    height += top_row_height;
    if (StereotypesItem *stereotypesItem = this->stereotypesItem()) {
        width = std::max(width, stereotypesItem->boundingRect().width());
        height += stereotypesItem->boundingRect().height();
    }
    if (m_namespace) {
        width = std::max(width, m_namespace->boundingRect().width());
        height += m_namespace->boundingRect().height();
    }
    if (nameItem()) {
        width = std::max(width, nameItem()->boundingRect().width());
        height += nameItem()->boundingRect().height();
    }
    if (m_contextLabel)
        height += m_contextLabel->height();
    if (m_attributesSeparator)
        height += 8.0;
    if (m_attributes) {
        width = std::max(width, m_attributes->boundingRect().width());
        height += m_attributes->boundingRect().height();
    }
    if (m_methodsSeparator)
        height += 8.0;
    if (m_methods) {
        width = std::max(width, m_methods->boundingRect().width());
        height += m_methods->boundingRect().height();
    }
    height += BODY_VERT_BORDER;

    width = BODY_HORIZ_BORDER + width + BODY_HORIZ_BORDER;

    return GeometryUtilities::ensureMinimumRasterSize(QSizeF(width, height), 2 * RASTER_WIDTH, 2 * RASTER_HEIGHT);
}

void ClassItem::updateGeometry()
{
    prepareGeometryChange();

    // calc width and height
    double width = 0.0;
    double height = 0.0;

    QSizeF geometry = calcMinimumGeometry();
    width = geometry.width();
    height = geometry.height();

    if (object()->isAutoSized()) {
        correctAutoSize(customIconItem(), width, height, MINIMUM_AUTO_WIDTH, MINIMUM_AUTO_HEIGHT);
    } else {
        QRectF rect = object()->rect();
        if (rect.width() > width)
            width = rect.width();
        if (rect.height() > height)
            height = rect.height();
    }

    // update sizes and positions
    double left = -width / 2.0;
    double right = width / 2.0;
    double top = -height / 2.0;
    //double bottom = height / 2.0;
    double y = top;

    setPos(object()->pos());

    QRectF rect(left, top, width, height);

    // the object is updated without calling DiagramController intentionally.
    // attribute rect is not a real attribute stored on DObject but
    // a backup for the graphics item used for manual resized and persistency.
    object()->setRect(rect);

    if (customIconItem()) {
        customIconItem()->setPos(left, top);
        customIconItem()->setActualSize(QSizeF(width, height));
    }

    if (m_shape)
        m_shape->setRect(rect);

    if (customIconItem()) {
        switch (shapeIcon().textAlignment()) {
        case qmt::StereotypeIcon::TextalignBelow:
            y += height + BODY_VERT_BORDER;
            break;
        case qmt::StereotypeIcon::TextalignCenter:
        {
            double h = 0.0;
            if (CustomIconItem *stereotypeIconItem = this->stereotypeIconItem())
                h += stereotypeIconItem->boundingRect().height();
            if (StereotypesItem *stereotypesItem = this->stereotypesItem())
                h += stereotypesItem->boundingRect().height();
            if (nameItem())
                h += nameItem()->boundingRect().height();
            if (m_contextLabel)
                h += m_contextLabel->height();
            y = top + (height - h) / 2.0;
            break;
        }
        case qmt::StereotypeIcon::TextalignNone:
            // nothing to do
            break;
        case qmt::StereotypeIcon::TextalignTop:
            y += BODY_VERT_BORDER;
            break;
        }
    } else {
        y += BODY_VERT_BORDER;
    }
    double first_row_height = 0;
    if (m_baseClasses) {
        m_baseClasses->setPos(left + BODY_HORIZ_BORDER, y);
        first_row_height = m_baseClasses->boundingRect().height();
    }
    if (CustomIconItem *stereotypeIconItem = this->stereotypeIconItem()) {
        stereotypeIconItem->setPos(right - stereotypeIconItem->boundingRect().width() - BODY_HORIZ_BORDER, y);
        first_row_height = std::max(first_row_height, stereotypeIconItem->boundingRect().height());
    }
    y += first_row_height;
    if (StereotypesItem *stereotypesItem = this->stereotypesItem()) {
        stereotypesItem->setPos(-stereotypesItem->boundingRect().width() / 2.0, y);
        y += stereotypesItem->boundingRect().height();
    }
    if (m_namespace) {
        m_namespace->setPos(-m_namespace->boundingRect().width() / 2.0, y);
        y += m_namespace->boundingRect().height();
    }
    if (nameItem()) {
        nameItem()->setPos(-nameItem()->boundingRect().width() / 2.0, y);
        y += nameItem()->boundingRect().height();
    }
    if (m_contextLabel) {
        if (customIconItem())
            m_contextLabel->resetMaxWidth();
        else
            m_contextLabel->setMaxWidth(width - 2 * BODY_HORIZ_BORDER);
        m_contextLabel->setPos(-m_contextLabel->boundingRect().width() / 2.0, y);
        y += m_contextLabel->boundingRect().height();
    }
    if (m_attributesSeparator) {
        m_attributesSeparator->setLine(left, 4.0, right, 4.0);
        m_attributesSeparator->setPos(0, y);
        y += 8.0;
    }
    if (m_attributes) {
        if (customIconItem())
            m_attributes->setPos(-m_attributes->boundingRect().width() / 2.0, y);
        else
            m_attributes->setPos(left + BODY_HORIZ_BORDER, y);
        y += m_attributes->boundingRect().height();
    }
    if (m_methodsSeparator) {
        m_methodsSeparator->setLine(left, 4.0, right, 4.0);
        m_methodsSeparator->setPos(0, y);
        y += 8.0;
    }
    if (m_methods) {
        if (customIconItem())
            m_methods->setPos(-m_methods->boundingRect().width() / 2.0, y);
        else
            m_methods->setPos(left + BODY_HORIZ_BORDER, y);
        y += m_methods->boundingRect().height();
    }

    if (m_templateParameterBox) {
        m_templateParameterBox->setBreakLines(false);
        double x = right - m_templateParameterBox->boundingRect().width() * 0.8;
        if (x < 0) {
            m_templateParameterBox->setBreakLines(true);
            x = right - m_templateParameterBox->boundingRect().width() * 0.8;
        }
        if (x < 0)
            x = 0;
        m_templateParameterBox->setPos(x, top - m_templateParameterBox->boundingRect().height() + BODY_VERT_BORDER);
    }

    updateSelectionMarkerGeometry(rect);
    updateRelationStarterGeometry(rect);
    updateAlignmentButtonsGeometry(rect);
    updateDepth();
}

void ClassItem::updateMembers(const Style *style)
{
    Q_UNUSED(style)

    m_attributesText.clear();
    m_methodsText.clear();

    MClassMember::Visibility attributesVisibility = MClassMember::VisibilityUndefined;
    MClassMember::Visibility methodsVisibility = MClassMember::VisibilityUndefined;
    QString attributesGroup;
    QString methodsGroup;

    MClassMember::Visibility *currentVisibility = nullptr;
    QString *currentGroup = nullptr;
    QString *text = nullptr;

    auto dclass = dynamic_cast<DClass *>(object());
    QMT_ASSERT(dclass, return);

    for (const MClassMember &member : dclass->members()) {
        switch (member.memberType()) {
        case MClassMember::MemberUndefined:
            QMT_ASSERT(false, return);
            break;
        case MClassMember::MemberAttribute:
            currentVisibility = &attributesVisibility;
            currentGroup = &attributesGroup;
            text = &m_attributesText;
            break;
        case MClassMember::MemberMethod:
            currentVisibility = &methodsVisibility;
            currentGroup = &methodsGroup;
            text = &m_methodsText;
            break;
        }

        bool needBr = false;
        if (text && !text->isEmpty())
            needBr = true;

        if (currentVisibility)
            *currentVisibility = member.visibility();
        if (currentGroup && member.group() != *currentGroup) {
            needBr = false;
            *text += QString("<p style=\"padding:0;margin-top:6;margin-bottom:0;\"><b>[%1]</b></p>").arg(member.group());
            *currentGroup = member.group();
        }

        if (needBr)
            *text += "<br/>";

        bool addSpace = false;
        bool haveSignal = false;
        bool haveSlot = false;
        if (member.visibility() != MClassMember::VisibilityUndefined) {
            QString vis;
            switch (member.visibility()) {
            case MClassMember::VisibilityUndefined:
                break;
            case MClassMember::VisibilityPublic:
                vis = "+";
                addSpace = true;
                break;
            case MClassMember::VisibilityProtected:
                vis = "#";
                addSpace = true;
                break;
            case MClassMember::VisibilityPrivate:
                vis = "-";
                addSpace = true;
                break;
            case MClassMember::VisibilitySignals:
                vis = "&gt;";
                haveSignal = true;
                addSpace = true;
                break;
            case MClassMember::VisibilityPrivateSlots:
                vis = "-$";
                haveSlot = true;
                addSpace = true;
                break;
            case MClassMember::VisibilityProtectedSlots:
                vis = "#$";
                haveSlot = true;
                addSpace = true;
                break;
            case MClassMember::VisibilityPublicSlots:
                vis = "+$";
                haveSlot = true;
                addSpace = true;
                break;
            }
            *text += vis;
        }

        if (member.properties() & MClassMember::PropertyQsignal && !haveSignal) {
            *text += "&gt;";
            addSpace = true;
        }
        if (member.properties() & MClassMember::PropertyQslot && !haveSlot) {
            *text += "$";
            addSpace = true;
        }
        if (addSpace)
            *text += " ";
        if (member.properties() & MClassMember::PropertyQinvokable)
            *text += "invokable ";
        if (!member.stereotypes().isEmpty()) {
            *text += StereotypesItem::format(member.stereotypes());
            *text += " ";
        }
        if (member.properties() & MClassMember::PropertyStatic)
            *text += "static ";
        if (member.properties() & MClassMember::PropertyVirtual)
            *text += "virtual ";
        *text += member.declaration().toHtmlEscaped();
        if (member.properties() & MClassMember::PropertyConst)
            *text += " const";
        if (member.properties() & MClassMember::PropertyOverride)
            *text += " override";
        if (member.properties() & MClassMember::PropertyFinal)
            *text += " final";
        if (member.properties() & MClassMember::PropertyAbstract)
            *text += " = 0";
    }
}

} // namespace qmt