on xenial help sign in
Don't use context properties but properties on the delegates
fdf9a7eAlbert Astals Cid
plugins/Dash/listviewwithpageheader.cpp+13−16
@@ -97,6 +97,7 @@
97#include <qqmlengine.h>
97#include <qqmlengine.h>
98#pragma GCC diagnostic push
98#pragma GCC diagnostic push
99#pragma GCC diagnostic ignored "-pedantic"
99#pragma GCC diagnostic ignored "-pedantic"
100+#include <private/qqmlcontext_p.h>
100#include <private/qqmldelegatemodel_p.h>
101#include <private/qqmldelegatemodel_p.h>
101#include <private/qqmlglobal_p.h>
102#include <private/qqmlglobal_p.h>
102#include <private/qquickitem_p.h>
103#include <private/qquickitem_p.h>
@@ -750,8 +751,6 @@ QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
750
751
751 QQmlContext *creationContext = m_sectionDelegate->creationContext();
752 QQmlContext *creationContext = m_sectionDelegate->creationContext();
752 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
753 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
753− context->setContextProperty(QStringLiteral("section"), sectionText);
754− context->setContextProperty(QStringLiteral("delegateIndex"), -1);
755 QObject *nobj = m_sectionDelegate->beginCreate(context);
754 QObject *nobj = m_sectionDelegate->beginCreate(context);
756 if (nobj) {
755 if (nobj) {
757 QQml_setParent_noEvent(context, nobj);
756 QQml_setParent_noEvent(context, nobj);
@@ -759,6 +758,8 @@ QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
759 if (!sectionItem) {
758 if (!sectionItem) {
760 delete nobj;
759 delete nobj;
761 } else {
760 } else {
761+ sectionItem->setProperty("text", sectionText);
762+ sectionItem->setProperty("delegateIndex", -1);
762 sectionItem->setZ(2);
763 sectionItem->setZ(2);
763 QQml_setParent_noEvent(sectionItem, m_clipItem);
764 QQml_setParent_noEvent(sectionItem, m_clipItem);
764 sectionItem->setParentItem(m_clipItem);
765 sectionItem->setParentItem(m_clipItem);
@@ -792,8 +793,7 @@ void ListViewWithPageHeader::updateSectionItem(int modelIndex)
792 if (!item->sectionItem()) {
793 if (!item->sectionItem()) {
793 item->setSectionItem(getSectionItem(sectionText));
794 item->setSectionItem(getSectionItem(sectionText));
794 } else {
795 } else {
795− QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
796+ item->sectionItem()->setProperty("text", sectionText);
796− context->setContextProperty(QStringLiteral("section"), sectionText);
797 }
797 }
798 } else {
798 } else {
799 if (item->sectionItem()) {
799 if (item->sectionItem()) {
@@ -914,8 +914,7 @@ ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIn
914 polish();
914 polish();
915 }
915 }
916 if (listItem->sectionItem()) {
916 if (listItem->sectionItem()) {
917− QQmlContext *context = QQmlEngine::contextForObject(listItem->sectionItem())->parentContext();
918− context->setContextProperty(QStringLiteral("delegateIndex"), modelIndex);
917+ listItem->sectionItem()->setProperty("delegateIndex", modelIndex);
919 }
918 }
920 adjustMinYExtent();
919 adjustMinYExtent();
921 m_contentHeightDirty = true;
920 m_contentHeightDirty = true;
@@ -939,9 +938,10 @@ void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
939 return;
938 return;
940
939
941 item->setParentItem(m_clipItem);
940 item->setParentItem(m_clipItem);
941+ // FIXME Why do we need the refreshExpressions call?
942 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
942 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
943− context->setContextProperty(QStringLiteral("ListViewWithPageHeader"), this);
943+ QQmlContextPrivate::get(context)->data->refreshExpressions();
944− context->setContextProperty(QStringLiteral("heightToClip"), QVariant::fromValue<int>(0));
944+ item->setProperty("heightToClip", QVariant::fromValue<int>(0));
945 if (modelIndex == m_asyncRequestedIndex) {
945 if (modelIndex == m_asyncRequestedIndex) {
946 createItem(modelIndex, false);
946 createItem(modelIndex, false);
947 refill();
947 refill();
@@ -1107,8 +1107,7 @@ void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool
1107 for (int i = 0; i < m_visibleItems.count(); ++i) {
1107 for (int i = 0; i < m_visibleItems.count(); ++i) {
1108 ListItem *item = m_visibleItems[i];
1108 ListItem *item = m_visibleItems[i];
1109 if (item->sectionItem()) {
1109 if (item->sectionItem()) {
1110− QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1111− context->setContextProperty(QStringLiteral("delegateIndex"), m_firstVisibleIndex + i);
1110+ item->sectionItem()->setProperty("delegateIndex", m_firstVisibleIndex + i);
1112 }
1111 }
1113 }
1112 }
1114
1113
@@ -1270,8 +1269,7 @@ void ListViewWithPageHeader::layout()
1270 } else {
1269 } else {
1271 // Update the top sticky section header
1270 // Update the top sticky section header
1272 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1271 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1273− QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1272+ m_topSectionItem->setProperty("text", section);
1274− context->setContextProperty(QStringLiteral("section"), section);
1275
1273
1276 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1274 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1277 m_topSectionItem->setY(topSectionStickPos);
1275 m_topSectionItem->setY(topSectionStickPos);
@@ -1283,19 +1281,18 @@ void ListViewWithPageHeader::layout()
1283 break;
1281 break;
1284 delegateIndex--;
1282 delegateIndex--;
1285 }
1283 }
1286− context->setContextProperty(QStringLiteral("delegateIndex"), delegateIndex);
1284+ m_topSectionItem->setProperty("delegateIndex", delegateIndex);
1287 if (item->sectionItem()) {
1285 if (item->sectionItem()) {
1288 QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
1286 QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
1289 }
1287 }
1290 }
1288 }
1291 }
1289 }
1292 }
1290 }
1293− QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1294 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1291 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1295 if (!cull && pos < clipFrom) {
1292 if (!cull && pos < clipFrom) {
1296− context->setContextProperty(QStringLiteral("heightToClip"), clipFrom - pos);
1293+ item->m_item->setProperty("heightToClip", clipFrom - pos);
1297 } else {
1294 } else {
1298− context->setContextProperty(QStringLiteral("heightToClip"), QVariant::fromValue<int>(0));
1295+ item->m_item->setProperty("heightToClip", QVariant::fromValue<int>(0));
1299 }
1296 }
1300// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1297// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1301 pos += item->height();
1298 pos += item->height();
qml/Dash/DashCategoryBase.qml+3−11
@@ -24,17 +24,9 @@ Item {
24 /* Relevant really only for ListViewWithPageHeader case: specify how many pixels we can overlap with the section header */
24 /* Relevant really only for ListViewWithPageHeader case: specify how many pixels we can overlap with the section header */
25 readonly property int allowedOverlap: units.dp(1)
25 readonly property int allowedOverlap: units.dp(1)
26
26
27− property real __heightToClip: {
27+ property real heightToClip: 0
28− // Check this is in position where clipping is needed
29− if (typeof ListViewWithPageHeader !== 'undefined') {
30− if (typeof heightToClip !== 'undefined') {
31− if (heightToClip >= allowedOverlap) {
28+ readonly property real __heightToClip: heightToClip >= allowedOverlap ? heightToClip - allowedOverlap : 0
32− return heightToClip - allowedOverlap;
29+
33− }
34− }
35− }
36− return 0;
37− }
38
30
39 /*!
31 /*!
40 \internal
32 \internal
qml/Dash/GenericScopeView.qml+2−2
@@ -604,10 +604,10 @@ FocusScope {
604 sectionProperty: "name"
604 sectionProperty: "name"
605 sectionDelegate: ListItems.Header {
605 sectionDelegate: ListItems.Header {
606 objectName: "dashSectionHeader" + (delegate ? delegate.category : "")
606 objectName: "dashSectionHeader" + (delegate ? delegate.category : "")
607+ property int delegateIndex: -1
607 readonly property var delegate: categoryView.item(delegateIndex)
608 readonly property var delegate: categoryView.item(delegateIndex)
608 width: categoryView.width
609 width: categoryView.width
609− height: section != "" ? units.gu(5) : 0
610+ height: text != "" ? units.gu(5) : 0
610− text: section
611 color: scopeStyle ? scopeStyle.foreground : theme.palette.normal.baseText
611 color: scopeStyle ? scopeStyle.foreground : theme.palette.normal.baseText
612 iconName: delegate && delegate.headerLink ? "go-next" : ""
612 iconName: delegate && delegate.headerLink ? "go-next" : ""
613 onClicked: {
613 onClicked: {
tests/plugins/Dash/listviewwithpageheadersectiontest.cpp+2−2
@@ -116,12 +116,12 @@ private:
116
116
117 QString section(QQuickItem *item) const
117 QString section(QQuickItem *item) const
118 {
118 {
119− return item ? QQmlEngine::contextForObject(item)->parentContext()->contextProperty(QLatin1String("section")).toString() : QString();
119+ return item ? item->property("text").toString() : QString();
120 }
120 }
121
121
122 int sectionDelegateIndex(QQuickItem *item) const
122 int sectionDelegateIndex(QQuickItem *item) const
123 {
123 {
124− return item ? QQmlEngine::contextForObject(item)->parentContext()->contextProperty(QLatin1String("delegateIndex")).toInt() : -1;
124+ return item ? item->property("delegateIndex").toInt() : -1;
125 }
125 }
126
126
127private Q_SLOTS:
127private Q_SLOTS:
tests/plugins/Dash/listviewwithpageheadertestsection.qml+2−1
@@ -97,9 +97,10 @@ Rectangle {
97 sectionProperty: "type"
97 sectionProperty: "type"
98 sectionDelegate: Component {
98 sectionDelegate: Component {
99 Rectangle {
99 Rectangle {
100+ property alias text: theText.text
100 color: "green"
101 color: "green"
101 height: 40
102 height: 40
102− Text { text: section; font.pixelSize: 34 }
103+ Text { id: theText; font.pixelSize: 34 }
103 anchors { left: parent.left; right: parent.right }
104 anchors { left: parent.left; right: parent.right }
104 }
105 }
105 }
106 }
tests/plugins/Dash/tst_ListViewWithPageHeaderQML.qml+4−2
@@ -81,9 +81,10 @@ Rectangle {
81 sectionDelegate: Component {
81 sectionDelegate: Component {
82 id: sectionHeaderComponent
82 id: sectionHeaderComponent
83 Rectangle {
83 Rectangle {
84+ property alias text: theText.text
84 color: "green"
85 color: "green"
85 height: listView.sectionHeaderHeight
86 height: listView.sectionHeaderHeight
86− Text { text: section; font.pixelSize: 34 }
87+ Text { id: theText; font.pixelSize: 34 }
87 anchors { left: parent.left; right: parent.right }
88 anchors { left: parent.left; right: parent.right }
88 }
89 }
89 }
90 }
@@ -92,9 +93,10 @@ Rectangle {
92 Component {
93 Component {
93 id: otherSectionHeaderComponent
94 id: otherSectionHeaderComponent
94 Rectangle {
95 Rectangle {
96+ property alias text: theText.text
95 color: "green"
97 color: "green"
96 height: 50
98 height: 50
97− Text { text: section; font.pixelSize: 34 }
99+ Text { id: theText; font.pixelSize: 34 }
98 anchors { left: parent.left; right: parent.right }
100 anchors { left: parent.left; right: parent.right }
99 }
101 }
100 }
102 }