summaryrefslogtreecommitdiffstats
path: root/examples/scxml/mediaplayer/MainWindow.qml
blob: de4f710dcee2e562742c46e9f895d798d6075937 (plain)
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
// 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 Mediaplayer

Window {
    id: root

    MediaPlayerDataModel {
        id: model
    }

    MediaPlayerStateMachine {
        id: stateMachine
        onDataModelChanged: stateMachine.start()
        dataModel: model
    }

    visible: true
    width: 750
    height: 350
    color: "white"

    ListView {
        id: theList
        width: parent.width / 2
        height: parent.height
        keyNavigationWraps: true
        highlightMoveDuration: 0
        focus: true
        model: ListModel {
            id: theModel
            ListElement { media: "Song 1" }
            ListElement { media: "Song 2" }
            ListElement { media: "Song 3" }
        }
        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: root.tap(delegateRect.index)
            }
            Text {
                id: txt
                anchors.fill: parent
                text: delegateRect.media
                verticalAlignment: Text.AlignVCenter
            }
        }
    }

    Text {
        id: theLog
        anchors.left: theList.right
        anchors.top: theText.bottom
        anchors.right: parent.right
        anchors.bottom: parent.bottom
    }

    Text {
        id: theText
        anchors.left: theList.right
        anchors.right: parent.right;
        anchors.top:  parent.top
        text: "Stopped"
        color: stateMachine.playing ? "green" : "red"
    }

    EventConnection {
        stateMachine: stateMachine
        events: ["playbackStarted", "playbackStopped"]
        onOccurred: (event)=> {
            var media = event.data.media;
            if (event.name === "playbackStarted") {
                theText.text = "Playing '" + media + "'";
                theLog.text = theLog.text + "\nplaybackStarted with data: "
                                          + JSON.stringify(event.data);
            } else if (event.name === "playbackStopped") {
                theText.text = "Stopped '" + media + "'";
                theLog.text = theLog.text + "\nplaybackStopped with data: "
                                          + JSON.stringify(event.data);
            }
        }
    }

    // Submit tap event to state machine.
    // "tap" toggles playing state of the current media.
    function tap(idx) {
        var media = theModel.get(idx).media;
        var data = { "media": media };
        stateMachine.submitEvent("tap", data);
    }
}