diff options
-rw-r--r-- | src/scxml/qscxmlstatemachine.cpp | 28 | ||||
-rw-r--r-- | src/scxml/qscxmlstatemachine.h | 1 | ||||
-rw-r--r-- | tests/auto/auto.pro | 2 | ||||
-rw-r--r-- | tests/auto/statemachine/statemachine.pro | 15 | ||||
-rw-r--r-- | tests/auto/statemachine/test1.scxml | 11 | ||||
-rw-r--r-- | tests/auto/statemachine/tst_statemachine.cpp | 79 | ||||
-rw-r--r-- | tests/auto/statemachine/tst_statemachine.qrc | 5 |
7 files changed, 140 insertions, 1 deletions
diff --git a/src/scxml/qscxmlstatemachine.cpp b/src/scxml/qscxmlstatemachine.cpp index 1058e5a..faef43f 100644 --- a/src/scxml/qscxmlstatemachine.cpp +++ b/src/scxml/qscxmlstatemachine.cpp @@ -787,6 +787,34 @@ int QScxmlInternal::WrappedQStateMachinePrivate::eventIdForDelayedEvent(const QS } /*! + * \brief Retrieves a list of state names of all states. + * + * When \a compress is true (the default), the states which contain child states + * will be filtered out and only the "leaf states" will be returned. + * Passing in false for \a compress will return the full list of all states. + */ +QStringList QScxmlStateMachine::stateNames(bool compress) const +{ + Q_D(const QScxmlStateMachine); + + QList<QObject *> worklist; + worklist.reserve(d->m_qStateMachine->children().size() + d->m_qStateMachine->configuration().size()); + worklist.append(d->m_qStateMachine->children()); + + QStringList res; + while (!worklist.isEmpty()) { + QObject *obj = worklist.takeLast(); + if (QAbstractState *state = qobject_cast<QAbstractState *>(obj)) { + if (!compress || !obj->children().count()) + res.append(state->objectName()); + worklist.append(obj->children()); + } + } + std::sort(res.begin(), res.end()); + return res; +} + +/*! * \brief Retrieves a list of state names of all active states. * * When a state is active, then by definition all its parent states are active too. When \a compress diff --git a/src/scxml/qscxmlstatemachine.h b/src/scxml/qscxmlstatemachine.h index b90b716..872b5fa 100644 --- a/src/scxml/qscxmlstatemachine.h +++ b/src/scxml/qscxmlstatemachine.h @@ -93,6 +93,7 @@ public: bool isRunning() const; QString name() const; + QStringList stateNames(bool compress = true) const; QStringList activeStateNames(bool compress = true) const; bool isActive(const QString &scxmlStateName) const; diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 2e545dd..b4876f0 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -1,2 +1,2 @@ TEMPLATE = subdirs -SUBDIRS = cmake parser scion +SUBDIRS = cmake parser scion statemachine diff --git a/tests/auto/statemachine/statemachine.pro b/tests/auto/statemachine/statemachine.pro new file mode 100644 index 0000000..c96ce21 --- /dev/null +++ b/tests/auto/statemachine/statemachine.pro @@ -0,0 +1,15 @@ +QT = core gui qml testlib scxml +CONFIG += testcase + +TARGET = tst_statemachine +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +RESOURCES += tst_statemachine.qrc + +SOURCES += \ + tst_statemachine.cpp + +load(qscxmlc) diff --git a/tests/auto/statemachine/test1.scxml b/tests/auto/statemachine/test1.scxml new file mode 100644 index 0000000..a8a9070 --- /dev/null +++ b/tests/auto/statemachine/test1.scxml @@ -0,0 +1,11 @@ +<?xml version="1.0" ?> +<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" + name="test1"> + <state id="a"> + <state id="a1"/> + <state id="a2"/> + </state> + <state id="b"> + <final id="final"/> + </state> +</scxml> diff --git a/tests/auto/statemachine/tst_statemachine.cpp b/tests/auto/statemachine/tst_statemachine.cpp new file mode 100644 index 0000000..3ebed91 --- /dev/null +++ b/tests/auto/statemachine/tst_statemachine.cpp @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#include <QtTest> +#include <QObject> +#include <QXmlStreamReader> +#include <QtScxml/qscxmlparser.h> +#include <QtScxml/qscxmlstatemachine.h> + +Q_DECLARE_METATYPE(QScxmlError); + +class tst_StateMachine: public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void stateNames_data(); + void stateNames(); +}; + +void tst_StateMachine::stateNames_data() +{ + QTest::addColumn<QString>("scxmlFileName"); + QTest::addColumn<bool>("compressed"); + QTest::addColumn<QStringList>("expectedStates"); + + QTest::newRow("test1-compressed") << QString(":/tst_statemachine/test1.scxml") + << true + << (QStringList() << QString("a1") << QString("a2") << QString("final")); + QTest::newRow("test1-not-compressed") << QString(":/tst_statemachine/test1.scxml") + << false + << (QStringList() << QString("a") << QString("a1") << QString("a2") << QString("b") << QString("final")); + +} + +void tst_StateMachine::stateNames() +{ + QFETCH(QString, scxmlFileName); + QFETCH(bool, compressed); + QFETCH(QStringList, expectedStates); + + QScopedPointer<QScxmlStateMachine> stateMachine(QScxmlStateMachine::fromFile(scxmlFileName)); + QVERIFY(!stateMachine.isNull()); + + QCOMPARE(stateMachine->stateNames(compressed), expectedStates); +} + +QTEST_MAIN(tst_StateMachine) + +#include "tst_statemachine.moc" + + diff --git a/tests/auto/statemachine/tst_statemachine.qrc b/tests/auto/statemachine/tst_statemachine.qrc new file mode 100644 index 0000000..1210ef3 --- /dev/null +++ b/tests/auto/statemachine/tst_statemachine.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/tst_statemachine"> + <file>test1.scxml</file> + </qresource> +</RCC> |