|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "window.h" |
|
43 #include "environment.h" |
|
44 #include "context2d.h" |
|
45 #include "qcontext2dcanvas.h" |
|
46 #include <QHBoxLayout> |
|
47 #include <QListWidget> |
|
48 #include <QDir> |
|
49 #include <QMessageBox> |
|
50 |
|
51 #ifndef QT_NO_SCRIPTTOOLS |
|
52 #include <QAction> |
|
53 #include <QApplication> |
|
54 #include <QMainWindow> |
|
55 #include <QPushButton> |
|
56 #include <QVBoxLayout> |
|
57 #include <QScriptEngineDebugger> |
|
58 #endif |
|
59 |
|
60 static QString scriptsDir() |
|
61 { |
|
62 if (QFile::exists("./scripts")) |
|
63 return "./scripts"; |
|
64 return ":/scripts"; |
|
65 } |
|
66 |
|
67 //! [0] |
|
68 Window::Window(QWidget *parent) |
|
69 : QWidget(parent) |
|
70 #ifndef QT_NO_SCRIPTTOOLS |
|
71 , m_debugger(0), m_debugWindow(0) |
|
72 #endif |
|
73 { |
|
74 m_env = new Environment(this); |
|
75 QObject::connect(m_env, SIGNAL(scriptError(QScriptValue)), |
|
76 this, SLOT(reportScriptError(QScriptValue))); |
|
77 |
|
78 Context2D *context = new Context2D(this); |
|
79 context->setSize(150, 150); |
|
80 m_canvas = new QContext2DCanvas(context, m_env, this); |
|
81 m_canvas->setFixedSize(context->size()); |
|
82 m_canvas->setObjectName("tutorial"); |
|
83 m_env->addCanvas(m_canvas); |
|
84 //! [0] |
|
85 |
|
86 #ifndef QT_NO_SCRIPTTOOLS |
|
87 QVBoxLayout *vbox = new QVBoxLayout(); |
|
88 vbox->addWidget(m_canvas); |
|
89 m_debugButton = new QPushButton(tr("Run in Debugger")); |
|
90 connect(m_debugButton, SIGNAL(clicked()), this, SLOT(runInDebugger())); |
|
91 vbox->addWidget(m_debugButton); |
|
92 #endif |
|
93 |
|
94 QHBoxLayout *hbox = new QHBoxLayout(this); |
|
95 m_view = new QListWidget(this); |
|
96 m_view->setEditTriggers(QAbstractItemView::NoEditTriggers); |
|
97 hbox->addWidget(m_view); |
|
98 #ifndef QT_NO_SCRIPTTOOLS |
|
99 hbox->addLayout(vbox); |
|
100 #else |
|
101 hbox->addWidget(m_canvas); |
|
102 #endif |
|
103 |
|
104 //! [1] |
|
105 QDir dir(scriptsDir()); |
|
106 QFileInfoList entries = dir.entryInfoList(QStringList() << "*.js"); |
|
107 for (int i = 0; i < entries.size(); ++i) |
|
108 m_view->addItem(entries.at(i).fileName()); |
|
109 connect(m_view, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), |
|
110 this, SLOT(selectScript(QListWidgetItem*))); |
|
111 //! [1] |
|
112 |
|
113 setWindowTitle(tr("Context 2D")); |
|
114 } |
|
115 |
|
116 //! [2] |
|
117 void Window::selectScript(QListWidgetItem *item) |
|
118 { |
|
119 QString fileName = item->text(); |
|
120 runScript(fileName, /*debug=*/false); |
|
121 } |
|
122 //! [2] |
|
123 |
|
124 void Window::reportScriptError(const QScriptValue &error) |
|
125 { |
|
126 QMessageBox::warning(this, tr("Context 2D"), tr("Line %0: %1") |
|
127 .arg(error.property("lineNumber").toInt32()) |
|
128 .arg(error.toString())); |
|
129 } |
|
130 |
|
131 #ifndef QT_NO_SCRIPTTOOLS |
|
132 //! [3] |
|
133 void Window::runInDebugger() |
|
134 { |
|
135 QListWidgetItem *item = m_view->currentItem(); |
|
136 if (item) { |
|
137 QString fileName = item->text(); |
|
138 runScript(fileName, /*debug=*/true); |
|
139 } |
|
140 } |
|
141 //! [3] |
|
142 #endif |
|
143 |
|
144 //! [4] |
|
145 void Window::runScript(const QString &fileName, bool debug) |
|
146 { |
|
147 QFile file(scriptsDir() + "/" + fileName); |
|
148 file.open(QIODevice::ReadOnly); |
|
149 QString contents = file.readAll(); |
|
150 file.close(); |
|
151 m_env->reset(); |
|
152 |
|
153 #ifndef QT_NO_SCRIPTTOOLS |
|
154 if (debug) { |
|
155 if (!m_debugger) { |
|
156 m_debugger = new QScriptEngineDebugger(this); |
|
157 m_debugWindow = m_debugger->standardWindow(); |
|
158 m_debugWindow->setWindowModality(Qt::ApplicationModal); |
|
159 m_debugWindow->resize(1280, 704); |
|
160 } |
|
161 m_debugger->attachTo(m_env->engine()); |
|
162 m_debugger->action(QScriptEngineDebugger::InterruptAction)->trigger(); |
|
163 } else { |
|
164 if (m_debugger) |
|
165 m_debugger->detach(); |
|
166 } |
|
167 #else |
|
168 Q_UNUSED(debug); |
|
169 #endif |
|
170 |
|
171 QScriptValue ret = m_env->evaluate(contents, fileName); |
|
172 |
|
173 #ifndef QT_NO_SCRIPTTOOLS |
|
174 if (m_debugWindow) |
|
175 m_debugWindow->hide(); |
|
176 #endif |
|
177 |
|
178 if (ret.isError()) |
|
179 reportScriptError(ret); |
|
180 } |
|
181 //! [4] |