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

Last change on this file since 265 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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
60static QString scriptsDir()
61{
62 if (QFile::exists("./scripts"))
63 return "./scripts";
64 return ":/scripts";
65}
66
67//! [0]
68Window::Window(QWidget *parent)
69 : QWidget(parent)
70{
71 m_env = new Environment(this);
72 QObject::connect(m_env, SIGNAL(scriptError(QScriptValue)),
73 this, SLOT(reportScriptError(QScriptValue)));
74
75 Context2D *context = new Context2D(this);
76 context->setSize(150, 150);
77 m_canvas = new QContext2DCanvas(context, m_env, this);
78 m_canvas->setFixedSize(context->size());
79 m_canvas->setObjectName("tutorial");
80 m_env->addCanvas(m_canvas);
81//! [0]
82
83#ifndef QT_NO_SCRIPTTOOLS
84 QVBoxLayout *vbox = new QVBoxLayout();
85 vbox->addWidget(m_canvas);
86 m_debugButton = new QPushButton(tr("Run in Debugger"));
87 connect(m_debugButton, SIGNAL(clicked()), this, SLOT(runInDebugger()));
88 vbox->addWidget(m_debugButton);
89#endif
90
91 QHBoxLayout *hbox = new QHBoxLayout(this);
92 m_view = new QListWidget(this);
93 m_view->setEditTriggers(QAbstractItemView::NoEditTriggers);
94 hbox->addWidget(m_view);
95#ifndef QT_NO_SCRIPTTOOLS
96 hbox->addLayout(vbox);
97#else
98 hbox->addWidget(m_canvas);
99#endif
100
101//! [1]
102 QDir dir(scriptsDir());
103 QFileInfoList entries = dir.entryInfoList(QStringList() << "*.js");
104 for (int i = 0; i < entries.size(); ++i)
105 m_view->addItem(entries.at(i).fileName());
106 connect(m_view, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
107 this, SLOT(selectScript(QListWidgetItem*)));
108//! [1]
109
110#ifndef QT_NO_SCRIPTTOOLS
111 m_debugger = new QScriptEngineDebugger(this);
112 m_debugger->attachTo(m_env->engine());
113 m_debugWindow = m_debugger->standardWindow();
114 m_debugWindow->setWindowModality(Qt::ApplicationModal);
115 m_debugWindow->resize(1280, 704);
116#endif
117
118 setWindowTitle(tr("Context 2D"));
119}
120
121//! [2]
122void Window::selectScript(QListWidgetItem *item)
123{
124 QString fileName = item->text();
125 runScript(fileName, /*debug=*/false);
126}
127//! [2]
128
129void Window::reportScriptError(const QScriptValue &error)
130{
131 QMessageBox::warning(this, tr("Context 2D"), tr("Line %0: %1")
132 .arg(error.property("lineNumber").toInt32())
133 .arg(error.toString()));
134}
135
136#ifndef QT_NO_SCRIPTTOOLS
137//! [3]
138void Window::runInDebugger()
139{
140 QListWidgetItem *item = m_view->currentItem();
141 if (item) {
142 QString fileName = item->text();
143 runScript(fileName, /*debug=*/true);
144 }
145}
146//! [3]
147#endif
148
149//! [4]
150void Window::runScript(const QString &fileName, bool debug)
151{
152 QFile file(scriptsDir() + "/" + fileName);
153 file.open(QIODevice::ReadOnly);
154 QString contents = file.readAll();
155 file.close();
156 m_env->reset();
157
158#ifndef QT_NO_SCRIPTTOOLS
159 if (debug)
160 m_debugger->action(QScriptEngineDebugger::InterruptAction)->trigger();
161#else
162 Q_UNUSED(debug);
163#endif
164
165 QScriptValue ret = m_env->evaluate(contents, fileName);
166
167#ifndef QT_NO_SCRIPTTOOLS
168 m_debugWindow->hide();
169#endif
170
171 if (ret.isError())
172 reportScriptError(ret);
173}
174//! [4]
Note: See TracBrowser for help on using the repository browser.