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 "window.h"
52#include "environment.h"
53#include "context2d.h"
54#include "qcontext2dcanvas.h"
55#include <QHBoxLayout>
56#include <QListWidget>
57#include <QDir>
58#include <QMessageBox>
59
60#ifndef QT_NO_SCRIPTTOOLS
61#include <QAction>
62#include <QApplication>
63#include <QMainWindow>
64#include <QPushButton>
65#include <QVBoxLayout>
66#include <QScriptEngineDebugger>
67#endif
68
69static QString scriptsDir()
70{
71 if (QFile::exists(fileName: "./scripts"))
72 return "./scripts";
73 return ":/scripts";
74}
75
76//! [0]
77Window::Window(QWidget *parent)
78 : QWidget(parent)
79#ifndef QT_NO_SCRIPTTOOLS
80 , m_debugger(0), m_debugWindow(0)
81#endif
82{
83 m_env = new Environment(this);
84 QObject::connect(sender: m_env, SIGNAL(scriptError(QScriptValue)),
85 receiver: this, SLOT(reportScriptError(QScriptValue)));
86
87 Context2D *context = new Context2D(this);
88 context->setSize(width: 150, height: 150);
89 m_canvas = new QContext2DCanvas(context, m_env, this);
90 m_canvas->setFixedSize(context->size());
91 m_canvas->setObjectName("tutorial");
92 m_env->addCanvas(canvas: m_canvas);
93//! [0]
94
95#ifndef QT_NO_SCRIPTTOOLS
96 QVBoxLayout *vbox = new QVBoxLayout();
97 vbox->addWidget(m_canvas);
98 m_debugButton = new QPushButton(tr(s: "Run in Debugger"));
99 connect(sender: m_debugButton, SIGNAL(clicked()), receiver: this, SLOT(runInDebugger()));
100 vbox->addWidget(m_debugButton);
101#endif
102
103 QHBoxLayout *hbox = new QHBoxLayout(this);
104 m_view = new QListWidget(this);
105 m_view->setEditTriggers(QAbstractItemView::NoEditTriggers);
106 hbox->addWidget(m_view);
107#ifndef QT_NO_SCRIPTTOOLS
108 hbox->addLayout(layout: vbox);
109#else
110 hbox->addWidget(m_canvas);
111#endif
112
113//! [1]
114 QDir dir(scriptsDir());
115 QFileInfoList entries = dir.entryInfoList(nameFilters: QStringList() << "*.js");
116 for (int i = 0; i < entries.size(); ++i)
117 m_view->addItem(label: entries.at(i).fileName());
118 connect(sender: m_view, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
119 receiver: this, SLOT(selectScript(QListWidgetItem*)));
120//! [1]
121
122 setWindowTitle(tr(s: "Context 2D"));
123}
124
125//! [2]
126void Window::selectScript(QListWidgetItem *item)
127{
128 QString fileName = item->text();
129 runScript(fileName, /*debug=*/false);
130}
131//! [2]
132
133void Window::reportScriptError(const QScriptValue &error)
134{
135 QMessageBox::warning(parent: this, title: tr(s: "Context 2D"), text: tr(s: "Line %0: %1")
136 .arg(a: error.property(name: "lineNumber").toInt32())
137 .arg(a: error.toString()));
138}
139
140#ifndef QT_NO_SCRIPTTOOLS
141//! [3]
142void Window::runInDebugger()
143{
144 QListWidgetItem *item = m_view->currentItem();
145 if (item) {
146 QString fileName = item->text();
147 runScript(fileName, /*debug=*/true);
148 }
149}
150//! [3]
151#endif
152
153//! [4]
154void Window::runScript(const QString &fileName, bool debug)
155{
156 QFile file(scriptsDir() + "/" + fileName);
157 file.open(flags: QIODevice::ReadOnly);
158 QString contents = file.readAll();
159 file.close();
160 m_env->reset();
161
162#ifndef QT_NO_SCRIPTTOOLS
163 if (debug) {
164 if (!m_debugger) {
165 m_debugger = new QScriptEngineDebugger(this);
166 m_debugWindow = m_debugger->standardWindow();
167 m_debugWindow->setWindowModality(Qt::ApplicationModal);
168 m_debugWindow->resize(w: 1280, h: 704);
169 }
170 m_debugger->attachTo(engine: m_env->engine());
171 m_debugger->action(action: QScriptEngineDebugger::InterruptAction)->trigger();
172 } else {
173 if (m_debugger)
174 m_debugger->detach();
175 }
176#else
177 Q_UNUSED(debug);
178#endif
179
180 QScriptValue ret = m_env->evaluate(code: contents, fileName);
181
182#ifndef QT_NO_SCRIPTTOOLS
183 if (m_debugWindow)
184 m_debugWindow->hide();
185#endif
186
187 if (ret.isError())
188 reportScriptError(error: ret);
189}
190//! [4]
191

source code of qtscript/examples/script/context2d/window.cpp