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
MainWindow(QScxmlStateMachine * machine,QWidget * parent)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(QLatin1String("cLightOn"), m_ui->cLabel);
68 initAndConnect(QLatin1String("rLightOn"), m_ui->rLabel);
69 initAndConnect(QLatin1String("aLightOn"), m_ui->aLabel);
70 initAndConnect(QLatin1String("zLightOn"), m_ui->zLabel);
71 initAndConnect(QLatin1String("yLightOn"), m_ui->yLabel);
72 initAndConnect(QLatin1String("hurryLightOn"), m_ui->hurryLabel);
73 initAndConnect(QLatin1String("jackpotLightOn"), m_ui->jackpotLabel);
74 initAndConnect(QLatin1String("gameOverLightOn"), m_ui->gameOverLabel);
75
76 // help labels
77 initAndConnect(QLatin1String("offState"), m_ui->offStateLabel);
78 initAndConnect(QLatin1String("hurryStateOff"), m_ui->normalStateLabel);
79 initAndConnect(QLatin1String("hurryStateOn"), m_ui->hurryStateLabel);
80 initAndConnect(QLatin1String("jackpotStateOn"), m_ui->jackpotStateLabel);
81
82 // context enablement
83 initAndConnect(QLatin1String("offState"), m_ui->startButton);
84 initAndConnect(QLatin1String("onState"), m_ui->cButton);
85 initAndConnect(QLatin1String("onState"), m_ui->rButton);
86 initAndConnect(QLatin1String("onState"), m_ui->aButton);
87 initAndConnect(QLatin1String("onState"), m_ui->zButton);
88 initAndConnect(QLatin1String("onState"), m_ui->yButton);
89 initAndConnect(QLatin1String("onState"), m_ui->ballOutButton);
90
91 // datamodel update
92 m_machine->connectToEvent("updateScore", [this] (const QScxmlEvent &event) {
93 const QVariant data = event.data();
94 const QString highScore = data.toMap().value("highScore").toString();
95 m_ui->highScoreLabel->setText(highScore);
96 const QString score = data.toMap().value("score").toString();
97 m_ui->scoreLabel->setText(score);
98 });
99
100 // gui interaction
101 connect(m_ui->cButton, &QAbstractButton::clicked,
102 [this] { m_machine->submitEvent("cLetterTriggered");
103 });
104 connect(m_ui->rButton, &QAbstractButton::clicked,
105 [this] { m_machine->submitEvent("rLetterTriggered");
106 });
107 connect(m_ui->aButton, &QAbstractButton::clicked,
108 [this] { m_machine->submitEvent("aLetterTriggered");
109 });
110 connect(m_ui->zButton, &QAbstractButton::clicked,
111 [this] { m_machine->submitEvent("zLetterTriggered");
112 });
113 connect(m_ui->yButton, &QAbstractButton::clicked,
114 [this] { m_machine->submitEvent("yLetterTriggered");
115 });
116 connect(m_ui->startButton, &QAbstractButton::clicked,
117 [this] { m_machine->submitEvent("startTriggered");
118 });
119 connect(m_ui->ballOutButton, &QAbstractButton::clicked,
120 [this] { m_machine->submitEvent("ballOutTriggered");
121 });
122 }
123
~MainWindow()124 MainWindow::~MainWindow()
125 {
126 delete m_ui;
127 }
128
initAndConnect(const QString & state,QWidget * widget)129 void MainWindow::initAndConnect(const QString &state, QWidget *widget)
130 {
131 widget->setEnabled(m_machine->isActive(state));
132 m_machine->connectToState(state, widget, &QWidget::setEnabled);
133 }
134