diff options
author | Juha Vuolle <juha.vuolle@qt.io> | 2023-03-08 14:03:52 +0200 |
---|---|---|
committer | Juha Vuolle <juha.vuolle@qt.io> | 2023-03-17 13:02:05 +0200 |
commit | 6c9e0421f9978b08aed615ca29a584c6e9d48493 (patch) | |
tree | 07bcb1d3c449ed6bd459885c44e5ec80b9d78a4c | |
parent | 4046e88de0f090b59e26de0c86192d564e46b9d9 (diff) |
Revamp scxml mediaplayer example
Pick-to: 6.5
Task-number: QTBUG-111323
Change-Id: I817c1f7ee10027964e9247dac1ad242d42c2d77b
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r-- | examples/scxml/mediaplayer/CMakeLists.txt | 32 | ||||
-rw-r--r-- | examples/scxml/mediaplayer/MainWindow.qml (renamed from examples/scxml/mediaplayer/Mediaplayer.qml) | 22 | ||||
-rw-r--r-- | examples/scxml/mediaplayer/doc/src/mediaplayer.qdoc | 2 | ||||
-rw-r--r-- | examples/scxml/mediaplayer/main.cpp | 17 | ||||
-rw-r--r-- | examples/scxml/mediaplayer/mediaplayer-qml.h | 20 | ||||
-rw-r--r-- | examples/scxml/mediaplayer/mediaplayer.pro | 16 | ||||
-rw-r--r-- | examples/scxml/mediaplayer/mediaplayer.qrc | 5 | ||||
-rw-r--r-- | examples/scxml/mediaplayer/mediaplayer.scxml | 2 | ||||
-rw-r--r-- | examples/scxml/mediaplayer/qmldir | 3 | ||||
-rw-r--r-- | examples/scxml/mediaplayer/thedatamodel.cpp | 8 | ||||
-rw-r--r-- | examples/scxml/mediaplayer/thedatamodel.h | 5 |
11 files changed, 80 insertions, 52 deletions
diff --git a/examples/scxml/mediaplayer/CMakeLists.txt b/examples/scxml/mediaplayer/CMakeLists.txt index d4ddfe8..0af987f 100644 --- a/examples/scxml/mediaplayer/CMakeLists.txt +++ b/examples/scxml/mediaplayer/CMakeLists.txt @@ -1,13 +1,9 @@ -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause cmake_minimum_required(VERSION 3.16) project(mediaplayer LANGUAGES CXX) -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(CMAKE_AUTOMOC ON) - if(NOT DEFINED INSTALL_EXAMPLESDIR) set(INSTALL_EXAMPLESDIR "examples") endif() @@ -16,9 +12,10 @@ set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/scxml/mediaplayer") find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml) +qt_standard_project_setup(REQUIRES 6.5) + qt_add_executable(mediaplayerscxml main.cpp - thedatamodel.cpp thedatamodel.h ) set_target_properties(mediaplayerscxml PROPERTIES @@ -26,29 +23,28 @@ set_target_properties(mediaplayerscxml PROPERTIES MACOSX_BUNDLE TRUE ) -target_link_libraries(mediaplayerscxml PUBLIC +target_link_libraries(mediaplayerscxml PRIVATE Qt::Core Qt::Gui Qt::Qml Qt::Scxml ) -set(mediaplayer_resource_files - "Mediaplayer.qml" -) - -qt6_add_resources(mediaplayerscxml "mediaplayer" - PREFIX - "/" - FILES - ${mediaplayer_resource_files} -) - # Statecharts: qt6_add_statecharts(mediaplayerscxml mediaplayer.scxml ) +qt_add_qml_module(mediaplayerscxml + URI Mediaplayer + VERSION 1.0 + QML_FILES + MainWindow.qml + SOURCES + mediaplayer-qml.h + thedatamodel.h thedatamodel.cpp +) + install(TARGETS mediaplayerscxml RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" diff --git a/examples/scxml/mediaplayer/Mediaplayer.qml b/examples/scxml/mediaplayer/MainWindow.qml index c966e4e..de4f710 100644 --- a/examples/scxml/mediaplayer/Mediaplayer.qml +++ b/examples/scxml/mediaplayer/MainWindow.qml @@ -1,12 +1,12 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +pragma ComponentBehavior: Bound + import QtQuick import QtQuick.Window import QtScxml - -import MediaPlayerStateMachine -import MediaPlayerDataModel +import Mediaplayer Window { id: root @@ -15,8 +15,9 @@ Window { id: model } - property StateMachine stateMachine: MediaPlayerStateMachine { - onDataModelChanged: start() + MediaPlayerStateMachine { + id: stateMachine + onDataModelChanged: stateMachine.start() dataModel: model } @@ -41,17 +42,20 @@ Window { highlight: Rectangle { color: "lightsteelblue" } currentIndex: -1 delegate: Rectangle { + id: delegateRect + required property string media + required property int index height: 40 width: parent.width color: "transparent" MouseArea { anchors.fill: parent; - onClicked: tap(index) + onClicked: root.tap(delegateRect.index) } Text { id: txt anchors.fill: parent - text: media + text: delegateRect.media verticalAlignment: Text.AlignVCenter } } @@ -75,7 +79,7 @@ Window { } EventConnection { - stateMachine: root.stateMachine + stateMachine: stateMachine events: ["playbackStarted", "playbackStopped"] onOccurred: (event)=> { var media = event.data.media; diff --git a/examples/scxml/mediaplayer/doc/src/mediaplayer.qdoc b/examples/scxml/mediaplayer/doc/src/mediaplayer.qdoc index 77f2c33..3fd8565 100644 --- a/examples/scxml/mediaplayer/doc/src/mediaplayer.qdoc +++ b/examples/scxml/mediaplayer/doc/src/mediaplayer.qdoc @@ -1,4 +1,4 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only /*! diff --git a/examples/scxml/mediaplayer/main.cpp b/examples/scxml/mediaplayer/main.cpp index 2468b66..66d9a75 100644 --- a/examples/scxml/mediaplayer/main.cpp +++ b/examples/scxml/mediaplayer/main.cpp @@ -1,22 +1,17 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QGuiApplication> -#include <QQmlApplicationEngine> -#include <QQmlContext> - -#include "mediaplayer.h" -#include "thedatamodel.h" +#include <QtGui/qguiapplication.h> +#include <QtQml/qqmlapplicationengine.h> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); - qmlRegisterType<TheDataModel>("MediaPlayerDataModel", 1, 0, "MediaPlayerDataModel"); - qmlRegisterType<MediaPlayerStateMachine>("MediaPlayerStateMachine", 1, 0, "MediaPlayerStateMachine"); - QQmlApplicationEngine engine; - engine.load(QUrl(QStringLiteral("qrc:///Mediaplayer.qml"))); + QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, + [](){ QCoreApplication::exit(EXIT_FAILURE); }, Qt::QueuedConnection); + engine.loadFromModule("Mediaplayer", "MainWindow"); return app.exec(); } diff --git a/examples/scxml/mediaplayer/mediaplayer-qml.h b/examples/scxml/mediaplayer/mediaplayer-qml.h new file mode 100644 index 0000000..9d036b7 --- /dev/null +++ b/examples/scxml/mediaplayer/mediaplayer-qml.h @@ -0,0 +1,20 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef MEDIAPLAYER_QML +#define MEDIAPLAYER_QML + +#include "mediaplayer.h" + +#include <QtQml/qqml.h> +#include <QtCore/qobject.h> + +struct MediaPlayerStateMachineRegistration +{ + Q_GADGET + QML_FOREIGN(MediaPlayerStateMachine) + QML_NAMED_ELEMENT(MediaPlayerStateMachine) + QML_ADDED_IN_VERSION(1, 0) +}; + +#endif // MEDIAPLAYER_QML diff --git a/examples/scxml/mediaplayer/mediaplayer.pro b/examples/scxml/mediaplayer/mediaplayer.pro index 3a9a5c8..5aaf236 100644 --- a/examples/scxml/mediaplayer/mediaplayer.pro +++ b/examples/scxml/mediaplayer/mediaplayer.pro @@ -2,16 +2,26 @@ TEMPLATE = app QT += qml scxml CONFIG += c++11 +CONFIG += qmltypes SOURCES += main.cpp \ thedatamodel.cpp -HEADERS += thedatamodel.h +HEADERS += thedatamodel.h \ + mediaplayer-qml.h -RESOURCES += mediaplayer.qrc +QML_IMPORT_NAME = Mediaplayer +QML_IMPORT_MAJOR_VERSION = 1 + +qml_resources.files = \ + qmldir \ + MainWindow.qml + +qml_resources.prefix = /qt/qml/Mediaplayer + +RESOURCES += qml_resources STATECHARTS = mediaplayer.scxml target.path = $$[QT_INSTALL_EXAMPLES]/scxml/mediaplayer INSTALLS += target - diff --git a/examples/scxml/mediaplayer/mediaplayer.qrc b/examples/scxml/mediaplayer/mediaplayer.qrc deleted file mode 100644 index 8ab4fbe..0000000 --- a/examples/scxml/mediaplayer/mediaplayer.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<RCC> - <qresource prefix="/"> - <file>Mediaplayer.qml</file> - </qresource> -</RCC> diff --git a/examples/scxml/mediaplayer/mediaplayer.scxml b/examples/scxml/mediaplayer/mediaplayer.scxml index 1f89c5e..402f9d9 100644 --- a/examples/scxml/mediaplayer/mediaplayer.scxml +++ b/examples/scxml/mediaplayer/mediaplayer.scxml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <!-- -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause --> <scxml diff --git a/examples/scxml/mediaplayer/qmldir b/examples/scxml/mediaplayer/qmldir new file mode 100644 index 0000000..1a35a2a --- /dev/null +++ b/examples/scxml/mediaplayer/qmldir @@ -0,0 +1,3 @@ +module Mediaplayer +prefer :/qt/qml/Mediaplayer/ +MainWindow 1.0 MainWindow.qml diff --git a/examples/scxml/mediaplayer/thedatamodel.cpp b/examples/scxml/mediaplayer/thedatamodel.cpp index 641d31d..ffff638 100644 --- a/examples/scxml/mediaplayer/thedatamodel.cpp +++ b/examples/scxml/mediaplayer/thedatamodel.cpp @@ -1,13 +1,15 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "thedatamodel.h" -#include <QScxmlEvent> +#include <QtScxml/qscxmlevent.h> + +using namespace Qt::Literals::StringLiterals; bool TheDataModel::isValidMedia() const { - QString eventMedia = eventData().value(QStringLiteral("media")).toString(); + QString eventMedia = eventData().value(u"media"_s).toString(); return eventMedia.size() > 0; } diff --git a/examples/scxml/mediaplayer/thedatamodel.h b/examples/scxml/mediaplayer/thedatamodel.h index fc09c66..936deee 100644 --- a/examples/scxml/mediaplayer/thedatamodel.h +++ b/examples/scxml/mediaplayer/thedatamodel.h @@ -1,15 +1,18 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef THEDATAMODEL_H #define THEDATAMODEL_H #include "qscxmlcppdatamodel.h" +#include <QtQml/qqml.h> class TheDataModel: public QScxmlCppDataModel { Q_OBJECT Q_SCXML_DATAMODEL + QML_NAMED_ELEMENT(MediaPlayerDataModel) + QML_ADDED_IN_VERSION(1, 0) private: bool isValidMedia() const; |