1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples 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 "tetrixboard.h"
52
53#include <QtWidgets/QPushButton>
54#include <QtWidgets/QApplication>
55#include <QtWidgets/QMainWindow>
56#include <QtWidgets/QAction>
57#include <QtScript>
58#include <QUiLoader>
59
60#ifndef QT_NO_SCRIPTTOOLS
61#include <QtScriptTools>
62#endif
63
64struct QtMetaObject : private QObject
65{
66public:
67 static const QMetaObject *get()
68 { return &static_cast<QtMetaObject*>(0)->staticQtMetaObject; }
69};
70
71//! [0]
72class TetrixUiLoader : public QUiLoader
73{
74public:
75 TetrixUiLoader(QObject *parent = 0)
76 : QUiLoader(parent)
77 { }
78 virtual QWidget *createWidget(const QString &className, QWidget *parent = 0,
79 const QString &name = QString())
80 {
81 if (className == QLatin1String("TetrixBoard")) {
82 QWidget *board = new TetrixBoard(parent);
83 board->setObjectName(name);
84 return board;
85 }
86 return QUiLoader::createWidget(className, parent, name);
87 }
88};
89//! [0]
90
91static QScriptValue evaluateFile(QScriptEngine &engine, const QString &fileName)
92{
93 QFile file(fileName);
94 file.open(flags: QIODevice::ReadOnly);
95 return engine.evaluate(program: file.readAll(), fileName);
96}
97
98int main(int argc, char *argv[])
99{
100 Q_INIT_RESOURCE(tetrix);
101
102//! [1]
103 QApplication app(argc, argv);
104 QScriptEngine engine;
105
106 QScriptValue Qt = engine.newQMetaObject(metaObject: QtMetaObject::get());
107 Qt.setProperty(name: "App", value: engine.newQObject(object: &app));
108 engine.globalObject().setProperty(name: "Qt", value: Qt);
109//! [1]
110
111#if !defined(QT_NO_SCRIPTTOOLS)
112 QScriptEngineDebugger debugger;
113 debugger.attachTo(engine: &engine);
114 QMainWindow *debugWindow = debugger.standardWindow();
115 debugWindow->resize(w: 1024, h: 640);
116#endif
117
118//! [2]
119 evaluateFile(engine, fileName: ":/tetrixpiece.js");
120 evaluateFile(engine, fileName: ":/tetrixboard.js");
121 evaluateFile(engine, fileName: ":/tetrixwindow.js");
122//! [2]
123
124//! [3]
125 TetrixUiLoader loader;
126 QFile uiFile(":/tetrixwindow.ui");
127 uiFile.open(flags: QIODevice::ReadOnly);
128 QWidget *ui = loader.load(device: &uiFile);
129 uiFile.close();
130
131 QScriptValue ctor = engine.evaluate(program: "TetrixWindow");
132 QScriptValue scriptUi = engine.newQObject(object: ui, ownership: QScriptEngine::ScriptOwnership);
133 QScriptValue tetrix = ctor.construct(args: QScriptValueList() << scriptUi);
134//! [3]
135
136 QPushButton *debugButton = ui->findChild<QPushButton*>(aName: "debugButton");
137#if !defined(QT_NO_SCRIPTTOOLS)
138 QObject::connect(sender: debugButton, SIGNAL(clicked()),
139 receiver: debugger.action(action: QScriptEngineDebugger::InterruptAction),
140 SIGNAL(triggered()));
141 QObject::connect(sender: debugButton, SIGNAL(clicked()),
142 receiver: debugWindow, SLOT(show()));
143#else
144 debugButton->hide();
145#endif
146
147//! [4]
148 ui->resize(w: 550, h: 370);
149 ui->show();
150
151 return app.exec();
152//! [4]
153}
154

source code of qtscript/examples/script/qstetrix/main.cpp