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

#include "quicktransitionitem.h"
#include "scxmleditortr.h"

#include <QGraphicsSceneMouseEvent>
#include <QPainter>

using namespace ScxmlEditor::PluginInterface;

const qreal CAPX = 4;
const qreal CAPY = 3;

QuickTransitionItem::QuickTransitionItem(int index, ItemType connectionType, QGraphicsItem *parent)
    : QGraphicsObject(parent)
    , m_connectionType(connectionType)
{
    setParentItem(parent);
    installSceneEventFilter(parent);
    setZValue(501);

    m_rect = QRectF(index * 25, -30, 20, 20);
    m_drawingRect = m_rect.adjusted(4, 4, -4, -4);
    m_stateRect = m_rect.adjusted(3, 4, -3, -4);
    m_brush.setStyle(Qt::SolidPattern);
    m_brush.setColor(QColor(0xe8, 0xe8, 0xe8));

    m_pen.setColor(QColor(0x12, 0x12, 0x12));
    m_pen.setCapStyle(Qt::RoundCap);
    setAcceptHoverEvents(true);
}

void QuickTransitionItem::setConnectionType(ItemType connectionType)
{
    m_connectionType = connectionType;
}

void QuickTransitionItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *e)
{
    Q_UNUSED(e)
    m_brush.setColor(QColor(0xe8, 0xe8, 0xe8));
    update();
}

void QuickTransitionItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
{
    Q_UNUSED(e)
    m_brush.setColor(QColor(0xff, 0xc4, 0xff));
    update();
}

void QuickTransitionItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)

    painter->save();

    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setPen(m_pen);
    painter->setBrush(m_brush);
    painter->drawRect(m_rect);

    if (m_connectionType == UnknownType) {
        QPointF endPoint = m_drawingRect.topRight();
        painter->drawLine(m_drawingRect.bottomLeft(), endPoint);
        painter->drawLine(endPoint, endPoint + QPointF(-5, 0));
        painter->drawLine(endPoint, endPoint + QPointF(0, 5));
    } else if (m_connectionType >= FinalStateType) {
        switch (m_connectionType) {
        case StateType:
            painter->setPen(m_pen);
            painter->setBrush(Qt::white);
            painter->drawRoundedRect(m_stateRect, 2, 2);
            break;

        case ParallelType:
            painter->setPen(m_pen);
            painter->setBrush(Qt::white);
            painter->drawRoundedRect(m_stateRect, 2, 2);

            painter->setPen(m_pen);
            painter->drawLine(QPointF(m_stateRect.left() + CAPX, m_stateRect.center().y()), QPointF(m_stateRect.right() - CAPX, m_stateRect.center().y()));
            painter->drawLine(QPointF(m_stateRect.center().x(), m_stateRect.top() + CAPY), QPointF(m_stateRect.center().x(), m_stateRect.bottom() - CAPY));
            painter->drawLine(QPointF(m_stateRect.right() - CAPX, m_stateRect.top() + CAPY), QPointF(m_stateRect.center().x(), m_stateRect.top() + CAPY));
            painter->drawLine(QPointF(m_stateRect.right() - CAPX, m_stateRect.bottom() - CAPY), QPointF(m_stateRect.center().x(), m_stateRect.bottom() - CAPY));
            break;

        case FinalStateType:
            painter->setPen(m_pen);
            painter->setBrush(Qt::white);
            painter->drawEllipse(m_stateRect.center(), 7, 7);

            painter->setPen(Qt::NoPen);
            painter->setBrush(Qt::black);
            painter->drawEllipse(m_stateRect.center(), 5, 5);
            break;

        case HistoryType:
            painter->setFont(QFont("Arial", 6));
            painter->setPen(m_pen);
            painter->setBrush(Qt::white);
            painter->drawEllipse(m_stateRect.center(), 7, 7);
            painter->drawText(m_stateRect, Qt::AlignCenter, Tr::tr("H"));
            break;
        default:
            break;
        }
    }

    painter->restore();
}