1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtScxml module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "mainwindow.h" |
52 | #include "ui_mainwindow.h" |
53 | |
54 | #include <QScxmlStateMachine> |
55 | #include <QStringListModel> |
56 | |
57 | QT_USE_NAMESPACE |
58 | |
59 | MainWindow::MainWindow(QScxmlStateMachine *machine, QWidget *parent) : |
60 | QWidget(parent), |
61 | m_ui(new Ui::MainWindow), |
62 | m_machine(machine) |
63 | { |
64 | m_ui->setupUi(this); |
65 | |
66 | // lights |
67 | initAndConnect(state: QLatin1String("cLightOn" ), widget: m_ui->cLabel); |
68 | initAndConnect(state: QLatin1String("rLightOn" ), widget: m_ui->rLabel); |
69 | initAndConnect(state: QLatin1String("aLightOn" ), widget: m_ui->aLabel); |
70 | initAndConnect(state: QLatin1String("zLightOn" ), widget: m_ui->zLabel); |
71 | initAndConnect(state: QLatin1String("yLightOn" ), widget: m_ui->yLabel); |
72 | initAndConnect(state: QLatin1String("hurryLightOn" ), widget: m_ui->hurryLabel); |
73 | initAndConnect(state: QLatin1String("jackpotLightOn" ), widget: m_ui->jackpotLabel); |
74 | initAndConnect(state: QLatin1String("gameOverLightOn" ), widget: m_ui->gameOverLabel); |
75 | |
76 | // help labels |
77 | initAndConnect(state: QLatin1String("offState" ), widget: m_ui->offStateLabel); |
78 | initAndConnect(state: QLatin1String("hurryStateOff" ), widget: m_ui->normalStateLabel); |
79 | initAndConnect(state: QLatin1String("hurryStateOn" ), widget: m_ui->hurryStateLabel); |
80 | initAndConnect(state: QLatin1String("jackpotStateOn" ), widget: m_ui->jackpotStateLabel); |
81 | |
82 | // context enablement |
83 | initAndConnect(state: QLatin1String("offState" ), widget: m_ui->startButton); |
84 | initAndConnect(state: QLatin1String("onState" ), widget: m_ui->cButton); |
85 | initAndConnect(state: QLatin1String("onState" ), widget: m_ui->rButton); |
86 | initAndConnect(state: QLatin1String("onState" ), widget: m_ui->aButton); |
87 | initAndConnect(state: QLatin1String("onState" ), widget: m_ui->zButton); |
88 | initAndConnect(state: QLatin1String("onState" ), widget: m_ui->yButton); |
89 | initAndConnect(state: QLatin1String("onState" ), widget: m_ui->ballOutButton); |
90 | |
91 | // datamodel update |
92 | m_machine->connectToEvent(scxmlEventSpec: "updateScore" , functor: [this] (const QScxmlEvent &event) { |
93 | const QVariant data = event.data(); |
94 | const QString highScore = data.toMap().value(akey: "highScore" ).toString(); |
95 | m_ui->highScoreLabel->setText(highScore); |
96 | const QString score = data.toMap().value(akey: "score" ).toString(); |
97 | m_ui->scoreLabel->setText(score); |
98 | }); |
99 | |
100 | // gui interaction |
101 | connect(sender: m_ui->cButton, signal: &QAbstractButton::clicked, |
102 | slot: [this] { m_machine->submitEvent(eventName: "cLetterTriggered" ); |
103 | }); |
104 | connect(sender: m_ui->rButton, signal: &QAbstractButton::clicked, |
105 | slot: [this] { m_machine->submitEvent(eventName: "rLetterTriggered" ); |
106 | }); |
107 | connect(sender: m_ui->aButton, signal: &QAbstractButton::clicked, |
108 | slot: [this] { m_machine->submitEvent(eventName: "aLetterTriggered" ); |
109 | }); |
110 | connect(sender: m_ui->zButton, signal: &QAbstractButton::clicked, |
111 | slot: [this] { m_machine->submitEvent(eventName: "zLetterTriggered" ); |
112 | }); |
113 | connect(sender: m_ui->yButton, signal: &QAbstractButton::clicked, |
114 | slot: [this] { m_machine->submitEvent(eventName: "yLetterTriggered" ); |
115 | }); |
116 | connect(sender: m_ui->startButton, signal: &QAbstractButton::clicked, |
117 | slot: [this] { m_machine->submitEvent(eventName: "startTriggered" ); |
118 | }); |
119 | connect(sender: m_ui->ballOutButton, signal: &QAbstractButton::clicked, |
120 | slot: [this] { m_machine->submitEvent(eventName: "ballOutTriggered" ); |
121 | }); |
122 | } |
123 | |
124 | MainWindow::~MainWindow() |
125 | { |
126 | delete m_ui; |
127 | } |
128 | |
129 | void MainWindow::initAndConnect(const QString &state, QWidget *widget) |
130 | { |
131 | widget->setEnabled(m_machine->isActive(scxmlStateName: state)); |
132 | m_machine->connectToState(scxmlStateName: state, receiver: widget, method: &QWidget::setEnabled); |
133 | } |
134 | |