|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 #include <qtest.h> |
|
42 #include <QtTest/QSignalSpy> |
|
43 #include <QStandardItemModel> |
|
44 #include <QtDeclarative/qdeclarativeengine.h> |
|
45 #include <QtDeclarative/qdeclarativecomponent.h> |
|
46 #include <QtDeclarative/qdeclarativecontext.h> |
|
47 #include <QtDeclarative/qdeclarativeexpression.h> |
|
48 #include <QtDeclarative/qdeclarativeview.h> |
|
49 #include <private/qdeclarativelistview_p.h> |
|
50 #include <private/qdeclarativetext_p.h> |
|
51 #include <private/qdeclarativevisualitemmodel_p.h> |
|
52 #include <private/qdeclarativevaluetype_p.h> |
|
53 #include <math.h> |
|
54 |
|
55 static void initStandardTreeModel(QStandardItemModel *model) |
|
56 { |
|
57 QStandardItem *item; |
|
58 item = new QStandardItem(QLatin1String("Row 1 Item")); |
|
59 model->insertRow(0, item); |
|
60 |
|
61 item = new QStandardItem(QLatin1String("Row 2 Item")); |
|
62 item->setCheckable(true); |
|
63 model->insertRow(1, item); |
|
64 |
|
65 QStandardItem *childItem = new QStandardItem(QLatin1String("Row 2 Child Item")); |
|
66 item->setChild(0, childItem); |
|
67 |
|
68 item = new QStandardItem(QLatin1String("Row 3 Item")); |
|
69 item->setIcon(QIcon()); |
|
70 model->insertRow(2, item); |
|
71 } |
|
72 |
|
73 class tst_qdeclarativevisualdatamodel : public QObject |
|
74 { |
|
75 Q_OBJECT |
|
76 public: |
|
77 tst_qdeclarativevisualdatamodel(); |
|
78 |
|
79 private slots: |
|
80 void rootIndex(); |
|
81 void objectListModel(); |
|
82 |
|
83 private: |
|
84 QDeclarativeEngine engine; |
|
85 template<typename T> |
|
86 T *findItem(QGraphicsObject *parent, const QString &objectName, int index); |
|
87 }; |
|
88 |
|
89 class DataObject : public QObject |
|
90 { |
|
91 Q_OBJECT |
|
92 |
|
93 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) |
|
94 Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged) |
|
95 |
|
96 public: |
|
97 DataObject(QObject *parent=0) : QObject(parent) {} |
|
98 DataObject(const QString &name, const QString &color, QObject *parent=0) |
|
99 : QObject(parent), m_name(name), m_color(color) { } |
|
100 |
|
101 |
|
102 QString name() const { return m_name; } |
|
103 void setName(const QString &name) { |
|
104 if (name != m_name) { |
|
105 m_name = name; |
|
106 emit nameChanged(); |
|
107 } |
|
108 } |
|
109 |
|
110 QString color() const { return m_color; } |
|
111 void setColor(const QString &color) { |
|
112 if (color != m_color) { |
|
113 m_color = color; |
|
114 emit colorChanged(); |
|
115 } |
|
116 } |
|
117 |
|
118 signals: |
|
119 void nameChanged(); |
|
120 void colorChanged(); |
|
121 |
|
122 private: |
|
123 QString m_name; |
|
124 QString m_color; |
|
125 }; |
|
126 |
|
127 tst_qdeclarativevisualdatamodel::tst_qdeclarativevisualdatamodel() |
|
128 { |
|
129 } |
|
130 |
|
131 void tst_qdeclarativevisualdatamodel::rootIndex() |
|
132 { |
|
133 QDeclarativeEngine engine; |
|
134 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/visualdatamodel.qml")); |
|
135 |
|
136 QStandardItemModel model; |
|
137 initStandardTreeModel(&model); |
|
138 |
|
139 engine.rootContext()->setContextProperty("myModel", &model); |
|
140 |
|
141 QDeclarativeVisualDataModel *obj = qobject_cast<QDeclarativeVisualDataModel*>(c.create()); |
|
142 QVERIFY(obj != 0); |
|
143 |
|
144 QMetaObject::invokeMethod(obj, "setRoot"); |
|
145 QVERIFY(qvariant_cast<QModelIndex>(obj->rootIndex()) == model.index(0,0)); |
|
146 |
|
147 QMetaObject::invokeMethod(obj, "setRootToParent"); |
|
148 QVERIFY(qvariant_cast<QModelIndex>(obj->rootIndex()) == QModelIndex()); |
|
149 |
|
150 delete obj; |
|
151 } |
|
152 |
|
153 void tst_qdeclarativevisualdatamodel::objectListModel() |
|
154 { |
|
155 QDeclarativeView view; |
|
156 |
|
157 QList<QObject*> dataList; |
|
158 dataList.append(new DataObject("Item 1", "red")); |
|
159 dataList.append(new DataObject("Item 2", "green")); |
|
160 dataList.append(new DataObject("Item 3", "blue")); |
|
161 dataList.append(new DataObject("Item 4", "yellow")); |
|
162 |
|
163 QDeclarativeContext *ctxt = view.rootContext(); |
|
164 ctxt->setContextProperty("myModel", QVariant::fromValue(dataList)); |
|
165 |
|
166 view.setSource(QUrl::fromLocalFile(SRCDIR "/data/objectlist.qml")); |
|
167 |
|
168 QDeclarativeListView *listview = qobject_cast<QDeclarativeListView*>(view.rootObject()); |
|
169 QVERIFY(listview != 0); |
|
170 |
|
171 QDeclarativeItem *viewport = listview->viewport(); |
|
172 QVERIFY(viewport != 0); |
|
173 |
|
174 QDeclarativeText *name = findItem<QDeclarativeText>(viewport, "name", 0); |
|
175 QCOMPARE(name->text(), QString("Item 1")); |
|
176 |
|
177 dataList[0]->setProperty("name", QLatin1String("Changed")); |
|
178 QCOMPARE(name->text(), QString("Changed")); |
|
179 } |
|
180 |
|
181 template<typename T> |
|
182 T *tst_qdeclarativevisualdatamodel::findItem(QGraphicsObject *parent, const QString &objectName, int index) |
|
183 { |
|
184 const QMetaObject &mo = T::staticMetaObject; |
|
185 //qDebug() << parent->childItems().count() << "children"; |
|
186 for (int i = 0; i < parent->childItems().count(); ++i) { |
|
187 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(parent->childItems().at(i)); |
|
188 if(!item) |
|
189 continue; |
|
190 //qDebug() << "try" << item; |
|
191 if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) { |
|
192 if (index != -1) { |
|
193 QDeclarativeExpression e(qmlContext(item), item, "index"); |
|
194 if (e.evaluate().toInt() == index) |
|
195 return static_cast<T*>(item); |
|
196 } else { |
|
197 return static_cast<T*>(item); |
|
198 } |
|
199 } |
|
200 item = findItem<T>(item, objectName, index); |
|
201 if (item) |
|
202 return static_cast<T*>(item); |
|
203 } |
|
204 |
|
205 return 0; |
|
206 } |
|
207 |
|
208 QTEST_MAIN(tst_qdeclarativevisualdatamodel) |
|
209 |
|
210 #include "tst_qdeclarativevisualdatamodel.moc" |