1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
#include <QtTest/QtTest>
#include <qlistmodelinterface.h>
#include <qmlview.h>
#include <qfxpathview.h>
#include <qfxtext.h>
#include <qfxrect.h>
#include <qmlcontext.h>
#include <qmlexpression.h>
class tst_QmlGraphicsPathView : public QObject
{
Q_OBJECT
public:
tst_QmlGraphicsPathView();
private slots:
void items();
void pathMoved();
void limitedItems();
private:
QmlView *createView(const QString &filename);
template<typename T>
T *findItem(QmlGraphicsItem *parent, const QString &id, int index=-1);
};
class TestModel : public QListModelInterface
{
Q_OBJECT
public:
TestModel(QObject *parent = 0) : QListModelInterface(parent) {}
~TestModel() {}
enum Roles { Name, Number };
QString name(int index) const { return list.at(index).first; }
QString number(int index) const { return list.at(index).second; }
int count() const { return list.count(); }
QList<int> roles() const { return QList<int>() << Name << Number; }
QString toString(int role) const {
switch(role) {
case Name:
return "name";
case Number:
return "number";
default:
return "";
}
}
QHash<int, QVariant> data(int index, const QList<int> &roles) const {
QHash<int,QVariant> returnHash;
for (int i = 0; i < roles.size(); ++i) {
int role = roles.at(i);
QVariant info;
switch(role) {
case Name:
info = list.at(index).first;
break;
case Number:
info = list.at(index).second;
break;
default:
break;
}
returnHash.insert(role, info);
}
return returnHash;
}
void addItem(const QString &name, const QString &number) {
list.append(QPair<QString,QString>(name, number));
emit itemsInserted(list.count()-1, 1);
}
void insertItem(int index, const QString &name, const QString &number) {
list.insert(index, QPair<QString,QString>(name, number));
emit itemsInserted(index, 1);
}
void removeItem(int index) {
list.removeAt(index);
emit itemsRemoved(index, 1);
}
void modifyItem(int index, const QString &name, const QString &number) {
list[index] = QPair<QString,QString>(name, number);
emit itemsChanged(index, 1, roles());
}
private:
QList<QPair<QString,QString> > list;
};
tst_QmlGraphicsPathView::tst_QmlGraphicsPathView()
{
}
void tst_QmlGraphicsPathView::items()
{
QmlView *canvas = createView(SRCDIR "/data/pathview.qml");
TestModel model;
model.addItem("Fred", "12345");
model.addItem("John", "2345");
model.addItem("Bob", "54321");
QmlContext *ctxt = canvas->rootContext();
ctxt->setContextProperty("testModel", &model);
canvas->execute();
qApp->processEvents();
QmlGraphicsPathView *pathview = findItem<QmlGraphicsPathView>(canvas->root(), "view");
QVERIFY(pathview != 0);
QCOMPARE(pathview->childItems().count(), model.count()); // assumes all are visible
for (int i = 0; i < model.count(); ++i) {
QmlGraphicsText *name = findItem<QmlGraphicsText>(pathview, "textName", i);
QVERIFY(name != 0);
QCOMPARE(name->text(), model.name(i));
QmlGraphicsText *number = findItem<QmlGraphicsText>(pathview, "textNumber", i);
QVERIFY(number != 0);
QCOMPARE(number->text(), model.number(i));
}
delete canvas;
}
void tst_QmlGraphicsPathView::pathMoved()
{
QmlView *canvas = createView(SRCDIR "/data/pathview.qml");
TestModel model;
model.addItem("Ben", "12345");
model.addItem("Bohn", "2345");
model.addItem("Bob", "54321");
model.addItem("Bill", "4321");
QmlContext *ctxt = canvas->rootContext();
ctxt->setContextProperty("testModel", &model);
canvas->execute();
qApp->processEvents();
QmlGraphicsPathView *pathview = findItem<QmlGraphicsPathView>(canvas->root(), "view");
QVERIFY(pathview != 0);
QmlGraphicsRect *firstItem = findItem<QmlGraphicsRect>(pathview, "wrapper", 0);
QVERIFY(firstItem);
QmlGraphicsPath *path = qobject_cast<QmlGraphicsPath*>(pathview->path());
QVERIFY(path);
QPointF start = path->pointAt(0.0);
QPointF offset;//Center of item is at point, but pos is from corner
offset.setX(firstItem->width()/2);
offset.setY(firstItem->height()/2);
QCOMPARE(firstItem->pos() + offset, start);
pathview->setOffset(10);
QTest::qWait(1000);//Moving is animated?
for(int i=0; i<model.count(); i++){
QmlGraphicsRect *curItem = findItem<QmlGraphicsRect>(pathview, "wrapper", i);
QCOMPARE(curItem->pos() + offset, path->pointAt(0.1 + i*0.25));
}
pathview->setOffset(100);
QTest::qWait(1000);//Moving is animated?
QCOMPARE(firstItem->pos() + offset, start);
delete canvas;
}
void tst_QmlGraphicsPathView::limitedItems()
{
QmlView *canvas = createView(SRCDIR "/data/pathview.qml");
TestModel model;
for(int i=0; i<100; i++)
model.addItem("Bob", QString::number(i));
QmlContext *ctxt = canvas->rootContext();
ctxt->setContextProperty("testModel", &model);
canvas->execute();
qApp->processEvents();
QmlGraphicsPathView *pathview = findItem<QmlGraphicsPathView>(canvas->root(), "view");
QVERIFY(pathview != 0);
pathview->setPathItemCount(10);
QCOMPARE(pathview->pathItemCount(), 10);
QmlGraphicsRect *testItem = findItem<QmlGraphicsRect>(pathview, "wrapper", 0);
QVERIFY(testItem != 0);
testItem = findItem<QmlGraphicsRect>(pathview, "wrapper", 9);
QVERIFY(testItem != 0);
testItem = findItem<QmlGraphicsRect>(pathview, "wrapper", 10);
QVERIFY(testItem == 0);
pathview->setCurrentIndex(50);
QTest::qWait(5100);//Moving is animated and it's travelling far - should be reconsidered.
testItem = findItem<QmlGraphicsRect>(pathview, "wrapper", 0);
QVERIFY(testItem == 0);
testItem = findItem<QmlGraphicsRect>(pathview, "wrapper", 1);
QVERIFY(testItem == 0);
testItem = findItem<QmlGraphicsRect>(pathview, "wrapper", 9);
QVERIFY(testItem == 0);
testItem = findItem<QmlGraphicsRect>(pathview, "wrapper", 50);
QVERIFY(testItem != 0);
}
QmlView *tst_QmlGraphicsPathView::createView(const QString &filename)
{
QmlView *canvas = new QmlView(0);
canvas->setFixedSize(240,320);
QFile file(filename);
file.open(QFile::ReadOnly);
QString qml = file.readAll();
canvas->setQml(qml, filename);
return canvas;
}
/*
Find an item with the specified objectName. If index is supplied then the
item must also evaluate the {index} expression equal to index
*/
template<typename T>
T *tst_QmlGraphicsPathView::findItem(QmlGraphicsItem *parent, const QString &objectName, int index)
{
const QMetaObject &mo = T::staticMetaObject;
for (int i = 0; i < parent->children().count(); ++i) {
QmlGraphicsItem *item = qobject_cast<QmlGraphicsItem*>(parent->children().at(i));
if(!item)
continue;
if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) {
if (index != -1) {
QmlExpression e(qmlContext(item), "index", item);
e.setTrackChange(false);
if (e.value().toInt() == index)
return static_cast<T*>(item);
} else {
return static_cast<T*>(item);
}
}
item = findItem<T>(item, objectName, index);
if (item)
return static_cast<T*>(item);
}
return 0;
}
QTEST_MAIN(tst_QmlGraphicsPathView)
#include "tst_pathview.moc"
|