on xenial help sign in
Don't use context properties but properties of the delegates

This increases a bit the requirements for delegates of the LVWPH but since we control them it's not really a problem. In ::layout the setContextProperty calls accounted for around 45% according to callgrind, now it's around 10% only.

commit2d4bcd3687930244421a3a795a17794da5ecd2b5 authorAlbert Astals Cid <albert.astals@canonical.com> dateFri 27 May 2016, 13:50 (10 years ago) parenta2395bb3fafbea
8 files changed+31−38
plugins/Dash/listviewwithpageheader.cpp+13−16 qml/Dash/DashCategoryBase.qml+3−11 qml/Dash/GenericScopeView.qml+2−2 tests/plugins/Dash/listviewwithpageheadersectionexternalmodeltest.cpp+2−2 tests/plugins/Dash/listviewwithpageheadersectiontest.cpp+2−2 tests/plugins/Dash/listviewwithpageheadertestsection.qml+3−2 tests/plugins/Dash/listviewwithpageheadertestsectionexternalmodel.qml+2−1 tests/plugins/Dash/tst_ListViewWithPageHeaderQML.qml+4−2
plugins/Dash/listviewwithpageheader.cpp+13−16
@@ -97,6 +97,7 @@
9797#include <qqmlengine.h>
9898#pragma GCC diagnostic push
9999#pragma GCC diagnostic ignored "-pedantic"
100+#include <private/qqmlcontext_p.h>
100101#include <private/qqmldelegatemodel_p.h>
101102#include <private/qqmlglobal_p.h>
102103#include <private/qquickitem_p.h>
@@ -757,8 +758,6 @@ QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText, b
757758
758759 QQmlContext *creationContext = m_sectionDelegate->creationContext();
759760 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
760− context->setContextProperty(QStringLiteral("section"), sectionText);
761− context->setContextProperty(QStringLiteral("delegateIndex"), -1);
762761 QObject *nobj = m_sectionDelegate->beginCreate(context);
763762 if (nobj) {
764763 QQml_setParent_noEvent(context, nobj);
@@ -766,6 +765,8 @@ QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText, b
766765 if (!sectionItem) {
767766 delete nobj;
768767 } else {
768+ sectionItem->setProperty("text", sectionText);
769+ sectionItem->setProperty("delegateIndex", -1);
769770 sectionItem->setZ(2);
770771 QQml_setParent_noEvent(sectionItem, m_clipItem);
771772 sectionItem->setParentItem(m_clipItem);
@@ -801,8 +802,7 @@ void ListViewWithPageHeader::updateSectionItem(int modelIndex)
801802 if (!item->sectionItem()) {
802803 item->setSectionItem(getSectionItem(sectionText));
803804 } else {
804− QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
805− context->setContextProperty(QStringLiteral("section"), sectionText);
805+ item->sectionItem()->setProperty("text", sectionText);
806806 }
807807 } else {
808808 if (item->sectionItem()) {
@@ -923,8 +923,7 @@ ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIn
923923 polish();
924924 }
925925 if (listItem->sectionItem()) {
926− QQmlContext *context = QQmlEngine::contextForObject(listItem->sectionItem())->parentContext();
927− context->setContextProperty(QStringLiteral("delegateIndex"), modelIndex);
926+ listItem->sectionItem()->setProperty("delegateIndex", modelIndex);
928927 }
929928 adjustMinYExtent();
930929 m_contentHeightDirty = true;
@@ -948,9 +947,10 @@ void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
948947 return;
949948
950949 item->setParentItem(m_clipItem);
950+ // FIXME Why do we need the refreshExpressions call?
951951 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
952− context->setContextProperty(QStringLiteral("ListViewWithPageHeader"), this);
953− context->setContextProperty(QStringLiteral("heightToClip"), QVariant::fromValue<int>(0));
952+ QQmlContextPrivate::get(context)->data->refreshExpressions();
953+ item->setProperty("heightToClip", QVariant::fromValue<int>(0));
954954 if (modelIndex == m_asyncRequestedIndex) {
955955 createItem(modelIndex, false);
956956 refill();
@@ -1116,8 +1116,7 @@ void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool
11161116 for (int i = 0; i < m_visibleItems.count(); ++i) {
11171117 ListItem *item = m_visibleItems[i];
11181118 if (item->sectionItem()) {
1119− QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1120− context->setContextProperty(QStringLiteral("delegateIndex"), m_firstVisibleIndex + i);
1119+ item->sectionItem()->setProperty("delegateIndex", m_firstVisibleIndex + i);
11211120 }
11221121 }
11231122
@@ -1287,8 +1286,7 @@ void ListViewWithPageHeader::layout()
12871286 } else {
12881287 // Update the top sticky section header
12891288 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1290− QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1291− context->setContextProperty(QStringLiteral("section"), section);
1289+ m_topSectionItem->setProperty("text", section);
12921290
12931291 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
12941292 m_topSectionItem->setY(topSectionStickPos);
@@ -1300,19 +1298,18 @@ void ListViewWithPageHeader::layout()
13001298 break;
13011299 delegateIndex--;
13021300 }
1303− context->setContextProperty(QStringLiteral("delegateIndex"), delegateIndex);
1301+ m_topSectionItem->setProperty("delegateIndex", delegateIndex);
13041302 if (item->sectionItem()) {
13051303 QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
13061304 }
13071305 }
13081306 }
13091307 }
1310− QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
13111308 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
13121309 if (!cull && pos < clipFrom) {
1313− context->setContextProperty(QStringLiteral("heightToClip"), clipFrom - pos);
1310+ item->m_item->setProperty("heightToClip", clipFrom - pos);
13141311 } else {
1315− context->setContextProperty(QStringLiteral("heightToClip"), QVariant::fromValue<int>(0));
1312+ item->m_item->setProperty("heightToClip", QVariant::fromValue<int>(0));
13161313 }
13171314// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
13181315 pos += item->height();
qml/Dash/DashCategoryBase.qml+3−11
@@ -24,17 +24,9 @@ Item {
2424 /* Relevant really only for ListViewWithPageHeader case: specify how many pixels we can overlap with the section header */
2525 readonly property int allowedOverlap: units.dp(1)
2626
27− property real __heightToClip: {
28− // Check this is in position where clipping is needed
29− if (typeof ListViewWithPageHeader !== 'undefined') {
30− if (typeof heightToClip !== 'undefined') {
31− if (heightToClip >= allowedOverlap) {
32− return heightToClip - allowedOverlap;
33− }
34− }
35− }
36− return 0;
37− }
27+ property real heightToClip: 0
28+ readonly property real __heightToClip: heightToClip >= allowedOverlap ? heightToClip - allowedOverlap : 0
29+
3830
3931 /*!
4032 \internal
qml/Dash/GenericScopeView.qml+2−2
@@ -616,10 +616,10 @@ FocusScope {
616616 sectionProperty: "name"
617617 sectionDelegate: ListItems.Header {
618618 objectName: "dashSectionHeader" + (delegate ? delegate.category : "")
619+ property int delegateIndex: -1
619620 readonly property var delegate: categoryView.item(delegateIndex)
620621 width: categoryView.width
621− height: section != "" ? units.gu(5) : 0
622− text: section
622+ height: text != "" ? units.gu(5) : 0
623623 color: scopeStyle ? scopeStyle.foreground : theme.palette.normal.baseText
624624 iconName: delegate && delegate.headerLink ? "go-next" : ""
625625 onClicked: {
tests/plugins/Dash/listviewwithpageheadersectionexternalmodeltest.cpp+2−2
@@ -151,12 +151,12 @@ private:
151151
152152 QString section(QQuickItem *item)
153153 {
154− return item ? QQmlEngine::contextForObject(item)->parentContext()->contextProperty(QLatin1String("section")).toString() : QString();
154+ return item ? item->property("text").toString() : QString();
155155 }
156156
157157 int sectionDelegateIndex(QQuickItem *item)
158158 {
159− return item ? QQmlEngine::contextForObject(item)->parentContext()->contextProperty(QLatin1String("delegateIndex")).toInt() : -1;
159+ return item ? item->property("delegateIndex").toInt() : -1;
160160 }
161161
162162private Q_SLOTS:
tests/plugins/Dash/listviewwithpageheadersectiontest.cpp+2−2
@@ -116,12 +116,12 @@ private:
116116
117117 QString section(QQuickItem *item) const
118118 {
119− return item ? QQmlEngine::contextForObject(item)->parentContext()->contextProperty(QLatin1String("section")).toString() : QString();
119+ return item ? item->property("text").toString() : QString();
120120 }
121121
122122 int sectionDelegateIndex(QQuickItem *item) const
123123 {
124− return item ? QQmlEngine::contextForObject(item)->parentContext()->contextProperty(QLatin1String("delegateIndex")).toInt() : -1;
124+ return item ? item->property("delegateIndex").toInt() : -1;
125125 }
126126
127127private Q_SLOTS:
tests/plugins/Dash/listviewwithpageheadertestsection.qml+3−2
@@ -97,9 +97,10 @@ Rectangle {
9797 sectionProperty: "type"
9898 sectionDelegate: Component {
9999 Rectangle {
100+ property alias text: theText.text
100101 color: "green"
101− height: section === "" ? 0 : section != "halfheight" ? 40 : 20;
102− Text { text: section; font.pixelSize: 34 }
102+ height: text === "" ? 0 : text != "halfheight" ? 40 : 20;
103+ Text { id: theText; font.pixelSize: 34 }
103104 anchors { left: parent.left; right: parent.right }
104105 }
105106 }
tests/plugins/Dash/listviewwithpageheadertestsectionexternalmodel.qml+2−1
@@ -65,9 +65,10 @@ Rectangle {
6565 sectionProperty: "type"
6666 sectionDelegate: Component {
6767 Rectangle {
68+ property alias text: theText.text
6869 color: "green"
6970 height: 40
70− Text { text: section; font.pixelSize: 34 }
71+ Text { id: theText; font.pixelSize: 34 }
7172 anchors { left: parent.left; right: parent.right }
7273 }
7374 }
tests/plugins/Dash/tst_ListViewWithPageHeaderQML.qml+4−2
@@ -81,9 +81,10 @@ Rectangle {
8181 sectionDelegate: Component {
8282 id: sectionHeaderComponent
8383 Rectangle {
84+ property alias text: theText.text
8485 color: "green"
8586 height: listView.sectionHeaderHeight
86− Text { text: section; font.pixelSize: 34 }
87+ Text { id: theText; font.pixelSize: 34 }
8788 anchors { left: parent.left; right: parent.right }
8889 }
8990 }
@@ -92,9 +93,10 @@ Rectangle {
9293 Component {
9394 id: otherSectionHeaderComponent
9495 Rectangle {
96+ property alias text: theText.text
9597 color: "green"
9698 height: 50
97− Text { text: section; font.pixelSize: 34 }
99+ Text { id: theText; font.pixelSize: 34 }
98100 anchors { left: parent.left; right: parent.right }
99101 }
100102 }