1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28#ifndef TESTTYPES_H
29#define TESTTYPES_H
30
31#include <QtCore/qobject.h>
32#include <QtQml/qqml.h>
33#include <QtQml/qqmlexpression.h>
34#include <QtCore/qpoint.h>
35#include <QtCore/qsize.h>
36#include <QtCore/qregularexpression.h>
37#include <QtQml/qqmllist.h>
38#include <QtCore/qrect.h>
39#include <QtGui/qcolor.h>
40#include <QtGui/qvector3d.h>
41#include <QtGui/QFont>
42#include <QtGui/QPixmap>
43#include <QtCore/qdatetime.h>
44#include <QtCore/qjsonarray.h>
45#include <QtCore/qjsonobject.h>
46#include <QtCore/qjsonvalue.h>
47#include <QtQml/qjsvalue.h>
48#include <QtQml/qqmlscriptstring.h>
49#include <QtQml/qqmlcomponent.h>
50#include <QtCore/QModelIndex>
51#include <QtCore/QPersistentModelIndex>
52#include <QtCore/QItemSelection>
53
54#include <private/qqmlengine_p.h>
55#include <private/qv4qobjectwrapper_p.h>
56
57class MyQmlAttachedObject : public QObject
58{
59 Q_OBJECT
60 Q_PROPERTY(int value READ value CONSTANT)
61 Q_PROPERTY(int value2 READ value2 WRITE setValue2 NOTIFY value2Changed)
62public:
63 MyQmlAttachedObject(QObject *parent) : QObject(parent), m_value2(0) {}
64
65 int value() const { return 19; }
66 int value2() const { return m_value2; }
67 void setValue2(int v) { if (m_value2 == v) return; m_value2 = v; emit value2Changed(); }
68
69 void emitMySignal() { emit mySignal(); }
70
71signals:
72 void value2Changed();
73 void mySignal();
74
75private:
76 int m_value2;
77};
78
79class MyEnumContainer : public QObject
80{
81 Q_OBJECT
82 Q_ENUMS(RelatedEnum)
83
84public:
85 enum RelatedEnum { RelatedInvalid = -1, RelatedValue = 42, MultiplyDefined = 666 };
86};
87
88class MyQmlObject : public QObject
89{
90 Q_OBJECT
91 Q_ENUMS(MyEnum)
92 Q_ENUMS(MyEnum2)
93 Q_ENUMS(MyEnum3)
94 Q_ENUMS(MyEnumContainer::RelatedEnum)
95 Q_PROPERTY(int deleteOnSet READ deleteOnSet WRITE setDeleteOnSet)
96 Q_PROPERTY(bool trueProperty READ trueProperty CONSTANT)
97 Q_PROPERTY(bool falseProperty READ falseProperty CONSTANT)
98 Q_PROPERTY(int value READ value WRITE setValue)
99 Q_PROPERTY(int console READ console CONSTANT)
100 Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty NOTIFY stringChanged)
101 Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty NOTIFY urlChanged)
102 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty NOTIFY objectChanged)
103 Q_PROPERTY(QQmlListProperty<QObject> objectListProperty READ objectListProperty CONSTANT)
104 Q_PROPERTY(int resettableProperty READ resettableProperty WRITE setResettableProperty RESET resetProperty)
105 Q_PROPERTY(QRegExp regExp READ regExp WRITE setRegExp)
106 Q_PROPERTY(QRegularExpression regularExpression READ regularExpression WRITE setRegularExpression)
107 Q_PROPERTY(int nonscriptable READ nonscriptable WRITE setNonscriptable SCRIPTABLE false)
108 Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty NOTIFY intChanged)
109 Q_PROPERTY(QJSValue qjsvalue READ qjsvalue WRITE setQJSValue NOTIFY qjsvalueChanged)
110 Q_PROPERTY(QJSValue qjsvalueWithReset READ qjsvalue WRITE setQJSValue RESET resetQJSValue NOTIFY qjsvalueChanged)
111 Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty)
112 Q_PROPERTY(MyEnumContainer::RelatedEnum relatedEnumProperty READ relatedEnumProperty WRITE setRelatedEnumProperty)
113 Q_PROPERTY(MyEnumContainer::RelatedEnum unrelatedEnumProperty READ unrelatedEnumProperty WRITE setUnrelatedEnumProperty)
114 Q_PROPERTY(MyEnum qtEnumProperty READ qtEnumProperty WRITE setQtEnumProperty)
115
116public:
117 MyQmlObject(): myinvokableObject(0), m_methodCalled(false), m_methodIntCalled(false), m_object(0), m_value(0), m_resetProperty(13), m_intProperty(0), m_buttons(0) {}
118
119 enum MyEnum { EnumValue1 = 0, EnumValue2 = 1 };
120 enum MyEnum2 { EnumValue3 = 2, EnumValue4 = 3, EnumValue5 = -1 };
121 enum MyEnum3 { MultiplyDefined = 333 };
122
123 bool trueProperty() const { return true; }
124 bool falseProperty() const { return false; }
125
126 QString stringProperty() const { return m_string; }
127 void setStringProperty(const QString &s)
128 {
129 if (s == m_string)
130 return;
131 m_string = s;
132 emit stringChanged();
133 }
134
135 QUrl urlProperty() const { return m_url; }
136 void setUrlProperty(const QUrl &url)
137 {
138 if (url == m_url)
139 return;
140 m_url = url;
141 emit urlChanged();
142 }
143
144 QObject *objectProperty() const { return m_object; }
145 void setObjectProperty(QObject *obj) {
146 if (obj == m_object)
147 return;
148 m_object = obj;
149 emit objectChanged();
150 }
151
152 QQmlListProperty<QObject> objectListProperty() { return QQmlListProperty<QObject>(this, m_objectQList); }
153
154 bool methodCalled() const { return m_methodCalled; }
155 bool methodIntCalled() const { return m_methodIntCalled; }
156
157 QString string() const { return m_string; }
158
159 static MyQmlAttachedObject *qmlAttachedProperties(QObject *o) {
160 return new MyQmlAttachedObject(o);
161 }
162
163 int deleteOnSet() const { return 1; }
164 void setDeleteOnSet(int v) { if(v) delete this; }
165
166 int value() const { return m_value; }
167 void setValue(int v) { m_value = v; }
168
169 int resettableProperty() const { return m_resetProperty; }
170 void setResettableProperty(int v) { m_resetProperty = v; }
171 void resetProperty() { m_resetProperty = 13; }
172
173 QRegExp regExp() { return m_regExp; }
174 void setRegExp(const QRegExp &regExp) { m_regExp = regExp; }
175
176 QRegularExpression regularExpression() { return m_regularExpression; }
177 void setRegularExpression(const QRegularExpression &regularExpression)
178 {
179 m_regularExpression = regularExpression;
180 }
181
182 int console() const { return 11; }
183
184 int nonscriptable() const { return 0; }
185 void setNonscriptable(int) {}
186
187 MyQmlObject *myinvokableObject;
188 Q_INVOKABLE MyQmlObject *returnme() { return this; }
189
190 struct MyType {
191 int value;
192 };
193 struct MyOtherType {
194 int value;
195 };
196 QVariant variant() const { return m_variant; }
197 QJSValue qjsvalue() const { return m_qjsvalue; }
198 void setQJSValue(const QJSValue &value) { m_qjsvalue = value; emit qjsvalueChanged(); }
199 void resetQJSValue() { m_qjsvalue = QJSValue(QLatin1String("Reset!")); emit qjsvalueChanged(); }
200
201 Qt::MouseButtons buttons() const { return m_buttons; }
202
203 int intProperty() const { return m_intProperty; }
204 void setIntProperty(int i) { m_intProperty = i; emit intChanged(); }
205
206 Q_INVOKABLE MyEnum2 getEnumValue() const { return EnumValue4; }
207
208 MyEnum enumPropertyValue;
209 MyEnum enumProperty() const {
210 return enumPropertyValue;
211 }
212 void setEnumProperty(MyEnum v) {
213 enumPropertyValue = v;
214 }
215
216 MyEnumContainer::RelatedEnum relatedEnumPropertyValue;
217 MyEnumContainer::RelatedEnum relatedEnumProperty() const {
218 return relatedEnumPropertyValue;
219 }
220 void setRelatedEnumProperty(MyEnumContainer::RelatedEnum v) {
221 relatedEnumPropertyValue = v;
222 }
223
224 MyEnumContainer::RelatedEnum unrelatedEnumPropertyValue;
225 MyEnumContainer::RelatedEnum unrelatedEnumProperty() const {
226 return unrelatedEnumPropertyValue;
227 }
228 void setUnrelatedEnumProperty(MyEnumContainer::RelatedEnum v) {
229 unrelatedEnumPropertyValue = v;
230 }
231
232 MyEnum qtEnumPropertyValue;
233 MyEnum qtEnumProperty() const {
234 return qtEnumPropertyValue;
235 }
236 void setQtEnumProperty(MyEnum v) {
237 qtEnumPropertyValue = v;
238 }
239
240signals:
241 void basicSignal();
242 void argumentSignal(int a, QString b, qreal c, MyEnum2 d, Qt::MouseButtons e);
243 void unnamedArgumentSignal(int a, qreal, QString c);
244 void stringChanged();
245 void urlChanged();
246 void objectChanged();
247 void anotherBasicSignal();
248 void thirdBasicSignal();
249 void signalWithUnknownType(const MyQmlObject::MyType &arg);
250 void signalWithCompletelyUnknownType(const MyQmlObject::MyOtherType &arg);
251 void signalWithVariant(const QVariant &arg);
252 void signalWithQJSValue(const QJSValue &arg);
253 void signalWithGlobalName(int parseInt);
254 void intChanged();
255 void qjsvalueChanged();
256 void qjsValueEmittingSignal(QJSValue value);
257
258public slots:
259 void deleteMe() { delete this; }
260 void methodNoArgs() { m_methodCalled = true; }
261 void method(int a) { if(a == 163) m_methodIntCalled = true; }
262 void setString(const QString &s) { m_string = s; }
263 void myinvokable(MyQmlObject *o) { myinvokableObject = o; }
264 void variantMethod(const QVariant &v) { m_variant = v; }
265 void qjsvalueMethod(const QJSValue &v) { m_qjsvalue = v; }
266 void v8function(QQmlV4Function*);
267 void registeredFlagMethod(Qt::MouseButtons v) { m_buttons = v; }
268 QString slotWithReturnValue(const QString &arg) { return arg; }
269
270private:
271 friend class tst_qqmlecmascript;
272 bool m_methodCalled;
273 bool m_methodIntCalled;
274
275 QObject *m_object;
276 QString m_string;
277 QUrl m_url;
278 QList<QObject *> m_objectQList;
279 int m_value;
280 int m_resetProperty;
281 QRegExp m_regExp;
282 QRegularExpression m_regularExpression;
283 QVariant m_variant;
284 QJSValue m_qjsvalue;
285 int m_intProperty;
286 Qt::MouseButtons m_buttons;
287};
288Q_DECLARE_METATYPE(QQmlListProperty<MyQmlObject>)
289
290QML_DECLARE_TYPEINFO(MyQmlObject, QML_HAS_ATTACHED_PROPERTIES)
291
292class MyInheritedQmlObject : public MyQmlObject
293{
294 Q_OBJECT
295 Q_PROPERTY(MyInheritedQmlObject *myInheritedQmlObjectProperty READ myInheritedQmlObject WRITE setMyInheritedQmlObject)
296 Q_PROPERTY(MyQmlObject *myQmlObjectProperty READ myQmlObject WRITE setMyQmlObject)
297 Q_PROPERTY(QObject *qobjectProperty READ qobject WRITE setQObject)
298public:
299 MyInheritedQmlObject() : m_myInheritedQmlObject(0), m_myQmlObject(0), m_qobject(0) {}
300
301 MyInheritedQmlObject *myInheritedQmlObject() const { return m_myInheritedQmlObject; }
302 void setMyInheritedQmlObject(MyInheritedQmlObject * o) { m_myInheritedQmlObject = o; }
303
304 MyQmlObject *myQmlObject() const { return m_myQmlObject; }
305 void setMyQmlObject(MyQmlObject * o) { m_myQmlObject = o; }
306
307 QObject *qobject() const { return m_qobject; }
308 void setQObject(QObject * o) { m_qobject = o; }
309
310 Q_INVOKABLE bool isItYouQObject(QObject *o);
311 Q_INVOKABLE bool isItYouMyQmlObject(MyQmlObject *o);
312 Q_INVOKABLE bool isItYouMyInheritedQmlObject(MyInheritedQmlObject *o);
313private:
314 MyInheritedQmlObject *m_myInheritedQmlObject;
315 MyQmlObject *m_myQmlObject;
316 QObject *m_qobject;
317};
318QML_DECLARE_TYPE(MyInheritedQmlObject)
319
320class MyQmlContainer : public QObject
321{
322 Q_OBJECT
323 Q_PROPERTY(QQmlListProperty<MyQmlObject> children READ children CONSTANT)
324public:
325 MyQmlContainer() {}
326
327 QQmlListProperty<MyQmlObject> children() { return QQmlListProperty<MyQmlObject>(this, m_children); }
328
329private:
330 QList<MyQmlObject*> m_children;
331};
332
333
334class MyExpression : public QQmlExpression
335{
336 Q_OBJECT
337public:
338 MyExpression(QQmlContext *ctxt, const QString &expr)
339 : QQmlExpression(ctxt, 0, expr), changed(false)
340 {
341 QObject::connect(sender: this, SIGNAL(valueChanged()), receiver: this, SLOT(expressionValueChanged()));
342 setNotifyOnValueChanged(true);
343 }
344
345 bool changed;
346
347public slots:
348 void expressionValueChanged() {
349 changed = true;
350 }
351};
352
353
354class MyDefaultObject1 : public QObject
355{
356 Q_OBJECT
357 Q_PROPERTY(int horseLegs READ horseLegs CONSTANT)
358 Q_PROPERTY(int antLegs READ antLegs CONSTANT)
359 Q_PROPERTY(int emuLegs READ emuLegs CONSTANT)
360public:
361 int horseLegs() const { return 4; }
362 int antLegs() const { return 6; }
363 int emuLegs() const { return 2; }
364};
365
366class MyDefaultObject3 : public QObject
367{
368 Q_OBJECT
369 Q_PROPERTY(int antLegs READ antLegs CONSTANT)
370 Q_PROPERTY(int humanLegs READ humanLegs CONSTANT)
371public:
372 int antLegs() const { return 7; } // Mutant
373 int humanLegs() const { return 2; }
374 int millipedeLegs() const { return 1000; }
375};
376
377class MyDeferredObject : public QObject
378{
379 Q_OBJECT
380 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
381 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
382 Q_PROPERTY(QObject *objectProperty2 READ objectProperty2 WRITE setObjectProperty2)
383 Q_CLASSINFO("DeferredPropertyNames", "value,objectProperty,objectProperty2")
384
385public:
386 MyDeferredObject() : m_value(0), m_object(0), m_object2(0) {}
387
388 int value() const { return m_value; }
389 void setValue(int v) { m_value = v; emit valueChanged(); }
390
391 QObject *objectProperty() const { return m_object; }
392 void setObjectProperty(QObject *obj) { m_object = obj; }
393
394 QObject *objectProperty2() const { return m_object2; }
395 void setObjectProperty2(QObject *obj) { m_object2 = obj; }
396
397signals:
398 void valueChanged();
399
400private:
401 int m_value;
402 QObject *m_object;
403 QObject *m_object2;
404};
405
406class MyVeryDeferredObject : public QObject
407{
408 Q_OBJECT
409 //For inDestruction test
410 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
411 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
412 Q_CLASSINFO("DeferredPropertyNames", "objectProperty")
413
414public:
415 MyVeryDeferredObject() : m_value(0), m_object(0) {}
416 ~MyVeryDeferredObject() {
417 qmlExecuteDeferred(this); //Not a realistic case, see QTBUG-33112 to see how this could happen in practice
418 }
419
420 int value() const { return m_value; }
421 void setValue(int v) { m_value = v; emit valueChanged(); }
422
423 QObject *objectProperty() const { return m_object; }
424 void setObjectProperty(QObject *obj) { m_object = obj; }
425
426signals:
427 void valueChanged();
428
429private:
430 int m_value;
431 QObject *m_object;
432};
433
434class MyBaseExtendedObject : public QObject
435{
436Q_OBJECT
437Q_PROPERTY(int baseProperty READ baseProperty WRITE setBaseProperty)
438public:
439 MyBaseExtendedObject() : m_value(0) {}
440
441 int baseProperty() const { return m_value; }
442 void setBaseProperty(int v) { m_value = v; }
443
444private:
445 int m_value;
446};
447
448class MyExtendedObject : public MyBaseExtendedObject
449{
450Q_OBJECT
451Q_PROPERTY(int coreProperty READ coreProperty WRITE setCoreProperty)
452public:
453 MyExtendedObject() : m_value(0) {}
454
455 int coreProperty() const { return m_value; }
456 void setCoreProperty(int v) { m_value = v; }
457
458private:
459 int m_value;
460};
461
462class MyTypeObject : public QObject
463{
464 Q_OBJECT
465 Q_ENUMS(MyEnum)
466 Q_ENUMS(MyEnumContainer::RelatedEnum)
467 Q_FLAGS(MyFlags)
468
469 Q_PROPERTY(QString id READ id WRITE setId)
470 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
471 Q_PROPERTY(QQmlComponent *componentProperty READ componentProperty WRITE setComponentProperty)
472 Q_PROPERTY(MyFlags flagProperty READ flagProperty WRITE setFlagProperty)
473 Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty)
474 Q_PROPERTY(MyEnumContainer::RelatedEnum relatedEnumProperty READ relatedEnumProperty WRITE setRelatedEnumProperty)
475 Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty)
476 Q_PROPERTY(uint uintProperty READ uintProperty WRITE setUintProperty)
477 Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty)
478 Q_PROPERTY(qreal realProperty READ realProperty WRITE setRealProperty)
479 Q_PROPERTY(double doubleProperty READ doubleProperty WRITE setDoubleProperty)
480 Q_PROPERTY(float floatProperty READ floatProperty WRITE setFloatProperty)
481 Q_PROPERTY(QColor colorProperty READ colorProperty WRITE setColorProperty)
482 Q_PROPERTY(QDate dateProperty READ dateProperty WRITE setDateProperty)
483 Q_PROPERTY(QTime timeProperty READ timeProperty WRITE setTimeProperty)
484 Q_PROPERTY(QDateTime dateTimeProperty READ dateTimeProperty WRITE setDateTimeProperty)
485 Q_PROPERTY(QDateTime dateTimeProperty2 READ dateTimeProperty2 WRITE setDateTimeProperty2)
486 Q_PROPERTY(QPoint pointProperty READ pointProperty WRITE setPointProperty)
487 Q_PROPERTY(QPointF pointFProperty READ pointFProperty WRITE setPointFProperty)
488 Q_PROPERTY(QSize sizeProperty READ sizeProperty WRITE setSizeProperty)
489 Q_PROPERTY(QSizeF sizeFProperty READ sizeFProperty WRITE setSizeFProperty)
490 Q_PROPERTY(QRect rectProperty READ rectProperty WRITE setRectProperty NOTIFY rectPropertyChanged)
491 Q_PROPERTY(QRect rectProperty2 READ rectProperty2 WRITE setRectProperty2)
492 Q_PROPERTY(QRectF rectFProperty READ rectFProperty WRITE setRectFProperty)
493 Q_PROPERTY(bool boolProperty READ boolProperty WRITE setBoolProperty)
494 Q_PROPERTY(QVariant variantProperty READ variantProperty WRITE setVariantProperty)
495 Q_PROPERTY(QVector3D vectorProperty READ vectorProperty WRITE setVectorProperty)
496 Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty)
497
498 Q_PROPERTY(QQmlScriptString scriptProperty READ scriptProperty WRITE setScriptProperty)
499
500public:
501 MyTypeObject()
502 : objectPropertyValue(0), componentPropertyValue(0) {}
503
504 QString idValue;
505 QString id() const {
506 return idValue;
507 }
508 void setId(const QString &v) {
509 idValue = v;
510 }
511
512 QObject *objectPropertyValue;
513 QObject *objectProperty() const {
514 return objectPropertyValue;
515 }
516 void setObjectProperty(QObject *v) {
517 objectPropertyValue = v;
518 }
519
520 QQmlComponent *componentPropertyValue;
521 QQmlComponent *componentProperty() const {
522 return componentPropertyValue;
523 }
524 void setComponentProperty(QQmlComponent *v) {
525 componentPropertyValue = v;
526 }
527
528 enum MyFlag { FlagVal1 = 0x01, FlagVal2 = 0x02, FlagVal3 = 0x04 };
529 Q_DECLARE_FLAGS(MyFlags, MyFlag)
530 MyFlags flagPropertyValue;
531 MyFlags flagProperty() const {
532 return flagPropertyValue;
533 }
534 void setFlagProperty(MyFlags v) {
535 flagPropertyValue = v;
536 }
537
538 enum MyEnum { EnumVal1, EnumVal2 };
539 MyEnum enumPropertyValue;
540 MyEnum enumProperty() const {
541 return enumPropertyValue;
542 }
543 void setEnumProperty(MyEnum v) {
544 enumPropertyValue = v;
545 }
546
547 MyEnumContainer::RelatedEnum relatedEnumPropertyValue;
548 MyEnumContainer::RelatedEnum relatedEnumProperty() const {
549 return relatedEnumPropertyValue;
550 }
551 void setRelatedEnumProperty(MyEnumContainer::RelatedEnum v) {
552 relatedEnumPropertyValue = v;
553 }
554
555 QString stringPropertyValue;
556 QString stringProperty() const {
557 return stringPropertyValue;
558 }
559 void setStringProperty(const QString &v) {
560 stringPropertyValue = v;
561 }
562
563 uint uintPropertyValue;
564 uint uintProperty() const {
565 return uintPropertyValue;
566 }
567 void setUintProperty(const uint &v) {
568 uintPropertyValue = v;
569 }
570
571 int intPropertyValue;
572 int intProperty() const {
573 return intPropertyValue;
574 }
575 void setIntProperty(const int &v) {
576 intPropertyValue = v;
577 }
578
579 qreal realPropertyValue;
580 qreal realProperty() const {
581 return realPropertyValue;
582 }
583 void setRealProperty(const qreal &v) {
584 realPropertyValue = v;
585 }
586
587 double doublePropertyValue;
588 double doubleProperty() const {
589 return doublePropertyValue;
590 }
591 void setDoubleProperty(const double &v) {
592 doublePropertyValue = v;
593 }
594
595 float floatPropertyValue;
596 float floatProperty() const {
597 return floatPropertyValue;
598 }
599 void setFloatProperty(const float &v) {
600 floatPropertyValue = v;
601 }
602
603 QColor colorPropertyValue;
604 QColor colorProperty() const {
605 return colorPropertyValue;
606 }
607 void setColorProperty(const QColor &v) {
608 colorPropertyValue = v;
609 }
610
611 QDate datePropertyValue;
612 QDate dateProperty() const {
613 return datePropertyValue;
614 }
615 void setDateProperty(const QDate &v) {
616 datePropertyValue = v;
617 }
618
619 QTime timePropertyValue;
620 QTime timeProperty() const {
621 return timePropertyValue;
622 }
623 void setTimeProperty(const QTime &v) {
624 timePropertyValue = v;
625 }
626
627 QDateTime dateTimePropertyValue;
628 QDateTime dateTimeProperty() const {
629 return dateTimePropertyValue;
630 }
631 void setDateTimeProperty(const QDateTime &v) {
632 dateTimePropertyValue = v;
633 }
634
635 QDateTime dateTimePropertyValue2;
636 QDateTime dateTimeProperty2() const {
637 return dateTimePropertyValue2;
638 }
639 void setDateTimeProperty2(const QDateTime &v) {
640 dateTimePropertyValue2 = v;
641 }
642
643 QPoint pointPropertyValue;
644 QPoint pointProperty() const {
645 return pointPropertyValue;
646 }
647 void setPointProperty(const QPoint &v) {
648 pointPropertyValue = v;
649 }
650
651 QPointF pointFPropertyValue;
652 QPointF pointFProperty() const {
653 return pointFPropertyValue;
654 }
655 void setPointFProperty(const QPointF &v) {
656 pointFPropertyValue = v;
657 }
658
659 QSize sizePropertyValue;
660 QSize sizeProperty() const {
661 return sizePropertyValue;
662 }
663 void setSizeProperty(const QSize &v) {
664 sizePropertyValue = v;
665 }
666
667 QSizeF sizeFPropertyValue;
668 QSizeF sizeFProperty() const {
669 return sizeFPropertyValue;
670 }
671 void setSizeFProperty(const QSizeF &v) {
672 sizeFPropertyValue = v;
673 }
674
675 QRect rectPropertyValue;
676 QRect rectProperty() const {
677 return rectPropertyValue;
678 }
679 void setRectProperty(const QRect &v) {
680 rectPropertyValue = v;
681 emit rectPropertyChanged();
682 }
683
684 QRect rectPropertyValue2;
685 QRect rectProperty2() const {
686 return rectPropertyValue2;
687 }
688 void setRectProperty2(const QRect &v) {
689 rectPropertyValue2 = v;
690 }
691
692 QRectF rectFPropertyValue;
693 QRectF rectFProperty() const {
694 return rectFPropertyValue;
695 }
696 void setRectFProperty(const QRectF &v) {
697 rectFPropertyValue = v;
698 }
699
700 bool boolPropertyValue;
701 bool boolProperty() const {
702 return boolPropertyValue;
703 }
704 void setBoolProperty(const bool &v) {
705 boolPropertyValue = v;
706 }
707
708 QVariant variantPropertyValue;
709 QVariant variantProperty() const {
710 return variantPropertyValue;
711 }
712 void setVariantProperty(const QVariant &v) {
713 variantPropertyValue = v;
714 }
715
716 QVector3D vectorPropertyValue;
717 QVector3D vectorProperty() const {
718 return vectorPropertyValue;
719 }
720 void setVectorProperty(const QVector3D &v) {
721 vectorPropertyValue = v;
722 }
723
724 QUrl urlPropertyValue;
725 QUrl urlProperty() const {
726 return urlPropertyValue;
727 }
728 void setUrlProperty(const QUrl &v) {
729 urlPropertyValue = v;
730 }
731
732 QQmlScriptString scriptPropertyValue;
733 QQmlScriptString scriptProperty() const {
734 return scriptPropertyValue;
735 }
736 void setScriptProperty(const QQmlScriptString &v) {
737 scriptPropertyValue = v;
738 }
739
740 void doAction() { emit action(); }
741signals:
742 void action();
743 void rectPropertyChanged();
744};
745Q_DECLARE_OPERATORS_FOR_FLAGS(MyTypeObject::MyFlags)
746
747class MyDerivedObject : public MyTypeObject
748{
749 Q_OBJECT
750public:
751 Q_INVOKABLE bool intProperty() const {
752 return true;
753 }
754};
755
756class MyInvokableBaseObject : public QObject
757{
758 Q_OBJECT
759public:
760 inline ~MyInvokableBaseObject() = 0;
761
762 Q_INVOKABLE inline void method_inherited(int a);
763 Q_INVOKABLE inline void method_overload();
764};
765
766struct NonRegisteredType
767{
768
769};
770
771class MyInvokableObject : public MyInvokableBaseObject
772{
773 Q_OBJECT
774 Q_ENUMS(TestEnum)
775public:
776 enum TestEnum { EnumValue1, EnumValue2 };
777 MyInvokableObject() { reset(); }
778
779 int invoked() const { return m_invoked; }
780 bool error() const { return m_invokedError; }
781 const QVariantList &actuals() const { return m_actuals; }
782 void reset() { m_invoked = -1; m_invokedError = false; m_actuals.clear(); }
783
784 Q_INVOKABLE QPointF method_get_QPointF() { return QPointF(99.3, -10.2); }
785 Q_INVOKABLE QPoint method_get_QPoint() { return QPoint(9, 12); }
786
787 Q_INVOKABLE void method_NoArgs() { invoke(idx: 0); }
788 Q_INVOKABLE int method_NoArgs_int() { invoke(idx: 1); return 6; }
789 Q_INVOKABLE qreal method_NoArgs_real() { invoke(idx: 2); return 19.75; }
790 Q_INVOKABLE QPointF method_NoArgs_QPointF() { invoke(idx: 3); return QPointF(123, 4.5); }
791 Q_INVOKABLE QObject *method_NoArgs_QObject() { invoke(idx: 4); return this; }
792 Q_INVOKABLE MyInvokableObject *method_NoArgs_unknown() { invoke(idx: 5); return this; }
793 Q_INVOKABLE QJSValue method_NoArgs_QScriptValue() { invoke(idx: 6); return QJSValue("Hello world"); }
794 Q_INVOKABLE QVariant method_NoArgs_QVariant() { invoke(idx: 7); return QVariant("QML rocks"); }
795
796 Q_INVOKABLE void method_int(int a) { invoke(idx: 8); m_actuals << a; }
797 Q_INVOKABLE void method_intint(int a, int b) { invoke(idx: 9); m_actuals << a << b; }
798 Q_INVOKABLE void method_real(qreal a) { invoke(idx: 10); m_actuals << a; }
799 Q_INVOKABLE void method_QString(QString a) { invoke(idx: 11); m_actuals << a; }
800 Q_INVOKABLE void method_QPointF(QPointF a) { invoke(idx: 12); m_actuals << a; }
801 Q_INVOKABLE void method_QObject(QObject *a) { invoke(idx: 13); m_actuals << QVariant::fromValue(value: a); }
802 Q_INVOKABLE void method_QScriptValue(QJSValue a) { invoke(idx: 14); m_actuals << QVariant::fromValue(value: a); }
803 Q_INVOKABLE void method_intQScriptValue(int a, QJSValue b) { invoke(idx: 15); m_actuals << a << QVariant::fromValue(value: b); }
804 Q_INVOKABLE void method_QByteArray(QByteArray value) { invoke(idx: 29); m_actuals << value; }
805 Q_INVOKABLE QJSValue method_intQJSValue(int a, QJSValue b) { invoke(idx: 30); m_actuals << a << QVariant::fromValue(value: b); return b.call(); }
806 Q_INVOKABLE QJSValue method_intQJSValue(int a, int b) { m_actuals << a << b; return QJSValue();} // Should never be called.
807
808 Q_INVOKABLE void method_overload(int a) { invoke(idx: 16); m_actuals << a; }
809 Q_INVOKABLE void method_overload(int a, int b) { invoke(idx: 17); m_actuals << a << b; }
810 Q_INVOKABLE void method_overload(QString a) { invoke(idx: 18); m_actuals << a; }
811
812 Q_INVOKABLE void method_with_enum(TestEnum e) { invoke(idx: 19); m_actuals << (int)e; }
813
814 Q_INVOKABLE int method_default(int a, int b = 19) { invoke(idx: 20); m_actuals << a << b; return b; }
815
816 Q_INVOKABLE void method_QVariant(QVariant a, QVariant b = QVariant()) { invoke(idx: 21); m_actuals << a << b; }
817
818 Q_INVOKABLE void method_QJsonObject(const QJsonObject &a) { invoke(idx: 22); m_actuals << QVariant::fromValue(value: a); }
819 Q_INVOKABLE void method_QJsonArray(const QJsonArray &a) { invoke(idx: 23); m_actuals << QVariant::fromValue(value: a); }
820 Q_INVOKABLE void method_QJsonValue(const QJsonValue &a) { invoke(idx: 24); m_actuals << QVariant::fromValue(value: a); }
821
822 Q_INVOKABLE void method_overload(const QJsonObject &a) { invoke(idx: 25); m_actuals << QVariant::fromValue(value: a); }
823 Q_INVOKABLE void method_overload(const QJsonArray &a) { invoke(idx: 26); m_actuals << QVariant::fromValue(value: a); }
824 Q_INVOKABLE void method_overload(const QJsonValue &a) { invoke(idx: 27); m_actuals << QVariant::fromValue(value: a); }
825
826 Q_INVOKABLE void method_unknown(NonRegisteredType) { invoke(idx: 28); }
827
828private:
829 friend class MyInvokableBaseObject;
830 void invoke(int idx) { if (m_invoked != -1) m_invokedError = true; m_invoked = idx;}
831 int m_invoked;
832 bool m_invokedError;
833 QVariantList m_actuals;
834};
835
836MyInvokableBaseObject::~MyInvokableBaseObject() {}
837
838void MyInvokableBaseObject::method_inherited(int a)
839{
840 static_cast<MyInvokableObject *>(this)->invoke(idx: -3);
841 static_cast<MyInvokableObject *>(this)->m_actuals << a;
842}
843
844// This is a hidden overload of the MyInvokableObject::method_overload() method
845void MyInvokableBaseObject::method_overload()
846{
847 static_cast<MyInvokableObject *>(this)->invoke(idx: -2);
848}
849
850class NumberAssignment : public QObject
851{
852 Q_OBJECT
853public:
854 Q_PROPERTY(qreal test1 READ test1 WRITE setTest1)
855 qreal _test1;
856 qreal test1() const { return _test1; }
857 void setTest1(qreal v) { _test1 = v; }
858
859 Q_PROPERTY(qreal test2 READ test2 WRITE setTest2)
860 qreal _test2;
861 qreal test2() const { return _test2; }
862 void setTest2(qreal v) { _test2 = v; }
863
864 Q_PROPERTY(qreal test3 READ test3 WRITE setTest3)
865 qreal _test3;
866 qreal test3() const { return _test3; }
867 void setTest3(qreal v) { _test3 = v; }
868
869 Q_PROPERTY(qreal test4 READ test4 WRITE setTest4)
870 qreal _test4;
871 qreal test4() const { return _test4; }
872 void setTest4(qreal v) { _test4 = v; }
873
874 Q_PROPERTY(int test5 READ test5 WRITE setTest5)
875 int _test5;
876 int test5() const { return _test5; }
877 void setTest5(int v) { _test5 = v; }
878
879 Q_PROPERTY(int test6 READ test6 WRITE setTest6)
880 int _test6;
881 int test6() const { return _test6; }
882 void setTest6(int v) { _test6 = v; }
883
884 Q_PROPERTY(int test7 READ test7 WRITE setTest7)
885 int _test7;
886 int test7() const { return _test7; }
887 void setTest7(int v) { _test7 = v; }
888
889 Q_PROPERTY(int test8 READ test8 WRITE setTest8)
890 int _test8;
891 int test8() const { return _test8; }
892 void setTest8(int v) { _test8 = v; }
893
894 Q_PROPERTY(unsigned int test9 READ test9 WRITE setTest9)
895 unsigned int _test9;
896 unsigned int test9() const { return _test9; }
897 void setTest9(unsigned int v) { _test9 = v; }
898
899 Q_PROPERTY(unsigned int test10 READ test10 WRITE setTest10)
900 unsigned int _test10;
901 unsigned int test10() const { return _test10; }
902 void setTest10(unsigned int v) { _test10 = v; }
903
904 Q_PROPERTY(unsigned int test11 READ test11 WRITE setTest11)
905 unsigned int _test11;
906 unsigned int test11() const { return _test11; }
907 void setTest11(unsigned int v) { _test11 = v; }
908
909 Q_PROPERTY(unsigned int test12 READ test12 WRITE setTest12)
910 unsigned int _test12;
911 unsigned int test12() const { return _test12; }
912 void setTest12(unsigned int v) { _test12 = v; }
913};
914
915class DefaultPropertyExtendedObject : public QObject
916{
917 Q_OBJECT
918 Q_PROPERTY(QObject *firstProperty READ firstProperty WRITE setFirstProperty)
919 Q_PROPERTY(QObject *secondProperty READ secondProperty WRITE setSecondProperty)
920public:
921 DefaultPropertyExtendedObject(QObject *parent = 0) : QObject(parent), m_firstProperty(0), m_secondProperty(0) {}
922
923 QObject *firstProperty() const { return m_firstProperty; }
924 QObject *secondProperty() const { return m_secondProperty; }
925 void setFirstProperty(QObject *property) { m_firstProperty = property; }
926 void setSecondProperty(QObject *property) { m_secondProperty = property; }
927private:
928 QObject* m_firstProperty;
929 QObject* m_secondProperty;
930};
931
932class OverrideDefaultPropertyObject : public DefaultPropertyExtendedObject
933{
934 Q_OBJECT
935 Q_CLASSINFO("DefaultProperty", "secondProperty")
936public:
937 OverrideDefaultPropertyObject() {}
938};
939
940class MyRevisionedBaseClassRegistered : public QObject
941{
942Q_OBJECT
943 Q_PROPERTY(qreal propA READ propA WRITE setPropA NOTIFY propAChanged)
944 Q_PROPERTY(qreal propB READ propB WRITE setPropB NOTIFY propBChanged REVISION 1)
945
946public:
947 MyRevisionedBaseClassRegistered() : m_pa(1), m_pb(2) {}
948
949 qreal propA() const { return m_pa; }
950 void setPropA(qreal p) {
951 if (p != m_pa) {
952 m_pa = p;
953 emit propAChanged();
954 }
955 }
956 qreal propB() const { return m_pb; }
957 void setPropB(qreal p) {
958 if (p != m_pb) {
959 m_pb = p;
960 emit propBChanged();
961 }
962 }
963
964 Q_INVOKABLE void methodA() { }
965 Q_INVOKABLE Q_REVISION(1) void methodB() { }
966
967signals:
968 void propAChanged();
969 void propBChanged();
970
971 void signalA();
972 Q_REVISION(1) void signalB();
973
974protected:
975 qreal m_pa;
976 qreal m_pb;
977};
978
979class MyRevisionedBaseClassUnregistered : public MyRevisionedBaseClassRegistered
980{
981Q_OBJECT
982 Q_PROPERTY(qreal propC READ propC WRITE setPropC NOTIFY propCChanged)
983 Q_PROPERTY(qreal propD READ propD WRITE setPropD NOTIFY propDChanged REVISION 1)
984
985public:
986 MyRevisionedBaseClassUnregistered() : m_pc(1), m_pd(2) {}
987
988 qreal propC() const { return m_pc; }
989 void setPropC(qreal p) {
990 if (p != m_pc) {
991 m_pc = p;
992 emit propCChanged();
993 }
994 }
995 qreal propD() const { return m_pd; }
996 void setPropD(qreal p) {
997 if (p != m_pd) {
998 m_pd = p;
999 emit propDChanged();
1000 }
1001 }
1002
1003 Q_INVOKABLE void methodC() { }
1004 Q_INVOKABLE Q_REVISION(1) void methodD() { }
1005
1006signals:
1007 void propCChanged();
1008 void propDChanged();
1009
1010 void signalC();
1011 Q_REVISION(1) void signalD();
1012
1013protected:
1014 qreal m_pc;
1015 qreal m_pd;
1016};
1017
1018class MyRevisionedClass : public MyRevisionedBaseClassUnregistered
1019{
1020 Q_OBJECT
1021 Q_PROPERTY(qreal prop1 READ prop1 WRITE setProp1 NOTIFY prop1Changed)
1022 Q_PROPERTY(qreal prop2 READ prop2 WRITE setProp2 NOTIFY prop2Changed REVISION 1)
1023
1024public:
1025 MyRevisionedClass() : m_p1(0), m_p2(0) {}
1026
1027 qreal prop1() const { return m_p1; }
1028 void setProp1(qreal p) {
1029 if (p != m_p1) {
1030 m_p1 = p;
1031 emit prop1Changed();
1032 }
1033 }
1034 qreal prop2() const { return m_p2; }
1035 void setProp2(qreal p) {
1036 if (p != m_p2) {
1037 m_p2 = p;
1038 emit prop2Changed();
1039 }
1040 }
1041
1042 Q_INVOKABLE void method1() { }
1043 Q_INVOKABLE Q_REVISION(1) void method2() { }
1044
1045signals:
1046 void prop1Changed();
1047 void prop2Changed();
1048
1049 void signal1();
1050 Q_REVISION(1) void signal2();
1051
1052protected:
1053 qreal m_p1;
1054 qreal m_p2;
1055};
1056
1057class MyRevisionedSubclass : public MyRevisionedClass
1058{
1059 Q_OBJECT
1060 Q_PROPERTY(qreal prop3 READ prop3 WRITE setProp3 NOTIFY prop3Changed)
1061 Q_PROPERTY(qreal prop4 READ prop4 WRITE setProp4 NOTIFY prop4Changed REVISION 1)
1062
1063public:
1064 MyRevisionedSubclass() : m_p3(3), m_p4(4) {}
1065
1066 qreal prop3() const { return m_p3; }
1067 void setProp3(qreal p) {
1068 if (p != m_p3) {
1069 m_p3 = p;
1070 emit prop3Changed();
1071 }
1072 }
1073 qreal prop4() const { return m_p4; }
1074 void setProp4(qreal p) {
1075 if (p != m_p4) {
1076 m_p4 = p;
1077 emit prop4Changed();
1078 }
1079 }
1080
1081 Q_INVOKABLE void method3() { }
1082 Q_INVOKABLE Q_REVISION(1) void method4() { }
1083
1084signals:
1085 void prop3Changed();
1086 void prop4Changed();
1087
1088 void signal3();
1089 Q_REVISION(1) void signal4();
1090
1091protected:
1092 qreal m_p3;
1093 qreal m_p4;
1094};
1095
1096
1097class MyItemUsingRevisionedObject : public QObject
1098{
1099 Q_OBJECT
1100 Q_PROPERTY(MyRevisionedClass *revisioned READ revisioned)
1101
1102public:
1103 MyItemUsingRevisionedObject() : m_revisioned (new MyRevisionedClass) {}
1104
1105 MyRevisionedClass *revisioned() const { return m_revisioned.get(); }
1106private:
1107 QScopedPointer<MyRevisionedClass> m_revisioned;
1108};
1109
1110QML_DECLARE_TYPE(MyRevisionedBaseClassRegistered)
1111QML_DECLARE_TYPE(MyRevisionedBaseClassUnregistered)
1112QML_DECLARE_TYPE(MyRevisionedClass)
1113QML_DECLARE_TYPE(MyRevisionedSubclass)
1114QML_DECLARE_TYPE(MyItemUsingRevisionedObject)
1115Q_DECLARE_METATYPE(MyQmlObject::MyType)
1116
1117
1118class ScarceResourceObject : public QObject
1119{
1120 Q_OBJECT
1121 Q_PROPERTY(QPixmap scarceResource READ scarceResource WRITE setScarceResource NOTIFY scarceResourceChanged)
1122public:
1123 ScarceResourceObject(QObject *parent = 0) : QObject(parent), m_value(100, 100) { m_value.fill(fillColor: Qt::blue); }
1124 ~ScarceResourceObject() {}
1125
1126 QPixmap scarceResource() const { return m_value; }
1127 void setScarceResource(QPixmap v) { m_value = v; emit scarceResourceChanged(); }
1128
1129 bool scarceResourceIsDetached() const { return m_value.isDetached(); }
1130
1131 // this particular one returns a new one each time
1132 // this means that every Scarce Resource Copy will
1133 // consume resources (so that we can track disposal
1134 // of v8 handles with circular references).
1135 Q_INVOKABLE QPixmap newScarceResource() const
1136 {
1137 QPixmap retn(800, 600);
1138 retn.fill(fillColor: QColor(100, 110, 120, 45));
1139 return retn;
1140 }
1141
1142signals:
1143 void scarceResourceChanged();
1144
1145private:
1146 QPixmap m_value;
1147};
1148QML_DECLARE_TYPE(ScarceResourceObject)
1149
1150class testQObjectApi : public QObject
1151{
1152 Q_OBJECT
1153 Q_ENUMS(MyEnum)
1154 Q_PROPERTY (int qobjectTestProperty READ qobjectTestProperty NOTIFY qobjectTestPropertyChanged FINAL)
1155 Q_PROPERTY (int qobjectTestWritableProperty READ qobjectTestWritableProperty WRITE setQObjectTestWritableProperty NOTIFY qobjectTestWritablePropertyChanged)
1156 Q_PROPERTY (int qobjectTestWritableFinalProperty READ qobjectTestWritableFinalProperty WRITE setQObjectTestWritableFinalProperty NOTIFY qobjectTestWritableFinalPropertyChanged FINAL)
1157
1158public:
1159 testQObjectApi(QObject* parent = 0)
1160 : QObject(parent), m_testProperty(0), m_testWritableProperty(0), m_testWritableFinalProperty(0), m_methodCallCount(0), m_trackedObject(0)
1161 {
1162 }
1163
1164 ~testQObjectApi() {}
1165
1166 enum MyEnum { EnumValue1 = 25, EnumValue2 = 42 };
1167 Q_INVOKABLE int qobjectEnumTestMethod(MyEnum val) { return (static_cast<int>(val) + 5); }
1168 Q_INVOKABLE int qobjectTestMethod(int increment = 1) { m_methodCallCount += increment; return m_methodCallCount; }
1169
1170 Q_INVOKABLE void trackObject(QObject *obj) { m_trackedObject = obj; }
1171 Q_INVOKABLE QObject *trackedObject() const { return m_trackedObject; }
1172 Q_INVOKABLE void setTrackedObjectProperty(const QString &propName) const { m_trackedObject->setProperty(qPrintable(propName), value: QVariant(5)); }
1173 Q_INVOKABLE QVariant trackedObjectProperty(const QString &propName) const { return m_trackedObject->property(qPrintable(propName)); }
1174
1175 Q_INVOKABLE void setSpecificProperty(QObject *obj, const QString & propName, const QVariant & v) const { obj->setProperty(qPrintable(propName), value: v); }
1176 Q_INVOKABLE void changeQObjectParent(QObject *obj) { obj->setParent(this); }
1177
1178 int qobjectTestProperty() const { return m_testProperty; }
1179 void setQObjectTestProperty(int tp) { m_testProperty = tp; emit qobjectTestPropertyChanged(testProperty: tp); }
1180
1181 int qobjectTestWritableProperty() const { return m_testWritableProperty; }
1182 void setQObjectTestWritableProperty(int tp) { m_testWritableProperty = tp; emit qobjectTestWritablePropertyChanged(testWritableProperty: tp); }
1183
1184 int qobjectTestWritableFinalProperty() const { return m_testWritableFinalProperty; }
1185 void setQObjectTestWritableFinalProperty(int tp) { m_testWritableFinalProperty = tp; emit qobjectTestWritableFinalPropertyChanged(); }
1186
1187 Q_INVOKABLE bool trackedObjectHasJsOwnership() {
1188 QObject * object = m_trackedObject;
1189
1190 if (!object)
1191 return false;
1192
1193 QQmlData *ddata = QQmlData::get(object, create: false);
1194 if (!ddata)
1195 return false;
1196 else
1197 return ddata->indestructible?false:true;
1198 }
1199
1200 Q_INVOKABLE void deleteQObject(QObject *obj) { delete obj; }
1201
1202signals:
1203 void qobjectTestPropertyChanged(int testProperty);
1204 void qobjectTestWritablePropertyChanged(int testWritableProperty);
1205 void qobjectTestWritableFinalPropertyChanged();
1206
1207private:
1208 int m_testProperty;
1209 int m_testWritableProperty;
1210 int m_testWritableFinalProperty;
1211 int m_methodCallCount;
1212 QObject *m_trackedObject;
1213};
1214
1215class testQObjectApiTwo : public QObject
1216{
1217 Q_OBJECT
1218 Q_PROPERTY(int twoTestProperty READ twoTestProperty WRITE setTwoTestProperty NOTIFY twoTestPropertyChanged)
1219
1220public:
1221 testQObjectApiTwo(QObject *parent = 0) : QObject(parent), m_ttp(42) {}
1222 ~testQObjectApiTwo() {}
1223
1224 void setTwoTestProperty(int v) { m_ttp = v; emit twoTestPropertyChanged(); }
1225 int twoTestProperty() const { return m_ttp; }
1226
1227signals:
1228 void twoTestPropertyChanged();
1229
1230private:
1231 int m_ttp;
1232};
1233
1234class testImportOrderApi : public QObject
1235{
1236 Q_OBJECT
1237
1238public:
1239 testImportOrderApi(int value, QObject *parent = 0) : QObject(parent), m_value(value) {}
1240
1241 Q_PROPERTY(int value READ value)
1242
1243 int value() const { return m_value; }
1244
1245private:
1246 int m_value;
1247};
1248
1249class CircularReferenceObject : public QObject
1250{
1251 Q_OBJECT
1252
1253public:
1254 CircularReferenceObject(QObject *parent = 0)
1255 : QObject(parent), m_dtorCount(0)
1256 {
1257 }
1258
1259 ~CircularReferenceObject()
1260 {
1261 if (m_dtorCount) *m_dtorCount = *m_dtorCount + 1;
1262 }
1263
1264 Q_INVOKABLE void setDtorCount(int *dtorCount)
1265 {
1266 m_dtorCount = dtorCount;
1267 }
1268
1269 Q_INVOKABLE CircularReferenceObject *generate(QObject *parent = 0)
1270 {
1271 CircularReferenceObject *retn = new CircularReferenceObject(parent);
1272 retn->m_dtorCount = m_dtorCount;
1273 return retn;
1274 }
1275
1276 Q_INVOKABLE void addReference(QObject *other)
1277 {
1278 QQmlData *ddata = QQmlData::get(object: this);
1279 Q_ASSERT(ddata);
1280 QV4::ExecutionEngine *v4 = ddata->jsWrapper.engine();
1281 Q_ASSERT(v4);
1282 QV4::Scope scope(v4);
1283 QV4::Scoped<QV4::QObjectWrapper> thisObject(scope, ddata->jsWrapper.value());
1284 Q_ASSERT(thisObject);
1285
1286 QQmlData *otherDData = QQmlData::get(object: other);
1287 Q_ASSERT(otherDData);
1288
1289 QV4::ScopedValue v(scope, otherDData->jsWrapper.value());
1290 thisObject->defineDefaultProperty(QStringLiteral("autoTestStrongRef"), value: v);
1291 }
1292
1293private:
1294 int *m_dtorCount;
1295};
1296Q_DECLARE_METATYPE(CircularReferenceObject*)
1297
1298class MyDynamicCreationDestructionObject : public QObject
1299{
1300 Q_OBJECT
1301 Q_PROPERTY (int intProperty READ intProperty WRITE setIntProperty NOTIFY intPropertyChanged)
1302
1303public:
1304 MyDynamicCreationDestructionObject(QObject *parent = 0) : QObject(parent), m_intProperty(0), m_dtorCount(0)
1305 {
1306 }
1307
1308 ~MyDynamicCreationDestructionObject()
1309 {
1310 if (m_dtorCount) {
1311 (*m_dtorCount)++;
1312 }
1313 }
1314
1315 int intProperty() const { return m_intProperty; }
1316 void setIntProperty(int val) { m_intProperty = val; emit intPropertyChanged(); }
1317
1318 Q_INVOKABLE MyDynamicCreationDestructionObject *createNew()
1319 {
1320 // no parent == ownership transfers to JS; same dtor counter.
1321 MyDynamicCreationDestructionObject *retn = new MyDynamicCreationDestructionObject;
1322 retn->setDtorCount(m_dtorCount);
1323 return retn;
1324 }
1325
1326 void setDtorCount(int *dtorCount)
1327 {
1328 m_dtorCount = dtorCount;
1329 }
1330
1331signals:
1332 void intPropertyChanged();
1333
1334private:
1335 int m_intProperty;
1336 int *m_dtorCount;
1337};
1338
1339class WriteCounter : public QObject
1340{
1341 Q_OBJECT
1342 Q_PROPERTY(int value READ value WRITE setValue);
1343public:
1344 WriteCounter() : m_value(0), m_count(0) {}
1345
1346 int value() const { return m_value; }
1347 void setValue(int v) { m_value = v; ++m_count; }
1348
1349 int count() const { return m_count; }
1350
1351private:
1352 int m_value;
1353 int m_count;
1354};
1355
1356class MySequenceConversionObject : public QObject
1357{
1358 Q_OBJECT
1359
1360 Q_PROPERTY (QList<int> intListProperty READ intListProperty WRITE setIntListProperty NOTIFY intListPropertyChanged)
1361 Q_PROPERTY (QList<int> intListProperty2 READ intListProperty2 WRITE setIntListProperty2 NOTIFY intListProperty2Changed)
1362 Q_PROPERTY (QList<qreal> qrealListProperty READ qrealListProperty WRITE setQrealListProperty NOTIFY qrealListPropertyChanged)
1363 Q_PROPERTY (QList<bool> boolListProperty READ boolListProperty WRITE setBoolListProperty NOTIFY boolListPropertyChanged)
1364 Q_PROPERTY (QList<QString> stringListProperty READ stringListProperty WRITE setStringListProperty NOTIFY stringListPropertyChanged)
1365 Q_PROPERTY (QList<QUrl> urlListProperty READ urlListProperty WRITE setUrlListProperty NOTIFY urlListPropertyChanged)
1366 Q_PROPERTY (QStringList qstringListProperty READ qstringListProperty WRITE setQStringListProperty NOTIFY qstringListPropertyChanged)
1367
1368 Q_PROPERTY (QList<QPoint> pointListProperty READ pointListProperty WRITE setPointListProperty NOTIFY pointListPropertyChanged)
1369 Q_PROPERTY (QList<NonRegisteredType> typeListProperty READ typeListProperty WRITE setTypeListProperty NOTIFY typeListPropertyChanged)
1370 Q_PROPERTY (QList<QVariant> variantListProperty READ variantListProperty WRITE setVariantListProperty NOTIFY variantListPropertyChanged)
1371
1372 Q_PROPERTY (qint32 maxIndex READ maxIndex CONSTANT)
1373 Q_PROPERTY (quint32 tooBigIndex READ tooBigIndex CONSTANT)
1374 Q_PROPERTY (qint32 negativeIndex READ negativeIndex CONSTANT)
1375
1376public:
1377 MySequenceConversionObject()
1378 {
1379 m_intList << 1 << 2 << 3 << 4;
1380 m_intList2 << 1 << 2 << 3 << 4;
1381 m_qrealList << 1.1 << 2.2 << 3.3 << 4.4;
1382 m_boolList << true << false << true << false;
1383 m_stringList << QLatin1String("first") << QLatin1String("second") << QLatin1String("third") << QLatin1String("fourth");
1384 m_urlList << QUrl("http://www.example1.com") << QUrl("http://www.example2.com") << QUrl("http://www.example3.com");
1385 m_qstringList << QLatin1String("first") << QLatin1String("second") << QLatin1String("third") << QLatin1String("fourth");
1386
1387 m_pointList << QPoint(1, 2) << QPoint(3, 4) << QPoint(5, 6);
1388 m_variantList << QVariant(QLatin1String("one")) << QVariant(true) << QVariant(3);
1389 }
1390
1391 ~MySequenceConversionObject() {}
1392
1393 qint32 maxIndex() const
1394 {
1395 return INT_MAX;
1396 }
1397 quint32 tooBigIndex() const
1398 {
1399 quint32 retn = 7;
1400 retn += INT_MAX;
1401 return retn;
1402 }
1403 qint32 negativeIndex() const
1404 {
1405 return -5;
1406 }
1407
1408 QList<int> intListProperty() const { return m_intList; }
1409 void setIntListProperty(const QList<int> &list) { m_intList = list; emit intListPropertyChanged(); }
1410 QList<int> intListProperty2() const { return m_intList2; }
1411 void setIntListProperty2(const QList<int> &list) { m_intList2 = list; emit intListProperty2Changed(); }
1412 QList<qreal> qrealListProperty() const { return m_qrealList; }
1413 void setQrealListProperty(const QList<qreal> &list) { m_qrealList = list; emit qrealListPropertyChanged(); }
1414 QList<bool> boolListProperty() const { return m_boolList; }
1415 void setBoolListProperty(const QList<bool> &list) { m_boolList = list; emit boolListPropertyChanged(); }
1416 QList<QString> stringListProperty() const { return m_stringList; }
1417 void setStringListProperty(const QList<QString> &list) { m_stringList = list; emit stringListPropertyChanged(); }
1418 QList<QUrl> urlListProperty() const { return m_urlList; }
1419 void setUrlListProperty(const QList<QUrl> &list) { m_urlList = list; emit urlListPropertyChanged(); }
1420 QStringList qstringListProperty() const { return m_qstringList; }
1421 void setQStringListProperty(const QStringList &list) { m_qstringList = list; emit qstringListPropertyChanged(); }
1422 QList<QPoint> pointListProperty() const { return m_pointList; }
1423 void setPointListProperty(const QList<QPoint> &list) { m_pointList = list; emit pointListPropertyChanged(); }
1424 QList<NonRegisteredType> typeListProperty() const { return m_typeList; }
1425 void setTypeListProperty(const QList<NonRegisteredType> &list) { m_typeList = list; emit typeListPropertyChanged(); }
1426 QList<QVariant> variantListProperty() const { return m_variantList; }
1427 void setVariantListProperty(const QList<QVariant> &list) { m_variantList = list; emit variantListPropertyChanged(); }
1428
1429 // now for "copy resource" sequences:
1430 Q_INVOKABLE QList<int> generateIntSequence() const { QList<int> retn; retn << 1 << 2 << 3; return retn; }
1431 Q_INVOKABLE QList<qreal> generateQrealSequence() const { QList<qreal> retn; retn << 1.1 << 2.2 << 3.3; return retn; }
1432 Q_INVOKABLE QList<bool> generateBoolSequence() const { QList<bool> retn; retn << true << false << true; return retn; }
1433 Q_INVOKABLE QList<QString> generateStringSequence() const { QList<QString> retn; retn << "one" << "two" << "three"; return retn; }
1434 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; }
1435 Q_INVOKABLE QStringList generateQStringSequence() const { QStringList retn; retn << "one" << "two" << "three"; return retn; }
1436 Q_INVOKABLE bool parameterEqualsGeneratedIntSequence(const QList<int>& param) const { return (param == generateIntSequence()); }
1437
1438 // "reference resource" underlying qobject deletion test:
1439 Q_INVOKABLE MySequenceConversionObject *generateTestObject() const { return new MySequenceConversionObject; }
1440 Q_INVOKABLE void deleteTestObject(QObject *object) const { delete object; }
1441
1442signals:
1443 void intListPropertyChanged();
1444 void intListProperty2Changed();
1445 void qrealListPropertyChanged();
1446 void boolListPropertyChanged();
1447 void stringListPropertyChanged();
1448 void urlListPropertyChanged();
1449 void qstringListPropertyChanged();
1450 void pointListPropertyChanged();
1451 void typeListPropertyChanged();
1452 void variantListPropertyChanged();
1453
1454private:
1455 QList<int> m_intList;
1456 QList<int> m_intList2;
1457 QList<qreal> m_qrealList;
1458 QList<bool> m_boolList;
1459 QList<QString> m_stringList;
1460 QList<QUrl> m_urlList;
1461 QStringList m_qstringList;
1462
1463 QList<QPoint> m_pointList;
1464 QList<NonRegisteredType> m_typeList; // not a supported sequence type
1465 QList<QVariant> m_variantList; // not a supported sequence type, but QVariantList support is hardcoded.
1466};
1467
1468class MyDeleteObject : public QObject
1469{
1470 Q_OBJECT
1471 Q_PROPERTY(QObject *nestedObject READ nestedObject NOTIFY nestedObjectChanged)
1472 Q_PROPERTY(int deleteNestedObject READ deleteNestedObject NOTIFY deleteNestedObjectChanged)
1473 Q_PROPERTY(QObject *object2 READ object2 NOTIFY object2Changed)
1474
1475public:
1476 MyDeleteObject() : m_nestedObject(new MyQmlObject), m_object1(0), m_object2(0) {}
1477
1478 Q_INVOKABLE QObject *object1() const { return m_object1; }
1479 Q_INVOKABLE QObject *object2() const { return m_object2; }
1480 void setObject1(QObject *object) { m_object1 = object; }
1481 void setObject2(QObject *object) { m_object2 = object; emit object2Changed(); }
1482 QObject *nestedObject() const { return m_nestedObject; }
1483 int deleteNestedObject() { delete m_nestedObject; m_nestedObject = 0; return 1; }
1484
1485signals:
1486 void nestedObjectChanged();
1487 void deleteNestedObjectChanged();
1488 void object2Changed();
1489
1490private:
1491 MyQmlObject *m_nestedObject;
1492 QObject *m_object1;
1493 QObject *m_object2;
1494};
1495
1496class DateTimeExporter : public QObject
1497{
1498 Q_OBJECT
1499
1500public:
1501 DateTimeExporter(const QDateTime &dt) : m_datetime(dt), m_offset(0), m_timespec("UTC")
1502 {
1503 switch (m_datetime.timeSpec()) {
1504 case Qt::LocalTime:
1505 {
1506 QDateTime utc(m_datetime.toUTC());
1507 utc.setTimeSpec(Qt::LocalTime);
1508 m_offset = m_datetime.secsTo(utc) / 60;
1509 m_timespec = "LocalTime";
1510 }
1511 break;
1512 case Qt::OffsetFromUTC:
1513 m_offset = m_datetime.offsetFromUtc() / 60;
1514 m_timespec = QString("%1%2:%3").arg(a: m_offset < 0 ? '-' : '+')
1515 .arg(a: abs(x: m_offset) / 60)
1516 .arg(a: abs(x: m_offset) % 60);
1517 default:
1518 break;
1519 }
1520 }
1521
1522 Q_INVOKABLE QDate getDate() const { return m_datetime.date(); }
1523 Q_INVOKABLE QDateTime getDateTime() const { return m_datetime; }
1524 Q_INVOKABLE int getDateTimeOffset() const { return m_offset; }
1525 Q_INVOKABLE QString getTimeSpec() const { return m_timespec; }
1526
1527private:
1528 QDateTime m_datetime;
1529 int m_offset;
1530 QString m_timespec;
1531};
1532
1533class MyWorkerObject : public QObject
1534{
1535 Q_OBJECT
1536public:
1537 ~MyWorkerObject();
1538
1539public Q_SLOTS:
1540 void doIt();
1541
1542Q_SIGNALS:
1543 void done(const QString &result);
1544
1545private:
1546 QThread *m_thread = 0;
1547};
1548
1549class MyUnregisteredEnumTypeObject : public QObject
1550{
1551 Q_OBJECT
1552 Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty)
1553
1554public:
1555 MyUnregisteredEnumTypeObject() : QObject(), m_ev(FirstValue) {}
1556 ~MyUnregisteredEnumTypeObject() {}
1557
1558 enum MyEnum {
1559 FirstValue = 1,
1560 SecondValue = 2
1561 };
1562
1563 MyEnum enumProperty() const { return m_ev; }
1564 void setEnumProperty(MyEnum v) { m_ev = v; }
1565
1566private:
1567 MyEnum m_ev;
1568};
1569
1570class FallbackBindingsObject : public QObject
1571{
1572 Q_OBJECT
1573 Q_PROPERTY (int test READ test NOTIFY testChanged)
1574public:
1575 FallbackBindingsObject(QObject* parent = 0)
1576 : QObject(parent), m_test(100)
1577 {
1578 }
1579
1580 int test() const { return m_test; }
1581
1582Q_SIGNALS:
1583 void testChanged();
1584
1585private:
1586 int m_test;
1587};
1588
1589class FallbackBindingsDerived : public FallbackBindingsObject
1590{
1591 Q_OBJECT
1592 Q_PROPERTY (QString test READ test NOTIFY testChanged)
1593public:
1594 FallbackBindingsDerived(QObject* parent = 0)
1595 : FallbackBindingsObject(parent), m_test("hello")
1596 {
1597 }
1598
1599 QString test() const { return m_test; }
1600
1601Q_SIGNALS:
1602 void testChanged();
1603
1604private:
1605 QString m_test;
1606};
1607
1608class FallbackBindingsAttachedObject : public QObject
1609{
1610 Q_OBJECT
1611 Q_PROPERTY (int test READ test NOTIFY testChanged)
1612public:
1613 FallbackBindingsAttachedObject(QObject *parent) : QObject(parent), m_test(100) {}
1614
1615 int test() const { return m_test; }
1616
1617Q_SIGNALS:
1618 void testChanged();
1619
1620private:
1621 int m_test;
1622};
1623
1624class FallbackBindingsAttachedDerived : public FallbackBindingsAttachedObject
1625{
1626 Q_OBJECT
1627 Q_PROPERTY (QString test READ test NOTIFY testChanged)
1628public:
1629 FallbackBindingsAttachedDerived(QObject* parent = 0)
1630 : FallbackBindingsAttachedObject(parent), m_test("hello")
1631 {
1632 }
1633
1634 QString test() const { return m_test; }
1635
1636Q_SIGNALS:
1637 void testChanged();
1638
1639private:
1640 QString m_test;
1641};
1642
1643class FallbackBindingsTypeObject : public QObject
1644{
1645 Q_OBJECT
1646public:
1647 FallbackBindingsTypeObject() : QObject() {}
1648
1649 static FallbackBindingsAttachedObject *qmlAttachedProperties(QObject *o) {
1650 return new FallbackBindingsAttachedObject(o);
1651 }
1652};
1653
1654class FallbackBindingsTypeDerived : public QObject
1655{
1656 Q_OBJECT
1657public:
1658 FallbackBindingsTypeDerived() : QObject() {}
1659
1660 static FallbackBindingsAttachedObject *qmlAttachedProperties(QObject *o) {
1661 return new FallbackBindingsAttachedDerived(o);
1662 }
1663};
1664
1665QML_DECLARE_TYPEINFO(FallbackBindingsTypeObject, QML_HAS_ATTACHED_PROPERTIES)
1666QML_DECLARE_TYPEINFO(FallbackBindingsTypeDerived, QML_HAS_ATTACHED_PROPERTIES)
1667
1668class SingletonWithEnum : public QObject
1669{
1670 Q_OBJECT
1671 Q_ENUMS(TestEnum)
1672public:
1673 enum TestEnum {
1674 TestValue = 42,
1675 TestValue_MinusOne = -1
1676 };
1677};
1678
1679// Like QtObject, but with default property
1680class QObjectContainer : public QObject
1681{
1682 Q_OBJECT
1683 Q_CLASSINFO("DefaultProperty", "data")
1684 Q_PROPERTY(QQmlListProperty<QObject> data READ data DESIGNABLE false)
1685public:
1686 QObjectContainer();
1687
1688 QQmlListProperty<QObject> data();
1689
1690 static void children_append(QQmlListProperty<QObject> *prop, QObject *o);
1691 static int children_count(QQmlListProperty<QObject> *prop);
1692 static QObject *children_at(QQmlListProperty<QObject> *prop, int index);
1693 static void children_clear(QQmlListProperty<QObject> *prop);
1694
1695 QList<QObject*> dataChildren;
1696 QWidget *widgetParent;
1697 bool gcOnAppend;
1698
1699protected slots:
1700 void childDestroyed(QObject *child);
1701};
1702
1703class QObjectContainerWithGCOnAppend : public QObjectContainer
1704{
1705 Q_OBJECT
1706public:
1707 QObjectContainerWithGCOnAppend()
1708 {
1709 gcOnAppend = true;
1710 }
1711};
1712
1713class FloatingQObject : public QObject, public QQmlParserStatus
1714{
1715 Q_OBJECT
1716 Q_INTERFACES(QQmlParserStatus)
1717public:
1718 FloatingQObject() {}
1719
1720 virtual void classBegin();
1721 virtual void componentComplete();
1722};
1723
1724class ClashingNames : public QObject
1725{
1726 Q_OBJECT
1727 Q_PROPERTY(bool clashes READ clashes CONSTANT)
1728public:
1729 Q_INVOKABLE bool clashes() const { return true; }
1730};
1731
1732class VariantConvertObject : public QObject
1733{
1734 Q_OBJECT
1735public:
1736 QString funcCalled;
1737public slots:
1738 QPersistentModelIndex getIndex() const { return QPersistentModelIndex(QModelIndex()); }
1739 void selection(const QModelIndex &mi, int n = 0) { funcCalled = QLatin1String("QModelIndex"); }
1740 void selection(const QItemSelection &is, int n = 0) { funcCalled = QLatin1String("QItemSelection"); }
1741};
1742
1743void registerTypes();
1744
1745#endif // TESTTYPES_H
1746
1747

source code of qtdeclarative/tests/auto/qml/qqmlecmascript/testtypes.h