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 "qcontext2dcanvas.h"
|
---|
42 |
|
---|
43 | #include "context2d.h"
|
---|
44 | #include "environment.h"
|
---|
45 | #include "domimage.h"
|
---|
46 |
|
---|
47 | #include <QPainter>
|
---|
48 | #include <QPaintEvent>
|
---|
49 |
|
---|
50 | //! [3]
|
---|
51 | QContext2DCanvas::QContext2DCanvas(Context2D *context, Environment *env, QWidget *parent)
|
---|
52 | : QWidget(parent), m_context(context), m_env(env)
|
---|
53 | {
|
---|
54 | QObject::connect(context, SIGNAL(changed(QImage)), this, SLOT(contentsChanged(QImage)));
|
---|
55 | setMouseTracking(true);
|
---|
56 | }
|
---|
57 | //! [3]
|
---|
58 |
|
---|
59 | QContext2DCanvas::~QContext2DCanvas()
|
---|
60 | {
|
---|
61 | }
|
---|
62 |
|
---|
63 | Context2D *QContext2DCanvas::context() const
|
---|
64 | {
|
---|
65 | return m_context;
|
---|
66 | }
|
---|
67 |
|
---|
68 | //! [0]
|
---|
69 | QScriptValue QContext2DCanvas::getContext(const QString &str)
|
---|
70 | {
|
---|
71 | if (str != "2d")
|
---|
72 | return QScriptValue();
|
---|
73 | return m_env->toWrapper(m_context);
|
---|
74 | }
|
---|
75 | //! [0]
|
---|
76 |
|
---|
77 | //! [1]
|
---|
78 | void QContext2DCanvas::contentsChanged(const QImage &image)
|
---|
79 | {
|
---|
80 | m_image = image;
|
---|
81 | update();
|
---|
82 | }
|
---|
83 |
|
---|
84 | void QContext2DCanvas::paintEvent(QPaintEvent *e)
|
---|
85 | {
|
---|
86 | QPainter p(this);
|
---|
87 | #ifdef Q_WS_S60
|
---|
88 | // Draw white rect first since in with some themes the js-file content will produce black-on-black.
|
---|
89 | QBrush whiteBgBrush(Qt::white);
|
---|
90 | p.fillRect(e->rect(), whiteBgBrush);
|
---|
91 | #endif
|
---|
92 | p.setClipRect(e->rect());
|
---|
93 | p.drawImage(0, 0, m_image);
|
---|
94 | }
|
---|
95 | //! [1]
|
---|
96 |
|
---|
97 | //! [2]
|
---|
98 | void QContext2DCanvas::mouseMoveEvent(QMouseEvent *e)
|
---|
99 | {
|
---|
100 | m_env->handleEvent(this, e);
|
---|
101 | }
|
---|
102 |
|
---|
103 | void QContext2DCanvas::mousePressEvent(QMouseEvent *e)
|
---|
104 | {
|
---|
105 | m_env->handleEvent(this, e);
|
---|
106 | }
|
---|
107 |
|
---|
108 | void QContext2DCanvas::mouseReleaseEvent(QMouseEvent *e)
|
---|
109 | {
|
---|
110 | m_env->handleEvent(this, e);
|
---|
111 | }
|
---|
112 |
|
---|
113 | void QContext2DCanvas::keyPressEvent(QKeyEvent *e)
|
---|
114 | {
|
---|
115 | m_env->handleEvent(this, e);
|
---|
116 | }
|
---|
117 |
|
---|
118 | void QContext2DCanvas::keyReleaseEvent(QKeyEvent *e)
|
---|
119 | {
|
---|
120 | m_env->handleEvent(this, e);
|
---|
121 | }
|
---|
122 | //! [2]
|
---|
123 |
|
---|
124 | void QContext2DCanvas::resizeEvent(QResizeEvent *e)
|
---|
125 | {
|
---|
126 | m_context->setSize(e->size().width(), e->size().height());
|
---|
127 | }
|
---|
128 |
|
---|
129 | void QContext2DCanvas::resize(int width, int height)
|
---|
130 | {
|
---|
131 | QWidget::resize(width, height);
|
---|
132 | }
|
---|
133 |
|
---|
134 | void QContext2DCanvas::reset()
|
---|
135 | {
|
---|
136 | m_context->reset();
|
---|
137 | }
|
---|
138 |
|
---|
139 | void QContext2DCanvas::addEventListener(const QString &type, const QScriptValue &listener,
|
---|
140 | bool useCapture)
|
---|
141 | {
|
---|
142 | Q_UNUSED(useCapture);
|
---|
143 | if (listener.isFunction()) {
|
---|
144 | QScriptValue self = m_env->toWrapper(this);
|
---|
145 | self.setProperty("on" + type, listener);
|
---|
146 | }
|
---|
147 | }
|
---|