diff options
Diffstat (limited to 'src/statemachine/doc/snippets')
20 files changed, 1062 insertions, 0 deletions
diff --git a/src/statemachine/doc/snippets/code/src_corelib_statemachine_qstatemachine.cpp b/src/statemachine/doc/snippets/code/src_corelib_statemachine_qstatemachine.cpp new file mode 100644 index 0000000..4c9a0a5 --- /dev/null +++ b/src/statemachine/doc/snippets/code/src_corelib_statemachine_qstatemachine.cpp @@ -0,0 +1,18 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [simple state machine] +QPushButton button; + +QStateMachine machine; +QState *s1 = new QState(); +s1->assignProperty(&button, "text", "Click me"); + +QFinalState *s2 = new QFinalState(); +s1->addTransition(&button, &QPushButton::clicked, s2); + +machine.addState(s1); +machine.addState(s2); +machine.setInitialState(s1); +machine.start(); +//! [simple state machine] diff --git a/src/statemachine/doc/snippets/qml/statemachine/Button.qml b/src/statemachine/doc/snippets/qml/statemachine/Button.qml new file mode 100644 index 0000000..b968a18 --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/Button.qml @@ -0,0 +1,51 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Window + +Item { + id: container + + property alias text: buttonLabel.text + property alias label: buttonLabel + signal clicked + property alias containsMouse: mouseArea.containsMouse + property alias pressed: mouseArea.pressed + implicitHeight: Math.max(Screen.pixelDensity * 7, buttonLabel.implicitHeight * 1.2) + implicitWidth: Math.max(Screen.pixelDensity * 11, buttonLabel.implicitWidth * 1.3) + height: implicitHeight + width: implicitWidth + property bool checkable: false + property bool checked: false + + SystemPalette { id: palette } + + Rectangle { + id: frame + anchors.fill: parent + color: palette.button + gradient: Gradient { + GradientStop { position: 0.0; color: mouseArea.pressed ? Qt.darker(palette.button, 1.3) : palette.button } + GradientStop { position: 1.0; color: Qt.darker(palette.button, 1.3) } + } + antialiasing: true + radius: height / 6 + border.color: Qt.darker(palette.button, 1.5) + border.width: 1 + } + + MouseArea { + id: mouseArea + anchors.fill: parent + onClicked: container.clicked() + hoverEnabled: true + } + + Text { + id: buttonLabel + text: container.text + color: palette.buttonText + anchors.centerIn: parent + } +} diff --git a/src/statemachine/doc/snippets/qml/statemachine/basicstate.qml b/src/statemachine/doc/snippets/qml/statemachine/basicstate.qml new file mode 100644 index 0000000..dac2914 --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/basicstate.qml @@ -0,0 +1,18 @@ +// Copyright (C) 2014 Ford Motor Company +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [document] +import QtQuick +import QtQml.StateMachine as DSM + +Rectangle { + DSM.StateMachine { + id: stateMachine + initialState: state + running: true + DSM.State { + id: state + } + } +} +//! [document] diff --git a/src/statemachine/doc/snippets/qml/statemachine/finalstate.qml b/src/statemachine/doc/snippets/qml/statemachine/finalstate.qml new file mode 100644 index 0000000..160419a --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/finalstate.qml @@ -0,0 +1,26 @@ +// Copyright (C) 2014 Ford Motor Company +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [document] +import QtQuick +import QtQml.StateMachine as DSM + +Rectangle { + DSM.StateMachine { + id: stateMachine + initialState: state + running: true + DSM.State { + id: state + DSM.TimeoutTransition { + targetState: finalState + timeout: 200 + } + } + DSM.FinalState { + id: finalState + } + onFinished: console.log("state finished") + } +} +//! [document] diff --git a/src/statemachine/doc/snippets/qml/statemachine/guardcondition.qml b/src/statemachine/doc/snippets/qml/statemachine/guardcondition.qml new file mode 100644 index 0000000..9bacea3 --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/guardcondition.qml @@ -0,0 +1,31 @@ +// Copyright (C) 2014 Ford Motor Company +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [document] +import QtQuick +import QtQml.StateMachine as DSM + +Rectangle { + Button { + anchors.fill: parent + id: button + DSM.StateMachine { + DSM.State { + DSM.SignalTransition { + targetState: finalState + signal: button.mysignal + // the guard condition uses the mystr string argument from mysignal + guard: mystr == "test" + } + } + DSM.FinalState { + id: finalState + } + } + // define the signal the SignalTransition is connected with + signal mysignal(mystr: string) + // on clicking the button emit the signal with a single string argument + onClicked: button.mysignal("test") + } +} +//! [document] diff --git a/src/statemachine/doc/snippets/qml/statemachine/historystate.qml b/src/statemachine/doc/snippets/qml/statemachine/historystate.qml new file mode 100644 index 0000000..e3e7a43 --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/historystate.qml @@ -0,0 +1,48 @@ +// Copyright (C) 2014 Ford Motor Company +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [document] +import QtQuick +import QtQml.StateMachine as DSM + +Rectangle { + Button { + anchors.fill: parent + id: button + text: "Press me" + DSM.StateMachine { + id: stateMachine + initialState: parentState + running: true + DSM.State { + id: parentState + initialState: child2 + onEntered: console.log("parentState entered") + onExited: console.log("parentState exited") + DSM.State { + id: child1 + onEntered: console.log("child1 entered") + onExited: console.log("child1 exited") + } + DSM.State { + id: child2 + onEntered: console.log("child2 entered") + onExited: console.log("child2 exited") + } + DSM.HistoryState { + id: historyState + defaultState: child1 + } + DSM.SignalTransition { + targetState: historyState + + // Clicking the button will cause the state machine to enter the child state + // that parentState was in the last time parentState was exited, or the history state's default + // state if parentState has never been entered. + signal: button.clicked + } + } + } + } +} +//! [document] diff --git a/src/statemachine/doc/snippets/qml/statemachine/signaltransition.qml b/src/statemachine/doc/snippets/qml/statemachine/signaltransition.qml new file mode 100644 index 0000000..3d7b4b0 --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/signaltransition.qml @@ -0,0 +1,40 @@ +// Copyright (C) 2014 Ford Motor Company +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [document] +import QtQuick +import QtQml.StateMachine as DSM + +Rectangle { + DSM.StateMachine { + id: stateMachine + initialState: state + running: true + DSM.State { + id: state + DSM.SignalTransition { + targetState: finalState + signal: button.clicked + guard: guardButton.checked + } + } + DSM.FinalState { + id: finalState + } + onFinished: Qt.quit() + } + Row { + spacing: 2 + Button { + id: button + text: "Finish state" + } + + Button { + id: guardButton + checkable: true + text: checked ? "Press to block the SignalTransition" : "Press to unblock the SignalTransition" + } + } +} +//! [document] diff --git a/src/statemachine/doc/snippets/qml/statemachine/signaltransitionsignal.qml b/src/statemachine/doc/snippets/qml/statemachine/signaltransitionsignal.qml new file mode 100644 index 0000000..2aeae2a --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/signaltransitionsignal.qml @@ -0,0 +1,25 @@ +// Copyright (C) 2014 Ford Motor Company +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [document] +import QtQuick +import QtQml.StateMachine as DSM + +Rectangle { + Button { + anchors.fill: parent + id: button + DSM.StateMachine { + DSM.State { + DSM.SignalTransition { + targetState: finalState + signal: button.clicked + } + } + DSM.FinalState { + id: finalState + } + } + } +} +//! [document] diff --git a/src/statemachine/doc/snippets/qml/statemachine/simplestatemachine.qml b/src/statemachine/doc/snippets/qml/statemachine/simplestatemachine.qml new file mode 100644 index 0000000..cd7e01e --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/simplestatemachine.qml @@ -0,0 +1,31 @@ +// Copyright (C) 2014 Ford Motor Company +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [document] +import QtQuick +import QtQml.StateMachine as DSM + +Rectangle { + Button { + anchors.fill: parent + id: button + text: "Finish state" + DSM.StateMachine { + id: stateMachine + initialState: state + running: true + DSM.State { + id: state + DSM.SignalTransition { + targetState: finalState + signal: button.clicked + } + } + DSM.FinalState { + id: finalState + } + onFinished: Qt.quit() + } + } +} +//! [document] diff --git a/src/statemachine/doc/snippets/qml/statemachine/statemachine-button-history.qml b/src/statemachine/doc/snippets/qml/statemachine/statemachine-button-history.qml new file mode 100644 index 0000000..e5947ae --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/statemachine-button-history.qml @@ -0,0 +1,112 @@ +// Copyright (C) 2014 Ford Motor Company +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [document] +import QtQuick +import QtQml.StateMachine + +Rectangle { +//![0] + Row { + anchors.fill: parent + spacing: 2 + Button { + id: button + // change the button label to the active state id + text: s11.active ? "s11" : s12.active ? "s12" : s13.active ? "s13" : "s3" + } + Button { + id: interruptButton + text: s1.active ? "Interrupt" : "Resume" + } + Button { + id: quitButton + text: "quit" + } + } + + StateMachine { + id: stateMachine + // set the initial state + initialState: s1 + + // start the state machine + running: true + + State { + id: s1 + // set the initial state + initialState: s11 + + // create a transition from s1 to s2 when the button is clicked + SignalTransition { + targetState: s2 + signal: quitButton.clicked + } + // do something when the state enters/exits + onEntered: console.log("s1 entered") + onExited: console.log("s1 exited") + State { + id: s11 + // create a transition from s1 to s2 when the button is clicked + SignalTransition { + targetState: s12 + signal: button.clicked + } + // do something when the state enters/exits + onEntered: console.log("s11 entered") + onExited: console.log("s11 exited") + } + + State { + id: s12 + // create a transition from s2 to s3 when the button is clicked + SignalTransition { + targetState: s13 + signal: button.clicked + } + // do something when the state enters/exits + onEntered: console.log("s12 entered") + onExited: console.log("s12 exited") + } + State { + id: s13 + // create a transition from s3 to s1 when the button is clicked + SignalTransition { + targetState: s1 + signal: button.clicked + } + // do something when the state enters/exits + onEntered: console.log("s13 entered") + onExited: console.log("s13 exited") + } + + // create a transition from s1 to s3 when the button is clicked + SignalTransition { + targetState: s3 + signal: interruptButton.clicked + } + HistoryState { + id: s1h + } + } + FinalState { + id: s2 + onEntered: console.log("s2 entered") + onExited: console.log("s2 exited") + } + State { + id: s3 + SignalTransition { + targetState: s1h + signal: interruptButton.clicked + } + // do something when the state enters/exits + onEntered: console.log("s3 entered") + onExited: console.log("s3 exited") + } + onFinished: Qt.quit() + } +//![0] +} +//! [document] diff --git a/src/statemachine/doc/snippets/qml/statemachine/statemachine-button-nested-ignore-quit.qml b/src/statemachine/doc/snippets/qml/statemachine/statemachine-button-nested-ignore-quit.qml new file mode 100644 index 0000000..db40f79 --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/statemachine-button-nested-ignore-quit.qml @@ -0,0 +1,97 @@ +// Copyright (C) 2014 Ford Motor Company +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [document] +import QtQuick +import QtQml.StateMachine + +Rectangle { + Row { + anchors.fill: parent + spacing: 2 + Button { + id: button + // change the button label to the active state id + text: s11.active ? "s11" : s12.active ? "s12" : "s13" + } + Button { + id: quitButton + text: "quit" + } + } + + StateMachine { + id: stateMachine + // set the initial state + initialState: s1 + + // start the state machine + running: true + + State { + id: s1 + // set the initial state + initialState: s11 + + // create a transition from s1 to s2 when the button is clicked + SignalTransition { + targetState: s2 + signal: quitButton.clicked + } + // do something when the state enters/exits + onEntered: console.log("s1 entered") + onExited: console.log("s1 exited") + State { + id: s11 + // create a transition from s11 to s12 when the button is clicked + SignalTransition { + targetState: s12 + signal: button.clicked + } + // do something when the state enters/exits + onEntered: console.log("s11 entered") + onExited: console.log("s11 exited") + } + +//![0] + State { + id: s12 + // create a transition from s12 to s13 when the button is clicked + SignalTransition { + targetState: s13 + signal: button.clicked + } + + // ignore Quit button when we are in state 12 + SignalTransition { + targetState: s12 + signal: quitButton.clicked + } + + // do something when the state enters/exits + onEntered: console.log("s12 entered") + onExited: console.log("s12 exited") + } +//![0] + + State { + id: s13 + // create a transition from s13 to s11 when the button is clicked + SignalTransition { + targetState: s11 + signal: button.clicked + } + // do something when the state enters/exits + onEntered: console.log("s13 entered") + onExited: console.log("s13 exited") + } + } + FinalState { + id: s2 + onEntered: console.log("s2 entered") + onExited: console.log("s2 exited") + } + onFinished: Qt.quit() + } +} +//! [document] diff --git a/src/statemachine/doc/snippets/qml/statemachine/statemachine-button-nested.qml b/src/statemachine/doc/snippets/qml/statemachine/statemachine-button-nested.qml new file mode 100644 index 0000000..eae7427 --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/statemachine-button-nested.qml @@ -0,0 +1,89 @@ +// Copyright (C) 2014 Ford Motor Company +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [document] +import QtQuick +import QtQml.StateMachine + +Rectangle { +//![0] + Row { + anchors.fill: parent + spacing: 2 + Button { + id: button + // change the button label to the active state id + text: s11.active ? "s11" : s12.active ? "s12" : "s13" + } + Button { + id: quitButton + text: "quit" + } + } + + StateMachine { + id: stateMachine + // set the initial state + initialState: s1 + + // start the state machine + running: true + + State { + id: s1 + // set the initial state + initialState: s11 + + // create a transition from s1 to s2 when the button is clicked + SignalTransition { + targetState: s2 + signal: quitButton.clicked + } + // do something when the state enters/exits + onEntered: console.log("s1 entered") + onExited: console.log("s1 exited") + State { + id: s11 + // create a transition from s11 to s12 when the button is clicked + SignalTransition { + targetState: s12 + signal: button.clicked + } + // do something when the state enters/exits + onEntered: console.log("s11 entered") + onExited: console.log("s11 exited") + } + + State { + id: s12 + // create a transition from s12 to s13 when the button is clicked + SignalTransition { + targetState: s13 + signal: button.clicked + } + // do something when the state enters/exits + onEntered: console.log("s12 entered") + onExited: console.log("s12 exited") + } + State { + id: s13 + // create a transition from s13 to s11 when the button is clicked + SignalTransition { + targetState: s11 + signal: button.clicked + } + // do something when the state enters/exits + onEntered: console.log("s13 entered") + onExited: console.log("s13 exited") + } + } + FinalState { + id: s2 + onEntered: console.log("s2 entered") + onExited: console.log("s2 exited") + } + onFinished: Qt.quit() + } +//![0] +} +//! [document] diff --git a/src/statemachine/doc/snippets/qml/statemachine/statemachine-button.qml b/src/statemachine/doc/snippets/qml/statemachine/statemachine-button.qml new file mode 100644 index 0000000..8873117 --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/statemachine-button.qml @@ -0,0 +1,63 @@ +// Copyright (C) 2014 Ford Motor Company +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [document] +import QtQuick +import QtQml.StateMachine + +Rectangle { +//![0] + Button { + anchors.fill: parent + id: button + + // change the button label to the active state id + text: s1.active ? "s1" : s2.active ? "s2" : "s3" + } + + StateMachine { + id: stateMachine + // set the initial state + initialState: s1 + + // start the state machine + running: true + + State { + id: s1 + // create a transition from s1 to s2 when the button is clicked + SignalTransition { + targetState: s2 + signal: button.clicked + } + // do something when the state enters/exits + onEntered: console.log("s1 entered") + onExited: console.log("s1 exited") + } + + State { + id: s2 + // create a transition from s2 to s3 when the button is clicked + SignalTransition { + targetState: s3 + signal: button.clicked + } + // do something when the state enters/exits + onEntered: console.log("s2 entered") + onExited: console.log("s2 exited") + } + State { + id: s3 + // create a transition from s3 to s1 when the button is clicked + SignalTransition { + targetState: s1 + signal: button.clicked + } + // do something when the state enters/exits + onEntered: console.log("s3 entered") + onExited: console.log("s3 exited") + } + } +//![0] +} +//! [document] diff --git a/src/statemachine/doc/snippets/qml/statemachine/timeouttransition.qml b/src/statemachine/doc/snippets/qml/statemachine/timeouttransition.qml new file mode 100644 index 0000000..60bd420 --- /dev/null +++ b/src/statemachine/doc/snippets/qml/statemachine/timeouttransition.qml @@ -0,0 +1,32 @@ +// Copyright (C) 2014 Ford Motor Company +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [document] +import QtQuick +import QtQml.StateMachine as DSM + +Rectangle { + Button { + anchors.fill: parent + id: button + text: "Finish state" + enabled: !stateMachine.running + onClicked: stateMachine.running = true + DSM.StateMachine { + id: stateMachine + initialState: state + running: true + DSM.State { + id: state + DSM.TimeoutTransition { + targetState: finalState + timeout: 1000 + } + } + DSM.FinalState { + id: finalState + } + } + } +} +//! [document] diff --git a/src/statemachine/doc/snippets/statemachine/eventtest.cpp b/src/statemachine/doc/snippets/statemachine/eventtest.cpp new file mode 100644 index 0000000..14cb48b --- /dev/null +++ b/src/statemachine/doc/snippets/statemachine/eventtest.cpp @@ -0,0 +1,37 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QtGui> +#include <QtStateMachine> + +class MyTransition : public QAbstractTransition +{ + Q_OBJECT +public: + MyTransition() {} + +protected: +//![0] + bool eventTest(QEvent *event) override + { + if (event->type() == QEvent::Wrapped) { + QEvent *wrappedEvent = static_cast<QStateMachine::WrappedEvent *>(event)->event(); + if (wrappedEvent->type() == QEvent::KeyPress) { + QKeyEvent *keyEvent = static_cast<QKeyEvent *>(wrappedEvent); + // Do your event test + } + } + return false; + } +//![0] + + void onTransition(QEvent *event) override + { + + } +}; + +int main(int argv, char **args) +{ + return 0; +} diff --git a/src/statemachine/doc/snippets/statemachine/main.cpp b/src/statemachine/doc/snippets/statemachine/main.cpp new file mode 100644 index 0000000..3cbeb5d --- /dev/null +++ b/src/statemachine/doc/snippets/statemachine/main.cpp @@ -0,0 +1,52 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QtStateMachine> +#include <QtWidgets> + +int main(int argv, char **args) +{ + QApplication app(argv, args); + + QLabel *label = new QLabel; + QPushButton *button = new QPushButton; + +//![0] + QStateMachine machine; + QState *s1 = new QState(); + QState *s2 = new QState(); + QState *s3 = new QState(); +//![0] + +//![4] + s1->assignProperty(label, "text", "In state s1"); + s2->assignProperty(label, "text", "In state s2"); + s3->assignProperty(label, "text", "In state s3"); +//![4] + +//![5] + QObject::connect(s3, &QState::entered, button, &QPushButton:showMaximized); + QObject::connect(s3, &QState::exited, button, &QPushButton::showMinimized); +//![5] + +//![1] + s1->addTransition(button, &QPushButton::clicked, s2); + s2->addTransition(button, &QPushButton::clicked, s3); + s3->addTransition(button, &QPushButton::clicked, s1); +//![1] + +//![2] + machine.addState(s1); + machine.addState(s2); + machine.addState(s3); + machine.setInitialState(s1); +//![2] + +//![3] + machine.start(); +//![3] + + label->show(); + + return app.exec(); +} diff --git a/src/statemachine/doc/snippets/statemachine/main2.cpp b/src/statemachine/doc/snippets/statemachine/main2.cpp new file mode 100644 index 0000000..4fe4c19 --- /dev/null +++ b/src/statemachine/doc/snippets/statemachine/main2.cpp @@ -0,0 +1,57 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QtStateMachine> +#include <QtWidgets> + +int main(int argv, char **args) +{ + QApplication app(argv, args); + + QStateMachine machine; + +//![0] + QState *s1 = new QState(); + QState *s11 = new QState(s1); + QState *s12 = new QState(s1); + QState *s13 = new QState(s1); + s1->setInitialState(s11); + machine.addState(s1); +//![0] + +//![2] + s12->addTransition(quitButton, &QPushButton::clicked, s12); +//![2] + +//![1] + QFinalState *s2 = new QFinalState(); + s1->addTransition(quitButton, &QPushButton::clicked, s2); + machine.addState(s2); + machine.setInitialState(s1); + + QObject::connect(&machine, &QStateMachine::finished, + QCoreApplication::instance(), &QCoreApplication::quit); +//![1] + + QButton *interruptButton = new QPushButton("Interrupt Button"); + QWidget *mainWindow = new QWidget(); + +//![3] + QHistoryState *s1h = new QHistoryState(s1); + + QState *s3 = new QState(); + s3->assignProperty(label, "text", "In s3"); + QMessageBox *mbox = new QMessageBox(mainWindow); + mbox->addButton(QMessageBox::Ok); + mbox->setText("Interrupted!"); + mbox->setIcon(QMessageBox::Information); + QObject::connect(s3, &QState::entered, mbox, &QMessageBox::exec); + s3->addTransition(s1h); + machine.addState(s3); + + s1->addTransition(interruptButton, &QPushButton::clicked, s3); +//![3] + + return app.exec(); +} + diff --git a/src/statemachine/doc/snippets/statemachine/main3.cpp b/src/statemachine/doc/snippets/statemachine/main3.cpp new file mode 100644 index 0000000..7bae11f --- /dev/null +++ b/src/statemachine/doc/snippets/statemachine/main3.cpp @@ -0,0 +1,24 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QtGui> +#include <QtStateMachine> + +int main(int argv, char **args) +{ + QApplication app(argv, args); + +//![0] + QState *s1 = new QState(QState::ParallelStates); + // s11 and s12 will be entered in parallel + QState *s11 = new QState(s1); + QState *s12 = new QState(s1); +//![0] + +//![1] + s1->addTransition(s1, &QState::finished, s2); +//![1] + + return app.exec(); +} + diff --git a/src/statemachine/doc/snippets/statemachine/main4.cpp b/src/statemachine/doc/snippets/statemachine/main4.cpp new file mode 100644 index 0000000..bac255a --- /dev/null +++ b/src/statemachine/doc/snippets/statemachine/main4.cpp @@ -0,0 +1,75 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QtGui> +#include <QtStateMachine> + +//![0] +struct StringEvent : public QEvent +{ + StringEvent(const QString &val) + : QEvent(QEvent::Type(QEvent::User+1)), + value(val) {} + + QString value; +}; +//![0] + +//![1] +class StringTransition : public QAbstractTransition +{ + Q_OBJECT + +public: + StringTransition(const QString &value) + : m_value(value) {} + +protected: + bool eventTest(QEvent *e) override + { + if (e->type() != QEvent::Type(QEvent::User+1)) // StringEvent + return false; + StringEvent *se = static_cast<StringEvent*>(e); + return (m_value == se->value); + } + + void onTransition(QEvent *) override {} + +private: + QString m_value; +}; +//![1] + +int main(int argv, char **args) +{ + QApplication app(argv, args); + +//![2] + QStateMachine machine; + QState *s1 = new QState(); + QState *s2 = new QState(); + QFinalState *done = new QFinalState(); + + StringTransition *t1 = new StringTransition("Hello"); + t1->setTargetState(s2); + s1->addTransition(t1); + StringTransition *t2 = new StringTransition("world"); + t2->setTargetState(done); + s2->addTransition(t2); + + machine.addState(s1); + machine.addState(s2); + machine.addState(done); + machine.setInitialState(s1); +//![2] + +//![3] + machine.postEvent(new StringEvent("Hello")); + machine.postEvent(new StringEvent("world")); +//![3] + + return app.exec(); +} + +#include "main4.moc" + diff --git a/src/statemachine/doc/snippets/statemachine/main5.cpp b/src/statemachine/doc/snippets/statemachine/main5.cpp new file mode 100644 index 0000000..3b1b7e9 --- /dev/null +++ b/src/statemachine/doc/snippets/statemachine/main5.cpp @@ -0,0 +1,136 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QtGui> +#include <QtStateMachine> + +int main(int argv, char **args) +{ + QApplication app(argv, args); + QWidget *button; + + { +//![0] + QStateMachine machine; + machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); + + QState *s1 = new QState(); + s1->assignProperty(object, "fooBar", 1.0); + machine.addState(s1); + machine.setInitialState(s1); + + QState *s2 = new QState(); + machine.addState(s2); +//![0] + } + + { + +//![2] + QStateMachine machine; + machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); + + QState *s1 = new QState(); + s1->assignProperty(object, "fooBar", 1.0); + machine.addState(s1); + machine.setInitialState(s1); + + QState *s2 = new QState(s1); + s2->assignProperty(object, "fooBar", 2.0); + s1->setInitialState(s2); + + QState *s3 = new QState(s1); +//![2] + + } + + { +//![3] + QState *s1 = new QState(); + QState *s2 = new QState(); + + s1->assignProperty(button, "geometry", QRectF(0, 0, 50, 50)); + s2->assignProperty(button, "geometry", QRectF(0, 0, 100, 100)); + + s1->addTransition(button, &QPushButton::clicked, s2); +//![3] + + } + + { +//![4] + QState *s1 = new QState(); + QState *s2 = new QState(); + + s1->assignProperty(button, "geometry", QRectF(0, 0, 50, 50)); + s2->assignProperty(button, "geometry", QRectF(0, 0, 100, 100)); + + QSignalTransition *transition = s1->addTransition(button, &QPushButton::clicked, s2); + transition->addAnimation(new QPropertyAnimation(button, "geometry")); +//![4] + + } + + { + QMainWindow *mainWindow = 0; + +//![5] + QMessageBox *messageBox = new QMessageBox(mainWindow); + messageBox->addButton(QMessageBox::Ok); + messageBox->setText("Button geometry has been set!"); + messageBox->setIcon(QMessageBox::Information); + + QState *s1 = new QState(); + + QState *s2 = new QState(); + s2->assignProperty(button, "geometry", QRectF(0, 0, 50, 50)); + connect(s2, &QState::entered, messageBox, SLOT(exec())); + + s1->addTransition(button, &QPushButton::clicked, s2); +//![5] + } + + { + QMainWindow *mainWindow = 0; + +//![6] + QMessageBox *messageBox = new QMessageBox(mainWindow); + messageBox->addButton(QMessageBox::Ok); + messageBox->setText("Button geometry has been set!"); + messageBox->setIcon(QMessageBox::Information); + + QState *s1 = new QState(); + + QState *s2 = new QState(); + s2->assignProperty(button, "geometry", QRectF(0, 0, 50, 50)); + + QState *s3 = new QState(); + connect(s3, &QState::entered, messageBox, SLOT(exec())); + + s1->addTransition(button, &QPushButton::clicked, s2); + s2->addTransition(s2, &QState::propertiesAssigned, s3); +//![6] + + } + + { + +//![7] + QState *s1 = new QState(); + QState *s2 = new QState(); + + s2->assignProperty(object, "fooBar", 2.0); + s1->addTransition(s2); + + QStateMachine machine; + machine.setInitialState(s1); + machine.addDefaultAnimation(new QPropertyAnimation(object, "fooBar")); +//![7] + + } + + + + return app.exec(); +} + |