source: trunk/examples/script/context2d/window.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "window.h"
42#include "environment.h"
43#include "context2d.h"
44#include "qcontext2dcanvas.h"
45#include <QHBoxLayout>
46#include <QListWidget>
47#include <QDir>
48#include <QMessageBox>
49
50#ifndef QT_NO_SCRIPTTOOLS
51#include <QAction>
52#include <QApplication>
53#include <QMainWindow>
54#include <QPushButton>
55#include <QVBoxLayout>
56#include <QScriptEngineDebugger>
57#endif
58
59static QString scriptsDir()
60{
61 if (QFile::exists("./scripts"))
62 return "./scripts";
63 return ":/scripts";
64}
65
66//! [0]
67Window::Window(QWidget *parent)
68 : QWidget(parent)
69#ifndef QT_NO_SCRIPTTOOLS
70 , m_debugger(0), m_debugWindow(0)
71#endif
72{
73 m_env = new Environment(this);
74 QObject::connect(m_env, SIGNAL(scriptError(QScriptValue)),
75 this, SLOT(reportScriptError(QScriptValue)));
76
77 Context2D *context = new Context2D(this);
78 context->setSize(150, 150);
79 m_canvas = new QContext2DCanvas(context, m_env, this);
80 m_canvas->setFixedSize(context->size());
81 m_canvas->setObjectName("tutorial");
82 m_env->addCanvas(m_canvas);
83//! [0]
84
85#ifndef QT_NO_SCRIPTTOOLS
86 QVBoxLayout *vbox = new QVBoxLayout();
87 vbox->addWidget(m_canvas);
88 m_debugButton = new QPushButton(tr("Run in Debugger"));
89 connect(m_debugButton, SIGNAL(clicked()), this, SLOT(runInDebugger()));
90 vbox->addWidget(m_debugButton);
91#endif
92
93 QHBoxLayout *hbox = new QHBoxLayout(this);
94 m_view = new QListWidget(this);
95 m_view->setEditTriggers(QAbstractItemView::NoEditTriggers);
96 hbox->addWidget(m_view);
97#ifndef QT_NO_SCRIPTTOOLS
98 hbox->addLayout(vbox);
99#else
100 hbox->addWidget(m_canvas);
101#endif
102
103//! [1]
104 QDir dir(scriptsDir());
105 QFileInfoList entries = dir.entryInfoList(QStringList() << "*.js");
106 for (int i = 0; i < entries.size(); ++i)
107 m_view->addItem(entries.at(i).fileName());
108 connect(m_view, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
109 this, SLOT(selectScript(QListWidgetItem*)));
110//! [1]
111
112 setWindowTitle(tr("Context 2D"));
113}
114
115//! [2]
116void Window::selectScript(QListWidgetItem *item)
117{
118 QString fileName = item->text();
119 runScript(fileName, /*debug=*/false);
120}
121//! [2]
122
123void Window::reportScriptError(const QScriptValue &error)
124{
125 QMessageBox::warning(this, tr("Context 2D"), tr("Line %0: %1")
126 .arg(error.property("lineNumber").toInt32())
127 .arg(error.toString()));
128}
129
130#ifndef QT_NO_SCRIPTTOOLS
131//! [3]
132void Window::runInDebugger()
133{
134 QListWidgetItem *item = m_view->currentItem();
135 if (item) {
136 QString fileName = item->text();
137 runScript(fileName, /*debug=*/true);
138 }
139}
140//! [3]
141#endif
142
143//! [4]
144void Window::runScript(const QString &fileName, bool debug)
145{
146 QFile file(scriptsDir() + "/" + fileName);
147 file.open(QIODevice::ReadOnly);
148 QString contents = file.readAll();
149 file.close();
150 m_env->reset();
151
152#ifndef QT_NO_SCRIPTTOOLS
153 if (debug) {
154 if (!m_debugger) {
155 m_debugger = new QScriptEngineDebugger(this);
156 m_debugWindow = m_debugger->standardWindow();
157 m_debugWindow->setWindowModality(Qt::ApplicationModal);
158 m_debugWindow->resize(1280, 704);
159 }
160 m_debugger->attachTo(m_env->engine());
161 m_debugger->action(QScriptEngineDebugger::InterruptAction)->trigger();
162 } else {
163 if (m_debugger)
164 m_debugger->detach();
165 }
166#else
167 Q_UNUSED(debug);
168#endif
169
170 QScriptValue ret = m_env->evaluate(contents, fileName);
171
172#ifndef QT_NO_SCRIPTTOOLS
173 if (m_debugWindow)
174 m_debugWindow->hide();
175#endif
176
177 if (ret.isError())
178 reportScriptError(ret);
179}
180//! [4]
Note: See TracBrowser for help on using the repository browser.