File: qscxmlstatemachinedebuginterface.cpp

package info (click to toggle)
gammaray 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,612 kB
  • sloc: cpp: 94,643; ansic: 2,227; sh: 336; python: 164; yacc: 90; lex: 82; xml: 61; makefile: 26
file content (269 lines) | stat: -rw-r--r-- 8,173 bytes parent folder | download
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
/*
  qscxmlstatemachinedebuginterface.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Jan Arne Petersen <jan.petersen@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "qscxmlstatemachinedebuginterface.h"

#include <QScxmlStateMachine>
#include <private/qscxmlstatemachineinfo_p.h>

namespace GammaRay {

static QScxmlStateMachineInfo::StateId fromState(State state)
{
    return static_cast<QScxmlStateMachineInfo::StateId>(state);
}

static State toState(QScxmlStateMachineInfo::StateId state)
{
    return State(static_cast<quintptr>(state));
}

static QScxmlStateMachineInfo::TransitionId fromTransition(Transition transition)
{
    return static_cast<QScxmlStateMachineInfo::TransitionId>(transition);
}

static Transition toTransition(QScxmlStateMachineInfo::TransitionId transition)
{
    return Transition(static_cast<quintptr>(transition));
}

QScxmlStateMachineDebugInterface::QScxmlStateMachineDebugInterface(QScxmlStateMachine *stateMachine, QObject *parent)
    : StateMachineDebugInterface(parent)
    , m_stateMachine(stateMachine)
    , m_info(new QScxmlStateMachineInfo(stateMachine))
{
    // clang-format off
    connect(stateMachine, SIGNAL(runningChanged(bool)), this, SIGNAL(runningChanged(bool)));
    connect(stateMachine, SIGNAL(log(QString,QString)), this, SIGNAL(logMessage(QString,QString)));

    connect(m_info.data(), SIGNAL(statesEntered(QVector<QScxmlStateMachineInfo::StateId>)), this, SLOT(statesEntered(QVector<QScxmlStateMachineInfo::StateId>)));
    connect(m_info.data(), SIGNAL(statesExited(QVector<QScxmlStateMachineInfo::StateId>)), this, SLOT(statesExited(QVector<QScxmlStateMachineInfo::StateId>)));
    connect(m_info.data(), SIGNAL(transitionsTriggered(QVector<QScxmlStateMachineInfo::TransitionId>)), this, SLOT(transitionsTriggered(QVector<QScxmlStateMachineInfo::TransitionId>)));
    // clang-format on
}

QScxmlStateMachineDebugInterface::~QScxmlStateMachineDebugInterface()
{
    delete m_info;
}

bool QScxmlStateMachineDebugInterface::isRunning() const
{
    return m_stateMachine->isRunning();
}

void QScxmlStateMachineDebugInterface::start()
{
    m_stateMachine->start();
}

void QScxmlStateMachineDebugInterface::stop()
{
    m_stateMachine->stop();
}

QVector<State> QScxmlStateMachineDebugInterface::configuration() const
{
    const QVector<QScxmlStateMachineInfo::StateId> configuration = m_info->configuration();

    QVector<State> result;
    result.reserve(configuration.size());

    for (auto stateId : configuration)
        result.append(toState(stateId));

    std::sort(result.begin(), result.end());
    return result;
}

State QScxmlStateMachineDebugInterface::rootState() const
{
    return toState(QScxmlStateMachineInfo::InvalidStateId);
}

QVector<State> QScxmlStateMachineDebugInterface::stateChildren(State state) const
{
    const QVector<QScxmlStateMachineInfo::StateId> children = m_info->stateChildren(fromState(state));

    QVector<State> result;
    result.reserve(children.size());

    for (auto stateId : children)
        result.append(toState(stateId));

    return result;
}

State QScxmlStateMachineDebugInterface::parentState(State state) const
{
    return toState(m_info->stateParent(fromState(state)));
}

bool QScxmlStateMachineDebugInterface::isInitialState(State state) const
{
    const auto stateId = fromState(state);

    const auto parentState = m_info->stateParent(state);
    const auto parentInitialTransition = m_info->initialTransition(parentState);
    if (parentInitialTransition == QScxmlStateMachineInfo::InvalidTransitionId)
        return false;

    return m_info->transitionTargets(parentInitialTransition).contains(stateId);
}

QString QScxmlStateMachineDebugInterface::transitions(State state) const
{
    Q_UNUSED(state);

    return QString(); // FIXME
}

QString QScxmlStateMachineDebugInterface::stateLabel(State state) const
{
    auto stateId = fromState(state);
    if (stateId == QScxmlStateMachineInfo::InvalidStateId)
        return m_stateMachine->name();

    return QStringLiteral("%1 (%2)").arg(m_info->stateName(stateId)).arg(stateId);
}

QString QScxmlStateMachineDebugInterface::stateDisplay(State state) const
{
    return stateLabel(state);
}

QString QScxmlStateMachineDebugInterface::stateDisplayType(State state) const
{
    switch (m_info->stateType(fromState(state))) {
    case QScxmlStateMachineInfo::InvalidState:
        return QStringLiteral("StateMachineState");
    case QScxmlStateMachineInfo::NormalState:
        return QStringLiteral("NormalState");
    case QScxmlStateMachineInfo::ParallelState:
        return QStringLiteral("ParallelState");
    case QScxmlStateMachineInfo::FinalState:
        return QStringLiteral("FinalState");
    case QScxmlStateMachineInfo::ShallowHistoryState:
        return QStringLiteral("ShallowHistoryState");
    case QScxmlStateMachineInfo::DeepHistoryState:
        return QStringLiteral("DeepHistoryState");
    }

    return QString();
}

StateType QScxmlStateMachineDebugInterface::stateType(State state) const
{
    switch (m_info->stateType(fromState(state))) {
    case QScxmlStateMachineInfo::InvalidState:
        return StateMachineState;
    case QScxmlStateMachineInfo::NormalState:
        return OtherState;
    case QScxmlStateMachineInfo::ParallelState:
        return OtherState; // FIXME: No ParallelState. Bug.
    case QScxmlStateMachineInfo::FinalState:
        return FinalState;
    case QScxmlStateMachineInfo::ShallowHistoryState:
        return ShallowHistoryState;
    case QScxmlStateMachineInfo::DeepHistoryState:
        return DeepHistoryState;
    }

    return OtherState;
}

QVector<Transition> QScxmlStateMachineDebugInterface::stateTransitions(State state) const
{
    const auto stateId = fromState(state);
    const auto transitions = m_info->allTransitions();

    const auto initialTransition = m_info->initialTransition(stateId);

    QVector<Transition> result;
    for (auto transition : transitions) {
        if (transition != initialTransition && m_info->transitionSource(transition) == stateId)
            result.append(toTransition(transition));
    }

    return result;
}

QString QScxmlStateMachineDebugInterface::transitionLabel(Transition transition) const
{
    auto transitionId = fromTransition(transition);

    if (transitionId == QScxmlStateMachineInfo::InvalidTransitionId) {
        return QString();
    }

    auto events = m_info->transitionEvents(transition);
    if (events.empty()) {
        return QString();
    }

    return QStringLiteral("%1 (%2)").arg(events.first()).arg(transition.m_id);
}

State QScxmlStateMachineDebugInterface::transitionSource(Transition transition) const
{
    return toState(m_info->transitionSource(fromTransition(transition)));
}

QVector<State> QScxmlStateMachineDebugInterface::transitionTargets(Transition transition) const
{
    const auto targets = m_info->transitionTargets(fromTransition(transition));

    QVector<State> result;
    result.reserve(targets.size());

    for (auto stateId : targets)
        result.append(toState(stateId));

    return result;
}

void QScxmlStateMachineDebugInterface::statesEntered(const QVector<QScxmlStateMachineInfo::StateId> &states)
{
    for (auto state : states) {
        emit stateEntered(toState(state));
    }
}

void QScxmlStateMachineDebugInterface::statesExited(const QVector<QScxmlStateMachineInfo::StateId> &states)
{
    for (auto state : states) {
        emit stateExited(toState(state));
    }
}

void QScxmlStateMachineDebugInterface::transitionsTriggered(const QVector<QScxmlStateMachineInfo::TransitionId> &transitions)
{
    for (auto transition : transitions) {
        emit transitionTriggered(toTransition(transition));
    }
}

bool QScxmlStateMachineDebugInterface::stateValid(State state) const
{
    Q_UNUSED(state);
    return true;
}

QObject *QScxmlStateMachineDebugInterface::stateObject(State state) const
{
    Q_UNUSED(state);
    return m_stateMachine;
}

}