examples/script/context2d/environment.h
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 #ifndef ENVIRONMENT_H
       
    43 #define ENVIRONMENT_H
       
    44 
       
    45 #include <qobject.h>
       
    46 #include <qlist.h>
       
    47 #include <qhash.h>
       
    48 #include <QTimerEvent>
       
    49 #include <QMouseEvent>
       
    50 #include <QKeyEvent>
       
    51 #include <QScriptEngine>
       
    52 #include <QScriptable>
       
    53 class QContext2DCanvas;
       
    54 
       
    55 //! [0]
       
    56 class Environment : public QObject
       
    57 {
       
    58     Q_OBJECT
       
    59     Q_PROPERTY(QScriptValue document READ document)
       
    60 public:
       
    61     Environment(QObject *parent = 0);
       
    62     ~Environment();
       
    63 
       
    64     QScriptValue document() const;
       
    65 
       
    66     void addCanvas(QContext2DCanvas *canvas);
       
    67     QContext2DCanvas *canvasByName(const QString &name) const;
       
    68     QList<QContext2DCanvas*> canvases() const;
       
    69 
       
    70     QScriptValue evaluate(const QString &code,
       
    71                           const QString &fileName = QString());
       
    72 
       
    73     QScriptValue toWrapper(QObject *object);
       
    74 
       
    75     void handleEvent(QContext2DCanvas *canvas, QMouseEvent *e);
       
    76     void handleEvent(QContext2DCanvas *canvas, QKeyEvent *e);
       
    77 
       
    78     void reset();
       
    79 //! [0]
       
    80 
       
    81     QScriptEngine *engine() const;
       
    82 
       
    83 //! [1]
       
    84 public slots:
       
    85     int setInterval(const QScriptValue &expression, int delay);
       
    86     void clearInterval(int timerId);
       
    87 
       
    88     int setTimeout(const QScriptValue &expression, int delay);
       
    89     void clearTimeout(int timerId);
       
    90 //! [1]
       
    91 
       
    92 //! [2]
       
    93 signals:
       
    94     void scriptError(const QScriptValue &error);
       
    95 //! [2]
       
    96 
       
    97 protected:
       
    98     void timerEvent(QTimerEvent *event);
       
    99 
       
   100 private:
       
   101     QScriptValue eventHandler(QContext2DCanvas *canvas,
       
   102                               const QString &type, QScriptValue *who);
       
   103     QScriptValue newFakeDomEvent(const QString &type,
       
   104                                  const QScriptValue &target);
       
   105     void maybeEmitScriptError();
       
   106 
       
   107     QScriptEngine *m_engine;
       
   108     QScriptValue m_originalGlobalObject;
       
   109     QScriptValue m_document;
       
   110     QList<QContext2DCanvas*> m_canvases;
       
   111     QHash<int, QScriptValue> m_intervalHash;
       
   112     QHash<int, QScriptValue> m_timeoutHash;
       
   113 };
       
   114 
       
   115 //! [3]
       
   116 class Document : public QObject
       
   117 {
       
   118     Q_OBJECT
       
   119 public:
       
   120     Document(Environment *env);
       
   121     ~Document();
       
   122 
       
   123 public slots:
       
   124     QScriptValue getElementById(const QString &id) const;
       
   125     QScriptValue getElementsByTagName(const QString &name) const;
       
   126 
       
   127     // EventTarget
       
   128     void addEventListener(const QString &type, const QScriptValue &listener,
       
   129                           bool useCapture);
       
   130 };
       
   131 //! [3]
       
   132 
       
   133 class CanvasGradientPrototype : public QObject, public QScriptable
       
   134 {
       
   135     Q_OBJECT
       
   136 protected:
       
   137     CanvasGradientPrototype(QObject *parent = 0);
       
   138 public:
       
   139     static void setup(QScriptEngine *engine);
       
   140 
       
   141 public slots:
       
   142     void addColorStop(qreal offset, const QString &color);
       
   143 };
       
   144 
       
   145 #endif