diff options
Diffstat (limited to 'src/imports/statemachine')
-rw-r--r-- | src/imports/statemachine/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/imports/statemachine/childrenprivate.h | 138 | ||||
-rw-r--r-- | src/imports/statemachine/finalstate.cpp | 4 | ||||
-rw-r--r-- | src/imports/statemachine/finalstate.h | 3 | ||||
-rw-r--r-- | src/imports/statemachine/plugin.cpp | 8 | ||||
-rw-r--r-- | src/imports/statemachine/signaltransition.h | 1 | ||||
-rw-r--r-- | src/imports/statemachine/state.cpp | 4 | ||||
-rw-r--r-- | src/imports/statemachine/state.h | 3 | ||||
-rw-r--r-- | src/imports/statemachine/statemachine.cpp | 4 | ||||
-rw-r--r-- | src/imports/statemachine/statemachine.h | 3 | ||||
-rw-r--r-- | src/imports/statemachine/statemachine.pro | 2 | ||||
-rw-r--r-- | src/imports/statemachine/statemachineforeign.h | 4 | ||||
-rw-r--r-- | src/imports/statemachine/timeouttransition.h | 1 |
13 files changed, 144 insertions, 35 deletions
diff --git a/src/imports/statemachine/CMakeLists.txt b/src/imports/statemachine/CMakeLists.txt index 4e4b73cc7d..232f3f28d5 100644 --- a/src/imports/statemachine/CMakeLists.txt +++ b/src/imports/statemachine/CMakeLists.txt @@ -6,7 +6,7 @@ qt_add_qml_module(qtqmlstatemachine URI "QtQml.StateMachine" - VERSION "1.15" + VERSION "${CMAKE_PROJECT_VERSION}" CLASSNAME QtQmlStateMachinePlugin SKIP_TYPE_REGISTRATION GENERATE_QMLTYPES @@ -26,5 +26,5 @@ qt_add_qml_module(qtqmlstatemachine #### Keys ignored in scope 1:.:.:statemachine.pro:<TRUE>: # CXX_MODULE = "qml" -# IMPORT_VERSION = "1.15" +# QML_IMPORT_VERSION = "$$QT_VERSION" # TARGETPATH = "QtQml/StateMachine" diff --git a/src/imports/statemachine/childrenprivate.h b/src/imports/statemachine/childrenprivate.h index 57cda1c796..2dcecd6531 100644 --- a/src/imports/statemachine/childrenprivate.h +++ b/src/imports/statemachine/childrenprivate.h @@ -46,54 +46,142 @@ #include <QQmlInfo> #include <QQmlListProperty> -template <class T> -class ChildrenPrivate +enum class ChildrenMode { + None = 0x0, + State = 0x1, + Transition = 0x2, + StateOrTransition = State | Transition +}; + +template<typename T> +static T *parentObject(QQmlListProperty<QObject> *prop) { return static_cast<T *>(prop->object); } + +template<class T, ChildrenMode Mode> +struct ParentHandler { -public: - ChildrenPrivate() - {} + static bool unparentItem(QQmlListProperty<QObject> *prop, QObject *oldItem); + static bool parentItem(QQmlListProperty<QObject> *prop, QObject *item); +}; - static void append(QQmlListProperty<QObject> *prop, QObject *item) +template<class T> +struct ParentHandler<T, ChildrenMode::None> +{ + static bool unparentItem(QQmlListProperty<QObject> *, QObject *) { return true; } + static bool parentItem(QQmlListProperty<QObject> *, QObject *) { return true; } +}; + +template<class T> +struct ParentHandler<T, ChildrenMode::State> +{ + static bool parentItem(QQmlListProperty<QObject> *prop, QObject *item) + { + if (QAbstractState *state = qobject_cast<QAbstractState *>(item)) { + state->setParent(parentObject<T>(prop)); + return true; + } + return false; + } + + static bool unparentItem(QQmlListProperty<QObject> *, QObject *oldItem) + { + if (QAbstractState *state = qobject_cast<QAbstractState *>(oldItem)) { + state->setParent(nullptr); + return true; + } + return false; + } +}; + +template<class T> +struct ParentHandler<T, ChildrenMode::Transition> +{ + static bool parentItem(QQmlListProperty<QObject> *prop, QObject *item) { - QAbstractState *state = qobject_cast<QAbstractState*>(item); - if (state) { - item->setParent(prop->object); - } else { - QAbstractTransition *trans = qobject_cast<QAbstractTransition*>(item); - if (trans) - static_cast<T *>(prop->object)->addTransition(trans); + if (QAbstractTransition *trans = qobject_cast<QAbstractTransition *>(item)) { + parentObject<T>(prop)->addTransition(trans); + return true; } - static_cast<ChildrenPrivate<T>*>(prop->data)->children.append(item); - emit static_cast<T *>(prop->object)->childrenChanged(); + return false; } - static void appendNoTransition(QQmlListProperty<QObject> *prop, QObject *item) + static bool unparentItem(QQmlListProperty<QObject> *prop, QObject *oldItem) { - QAbstractState *state = qobject_cast<QAbstractState*>(item); - if (state) { - item->setParent(prop->object); + if (QAbstractTransition *trans = qobject_cast<QAbstractTransition *>(oldItem)) { + parentObject<T>(prop)->removeTransition(trans); + return true; } - static_cast<ChildrenPrivate<T>*>(prop->data)->children.append(item); - emit static_cast<T *>(prop->object)->childrenChanged(); + return false; + } +}; + +template<class T> +struct ParentHandler<T, ChildrenMode::StateOrTransition> +{ + static bool parentItem(QQmlListProperty<QObject> *prop, QObject *oldItem) + { + return ParentHandler<T, ChildrenMode::State>::parentItem(prop, oldItem) + || ParentHandler<T, ChildrenMode::Transition>::parentItem(prop, oldItem); + } + + static bool unparentItem(QQmlListProperty<QObject> *prop, QObject *oldItem) + { + return ParentHandler<T, ChildrenMode::State>::unparentItem(prop, oldItem) + || ParentHandler<T, ChildrenMode::Transition>::unparentItem(prop, oldItem); + } +}; + +template <class T, ChildrenMode Mode> +class ChildrenPrivate +{ +public: + static void append(QQmlListProperty<QObject> *prop, QObject *item) + { + Handler::parentItem(prop, item); + static_cast<Self *>(prop->data)->children.append(item); + emit parentObject<T>(prop)->childrenChanged(); } static int count(QQmlListProperty<QObject> *prop) { - return static_cast<ChildrenPrivate<T>*>(prop->data)->children.count(); + return static_cast<Self *>(prop->data)->children.count(); } static QObject *at(QQmlListProperty<QObject> *prop, int index) { - return static_cast<ChildrenPrivate<T>*>(prop->data)->children.at(index); + return static_cast<Self *>(prop->data)->children.at(index); } static void clear(QQmlListProperty<QObject> *prop) { - static_cast<ChildrenPrivate<T>*>(prop->data)->children.clear(); - emit static_cast<T *>(prop->object)->childrenChanged(); + auto &children = static_cast<Self *>(prop->data)->children; + for (QObject *oldItem : qAsConst(children)) + Handler::unparentItem(prop, oldItem); + + children.clear(); + emit parentObject<T>(prop)->childrenChanged(); + } + + static void replace(QQmlListProperty<QObject> *prop, int index, QObject *item) + { + auto &children = static_cast<Self *>(prop->data)->children; + + Handler::unparentItem(prop, children.at(index)); + Handler::parentItem(prop, item); + + children.replace(index, item); + emit parentObject<T>(prop)->childrenChanged(); + } + + static void removeLast(QQmlListProperty<QObject> *prop) + { + Handler::unparentItem(prop, static_cast<Self *>(prop->data)->children.takeLast()); + emit parentObject<T>(prop)->childrenChanged(); } private: + using Self = ChildrenPrivate<T, Mode>; + using Handler = ParentHandler<T, Mode>; + QList<QObject *> children; }; diff --git a/src/imports/statemachine/finalstate.cpp b/src/imports/statemachine/finalstate.cpp index 54dcc82bae..4d4c6b2cc7 100644 --- a/src/imports/statemachine/finalstate.cpp +++ b/src/imports/statemachine/finalstate.cpp @@ -50,7 +50,9 @@ FinalState::FinalState(QState *parent) QQmlListProperty<QObject> FinalState::children() { - return QQmlListProperty<QObject>(this, &m_children, m_children.appendNoTransition, m_children.count, m_children.at, m_children.clear); + return QQmlListProperty<QObject>(this, &m_children, + m_children.append, m_children.count, m_children.at, + m_children.clear, m_children.replace, m_children.removeLast); } /*! diff --git a/src/imports/statemachine/finalstate.h b/src/imports/statemachine/finalstate.h index 9cdbb51584..fef0e9c092 100644 --- a/src/imports/statemachine/finalstate.h +++ b/src/imports/statemachine/finalstate.h @@ -56,6 +56,7 @@ class FinalState : public QFinalState Q_PROPERTY(QQmlListProperty<QObject> children READ children NOTIFY childrenChanged) Q_CLASSINFO("DefaultProperty", "children") QML_ELEMENT + QML_ADDED_IN_VERSION(1, 0) public: explicit FinalState(QState *parent = 0); @@ -66,7 +67,7 @@ Q_SIGNALS: void childrenChanged(); private: - ChildrenPrivate<FinalState> m_children; + ChildrenPrivate<FinalState, ChildrenMode::State> m_children; }; QT_END_NAMESPACE diff --git a/src/imports/statemachine/plugin.cpp b/src/imports/statemachine/plugin.cpp index 4c991994f3..c370504029 100644 --- a/src/imports/statemachine/plugin.cpp +++ b/src/imports/statemachine/plugin.cpp @@ -48,6 +48,8 @@ #include <QQmlExtensionPlugin> #include <qqml.h> +extern void qml_register_types_QtQml_StateMachine(); + QT_BEGIN_NAMESPACE class QtQmlStateMachinePlugin : public QQmlEngineExtensionPlugin @@ -56,7 +58,11 @@ class QtQmlStateMachinePlugin : public QQmlEngineExtensionPlugin Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid) public: - QtQmlStateMachinePlugin(QObject *parent = nullptr) : QQmlEngineExtensionPlugin(parent) { } + QtQmlStateMachinePlugin(QObject *parent = nullptr) : QQmlEngineExtensionPlugin(parent) + { + volatile auto registration = &qml_register_types_QtQml_StateMachine; + Q_UNUSED(registration); + } }; QT_END_NAMESPACE diff --git a/src/imports/statemachine/signaltransition.h b/src/imports/statemachine/signaltransition.h index 748e230b3e..4d73368eb0 100644 --- a/src/imports/statemachine/signaltransition.h +++ b/src/imports/statemachine/signaltransition.h @@ -58,6 +58,7 @@ class SignalTransition : public QSignalTransition, public QQmlParserStatus Q_PROPERTY(QJSValue signal READ signal WRITE setSignal NOTIFY qmlSignalChanged) Q_PROPERTY(QQmlScriptString guard READ guard WRITE setGuard NOTIFY guardChanged) QML_ELEMENT + QML_ADDED_IN_VERSION(1, 0) public: explicit SignalTransition(QState *parent = nullptr); diff --git a/src/imports/statemachine/state.cpp b/src/imports/statemachine/state.cpp index af76087256..10530c2985 100644 --- a/src/imports/statemachine/state.cpp +++ b/src/imports/statemachine/state.cpp @@ -61,7 +61,9 @@ void State::componentComplete() QQmlListProperty<QObject> State::children() { - return QQmlListProperty<QObject>(this, &m_children, m_children.append, m_children.count, m_children.at, m_children.clear); + return QQmlListProperty<QObject>(this, &m_children, + m_children.append, m_children.count, m_children.at, + m_children.clear, m_children.replace, m_children.removeLast); } /*! diff --git a/src/imports/statemachine/state.h b/src/imports/statemachine/state.h index 4b17ea0e5f..8cb454f3cf 100644 --- a/src/imports/statemachine/state.h +++ b/src/imports/statemachine/state.h @@ -56,6 +56,7 @@ class State : public QState, public QQmlParserStatus Q_PROPERTY(QQmlListProperty<QObject> children READ children NOTIFY childrenChanged) Q_CLASSINFO("DefaultProperty", "children") QML_ELEMENT + QML_ADDED_IN_VERSION(1, 0) public: explicit State(QState *parent = 0); @@ -69,7 +70,7 @@ Q_SIGNALS: void childrenChanged(); private: - ChildrenPrivate<State> m_children; + ChildrenPrivate<State, ChildrenMode::StateOrTransition> m_children; }; QT_END_NAMESPACE diff --git a/src/imports/statemachine/statemachine.cpp b/src/imports/statemachine/statemachine.cpp index a983644018..bdad2e6bde 100644 --- a/src/imports/statemachine/statemachine.cpp +++ b/src/imports/statemachine/statemachine.cpp @@ -90,7 +90,9 @@ void StateMachine::componentComplete() QQmlListProperty<QObject> StateMachine::children() { - return QQmlListProperty<QObject>(this, &m_children, m_children.append, m_children.count, m_children.at, m_children.clear); + return QQmlListProperty<QObject>(this, &m_children, + m_children.append, m_children.count, m_children.at, + m_children.clear, m_children.replace, m_children.removeLast); } /*! diff --git a/src/imports/statemachine/statemachine.h b/src/imports/statemachine/statemachine.h index 04894477b3..85ac4cf26b 100644 --- a/src/imports/statemachine/statemachine.h +++ b/src/imports/statemachine/statemachine.h @@ -60,6 +60,7 @@ class StateMachine : public QStateMachine, public QQmlParserStatus Q_CLASSINFO("DefaultProperty", "children") QML_ELEMENT + QML_ADDED_IN_VERSION(1, 0) public: explicit StateMachine(QObject *parent = 0); @@ -82,7 +83,7 @@ Q_SIGNALS: void qmlRunningChanged(); private: - ChildrenPrivate<StateMachine> m_children; + ChildrenPrivate<StateMachine, ChildrenMode::StateOrTransition> m_children; bool m_completed; bool m_running; }; diff --git a/src/imports/statemachine/statemachine.pro b/src/imports/statemachine/statemachine.pro index b17ec6140d..cff81c2416 100644 --- a/src/imports/statemachine/statemachine.pro +++ b/src/imports/statemachine/statemachine.pro @@ -1,7 +1,7 @@ CXX_MODULE = qml TARGET = qtqmlstatemachine TARGETPATH = QtQml/StateMachine -IMPORT_VERSION = 1.15 +QML_IMPORT_VERSION = $$QT_VERSION QT = core-private qml-private diff --git a/src/imports/statemachine/statemachineforeign.h b/src/imports/statemachine/statemachineforeign.h index 363c9d0e31..7543d55fdf 100644 --- a/src/imports/statemachine/statemachineforeign.h +++ b/src/imports/statemachine/statemachineforeign.h @@ -51,6 +51,7 @@ struct QHistoryStateForeign Q_GADGET QML_FOREIGN(QHistoryState) QML_NAMED_ELEMENT(HistoryState) + QML_ADDED_IN_VERSION(1, 0) }; struct QStateForeign @@ -58,6 +59,7 @@ struct QStateForeign Q_GADGET QML_FOREIGN(QState) QML_NAMED_ELEMENT(QState) + QML_ADDED_IN_VERSION(1, 0) QML_UNCREATABLE("Don't use this, use State instead.") }; @@ -66,6 +68,7 @@ struct QAbstractStateForeign Q_GADGET QML_FOREIGN(QAbstractState) QML_NAMED_ELEMENT(QAbstractState) + QML_ADDED_IN_VERSION(1, 0) QML_UNCREATABLE("Don't use this, use State instead.") }; @@ -74,6 +77,7 @@ struct QSignalTransitionForeign Q_GADGET QML_FOREIGN(QSignalTransition) QML_NAMED_ELEMENT(QSignalTransition) + QML_ADDED_IN_VERSION(1, 0) QML_UNCREATABLE("Don't use this, use SignalTransition instead.") }; diff --git a/src/imports/statemachine/timeouttransition.h b/src/imports/statemachine/timeouttransition.h index cc3a22e0e5..3d056b5e41 100644 --- a/src/imports/statemachine/timeouttransition.h +++ b/src/imports/statemachine/timeouttransition.h @@ -53,6 +53,7 @@ class TimeoutTransition : public QSignalTransition, public QQmlParserStatus Q_PROPERTY(int timeout READ timeout WRITE setTimeout NOTIFY timeoutChanged) Q_INTERFACES(QQmlParserStatus) QML_ELEMENT + QML_ADDED_IN_VERSION(1, 0) public: TimeoutTransition(QState *parent = nullptr); |