examples/script/context2d/qcontext2dcanvas.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 "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]
       
    52 QContext2DCanvas::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 
       
    60 QContext2DCanvas::~QContext2DCanvas()
       
    61 {
       
    62 }
       
    63 
       
    64 Context2D *QContext2DCanvas::context() const
       
    65 {
       
    66     return m_context;
       
    67 }
       
    68 
       
    69 //! [0]
       
    70 QScriptValue 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]
       
    79 void QContext2DCanvas::contentsChanged(const QImage &image)
       
    80 {
       
    81     m_image = image;
       
    82     update();
       
    83 }
       
    84 
       
    85 void 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]
       
    99 void QContext2DCanvas::mouseMoveEvent(QMouseEvent *e)
       
   100 {
       
   101     m_env->handleEvent(this, e);
       
   102 }
       
   103 
       
   104 void QContext2DCanvas::mousePressEvent(QMouseEvent *e)
       
   105 {
       
   106     m_env->handleEvent(this, e);
       
   107 }
       
   108 
       
   109 void QContext2DCanvas::mouseReleaseEvent(QMouseEvent *e)
       
   110 {
       
   111     m_env->handleEvent(this, e);
       
   112 }
       
   113 
       
   114 void QContext2DCanvas::keyPressEvent(QKeyEvent *e)
       
   115 {
       
   116     m_env->handleEvent(this, e);
       
   117 }
       
   118 
       
   119 void QContext2DCanvas::keyReleaseEvent(QKeyEvent *e)
       
   120 {
       
   121     m_env->handleEvent(this, e);
       
   122 }
       
   123 //! [2]
       
   124 
       
   125 void QContext2DCanvas::resizeEvent(QResizeEvent *e)
       
   126 {
       
   127     m_context->setSize(e->size().width(), e->size().height());
       
   128 }
       
   129 
       
   130 void QContext2DCanvas::resize(int width, int height)
       
   131 {
       
   132     QWidget::resize(width, height);
       
   133 }
       
   134 
       
   135 void QContext2DCanvas::reset()
       
   136 {
       
   137     m_context->reset();
       
   138 }
       
   139 
       
   140 void 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 }