tools/qml/qdeclarativetester.h
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 tools applications 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 QDECLARATIVETESTER_H
       
    43 #define QDECLARATIVETESTER_H
       
    44 
       
    45 #include <QEvent>
       
    46 #include <QMouseEvent>
       
    47 #include <QKeyEvent>
       
    48 #include <QImage>
       
    49 #include <QUrl>
       
    50 #include <qmlruntime.h>
       
    51 
       
    52 QT_BEGIN_NAMESPACE
       
    53 
       
    54 class QDeclarativeVisualTest : public QObject
       
    55 {
       
    56     Q_OBJECT
       
    57     Q_PROPERTY(QDeclarativeListProperty<QObject> events READ events CONSTANT)
       
    58     Q_CLASSINFO("DefaultProperty", "events")
       
    59 public:
       
    60     QDeclarativeVisualTest() {}
       
    61 
       
    62     QDeclarativeListProperty<QObject> events() { return QDeclarativeListProperty<QObject>(this, m_events); }
       
    63 
       
    64     int count() const { return m_events.count(); }
       
    65     QObject *event(int idx) { return m_events.at(idx); }
       
    66 
       
    67 private:
       
    68     QList<QObject *> m_events;
       
    69 };
       
    70 
       
    71 QT_END_NAMESPACE
       
    72 
       
    73 QML_DECLARE_TYPE(QDeclarativeVisualTest)
       
    74 
       
    75 QT_BEGIN_NAMESPACE
       
    76 
       
    77 class QDeclarativeVisualTestFrame : public QObject
       
    78 {
       
    79     Q_OBJECT
       
    80     Q_PROPERTY(int msec READ msec WRITE setMsec)
       
    81     Q_PROPERTY(QString hash READ hash WRITE setHash)
       
    82     Q_PROPERTY(QUrl image READ image WRITE setImage)
       
    83 public:
       
    84     QDeclarativeVisualTestFrame() : m_msec(-1) {}
       
    85 
       
    86     int msec() const { return m_msec; }
       
    87     void setMsec(int m) { m_msec = m; }
       
    88 
       
    89     QString hash() const { return m_hash; }
       
    90     void setHash(const QString &hash) { m_hash = hash; }
       
    91 
       
    92     QUrl image() const { return m_image; }
       
    93     void setImage(const QUrl &image) { m_image = image; }
       
    94 
       
    95 private:
       
    96     int m_msec;
       
    97     QString m_hash;
       
    98     QUrl m_image;
       
    99 };
       
   100 
       
   101 QT_END_NAMESPACE
       
   102 
       
   103 QML_DECLARE_TYPE(QDeclarativeVisualTestFrame)
       
   104 
       
   105 QT_BEGIN_NAMESPACE
       
   106 
       
   107 class QDeclarativeVisualTestMouse : public QObject
       
   108 {
       
   109     Q_OBJECT
       
   110     Q_PROPERTY(int type READ type WRITE setType)
       
   111     Q_PROPERTY(int button READ button WRITE setButton)
       
   112     Q_PROPERTY(int buttons READ buttons WRITE setButtons)
       
   113     Q_PROPERTY(int x READ x WRITE setX)
       
   114     Q_PROPERTY(int y READ y WRITE setY)
       
   115     Q_PROPERTY(int modifiers READ modifiers WRITE setModifiers)
       
   116     Q_PROPERTY(bool sendToViewport READ sendToViewport WRITE setSendToViewport)
       
   117 public:
       
   118     QDeclarativeVisualTestMouse() : m_type(0), m_button(0), m_buttons(0), m_x(0), m_y(0), m_modifiers(0), m_viewport(false) {}
       
   119 
       
   120     int type() const { return m_type; }
       
   121     void setType(int t) { m_type = t; }
       
   122     
       
   123     int button() const { return m_button; }
       
   124     void setButton(int b) { m_button = b; }
       
   125 
       
   126     int buttons() const { return m_buttons; }
       
   127     void setButtons(int b) { m_buttons = b; }
       
   128 
       
   129     int x() const { return m_x; }
       
   130     void setX(int x) { m_x = x; }
       
   131 
       
   132     int y() const { return m_y; }
       
   133     void setY(int y) { m_y = y; }
       
   134 
       
   135     int modifiers() const { return m_modifiers; }
       
   136     void setModifiers(int modifiers) { m_modifiers = modifiers; }
       
   137 
       
   138     bool sendToViewport() const { return m_viewport; }
       
   139     void setSendToViewport(bool v) { m_viewport = v; }
       
   140 private:
       
   141     int m_type;
       
   142     int m_button;
       
   143     int m_buttons;
       
   144     int m_x;
       
   145     int m_y;
       
   146     int m_modifiers;
       
   147     bool m_viewport;
       
   148 };
       
   149 
       
   150 QT_END_NAMESPACE
       
   151 
       
   152 QML_DECLARE_TYPE(QDeclarativeVisualTestMouse)
       
   153 
       
   154 QT_BEGIN_NAMESPACE
       
   155 
       
   156 class QDeclarativeVisualTestKey : public QObject
       
   157 {
       
   158     Q_OBJECT
       
   159     Q_PROPERTY(int type READ type WRITE setType)
       
   160     Q_PROPERTY(int key READ key WRITE setKey)
       
   161     Q_PROPERTY(int modifiers READ modifiers WRITE setModifiers)
       
   162     Q_PROPERTY(QString text READ text WRITE setText)
       
   163     Q_PROPERTY(bool autorep READ autorep WRITE setAutorep)
       
   164     Q_PROPERTY(int count READ count WRITE setCount)
       
   165     Q_PROPERTY(bool sendToViewport READ sendToViewport WRITE setSendToViewport)
       
   166 public:
       
   167     QDeclarativeVisualTestKey() : m_type(0), m_key(0), m_modifiers(0), m_autorep(false), m_count(0), m_viewport(false) {}
       
   168 
       
   169     int type() const { return m_type; }
       
   170     void setType(int t) { m_type = t; }
       
   171 
       
   172     int key() const { return m_key; }
       
   173     void setKey(int k) { m_key = k; }
       
   174 
       
   175     int modifiers() const { return m_modifiers; }
       
   176     void setModifiers(int m) { m_modifiers = m; }
       
   177 
       
   178     QString text() const { return m_text; }
       
   179     void setText(const QString &t) { m_text = t; }
       
   180 
       
   181     bool autorep() const { return m_autorep; }
       
   182     void setAutorep(bool a) { m_autorep = a; }
       
   183 
       
   184     int count() const { return m_count; }
       
   185     void setCount(int c) { m_count = c; }
       
   186 
       
   187     bool sendToViewport() const { return m_viewport; }
       
   188     void setSendToViewport(bool v) { m_viewport = v; }
       
   189 private:
       
   190     int m_type;
       
   191     int m_key;
       
   192     int m_modifiers;
       
   193     QString m_text;
       
   194     bool m_autorep;
       
   195     int m_count;
       
   196     bool m_viewport;
       
   197 };
       
   198 
       
   199 QT_END_NAMESPACE
       
   200 
       
   201 QML_DECLARE_TYPE(QDeclarativeVisualTestKey)
       
   202 
       
   203 QT_BEGIN_NAMESPACE
       
   204 
       
   205 class QDeclarativeTester : public QAbstractAnimation
       
   206 {
       
   207 public:
       
   208     QDeclarativeTester(const QString &script, QDeclarativeViewer::ScriptOptions options, QDeclarativeView *parent);
       
   209     ~QDeclarativeTester();
       
   210 
       
   211     static void registerTypes();
       
   212 
       
   213     virtual int duration() const;
       
   214 
       
   215     void run();
       
   216     void save();
       
   217 
       
   218     void executefailure();
       
   219 protected:
       
   220     virtual void updateCurrentTime(int msecs);
       
   221     virtual bool eventFilter(QObject *, QEvent *);
       
   222 
       
   223 private:
       
   224     QString m_script;
       
   225 
       
   226     void imagefailure();
       
   227     void complete();
       
   228 
       
   229     enum Destination { View, ViewPort };
       
   230     void addKeyEvent(Destination, QKeyEvent *);
       
   231     void addMouseEvent(Destination, QMouseEvent *);
       
   232     QDeclarativeView *m_view;
       
   233 
       
   234     struct MouseEvent {
       
   235         MouseEvent(QMouseEvent *e)
       
   236             : type(e->type()), button(e->button()), buttons(e->buttons()), 
       
   237               pos(e->pos()), modifiers(e->modifiers()), destination(View) {}
       
   238 
       
   239         QEvent::Type type;
       
   240         Qt::MouseButton button;
       
   241         Qt::MouseButtons buttons;
       
   242         QPoint pos;
       
   243         Qt::KeyboardModifiers modifiers;
       
   244         Destination destination;
       
   245 
       
   246         int msec;
       
   247     };
       
   248     struct KeyEvent {
       
   249         KeyEvent(QKeyEvent *e)
       
   250             : type(e->type()), key(e->key()), modifiers(e->modifiers()), text(e->text()),
       
   251               autorep(e->isAutoRepeat()), count(e->count()), destination(View) {}
       
   252         QEvent::Type type;
       
   253         int key;
       
   254         Qt::KeyboardModifiers modifiers;
       
   255         QString text;
       
   256         bool autorep;
       
   257         ushort count;
       
   258         Destination destination;
       
   259 
       
   260         int msec;
       
   261     };
       
   262     struct FrameEvent {
       
   263         QImage image;
       
   264         QByteArray hash;
       
   265         int msec;
       
   266     };
       
   267     QList<MouseEvent> m_mouseEvents;
       
   268     QList<KeyEvent> m_keyEvents;
       
   269 
       
   270     QList<MouseEvent> m_savedMouseEvents;
       
   271     QList<KeyEvent> m_savedKeyEvents;
       
   272     QList<FrameEvent> m_savedFrameEvents;
       
   273     bool filterEvents;
       
   274 
       
   275     QDeclarativeViewer::ScriptOptions options;
       
   276     int testscriptidx;
       
   277     QDeclarativeVisualTest *testscript;
       
   278 
       
   279     bool hasCompleted;
       
   280     bool hasFailed;
       
   281 };
       
   282 
       
   283 
       
   284 QT_END_NAMESPACE
       
   285 
       
   286 #endif // QDECLARATIVETESTER_H