diff options
author | Nils Jeisecke <jeisecke@saltation.de> | 2013-10-07 11:55:12 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-02-26 09:19:23 +0100 |
commit | 1061c7e5cfae88a336f1327cc20b2d8a0a3c6015 (patch) | |
tree | 058e8a07c1da7671ac8fcd9c91b91b386d106a2e /src/quick | |
parent | 7d25db8ff452926e58b7a66608666b35c194fc69 (diff) |
Make GridView/ListView attached "view" property available to all delegate types
This is useful for accessing the view (e.g. "width", "cellWidth", ... properties)
from within section (ListView only), header, footer and highlight delegate components.
A typical usecase are components that are used in multiple views and therefore
cannot use the views's id for access.
The only attached property valid for those non-item delegates is "view". This
has been added to the documentation.
Change-Id: I33d976da778be23ed531a3b193ceee95ed9800d2
Task-number: QTBUG-32836
Reviewed-by: Alan Alpert <aalpert@blackberry.com>
Diffstat (limited to 'src/quick')
-rw-r--r-- | src/quick/items/qquickgridview.cpp | 10 | ||||
-rw-r--r-- | src/quick/items/qquickgridview_p.h | 19 | ||||
-rw-r--r-- | src/quick/items/qquickitemview.cpp | 6 | ||||
-rw-r--r-- | src/quick/items/qquickitemview_p.h | 12 | ||||
-rw-r--r-- | src/quick/items/qquickitemview_p_p.h | 2 | ||||
-rw-r--r-- | src/quick/items/qquicklistview.cpp | 15 | ||||
-rw-r--r-- | src/quick/items/qquicklistview_p.h | 16 |
7 files changed, 36 insertions, 44 deletions
diff --git a/src/quick/items/qquickgridview.cpp b/src/quick/items/qquickgridview.cpp index 8587da5ac3..f97229a76a 100644 --- a/src/quick/items/qquickgridview.cpp +++ b/src/quick/items/qquickgridview.cpp @@ -65,8 +65,8 @@ QT_BEGIN_NAMESPACE class FxGridItemSG : public FxViewItem { public: - FxGridItemSG(QQuickItem *i, QQuickGridView *v, bool own) : FxViewItem(i, v, own), view(v) { - attached = static_cast<QQuickGridViewAttached*>(qmlAttachedPropertiesObject<QQuickGridView>(item)); + FxGridItemSG(QQuickItem *i, QQuickGridView *v, bool own) : FxViewItem(i, v, own, static_cast<QQuickItemViewAttached*>(qmlAttachedPropertiesObject<QQuickGridView>(i))), view(v) + { } qreal position() const { @@ -1283,7 +1283,8 @@ void QQuickGridView::setHighlightFollowsCurrentItem(bool autoHighlight) \qmlattachedproperty GridView QtQuick::GridView::view This attached property holds the view that manages this delegate instance. - It is attached to each instance of the delegate. + It is attached to each instance of the delegate and also to the header, the footer + and the highlight delegates. \snippet qml/gridview/gridview.qml isCurrentItem */ @@ -2127,6 +2128,9 @@ void QQuickGridView::geometryChanged(const QRectF &newGeometry, const QRectF &ol void QQuickGridView::initItem(int index, QObject *obj) { QQuickItemView::initItem(index, obj); + + // setting the view from the FxViewItem wrapper is too late if the delegate + // needs access to the view in Component.onCompleted QQuickItem *item = qmlobject_cast<QQuickItem*>(obj); if (item) { QQuickGridViewAttached *attached = static_cast<QQuickGridViewAttached *>( diff --git a/src/quick/items/qquickgridview_p.h b/src/quick/items/qquickgridview_p.h index 8f41f39919..ff0e2217fa 100644 --- a/src/quick/items/qquickgridview_p.h +++ b/src/quick/items/qquickgridview_p.h @@ -42,8 +42,6 @@ #ifndef QQUICKGRIDVIEW_P_H #define QQUICKGRIDVIEW_P_H -#include <QtCore/qpointer.h> - #include "qquickitemview_p.h" @@ -118,23 +116,8 @@ class QQuickGridViewAttached : public QQuickItemViewAttached Q_OBJECT public: QQuickGridViewAttached(QObject *parent) - : QQuickItemViewAttached(parent), m_view(0) {} + : QQuickItemViewAttached(parent) {} ~QQuickGridViewAttached() {} - - Q_PROPERTY(QQuickGridView *view READ view NOTIFY viewChanged) - QQuickGridView *view() { return m_view; } - void setView(QQuickGridView *view) { - if (view != m_view) { - m_view = view; - Q_EMIT viewChanged(); - } - } - -Q_SIGNALS: - void viewChanged(); - -public: - QPointer<QQuickGridView> m_view; }; diff --git a/src/quick/items/qquickitemview.cpp b/src/quick/items/qquickitemview.cpp index 30d0619f7a..020d48613b 100644 --- a/src/quick/items/qquickitemview.cpp +++ b/src/quick/items/qquickitemview.cpp @@ -51,15 +51,17 @@ QT_BEGIN_NAMESPACE #define QML_VIEW_DEFAULTCACHEBUFFER 320 #endif -FxViewItem::FxViewItem(QQuickItem *i, QQuickItemView *v, bool own) +FxViewItem::FxViewItem(QQuickItem *i, QQuickItemView *v, bool own, QQuickItemViewAttached *attached) : item(i) , view(v) , transitionableItem(0) - , attached(0) + , attached(attached) , ownItem(own) , releaseAfterTransition(false) , trackGeom(false) { + if (attached) // can be null for default components (see createComponentItem) + attached->setView(view); } FxViewItem::~FxViewItem() diff --git a/src/quick/items/qquickitemview_p.h b/src/quick/items/qquickitemview_p.h index ad026a3152..57635d7780 100644 --- a/src/quick/items/qquickitemview_p.h +++ b/src/quick/items/qquickitemview_p.h @@ -43,6 +43,7 @@ #define QQUICKITEMVIEW_P_H #include "qquickflickable_p.h" +#include <qpointer.h> QT_BEGIN_NAMESPACE @@ -282,6 +283,7 @@ class Q_AUTOTEST_EXPORT QQuickItemViewAttached : public QObject { Q_OBJECT + Q_PROPERTY(QQuickItemView *view READ view NOTIFY viewChanged) Q_PROPERTY(bool isCurrentItem READ isCurrentItem NOTIFY currentItemChanged) Q_PROPERTY(bool delayRemove READ delayRemove WRITE setDelayRemove NOTIFY delayRemoveChanged) @@ -294,6 +296,14 @@ public: : QObject(parent), m_isCurrent(false), m_delayRemove(false) {} ~QQuickItemViewAttached() {} + QQuickItemView *view() { return m_view; } + void setView(QQuickItemView *view) { + if (view != m_view) { + m_view = view; + Q_EMIT viewChanged(); + } + } + bool isCurrentItem() const { return m_isCurrent; } void setIsCurrentItem(bool c) { if (m_isCurrent != c) { @@ -353,6 +363,7 @@ public: void emitRemove() { Q_EMIT remove(); } Q_SIGNALS: + void viewChanged(); void currentItemChanged(); void delayRemoveChanged(); @@ -364,6 +375,7 @@ Q_SIGNALS: void nextSectionChanged(); public: + QPointer<QQuickItemView> m_view; bool m_isCurrent : 1; bool m_delayRemove : 1; diff --git a/src/quick/items/qquickitemview_p_p.h b/src/quick/items/qquickitemview_p_p.h index af7c23c02e..a0d17b3bf9 100644 --- a/src/quick/items/qquickitemview_p_p.h +++ b/src/quick/items/qquickitemview_p_p.h @@ -56,7 +56,7 @@ QT_BEGIN_NAMESPACE class FxViewItem { public: - FxViewItem(QQuickItem *, QQuickItemView *, bool own); + FxViewItem(QQuickItem *, QQuickItemView *, bool own, QQuickItemViewAttached *attached); virtual ~FxViewItem(); qreal itemX() const; diff --git a/src/quick/items/qquicklistview.cpp b/src/quick/items/qquicklistview.cpp index 2dd61e386f..3ac28f438b 100644 --- a/src/quick/items/qquicklistview.cpp +++ b/src/quick/items/qquicklistview.cpp @@ -247,16 +247,14 @@ void QQuickViewSection::setLabelPositioning(int l) class FxListItemSG : public FxViewItem { public: - FxListItemSG(QQuickItem *i, QQuickListView *v, bool own) : FxViewItem(i, v, own), view(v) { - attached = static_cast<QQuickListViewAttached*>(qmlAttachedPropertiesObject<QQuickListView>(item)); + FxListItemSG(QQuickItem *i, QQuickListView *v, bool own) : FxViewItem(i, v, own, static_cast<QQuickItemViewAttached*>(qmlAttachedPropertiesObject<QQuickListView>(i))), view(v) + { } inline QQuickItem *section() const { return attached ? static_cast<QQuickListViewAttached*>(attached)->m_sectionItem : 0; } void setSection(QQuickItem *s) { - if (!attached) - attached = static_cast<QQuickListViewAttached*>(qmlAttachedPropertiesObject<QQuickListView>(item)); static_cast<QQuickListViewAttached*>(attached)->m_sectionItem = s; } @@ -972,6 +970,9 @@ QQuickItem * QQuickListViewPrivate::getSectionItem(const QString §ion) QQml_setParent_noEvent(sectionItem, contentItem); sectionItem->setParentItem(contentItem); } + // sections are not controlled by FxListItemSG, so apply attached properties here + QQuickItemViewAttached *attached = static_cast<QQuickItemViewAttached*>(qmlAttachedPropertiesObject<QQuickListView>(sectionItem)); + attached->setView(q); } else { delete context; } @@ -1787,7 +1788,8 @@ QQuickListView::~QQuickListView() \qmlattachedproperty ListView QtQuick::ListView::view This attached property holds the view that manages this delegate instance. - It is attached to each instance of the delegate. + It is attached to each instance of the delegate and also to the header, the footer, + the section and the highlight delegates. */ /*! @@ -2855,6 +2857,9 @@ void QQuickListView::geometryChanged(const QRectF &newGeometry, const QRectF &ol void QQuickListView::initItem(int index, QObject *object) { QQuickItemView::initItem(index, object); + + // setting the view from the FxViewItem wrapper is too late if the delegate + // needs access to the view in Component.onCompleted QQuickItem *item = qmlobject_cast<QQuickItem*>(object); if (item) { QQuickListViewAttached *attached = static_cast<QQuickListViewAttached *>( diff --git a/src/quick/items/qquicklistview_p.h b/src/quick/items/qquicklistview_p.h index b00501d274..2494e101cd 100644 --- a/src/quick/items/qquicklistview_p.h +++ b/src/quick/items/qquicklistview_p.h @@ -43,7 +43,6 @@ #define QQUICKLISTVIEW_P_H #include "qquickitemview_p.h" -#include <qpointer.h> QT_BEGIN_NAMESPACE @@ -175,23 +174,10 @@ class QQuickListViewAttached : public QQuickItemViewAttached public: QQuickListViewAttached(QObject *parent) - : QQuickItemViewAttached(parent), m_view(0), m_sectionItem(0) {} + : QQuickItemViewAttached(parent), m_sectionItem(0) {} ~QQuickListViewAttached() {} - Q_PROPERTY(QQuickListView *view READ view NOTIFY viewChanged) - QQuickListView *view() { return m_view; } - void setView(QQuickListView *view) { - if (view != m_view) { - m_view = view; - Q_EMIT viewChanged(); - } - } - -Q_SIGNALS: - void viewChanged(); - public: - QPointer<QQuickListView> m_view; QQuickItem *m_sectionItem; }; |