source: trunk/examples/script/context2d/qcontext2dcanvas.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 4.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qcontext2dcanvas.h"
43
44#include "context2d.h"
45#include "environment.h"
46#include "domimage.h"
47
48#include <QPainter>
49#include <QPaintEvent>
50
51//! [3]
52QContext2DCanvas::QContext2DCanvas(Context2D *context, Environment *env, QWidget *parent)
53 : QWidget(parent), m_context(context), m_env(env)
54{
55 QObject::connect(context, SIGNAL(changed(QImage)), this, SLOT(contentsChanged(QImage)));
56 setMouseTracking(true);
57}
58//! [3]
59
60QContext2DCanvas::~QContext2DCanvas()
61{
62}
63
64Context2D *QContext2DCanvas::context() const
65{
66 return m_context;
67}
68
69//! [0]
70QScriptValue QContext2DCanvas::getContext(const QString &str)
71{
72 if (str != "2d")
73 return QScriptValue();
74 return m_env->toWrapper(m_context);
75}
76//! [0]
77
78//! [1]
79void QContext2DCanvas::contentsChanged(const QImage &image)
80{
81 m_image = image;
82 update();
83}
84
85void QContext2DCanvas::paintEvent(QPaintEvent *e)
86{
87 QPainter p(this);
88#ifdef Q_WS_S60
89// Draw white rect first since in with some themes the js-file content will produce black-on-black.
90 QBrush whiteBgBrush(Qt::white);
91 p.fillRect(e->rect(), whiteBgBrush);
92#endif
93 p.setClipRect(e->rect());
94 p.drawImage(0, 0, m_image);
95}
96//! [1]
97
98//! [2]
99void QContext2DCanvas::mouseMoveEvent(QMouseEvent *e)
100{
101 m_env->handleEvent(this, e);
102}
103
104void QContext2DCanvas::mousePressEvent(QMouseEvent *e)
105{
106 m_env->handleEvent(this, e);
107}
108
109void QContext2DCanvas::mouseReleaseEvent(QMouseEvent *e)
110{
111 m_env->handleEvent(this, e);
112}
113
114void QContext2DCanvas::keyPressEvent(QKeyEvent *e)
115{
116 m_env->handleEvent(this, e);
117}
118
119void QContext2DCanvas::keyReleaseEvent(QKeyEvent *e)
120{
121 m_env->handleEvent(this, e);
122}
123//! [2]
124
125void QContext2DCanvas::resizeEvent(QResizeEvent *e)
126{
127 m_context->setSize(e->size().width(), e->size().height());
128}
129
130void QContext2DCanvas::resize(int width, int height)
131{
132 QWidget::resize(width, height);
133}
134
135void QContext2DCanvas::reset()
136{
137 m_context->reset();
138}
139
140void QContext2DCanvas::addEventListener(const QString &type, const QScriptValue &listener,
141 bool useCapture)
142{
143 Q_UNUSED(useCapture);
144 if (listener.isFunction()) {
145 QScriptValue self = m_env->toWrapper(this);
146 self.setProperty("on" + type, listener);
147 }
148}
Note: See TracBrowser for help on using the repository browser.