1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2015 The Qt Company Ltd. 4 ** Contact: http://www.qt.io/licensing/ 5 ** 6 ** This file is part of the examples of the Qt Toolkit. 7 ** 8 ** $QT_BEGIN_LICENSE:BSD$ 9 ** You may use this file under the terms of the BSD license as follows: 10 ** 11 ** "Redistribution and use in source and binary forms, with or without 12 ** modification, are permitted provided that the following conditions are 13 ** met: 14 ** * Redistributions of source code must retain the above copyright 15 ** notice, this list of conditions and the following disclaimer. 16 ** * Redistributions in binary form must reproduce the above copyright 17 ** notice, this list of conditions and the following disclaimer in 18 ** the documentation and/or other materials provided with the 19 ** distribution. 20 ** * Neither the name of The Qt Company Ltd nor the names of its 21 ** contributors may be used to endorse or promote products derived 22 ** from this software without specific prior written permission. 23 ** 24 ** 25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 36 ** 37 ** $QT_END_LICENSE$ 38 ** 39 ****************************************************************************/ 40 41 #ifndef ENVIRONMENT_H 42 #define ENVIRONMENT_H 43 44 #include <qobject.h> 45 #include <qlist.h> 46 #include <qhash.h> 47 #include <QTimerEvent> 48 #include <QMouseEvent> 49 #include <QKeyEvent> 50 #include <QScriptEngine> 51 #include <QScriptable> 52 class QContext2DCanvas; 53 54 //! [0] 55 class Environment : public QObject 56 { 57 Q_OBJECT 58 Q_PROPERTY(QScriptValue document READ document) 59 public: 60 Environment(QObject *parent = 0); 61 ~Environment(); 62 63 QScriptValue document() const; 64 65 void addCanvas(QContext2DCanvas *canvas); 66 QContext2DCanvas *canvasByName(const QString &name) const; 67 QList<QContext2DCanvas*> canvases() const; 68 69 QScriptValue evaluate(const QString &code, 70 const QString &fileName = QString()); 71 72 QScriptValue toWrapper(QObject *object); 73 74 void handleEvent(QContext2DCanvas *canvas, QMouseEvent *e); 75 void handleEvent(QContext2DCanvas *canvas, QKeyEvent *e); 76 77 void reset(); 78 //! [0] 79 80 QScriptEngine *engine() const; 81 bool hasIntervalTimers() const; 82 void triggerTimers(); 83 84 //! [1] 85 public slots: 86 int setInterval(const QScriptValue &expression, int delay); 87 void clearInterval(int timerId); 88 89 int setTimeout(const QScriptValue &expression, int delay); 90 void clearTimeout(int timerId); 91 //! [1] 92 93 //! [2] 94 signals: 95 void scriptError(const QScriptValue &error); 96 //! [2] 97 98 protected: 99 void timerEvent(QTimerEvent *event); 100 101 private: 102 QScriptValue eventHandler(QContext2DCanvas *canvas, 103 const QString &type, QScriptValue *who); 104 QScriptValue newFakeDomEvent(const QString &type, 105 const QScriptValue &target); 106 void maybeEmitScriptError(); 107 108 QScriptEngine *m_engine; 109 QScriptValue m_originalGlobalObject; 110 QScriptValue m_document; 111 QList<QContext2DCanvas*> m_canvases; 112 QHash<int, QScriptValue> m_intervalHash; 113 QHash<int, QScriptValue> m_timeoutHash; 114 }; 115 116 //! [3] 117 class Document : public QObject 118 { 119 Q_OBJECT 120 public: 121 Document(Environment *env); 122 ~Document(); 123 124 public slots: 125 QScriptValue getElementById(const QString &id) const; 126 QScriptValue getElementsByTagName(const QString &name) const; 127 128 // EventTarget 129 void addEventListener(const QString &type, const QScriptValue &listener, 130 bool useCapture); 131 }; 132 //! [3] 133 134 class CanvasGradientPrototype : public QObject, public QScriptable 135 { 136 Q_OBJECT 137 protected: 138 CanvasGradientPrototype(QObject *parent = 0); 139 public: 140 static void setup(QScriptEngine *engine); 141 142 public slots: 143 void addColorStop(qreal offset, const QString &color); 144 }; 145 146 #endif 147