aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/scxmleditor/plugin_interface/eventitem.cpp
blob: f61517f7cf67418f0bf6ba596d359dc314cb75d2 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "eventitem.h"

#include <QColor>
#include <QFont>
#include <QGraphicsItem>
#include <QList>
#include <QString>

namespace ScxmlEditor {

namespace PluginInterface {

EventItem::EventItem(const QPointF &pos, BaseItem *parent)
    : BaseItem(parent)
{
    m_eventNameItem = new TextItem(this);
    m_eventNameItem->setParentItem(this);
    QFont serifFont("Times", 13, QFont::Normal);
    m_eventNameItem->setFont(serifFont);

    QString color = editorInfo("fontColor");
    m_eventNameItem->setDefaultTextColor(color.isEmpty() ? QColor(Qt::black) : QColor(color));

    setPos(pos);
    m_eventNameItem->setTextInteractionFlags(Qt::NoTextInteraction);
    setItemBoundingRect(m_eventNameItem->boundingRect());
}

void EventItem::updateAttributes()
{
    QString text = "  " + tag()->tagName();
    if (tag()->attributeNames().size() > 0) {
        for (int i = 0; i < tag()->attributeNames().size(); ++i)
            if (tag()->attributeNames().at(i) == "event") {
                if (tag()->attributeValues().size() > i)
                    text += " / " + tag()->attributeValues().at(i);
                break;
            }
    }
    m_eventNameItem->setText(text);
    setItemBoundingRect(m_eventNameItem->boundingRect());
}

OnEntryExitItem::OnEntryExitItem(BaseItem *parent)
    : BaseItem(parent)
{
    m_eventNameItem = new TextItem(this);
    m_eventNameItem->setParentItem(this);
    QFont serifFont("Times", 13, QFont::Normal);
    m_eventNameItem->setFont(serifFont);
    m_eventNameItem->setDefaultTextColor(Qt::black);
    m_eventNameItem->setTextInteractionFlags(Qt::NoTextInteraction);
}

void OnEntryExitItem::updateAttributes()
{
    QString text = tag()->tagName();

    m_eventNameItem->setText(text);
    setItemBoundingRect(childBoundingRect());
    checkParentBoundingRect();
}

void OnEntryExitItem::finalizeCreation()
{
    auto children = tag()->allChildren();
    auto pos = m_eventNameItem->boundingRect().bottomLeft();
    for (auto child : children) {
        EventItem *item = new EventItem(pos, this);
        item->setTag(child);
        item->updateAttributes();
        pos = item->pos() + item->boundingRect().bottomLeft();
    }

    setItemBoundingRect(childBoundingRect());
}

void OnEntryExitItem::addChild(ScxmlTag *tag)
{
    auto pos = childBoundingRect().bottomLeft();
    EventItem *item = new EventItem(pos, this);
    item->setTag(tag);
    item->updateAttributes();

    setItemBoundingRect(childBoundingRect());
    checkParentBoundingRect();
}

QRectF OnEntryExitItem::childBoundingRect() const
{
    QRectF r = m_eventNameItem->boundingRect();

    const QList<QGraphicsItem *> children = childItems();

    for (const QGraphicsItem *child : children) {
        QRectF br = child->boundingRect();
        QPointF p = child->pos() + br.topLeft();
        br.moveTopLeft(p);
        r = r.united(br);
    }
    return r;
}

} // namespace PluginInterface
} // namespace ScxmlEditor