1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include <QtCore/qobject.h>
45 #include <QtQml/qqml.h>
46 #include <QtQml/qqmlexpression.h>
47 #include <QtCore/qpoint.h>
48 #include <QtCore/qsize.h>
49 #include <QtQml/qqmllist.h>
50 #include <QtCore/qrect.h>
51 #include <QtGui/qmatrix.h>
52 #include <QtGui/qcolor.h>
53 #include <QtGui/qvector3d.h>
54 #include <QtGui/QFont>
55 #include <QtGui/QPixmap>
56 #include <QtCore/qdatetime.h>
57 #include <QtCore/qjsonarray.h>
58 #include <QtCore/qjsonobject.h>
59 #include <QtCore/qjsonvalue.h>
60 #include <QtQml/qjsvalue.h>
61 #include <QtQml/qqmlscriptstring.h>
62 #include <QtQml/qqmlcomponent.h>
64 #include <private/qqmlengine_p.h>
65 #include <private/qv8engine_p.h>
67 class MyQmlAttachedObject : public QObject
70 Q_PROPERTY(int value READ value CONSTANT)
71 Q_PROPERTY(int value2 READ value2 WRITE setValue2 NOTIFY value2Changed)
73 MyQmlAttachedObject(QObject *parent) : QObject(parent), m_value2(0) {}
75 int value() const { return 19; }
76 int value2() const { return m_value2; }
77 void setValue2(int v) { if (m_value2 == v) return; m_value2 = v; emit value2Changed(); }
79 void emitMySignal() { emit mySignal(); }
89 class MyQmlObject : public QObject
94 Q_PROPERTY(int deleteOnSet READ deleteOnSet WRITE setDeleteOnSet)
95 Q_PROPERTY(bool trueProperty READ trueProperty CONSTANT)
96 Q_PROPERTY(bool falseProperty READ falseProperty CONSTANT)
97 Q_PROPERTY(int value READ value WRITE setValue)
98 Q_PROPERTY(int console READ console CONSTANT)
99 Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty NOTIFY stringChanged)
100 Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty NOTIFY urlChanged)
101 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty NOTIFY objectChanged)
102 Q_PROPERTY(QQmlListProperty<QObject> objectListProperty READ objectListProperty CONSTANT)
103 Q_PROPERTY(int resettableProperty READ resettableProperty WRITE setResettableProperty RESET resetProperty)
104 Q_PROPERTY(QRegExp regExp READ regExp WRITE setRegExp)
105 Q_PROPERTY(int nonscriptable READ nonscriptable WRITE setNonscriptable SCRIPTABLE false)
106 Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty NOTIFY intChanged)
109 MyQmlObject(): myinvokableObject(0), m_methodCalled(false), m_methodIntCalled(false), m_object(0), m_value(0), m_resetProperty(13), m_intProperty(0), m_buttons(0) {}
111 enum MyEnum { EnumValue1 = 0, EnumValue2 = 1 };
112 enum MyEnum2 { EnumValue3 = 2, EnumValue4 = 3 };
114 bool trueProperty() const { return true; }
115 bool falseProperty() const { return false; }
117 QString stringProperty() const { return m_string; }
118 void setStringProperty(const QString &s)
123 emit stringChanged();
126 QUrl urlProperty() const { return m_url; }
127 void setUrlProperty(const QUrl &url)
135 QObject *objectProperty() const { return m_object; }
136 void setObjectProperty(QObject *obj) {
140 emit objectChanged();
143 QQmlListProperty<QObject> objectListProperty() { return QQmlListProperty<QObject>(this, m_objectQList); }
145 bool methodCalled() const { return m_methodCalled; }
146 bool methodIntCalled() const { return m_methodIntCalled; }
148 QString string() const { return m_string; }
150 static MyQmlAttachedObject *qmlAttachedProperties(QObject *o) {
151 return new MyQmlAttachedObject(o);
154 int deleteOnSet() const { return 1; }
155 void setDeleteOnSet(int v) { if(v) delete this; }
157 int value() const { return m_value; }
158 void setValue(int v) { m_value = v; }
160 int resettableProperty() const { return m_resetProperty; }
161 void setResettableProperty(int v) { m_resetProperty = v; }
162 void resetProperty() { m_resetProperty = 13; }
164 QRegExp regExp() { return m_regExp; }
165 void setRegExp(const QRegExp ®Exp) { m_regExp = regExp; }
167 int console() const { return 11; }
169 int nonscriptable() const { return 0; }
170 void setNonscriptable(int) {}
172 MyQmlObject *myinvokableObject;
173 Q_INVOKABLE MyQmlObject *returnme() { return this; }
178 QVariant variant() const { return m_variant; }
179 QJSValue qjsvalue() const { return m_qjsvalue; }
180 Qt::MouseButtons buttons() const { return m_buttons; }
182 int intProperty() const { return m_intProperty; }
183 void setIntProperty(int i) { m_intProperty = i; emit intChanged(); }
187 void argumentSignal(int a, QString b, qreal c, MyEnum2 d, Qt::MouseButtons e);
188 void unnamedArgumentSignal(int a, qreal, QString c);
189 void stringChanged();
191 void objectChanged();
192 void anotherBasicSignal();
193 void thirdBasicSignal();
194 void signalWithUnknownType(const MyQmlObject::MyType &arg);
195 void signalWithVariant(const QVariant &arg);
196 void signalWithQJSValue(const QJSValue &arg);
197 void signalWithGlobalName(int parseInt);
201 void deleteMe() { delete this; }
202 void methodNoArgs() { m_methodCalled = true; }
203 void method(int a) { if(a == 163) m_methodIntCalled = true; }
204 void setString(const QString &s) { m_string = s; }
205 void myinvokable(MyQmlObject *o) { myinvokableObject = o; }
206 void variantMethod(const QVariant &v) { m_variant = v; }
207 void qjsvalueMethod(const QJSValue &v) { m_qjsvalue = v; }
208 void v8function(QQmlV8Function*);
209 void registeredFlagMethod(Qt::MouseButtons v) { m_buttons = v; }
212 friend class tst_qqmlecmascript;
214 bool m_methodIntCalled;
219 QList<QObject *> m_objectQList;
226 Qt::MouseButtons m_buttons;
229 QML_DECLARE_TYPEINFO(MyQmlObject, QML_HAS_ATTACHED_PROPERTIES)
231 class MyQmlContainer : public QObject
234 Q_PROPERTY(QQmlListProperty<MyQmlObject> children READ children CONSTANT)
238 QQmlListProperty<MyQmlObject> children() { return QQmlListProperty<MyQmlObject>(this, m_children); }
241 QList<MyQmlObject*> m_children;
245 class MyExpression : public QQmlExpression
249 MyExpression(QQmlContext *ctxt, const QString &expr)
250 : QQmlExpression(ctxt, 0, expr), changed(false)
252 QObject::connect(this, SIGNAL(valueChanged()), this, SLOT(expressionValueChanged()));
253 setNotifyOnValueChanged(true);
259 void expressionValueChanged() {
265 class MyDefaultObject1 : public QObject
268 Q_PROPERTY(int horseLegs READ horseLegs CONSTANT)
269 Q_PROPERTY(int antLegs READ antLegs CONSTANT)
270 Q_PROPERTY(int emuLegs READ emuLegs CONSTANT)
272 int horseLegs() const { return 4; }
273 int antLegs() const { return 6; }
274 int emuLegs() const { return 2; }
277 class MyDefaultObject3 : public QObject
280 Q_PROPERTY(int antLegs READ antLegs CONSTANT)
281 Q_PROPERTY(int humanLegs READ humanLegs CONSTANT)
283 int antLegs() const { return 7; } // Mutant
284 int humanLegs() const { return 2; }
285 int millipedeLegs() const { return 1000; }
288 class MyDeferredObject : public QObject
291 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
292 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
293 Q_PROPERTY(QObject *objectProperty2 READ objectProperty2 WRITE setObjectProperty2)
294 Q_CLASSINFO("DeferredPropertyNames", "value,objectProperty,objectProperty2")
297 MyDeferredObject() : m_value(0), m_object(0), m_object2(0) {}
299 int value() const { return m_value; }
300 void setValue(int v) { m_value = v; emit valueChanged(); }
302 QObject *objectProperty() const { return m_object; }
303 void setObjectProperty(QObject *obj) { m_object = obj; }
305 QObject *objectProperty2() const { return m_object2; }
306 void setObjectProperty2(QObject *obj) { m_object2 = obj; }
317 class MyBaseExtendedObject : public QObject
320 Q_PROPERTY(int baseProperty READ baseProperty WRITE setBaseProperty)
322 MyBaseExtendedObject() : m_value(0) {}
324 int baseProperty() const { return m_value; }
325 void setBaseProperty(int v) { m_value = v; }
331 class MyExtendedObject : public MyBaseExtendedObject
334 Q_PROPERTY(int coreProperty READ coreProperty WRITE setCoreProperty)
336 MyExtendedObject() : m_value(0) {}
338 int coreProperty() const { return m_value; }
339 void setCoreProperty(int v) { m_value = v; }
345 class MyTypeObject : public QObject
351 Q_PROPERTY(QString id READ id WRITE setId)
352 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
353 Q_PROPERTY(QQmlComponent *componentProperty READ componentProperty WRITE setComponentProperty)
354 Q_PROPERTY(MyFlags flagProperty READ flagProperty WRITE setFlagProperty)
355 Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty)
356 Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty)
357 Q_PROPERTY(uint uintProperty READ uintProperty WRITE setUintProperty)
358 Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty)
359 Q_PROPERTY(qreal realProperty READ realProperty WRITE setRealProperty)
360 Q_PROPERTY(double doubleProperty READ doubleProperty WRITE setDoubleProperty)
361 Q_PROPERTY(float floatProperty READ floatProperty WRITE setFloatProperty)
362 Q_PROPERTY(QColor colorProperty READ colorProperty WRITE setColorProperty)
363 Q_PROPERTY(QDate dateProperty READ dateProperty WRITE setDateProperty)
364 Q_PROPERTY(QTime timeProperty READ timeProperty WRITE setTimeProperty)
365 Q_PROPERTY(QDateTime dateTimeProperty READ dateTimeProperty WRITE setDateTimeProperty)
366 Q_PROPERTY(QDateTime dateTimeProperty2 READ dateTimeProperty2 WRITE setDateTimeProperty2)
367 Q_PROPERTY(QPoint pointProperty READ pointProperty WRITE setPointProperty)
368 Q_PROPERTY(QPointF pointFProperty READ pointFProperty WRITE setPointFProperty)
369 Q_PROPERTY(QSize sizeProperty READ sizeProperty WRITE setSizeProperty)
370 Q_PROPERTY(QSizeF sizeFProperty READ sizeFProperty WRITE setSizeFProperty)
371 Q_PROPERTY(QRect rectProperty READ rectProperty WRITE setRectProperty NOTIFY rectPropertyChanged)
372 Q_PROPERTY(QRect rectProperty2 READ rectProperty2 WRITE setRectProperty2)
373 Q_PROPERTY(QRectF rectFProperty READ rectFProperty WRITE setRectFProperty)
374 Q_PROPERTY(bool boolProperty READ boolProperty WRITE setBoolProperty)
375 Q_PROPERTY(QVariant variantProperty READ variantProperty WRITE setVariantProperty)
376 Q_PROPERTY(QVector3D vectorProperty READ vectorProperty WRITE setVectorProperty)
377 Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty)
379 Q_PROPERTY(QQmlScriptString scriptProperty READ scriptProperty WRITE setScriptProperty)
383 : objectPropertyValue(0), componentPropertyValue(0) {}
389 void setId(const QString &v) {
393 QObject *objectPropertyValue;
394 QObject *objectProperty() const {
395 return objectPropertyValue;
397 void setObjectProperty(QObject *v) {
398 objectPropertyValue = v;
401 QQmlComponent *componentPropertyValue;
402 QQmlComponent *componentProperty() const {
403 return componentPropertyValue;
405 void setComponentProperty(QQmlComponent *v) {
406 componentPropertyValue = v;
409 enum MyFlag { FlagVal1 = 0x01, FlagVal2 = 0x02, FlagVal3 = 0x04 };
410 Q_DECLARE_FLAGS(MyFlags, MyFlag)
411 MyFlags flagPropertyValue;
412 MyFlags flagProperty() const {
413 return flagPropertyValue;
415 void setFlagProperty(MyFlags v) {
416 flagPropertyValue = v;
419 enum MyEnum { EnumVal1, EnumVal2 };
420 MyEnum enumPropertyValue;
421 MyEnum enumProperty() const {
422 return enumPropertyValue;
424 void setEnumProperty(MyEnum v) {
425 enumPropertyValue = v;
428 QString stringPropertyValue;
429 QString stringProperty() const {
430 return stringPropertyValue;
432 void setStringProperty(const QString &v) {
433 stringPropertyValue = v;
436 uint uintPropertyValue;
437 uint uintProperty() const {
438 return uintPropertyValue;
440 void setUintProperty(const uint &v) {
441 uintPropertyValue = v;
444 int intPropertyValue;
445 int intProperty() const {
446 return intPropertyValue;
448 void setIntProperty(const int &v) {
449 intPropertyValue = v;
452 qreal realPropertyValue;
453 qreal realProperty() const {
454 return realPropertyValue;
456 void setRealProperty(const qreal &v) {
457 realPropertyValue = v;
460 double doublePropertyValue;
461 double doubleProperty() const {
462 return doublePropertyValue;
464 void setDoubleProperty(const double &v) {
465 doublePropertyValue = v;
468 float floatPropertyValue;
469 float floatProperty() const {
470 return floatPropertyValue;
472 void setFloatProperty(const float &v) {
473 floatPropertyValue = v;
476 QColor colorPropertyValue;
477 QColor colorProperty() const {
478 return colorPropertyValue;
480 void setColorProperty(const QColor &v) {
481 colorPropertyValue = v;
484 QDate datePropertyValue;
485 QDate dateProperty() const {
486 return datePropertyValue;
488 void setDateProperty(const QDate &v) {
489 datePropertyValue = v;
492 QTime timePropertyValue;
493 QTime timeProperty() const {
494 return timePropertyValue;
496 void setTimeProperty(const QTime &v) {
497 timePropertyValue = v;
500 QDateTime dateTimePropertyValue;
501 QDateTime dateTimeProperty() const {
502 return dateTimePropertyValue;
504 void setDateTimeProperty(const QDateTime &v) {
505 dateTimePropertyValue = v;
508 QDateTime dateTimePropertyValue2;
509 QDateTime dateTimeProperty2() const {
510 return dateTimePropertyValue2;
512 void setDateTimeProperty2(const QDateTime &v) {
513 dateTimePropertyValue2 = v;
516 QPoint pointPropertyValue;
517 QPoint pointProperty() const {
518 return pointPropertyValue;
520 void setPointProperty(const QPoint &v) {
521 pointPropertyValue = v;
524 QPointF pointFPropertyValue;
525 QPointF pointFProperty() const {
526 return pointFPropertyValue;
528 void setPointFProperty(const QPointF &v) {
529 pointFPropertyValue = v;
532 QSize sizePropertyValue;
533 QSize sizeProperty() const {
534 return sizePropertyValue;
536 void setSizeProperty(const QSize &v) {
537 sizePropertyValue = v;
540 QSizeF sizeFPropertyValue;
541 QSizeF sizeFProperty() const {
542 return sizeFPropertyValue;
544 void setSizeFProperty(const QSizeF &v) {
545 sizeFPropertyValue = v;
548 QRect rectPropertyValue;
549 QRect rectProperty() const {
550 return rectPropertyValue;
552 void setRectProperty(const QRect &v) {
553 rectPropertyValue = v;
554 emit rectPropertyChanged();
557 QRect rectPropertyValue2;
558 QRect rectProperty2() const {
559 return rectPropertyValue2;
561 void setRectProperty2(const QRect &v) {
562 rectPropertyValue2 = v;
565 QRectF rectFPropertyValue;
566 QRectF rectFProperty() const {
567 return rectFPropertyValue;
569 void setRectFProperty(const QRectF &v) {
570 rectFPropertyValue = v;
573 bool boolPropertyValue;
574 bool boolProperty() const {
575 return boolPropertyValue;
577 void setBoolProperty(const bool &v) {
578 boolPropertyValue = v;
581 QVariant variantPropertyValue;
582 QVariant variantProperty() const {
583 return variantPropertyValue;
585 void setVariantProperty(const QVariant &v) {
586 variantPropertyValue = v;
589 QVector3D vectorPropertyValue;
590 QVector3D vectorProperty() const {
591 return vectorPropertyValue;
593 void setVectorProperty(const QVector3D &v) {
594 vectorPropertyValue = v;
597 QUrl urlPropertyValue;
598 QUrl urlProperty() const {
599 return urlPropertyValue;
601 void setUrlProperty(const QUrl &v) {
602 urlPropertyValue = v;
605 QQmlScriptString scriptPropertyValue;
606 QQmlScriptString scriptProperty() const {
607 return scriptPropertyValue;
609 void setScriptProperty(const QQmlScriptString &v) {
610 scriptPropertyValue = v;
613 void doAction() { emit action(); }
616 void rectPropertyChanged();
618 Q_DECLARE_OPERATORS_FOR_FLAGS(MyTypeObject::MyFlags)
620 class MyDerivedObject : public MyTypeObject
624 Q_INVOKABLE bool intProperty() const {
629 Q_DECLARE_METATYPE(QJSValue);
630 class MyInvokableBaseObject : public QObject
634 inline ~MyInvokableBaseObject() = 0;
636 Q_INVOKABLE inline void method_inherited(int a);
637 Q_INVOKABLE inline void method_overload();
640 class MyInvokableObject : public MyInvokableBaseObject
645 enum TestEnum { EnumValue1, EnumValue2 };
646 MyInvokableObject() { reset(); }
648 int invoked() const { return m_invoked; }
649 bool error() const { return m_invokedError; }
650 const QVariantList &actuals() const { return m_actuals; }
651 void reset() { m_invoked = -1; m_invokedError = false; m_actuals.clear(); }
653 Q_INVOKABLE QPointF method_get_QPointF() { return QPointF(99.3, -10.2); }
654 Q_INVOKABLE QPoint method_get_QPoint() { return QPoint(9, 12); }
656 Q_INVOKABLE void method_NoArgs() { invoke(0); }
657 Q_INVOKABLE int method_NoArgs_int() { invoke(1); return 6; }
658 Q_INVOKABLE qreal method_NoArgs_real() { invoke(2); return 19.75; }
659 Q_INVOKABLE QPointF method_NoArgs_QPointF() { invoke(3); return QPointF(123, 4.5); }
660 Q_INVOKABLE QObject *method_NoArgs_QObject() { invoke(4); return this; }
661 Q_INVOKABLE MyInvokableObject *method_NoArgs_unknown() { invoke(5); return this; }
662 Q_INVOKABLE QJSValue method_NoArgs_QScriptValue() { invoke(6); return QJSValue("Hello world"); }
663 Q_INVOKABLE QVariant method_NoArgs_QVariant() { invoke(7); return QVariant("QML rocks"); }
665 Q_INVOKABLE void method_int(int a) { invoke(8); m_actuals << a; }
666 Q_INVOKABLE void method_intint(int a, int b) { invoke(9); m_actuals << a << b; }
667 Q_INVOKABLE void method_real(qreal a) { invoke(10); m_actuals << a; }
668 Q_INVOKABLE void method_QString(QString a) { invoke(11); m_actuals << a; }
669 Q_INVOKABLE void method_QPointF(QPointF a) { invoke(12); m_actuals << a; }
670 Q_INVOKABLE void method_QObject(QObject *a) { invoke(13); m_actuals << qVariantFromValue(a); }
671 Q_INVOKABLE void method_QScriptValue(QJSValue a) { invoke(14); m_actuals << qVariantFromValue(a); }
672 Q_INVOKABLE void method_intQScriptValue(int a, QJSValue b) { invoke(15); m_actuals << a << qVariantFromValue(b); }
674 Q_INVOKABLE void method_overload(int a) { invoke(16); m_actuals << a; }
675 Q_INVOKABLE void method_overload(int a, int b) { invoke(17); m_actuals << a << b; }
676 Q_INVOKABLE void method_overload(QString a) { invoke(18); m_actuals << a; }
678 Q_INVOKABLE void method_with_enum(TestEnum e) { invoke(19); m_actuals << (int)e; }
680 Q_INVOKABLE int method_default(int a, int b = 19) { invoke(20); m_actuals << a << b; return b; }
682 Q_INVOKABLE void method_QVariant(QVariant a, QVariant b = QVariant()) { invoke(21); m_actuals << a << b; }
684 Q_INVOKABLE void method_QJsonObject(const QJsonObject &a) { invoke(22); m_actuals << QVariant::fromValue(a); }
685 Q_INVOKABLE void method_QJsonArray(const QJsonArray &a) { invoke(23); m_actuals << QVariant::fromValue(a); }
686 Q_INVOKABLE void method_QJsonValue(const QJsonValue &a) { invoke(24); m_actuals << QVariant::fromValue(a); }
688 Q_INVOKABLE void method_overload(const QJsonObject &a) { invoke(25); m_actuals << QVariant::fromValue(a); }
689 Q_INVOKABLE void method_overload(const QJsonArray &a) { invoke(26); m_actuals << QVariant::fromValue(a); }
690 Q_INVOKABLE void method_overload(const QJsonValue &a) { invoke(27); m_actuals << QVariant::fromValue(a); }
693 friend class MyInvokableBaseObject;
694 void invoke(int idx) { if (m_invoked != -1) m_invokedError = true; m_invoked = idx;}
697 QVariantList m_actuals;
700 MyInvokableBaseObject::~MyInvokableBaseObject() {}
702 void MyInvokableBaseObject::method_inherited(int a)
704 static_cast<MyInvokableObject *>(this)->invoke(-3);
705 static_cast<MyInvokableObject *>(this)->m_actuals << a;
708 // This is a hidden overload of the MyInvokableObject::method_overload() method
709 void MyInvokableBaseObject::method_overload()
711 static_cast<MyInvokableObject *>(this)->invoke(-2);
714 class NumberAssignment : public QObject
718 Q_PROPERTY(qreal test1 READ test1 WRITE setTest1)
720 qreal test1() const { return _test1; }
721 void setTest1(qreal v) { _test1 = v; }
723 Q_PROPERTY(qreal test2 READ test2 WRITE setTest2)
725 qreal test2() const { return _test2; }
726 void setTest2(qreal v) { _test2 = v; }
728 Q_PROPERTY(qreal test3 READ test3 WRITE setTest3)
730 qreal test3() const { return _test3; }
731 void setTest3(qreal v) { _test3 = v; }
733 Q_PROPERTY(qreal test4 READ test4 WRITE setTest4)
735 qreal test4() const { return _test4; }
736 void setTest4(qreal v) { _test4 = v; }
738 Q_PROPERTY(int test5 READ test5 WRITE setTest5)
740 int test5() const { return _test5; }
741 void setTest5(int v) { _test5 = v; }
743 Q_PROPERTY(int test6 READ test6 WRITE setTest6)
745 int test6() const { return _test6; }
746 void setTest6(int v) { _test6 = v; }
748 Q_PROPERTY(int test7 READ test7 WRITE setTest7)
750 int test7() const { return _test7; }
751 void setTest7(int v) { _test7 = v; }
753 Q_PROPERTY(int test8 READ test8 WRITE setTest8)
755 int test8() const { return _test8; }
756 void setTest8(int v) { _test8 = v; }
758 Q_PROPERTY(unsigned int test9 READ test9 WRITE setTest9)
760 unsigned int test9() const { return _test9; }
761 void setTest9(unsigned int v) { _test9 = v; }
763 Q_PROPERTY(unsigned int test10 READ test10 WRITE setTest10)
764 unsigned int _test10;
765 unsigned int test10() const { return _test10; }
766 void setTest10(unsigned int v) { _test10 = v; }
768 Q_PROPERTY(unsigned int test11 READ test11 WRITE setTest11)
769 unsigned int _test11;
770 unsigned int test11() const { return _test11; }
771 void setTest11(unsigned int v) { _test11 = v; }
773 Q_PROPERTY(unsigned int test12 READ test12 WRITE setTest12)
774 unsigned int _test12;
775 unsigned int test12() const { return _test12; }
776 void setTest12(unsigned int v) { _test12 = v; }
779 class DefaultPropertyExtendedObject : public QObject
782 Q_PROPERTY(QObject *firstProperty READ firstProperty WRITE setFirstProperty)
783 Q_PROPERTY(QObject *secondProperty READ secondProperty WRITE setSecondProperty)
785 DefaultPropertyExtendedObject(QObject *parent = 0) : QObject(parent), m_firstProperty(0), m_secondProperty(0) {}
787 QObject *firstProperty() const { return m_firstProperty; }
788 QObject *secondProperty() const { return m_secondProperty; }
789 void setFirstProperty(QObject *property) { m_firstProperty = property; }
790 void setSecondProperty(QObject *property) { m_secondProperty = property; }
792 QObject* m_firstProperty;
793 QObject* m_secondProperty;
796 class OverrideDefaultPropertyObject : public DefaultPropertyExtendedObject
799 Q_CLASSINFO("DefaultProperty", "secondProperty")
801 OverrideDefaultPropertyObject() {}
804 class MyRevisionedBaseClassRegistered : public QObject
807 Q_PROPERTY(qreal propA READ propA WRITE setPropA NOTIFY propAChanged)
808 Q_PROPERTY(qreal propB READ propB WRITE setPropB NOTIFY propBChanged REVISION 1)
811 MyRevisionedBaseClassRegistered() : m_pa(1), m_pb(2) {}
813 qreal propA() const { return m_pa; }
814 void setPropA(qreal p) {
820 qreal propB() const { return m_pb; }
821 void setPropB(qreal p) {
828 Q_INVOKABLE void methodA() { }
829 Q_INVOKABLE Q_REVISION(1) void methodB() { }
836 Q_REVISION(1) void signalB();
843 class MyRevisionedBaseClassUnregistered : public MyRevisionedBaseClassRegistered
846 Q_PROPERTY(qreal propC READ propC WRITE setPropC NOTIFY propCChanged)
847 Q_PROPERTY(qreal propD READ propD WRITE setPropD NOTIFY propDChanged REVISION 1)
850 MyRevisionedBaseClassUnregistered() : m_pc(1), m_pd(2) {}
852 qreal propC() const { return m_pc; }
853 void setPropC(qreal p) {
859 qreal propD() const { return m_pd; }
860 void setPropD(qreal p) {
867 Q_INVOKABLE void methodC() { }
868 Q_INVOKABLE Q_REVISION(1) void methodD() { }
875 Q_REVISION(1) void signalD();
882 class MyRevisionedClass : public MyRevisionedBaseClassUnregistered
885 Q_PROPERTY(qreal prop1 READ prop1 WRITE setProp1 NOTIFY prop1Changed)
886 Q_PROPERTY(qreal prop2 READ prop2 WRITE setProp2 NOTIFY prop2Changed REVISION 1)
889 MyRevisionedClass() {}
891 qreal prop1() const { return m_p1; }
892 void setProp1(qreal p) {
898 qreal prop2() const { return m_p2; }
899 void setProp2(qreal p) {
906 Q_INVOKABLE void method1() { }
907 Q_INVOKABLE Q_REVISION(1) void method2() { }
914 Q_REVISION(1) void signal2();
921 class MyRevisionedSubclass : public MyRevisionedClass
924 Q_PROPERTY(qreal prop3 READ prop3 WRITE setProp3 NOTIFY prop3Changed)
925 Q_PROPERTY(qreal prop4 READ prop4 WRITE setProp4 NOTIFY prop4Changed REVISION 1)
928 MyRevisionedSubclass() : m_p3(3), m_p4(4) {}
930 qreal prop3() const { return m_p3; }
931 void setProp3(qreal p) {
937 qreal prop4() const { return m_p4; }
938 void setProp4(qreal p) {
945 Q_INVOKABLE void method3() { }
946 Q_INVOKABLE Q_REVISION(1) void method4() { }
953 Q_REVISION(1) void signal4();
960 QML_DECLARE_TYPE(MyRevisionedBaseClassRegistered)
961 QML_DECLARE_TYPE(MyRevisionedBaseClassUnregistered)
962 QML_DECLARE_TYPE(MyRevisionedClass)
963 QML_DECLARE_TYPE(MyRevisionedSubclass)
964 Q_DECLARE_METATYPE(MyQmlObject::MyType)
967 class ScarceResourceObject : public QObject
970 Q_PROPERTY(QPixmap scarceResource READ scarceResource WRITE setScarceResource NOTIFY scarceResourceChanged)
972 ScarceResourceObject(QObject *parent = 0) : QObject(parent), m_value(100, 100) { m_value.fill(Qt::blue); }
973 ~ScarceResourceObject() {}
975 QPixmap scarceResource() const { return m_value; }
976 void setScarceResource(QPixmap v) { m_value = v; emit scarceResourceChanged(); }
978 bool scarceResourceIsDetached() const { return m_value.isDetached(); }
980 // this particular one returns a new one each time
981 // this means that every Scarce Resource Copy will
982 // consume resources (so that we can track disposal
983 // of v8 handles with circular references).
984 Q_INVOKABLE QPixmap newScarceResource() const
986 QPixmap retn(800, 600);
987 retn.fill(QColor(100, 110, 120, 45));
992 void scarceResourceChanged();
997 QML_DECLARE_TYPE(ScarceResourceObject)
999 class testQObjectApi : public QObject
1003 Q_PROPERTY (int qobjectTestProperty READ qobjectTestProperty NOTIFY qobjectTestPropertyChanged)
1004 Q_PROPERTY (int qobjectTestWritableProperty READ qobjectTestWritableProperty WRITE setQObjectTestWritableProperty NOTIFY qobjectTestWritablePropertyChanged)
1005 Q_PROPERTY (int qobjectTestWritableFinalProperty READ qobjectTestWritableFinalProperty WRITE setQObjectTestWritableFinalProperty NOTIFY qobjectTestWritableFinalPropertyChanged FINAL)
1008 testQObjectApi(QObject* parent = 0)
1009 : QObject(parent), m_testProperty(0), m_testWritableProperty(0), m_testWritableFinalProperty(0), m_methodCallCount(0), m_trackedObject(0)
1013 ~testQObjectApi() {}
1015 enum MyEnum { EnumValue1 = 25, EnumValue2 = 42 };
1016 Q_INVOKABLE int qobjectEnumTestMethod(MyEnum val) { return (static_cast<int>(val) + 5); }
1017 Q_INVOKABLE int qobjectTestMethod(int increment = 1) { m_methodCallCount += increment; return m_methodCallCount; }
1019 Q_INVOKABLE void trackObject(QObject *obj) { m_trackedObject = obj; }
1020 Q_INVOKABLE QObject *trackedObject() const { return m_trackedObject; }
1021 Q_INVOKABLE void setTrackedObjectProperty(const QString &propName) const { m_trackedObject->setProperty(qPrintable(propName), QVariant(5)); }
1022 Q_INVOKABLE QVariant trackedObjectProperty(const QString &propName) const { return m_trackedObject->property(qPrintable(propName)); }
1024 int qobjectTestProperty() const { return m_testProperty; }
1025 void setQObjectTestProperty(int tp) { m_testProperty = tp; emit qobjectTestPropertyChanged(tp); }
1027 int qobjectTestWritableProperty() const { return m_testWritableProperty; }
1028 void setQObjectTestWritableProperty(int tp) { m_testWritableProperty = tp; emit qobjectTestWritablePropertyChanged(tp); }
1030 int qobjectTestWritableFinalProperty() const { return m_testWritableFinalProperty; }
1031 void setQObjectTestWritableFinalProperty(int tp) { m_testWritableFinalProperty = tp; emit qobjectTestWritableFinalPropertyChanged(); }
1034 void qobjectTestPropertyChanged(int testProperty);
1035 void qobjectTestWritablePropertyChanged(int testWritableProperty);
1036 void qobjectTestWritableFinalPropertyChanged();
1040 int m_testWritableProperty;
1041 int m_testWritableFinalProperty;
1042 int m_methodCallCount;
1043 QObject *m_trackedObject;
1046 class CircularReferenceObject : public QObject,
1047 public QV8GCCallback::Node
1052 CircularReferenceObject(QObject *parent = 0)
1053 : QObject(parent), QV8GCCallback::Node(callback), m_referenced(0), m_dtorCount(0)
1055 QV8GCCallback::addGcCallbackNode(this);
1058 ~CircularReferenceObject()
1060 if (m_dtorCount) *m_dtorCount = *m_dtorCount + 1;
1063 Q_INVOKABLE void setDtorCount(int *dtorCount)
1065 m_dtorCount = dtorCount;
1068 Q_INVOKABLE CircularReferenceObject *generate(QObject *parent = 0)
1070 CircularReferenceObject *retn = new CircularReferenceObject(parent);
1071 retn->m_dtorCount = m_dtorCount;
1072 retn->m_engine = m_engine;
1076 Q_INVOKABLE void addReference(QObject *other)
1078 m_referenced = other;
1081 static void callback(QV8GCCallback::Node *n)
1083 CircularReferenceObject *cro = static_cast<CircularReferenceObject*>(n);
1084 if (cro->m_referenced) {
1085 cro->m_engine->addRelationshipForGC(cro, cro->m_referenced);
1089 void setEngine(QQmlEngine* declarativeEngine)
1091 m_engine = QQmlEnginePrivate::get(declarativeEngine)->v8engine();
1095 QObject *m_referenced;
1097 QV8Engine* m_engine;
1099 Q_DECLARE_METATYPE(CircularReferenceObject*)
1101 class CircularReferenceHandle : public QObject,
1102 public QV8GCCallback::Node
1107 CircularReferenceHandle(QObject *parent = 0)
1108 : QObject(parent), QV8GCCallback::Node(gccallback), m_dtorCount(0), m_engine(0)
1110 QV8GCCallback::addGcCallbackNode(this);
1113 ~CircularReferenceHandle()
1115 if (m_dtorCount) *m_dtorCount = *m_dtorCount + 1;
1118 Q_INVOKABLE void setDtorCount(int *dtorCount)
1120 m_dtorCount = dtorCount;
1123 Q_INVOKABLE CircularReferenceHandle *generate(QObject *parent = 0)
1125 CircularReferenceHandle *retn = new CircularReferenceHandle(parent);
1126 retn->m_dtorCount = m_dtorCount;
1127 retn->m_engine = m_engine;
1131 Q_INVOKABLE void addReference(v8::Persistent<v8::Value> handle)
1133 m_referenced = qPersistentNew(handle);
1134 m_referenced.MakeWeak(static_cast<void*>(this), wrcallback);
1137 static void wrcallback(v8::Persistent<v8::Value> handle, void *params)
1139 CircularReferenceHandle *crh = static_cast<CircularReferenceHandle*>(params);
1140 qPersistentDispose(handle);
1141 crh->m_referenced.Clear();
1144 static void gccallback(QV8GCCallback::Node *n)
1146 CircularReferenceHandle *crh = static_cast<CircularReferenceHandle*>(n);
1147 crh->m_engine->addRelationshipForGC(crh, crh->m_referenced);
1150 void setEngine(QQmlEngine* declarativeEngine)
1152 m_engine = QQmlEnginePrivate::get(declarativeEngine)->v8engine();
1156 v8::Persistent<v8::Value> m_referenced;
1158 QV8Engine* m_engine;
1160 Q_DECLARE_METATYPE(CircularReferenceHandle*)
1162 class MyDynamicCreationDestructionObject : public QObject
1165 Q_PROPERTY (int intProperty READ intProperty WRITE setIntProperty NOTIFY intPropertyChanged)
1168 MyDynamicCreationDestructionObject(QObject *parent = 0) : QObject(parent), m_intProperty(0), m_dtorCount(0)
1172 ~MyDynamicCreationDestructionObject()
1179 int intProperty() const { return m_intProperty; }
1180 void setIntProperty(int val) { m_intProperty = val; emit intPropertyChanged(); }
1182 Q_INVOKABLE MyDynamicCreationDestructionObject *createNew()
1184 // no parent == ownership transfers to JS; same dtor counter.
1185 MyDynamicCreationDestructionObject *retn = new MyDynamicCreationDestructionObject;
1186 retn->setDtorCount(m_dtorCount);
1190 void setDtorCount(int *dtorCount)
1192 m_dtorCount = dtorCount;
1196 void intPropertyChanged();
1203 class WriteCounter : public QObject
1206 Q_PROPERTY(int value READ value WRITE setValue);
1208 WriteCounter() : m_value(0), m_count(0) {}
1210 int value() const { return m_value; }
1211 void setValue(int v) { m_value = v; ++m_count; }
1213 int count() const { return m_count; }
1220 class MySequenceConversionObject : public QObject
1224 Q_PROPERTY (QList<int> intListProperty READ intListProperty WRITE setIntListProperty NOTIFY intListPropertyChanged)
1225 Q_PROPERTY (QList<int> intListProperty2 READ intListProperty2 WRITE setIntListProperty2 NOTIFY intListProperty2Changed)
1226 Q_PROPERTY (QList<qreal> qrealListProperty READ qrealListProperty WRITE setQrealListProperty NOTIFY qrealListPropertyChanged)
1227 Q_PROPERTY (QList<bool> boolListProperty READ boolListProperty WRITE setBoolListProperty NOTIFY boolListPropertyChanged)
1228 Q_PROPERTY (QList<QString> stringListProperty READ stringListProperty WRITE setStringListProperty NOTIFY stringListPropertyChanged)
1229 Q_PROPERTY (QList<QUrl> urlListProperty READ urlListProperty WRITE setUrlListProperty NOTIFY urlListPropertyChanged)
1230 Q_PROPERTY (QStringList qstringListProperty READ qstringListProperty WRITE setQStringListProperty NOTIFY qstringListPropertyChanged)
1232 Q_PROPERTY (QList<QPoint> pointListProperty READ pointListProperty WRITE setPointListProperty NOTIFY pointListPropertyChanged)
1233 Q_PROPERTY (QList<QVariant> variantListProperty READ variantListProperty WRITE setVariantListProperty NOTIFY variantListPropertyChanged)
1235 Q_PROPERTY (qint32 maxIndex READ maxIndex CONSTANT)
1236 Q_PROPERTY (quint32 tooBigIndex READ tooBigIndex CONSTANT)
1237 Q_PROPERTY (qint32 negativeIndex READ negativeIndex CONSTANT)
1240 MySequenceConversionObject()
1242 m_intList << 1 << 2 << 3 << 4;
1243 m_intList2 << 1 << 2 << 3 << 4;
1244 m_qrealList << 1.1 << 2.2 << 3.3 << 4.4;
1245 m_boolList << true << false << true << false;
1246 m_stringList << QLatin1String("first") << QLatin1String("second") << QLatin1String("third") << QLatin1String("fourth");
1247 m_urlList << QUrl("http://www.example1.com") << QUrl("http://www.example2.com") << QUrl("http://www.example3.com");
1248 m_qstringList << QLatin1String("first") << QLatin1String("second") << QLatin1String("third") << QLatin1String("fourth");
1250 m_pointList << QPoint(1, 2) << QPoint(3, 4) << QPoint(5, 6);
1251 m_variantList << QVariant(QLatin1String("one")) << QVariant(true) << QVariant(3);
1254 ~MySequenceConversionObject() {}
1256 qint32 maxIndex() const
1260 quint32 tooBigIndex() const
1266 qint32 negativeIndex() const
1271 QList<int> intListProperty() const { return m_intList; }
1272 void setIntListProperty(const QList<int> &list) { m_intList = list; emit intListPropertyChanged(); }
1273 QList<int> intListProperty2() const { return m_intList2; }
1274 void setIntListProperty2(const QList<int> &list) { m_intList2 = list; emit intListProperty2Changed(); }
1275 QList<qreal> qrealListProperty() const { return m_qrealList; }
1276 void setQrealListProperty(const QList<qreal> &list) { m_qrealList = list; emit qrealListPropertyChanged(); }
1277 QList<bool> boolListProperty() const { return m_boolList; }
1278 void setBoolListProperty(const QList<bool> &list) { m_boolList = list; emit boolListPropertyChanged(); }
1279 QList<QString> stringListProperty() const { return m_stringList; }
1280 void setStringListProperty(const QList<QString> &list) { m_stringList = list; emit stringListPropertyChanged(); }
1281 QList<QUrl> urlListProperty() const { return m_urlList; }
1282 void setUrlListProperty(const QList<QUrl> &list) { m_urlList = list; emit urlListPropertyChanged(); }
1283 QStringList qstringListProperty() const { return m_qstringList; }
1284 void setQStringListProperty(const QStringList &list) { m_qstringList = list; emit qstringListPropertyChanged(); }
1285 QList<QPoint> pointListProperty() const { return m_pointList; }
1286 void setPointListProperty(const QList<QPoint> &list) { m_pointList = list; emit pointListPropertyChanged(); }
1287 QList<QVariant> variantListProperty() const { return m_variantList; }
1288 void setVariantListProperty(const QList<QVariant> &list) { m_variantList = list; emit variantListPropertyChanged(); }
1290 // now for "copy resource" sequences:
1291 Q_INVOKABLE QList<int> generateIntSequence() const { QList<int> retn; retn << 1 << 2 << 3; return retn; }
1292 Q_INVOKABLE QList<qreal> generateQrealSequence() const { QList<qreal> retn; retn << 1.1 << 2.2 << 3.3; return retn; }
1293 Q_INVOKABLE QList<bool> generateBoolSequence() const { QList<bool> retn; retn << true << false << true; return retn; }
1294 Q_INVOKABLE QList<QString> generateStringSequence() const { QList<QString> retn; retn << "one" << "two" << "three"; return retn; }
1295 Q_INVOKABLE QList<QUrl> generateUrlSequence() const { QList<QUrl> retn; retn << QUrl("http://www.example1.com") << QUrl("http://www.example2.com") << QUrl("http://www.example3.com"); return retn; }
1296 Q_INVOKABLE QStringList generateQStringSequence() const { QStringList retn; retn << "one" << "two" << "three"; return retn; }
1298 // "reference resource" underlying qobject deletion test:
1299 Q_INVOKABLE MySequenceConversionObject *generateTestObject() const { return new MySequenceConversionObject; }
1300 Q_INVOKABLE void deleteTestObject(QObject *object) const { delete object; }
1303 void intListPropertyChanged();
1304 void intListProperty2Changed();
1305 void qrealListPropertyChanged();
1306 void boolListPropertyChanged();
1307 void stringListPropertyChanged();
1308 void urlListPropertyChanged();
1309 void qstringListPropertyChanged();
1310 void pointListPropertyChanged();
1311 void variantListPropertyChanged();
1314 QList<int> m_intList;
1315 QList<int> m_intList2;
1316 QList<qreal> m_qrealList;
1317 QList<bool> m_boolList;
1318 QList<QString> m_stringList;
1319 QList<QUrl> m_urlList;
1320 QStringList m_qstringList;
1322 QList<QPoint> m_pointList; // not a supported sequence type
1323 QList<QVariant> m_variantList; // not a supported sequence type, but QVariantList support is hardcoded.
1326 class MyDeleteObject : public QObject
1329 Q_PROPERTY(QObject *nestedObject READ nestedObject NOTIFY nestedObjectChanged);
1330 Q_PROPERTY(int deleteNestedObject READ deleteNestedObject NOTIFY deleteNestedObjectChanged);
1333 MyDeleteObject() : m_nestedObject(new MyQmlObject) {}
1335 QObject *nestedObject() const { return m_nestedObject; }
1336 int deleteNestedObject() { delete m_nestedObject; m_nestedObject = 0; return 1; }
1339 void nestedObjectChanged();
1340 void deleteNestedObjectChanged();
1343 MyQmlObject *m_nestedObject;
1346 class DateTimeExporter : public QObject
1351 DateTimeExporter(const QDateTime &dt) : m_datetime(dt), m_offset(0), m_timespec("UTC")
1353 switch (m_datetime.timeSpec()) {
1356 QDateTime utc(m_datetime.toUTC());
1357 utc.setTimeSpec(Qt::LocalTime);
1358 m_offset = m_datetime.secsTo(utc) / 60;
1359 m_timespec = "LocalTime";
1362 case Qt::OffsetFromUTC:
1363 m_offset = m_datetime.utcOffset() / 60;
1364 m_timespec = QString("%1%2:%3").arg(m_offset < 0 ? '-' : '+')
1365 .arg(abs(m_offset) / 60)
1366 .arg(abs(m_offset) % 60);
1372 Q_INVOKABLE QDate getDate() const { return m_datetime.date(); }
1373 Q_INVOKABLE QDateTime getDateTime() const { return m_datetime; }
1374 Q_INVOKABLE int getDateTimeOffset() const { return m_offset; }
1375 Q_INVOKABLE QString getTimeSpec() const { return m_timespec; }
1378 QDateTime m_datetime;
1383 void registerTypes();
1385 #endif // TESTTYPES_H