Fix ListView components being unable to access context properties.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Fri, 7 Oct 2011 03:06:26 +0000 (13:06 +1000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 7 Oct 2011 03:55:18 +0000 (05:55 +0200)
Where avaialable use the components creation context instead of the
views context as the root context for the new item.

Task-number: QTBUG-21865
Change-Id: I07e564548de57d58413dc0d7cd151bd8a90886e7
Reviewed-on: http://codereview.qt-project.org/6199
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Bea Lam <bea.lam@nokia.com>

12 files changed:
src/declarative/items/qsgitemview.cpp
src/declarative/items/qsglistview.cpp
src/declarative/items/qsgvisualdatamodel.cpp
tests/auto/declarative/qsggridview/data/ComponentView.qml [new file with mode: 0644]
tests/auto/declarative/qsggridview/data/creationContext.qml [new file with mode: 0644]
tests/auto/declarative/qsggridview/tst_qsggridview.cpp
tests/auto/declarative/qsglistview/data/ComponentView.qml [new file with mode: 0644]
tests/auto/declarative/qsglistview/data/creationContext.qml [new file with mode: 0644]
tests/auto/declarative/qsglistview/tst_qsglistview.cpp
tests/auto/declarative/qsgpathview/data/ComponentView.qml [new file with mode: 0644]
tests/auto/declarative/qsgpathview/data/creationContext.qml [new file with mode: 0644]
tests/auto/declarative/qsgpathview/tst_qsgpathview.cpp

index b34ed82..9415f9e 100644 (file)
@@ -1624,7 +1624,9 @@ QSGItem *QSGItemViewPrivate::createComponentItem(QDeclarativeComponent *componen
 
     QSGItem *item = 0;
     if (component) {
-        QDeclarativeContext *context = new QDeclarativeContext(qmlContext(q));
+        QDeclarativeContext *creationContext = component->creationContext();
+        QDeclarativeContext *context = new QDeclarativeContext(
+                creationContext ? creationContext : qmlContext(q));
         QObject *nobj = component->create(context);
         if (nobj) {
             QDeclarative_setParent_noEvent(context, nobj);
index 7f9ce3a..beeeda4 100644 (file)
@@ -832,7 +832,9 @@ QSGItem * QSGListViewPrivate::getSectionItem(const QString &section)
         QDeclarativeContext *context = QDeclarativeEngine::contextForObject(sectionItem)->parentContext();
         context->setContextProperty(QLatin1String("section"), section);
     } else {
-        QDeclarativeContext *context = new QDeclarativeContext(qmlContext(q));
+        QDeclarativeContext *creationContext = sectionCriteria->delegate()->creationContext();
+        QDeclarativeContext *context = new QDeclarativeContext(
+                creationContext ? creationContext : qmlContext(q));
         context->setContextProperty(QLatin1String("section"), section);
         QObject *nobj = sectionCriteria->delegate()->beginCreate(context);
         if (nobj) {
index f5c86b8..e0d353a 100644 (file)
@@ -939,7 +939,9 @@ QObject *QSGVisualDataModelPrivate::object(Compositor::Group group, int index, b
     if (!cacheItem->object) {
         QObject *data = m_adaptorModel->data(it.modelIndex());
 
-        QDeclarativeContext *rootContext = new QDeclarativeContext(m_context);
+        QDeclarativeContext *creationContext = m_delegate->creationContext();
+        QDeclarativeContext *rootContext = new QDeclarativeContext(
+                creationContext ? creationContext : m_context.data());
         QDeclarativeContext *ctxt = rootContext;
         if (m_adaptorModel->flags() & QSGVisualAdaptorModel::ProxiedObject) {
             if (QSGVisualAdaptorModelProxyInterface *proxy = qobject_cast<QSGVisualAdaptorModelProxyInterface *>(data)) {
diff --git a/tests/auto/declarative/qsggridview/data/ComponentView.qml b/tests/auto/declarative/qsggridview/data/ComponentView.qml
new file mode 100644 (file)
index 0000000..12ab6c9
--- /dev/null
@@ -0,0 +1,14 @@
+import QtQuick 2.0
+
+GridView {
+    id: view
+
+    property string title
+
+    width: 100; height: 100;
+
+    model: 1
+    delegate: Text { objectName: "listItem"; text: view.title }
+    header: Text { objectName: "header"; text: view.title }
+    footer: Text { objectName: "footer"; text: view.title }
+}
diff --git a/tests/auto/declarative/qsggridview/data/creationContext.qml b/tests/auto/declarative/qsggridview/data/creationContext.qml
new file mode 100644 (file)
index 0000000..79a6827
--- /dev/null
@@ -0,0 +1,5 @@
+import QtQuick 2.0
+
+ComponentView {
+    title: "Hello!"
+}
index 24471e9..a5b7c5b 100644 (file)
@@ -64,7 +64,7 @@ class tst_QSGGridView : public QObject
 public:
     tst_QSGGridView();
 
-private slots:
+//private slots:
     void initTestCase();
     void cleanupTestCase();
     void items();
@@ -107,6 +107,8 @@ private slots:
     void testQtQuick11Attributes_data();
     void columnCount();
     void margins();
+private slots:
+    void creationContext();
 
 private:
     QSGView *createView();
@@ -2926,6 +2928,26 @@ void tst_QSGGridView::margins()
     }
 }
 
+void tst_QSGGridView::creationContext()
+{
+    QSGView canvas;
+    canvas.setGeometry(0,0,240,320);
+    canvas.setSource(QUrl::fromLocalFile(SRCDIR "/data/creationContext.qml"));
+    qApp->processEvents();
+
+    QSGItem *rootItem = qobject_cast<QSGItem *>(canvas.rootObject());
+    QVERIFY(rootItem);
+    QVERIFY(rootItem->property("count").toInt() > 0);
+
+    QSGItem *item;
+    QVERIFY(item = rootItem->findChild<QSGItem *>("listItem"));
+    QCOMPARE(item->property("text").toString(), QString("Hello!"));
+    QVERIFY(item = rootItem->findChild<QSGItem *>("header"));
+    QCOMPARE(item->property("text").toString(), QString("Hello!"));
+    QVERIFY(item = rootItem->findChild<QSGItem *>("footer"));
+    QCOMPARE(item->property("text").toString(), QString("Hello!"));
+}
+
 QSGView *tst_QSGGridView::createView()
 {
     QSGView *canvas = new QSGView(0);
diff --git a/tests/auto/declarative/qsglistview/data/ComponentView.qml b/tests/auto/declarative/qsglistview/data/ComponentView.qml
new file mode 100644 (file)
index 0000000..3e87be8
--- /dev/null
@@ -0,0 +1,16 @@
+import QtQuick 2.0
+
+ListView {
+    id: view
+
+    property string title
+
+    width: 100; height: 100;
+
+    model: 1
+    delegate: Text { objectName: "listItem"; text: view.title }
+    header: Text { objectName: "header"; text: view.title }
+    footer: Text { objectName: "footer"; text: view.title }
+    section.delegate: Text { objectName: "section"; text: view.title }
+    section.property: "modelData"
+}
diff --git a/tests/auto/declarative/qsglistview/data/creationContext.qml b/tests/auto/declarative/qsglistview/data/creationContext.qml
new file mode 100644 (file)
index 0000000..79a6827
--- /dev/null
@@ -0,0 +1,5 @@
+import QtQuick 2.0
+
+ComponentView {
+    title: "Hello!"
+}
index 6e8b247..cca6cd3 100644 (file)
@@ -134,6 +134,7 @@ private slots:
     void rightToLeft();
     void test_mirroring();
     void margins();
+    void creationContext();
 
 private:
     template <class T> void items();
@@ -3712,6 +3713,28 @@ void tst_QSGListView::qAbstractItemModel_clear()
     clear<TestModel2>();
 }
 
+void tst_QSGListView::creationContext()
+{
+    QSGView canvas;
+    canvas.setGeometry(0,0,240,320);
+    canvas.setSource(QUrl::fromLocalFile(SRCDIR "/data/creationContext.qml"));
+    qApp->processEvents();
+
+    QSGItem *rootItem = qobject_cast<QSGItem *>(canvas.rootObject());
+    QVERIFY(rootItem);
+    QVERIFY(rootItem->property("count").toInt() > 0);
+
+    QSGItem *item;
+    QVERIFY(item = rootItem->findChild<QSGItem *>("listItem"));
+    QCOMPARE(item->property("text").toString(), QString("Hello!"));
+    QVERIFY(item = rootItem->findChild<QSGItem *>("header"));
+    QCOMPARE(item->property("text").toString(), QString("Hello!"));
+    QVERIFY(item = rootItem->findChild<QSGItem *>("footer"));
+    QCOMPARE(item->property("text").toString(), QString("Hello!"));
+    QVERIFY(item = rootItem->findChild<QSGItem *>("section"));
+    QCOMPARE(item->property("text").toString(), QString("Hello!"));
+}
+
 QSGView *tst_QSGListView::createView()
 {
     QSGView *canvas = new QSGView(0);
@@ -3794,7 +3817,6 @@ void tst_QSGListView::dumpTree(QSGItem *parent, int depth)
     }
 }
 
-
 QTEST_MAIN(tst_QSGListView)
 
 #include "tst_qsglistview.moc"
diff --git a/tests/auto/declarative/qsgpathview/data/ComponentView.qml b/tests/auto/declarative/qsgpathview/data/ComponentView.qml
new file mode 100644 (file)
index 0000000..b61033d
--- /dev/null
@@ -0,0 +1,17 @@
+import QtQuick 2.0
+
+PathView {
+    id: view
+
+    property string title
+
+    width: 100; height: 100;
+
+    model: 1
+    delegate: Text { objectName: "listItem"; text: view.title }
+
+    path: Path {
+        startX: 25; startY: 25;
+        PathLine { x: 75; y: 75 }
+    }
+}
diff --git a/tests/auto/declarative/qsgpathview/data/creationContext.qml b/tests/auto/declarative/qsgpathview/data/creationContext.qml
new file mode 100644 (file)
index 0000000..79a6827
--- /dev/null
@@ -0,0 +1,5 @@
+import QtQuick 2.0
+
+ComponentView {
+    title: "Hello!"
+}
index 1a7368e..6dd2a57 100644 (file)
@@ -111,6 +111,7 @@ private slots:
     void treeModel();
     void changePreferredHighlight();
     void missingPercent();
+    void creationContext();
 
 private:
     QSGView *createView();
@@ -1066,6 +1067,21 @@ void tst_QSGPathView::changePreferredHighlight()
     delete canvas;
 }
 
+void tst_QSGPathView::creationContext()
+{
+    QSGView canvas;
+    canvas.setGeometry(0,0,240,320);
+    canvas.setSource(QUrl::fromLocalFile(SRCDIR "/data/creationContext.qml"));
+
+    QSGItem *rootItem = qobject_cast<QSGItem *>(canvas.rootObject());
+    QVERIFY(rootItem);
+    QVERIFY(rootItem->property("count").toInt() > 0);
+
+    QSGItem *item;
+    QVERIFY(item = findItem<QSGItem>(rootItem, "listItem", 0));
+    QCOMPARE(item->property("text").toString(), QString("Hello!"));
+}
+
 QSGView *tst_QSGPathView::createView()
 {
     QSGView *canvas = new QSGView(0);