From ae4d11d918938fd9087f2035dac247969c1f2313 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Sat, 21 Apr 2012 21:28:16 +0200 Subject: Prepare view-engine for non-KFileItem usecase Up to now the view-engine only provided a model-implementation that supports file-items. The view-engine always had been designed to be able to work with any kind of model, so now a KStandardItemModel is available. The plan is to convert the places panel to the new view-engine. It should be no problem to fix this until the feature freeze - in the worst case the places-panel code could be reverted while still keeping the KStandardItemModel changes. --- src/kitemviews/kitemlistview.h | 91 +++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 32 deletions(-) (limited to 'src/kitemviews/kitemlistview.h') diff --git a/src/kitemviews/kitemlistview.h b/src/kitemviews/kitemlistview.h index 13f62f89b..3c47e95c6 100644 --- a/src/kitemviews/kitemlistview.h +++ b/src/kitemviews/kitemlistview.h @@ -42,6 +42,7 @@ class KItemListRubberBand; class KItemListViewAnimation; class KItemListViewLayouter; class KItemListWidget; +class KItemListWidgetInformant; class KItemListWidgetCreatorBase; class KItemListViewCreatorBase; class QTimer; @@ -53,11 +54,8 @@ class QTimer; * a GraphicsItem. Each visible item is represented by a KItemListWidget. * * The created view must be applied to the KItemListController with - * KItemListController::setView(). For showing a custom model it is not - * mandatory to derive from KItemListView, all that is necessary is - * to set a widget-creator that is capable to create KItemListWidgets - * showing the model items. A widget-creator can be set with - * KItemListView::setWidgetCreator(). + * KItemListController::setView() or with the constructor of + * KItemListController. * * @see KItemListWidget * @see KItemModelBase @@ -73,17 +71,6 @@ public: KItemListView(QGraphicsWidget* parent = 0); virtual ~KItemListView(); - /** - * If the scroll-orientation is vertical, the items are ordered - * from top to bottom (= default setting). If the scroll-orientation - * is horizontal, the items are ordered from left to right. - */ - void setScrollOrientation(Qt::Orientation orientation); - Qt::Orientation scrollOrientation() const; - - void setItemSize(const QSizeF& size); - QSizeF itemSize() const; - /** * Offset of the scrollbar that represents the scroll-orientation * (see setScrollOrientation()). @@ -145,17 +132,27 @@ public: * * itemListView->setWidgetCreator(new KItemListWidgetCreator()); * - * Note that the ownership of the widget creator is not transferred to - * the item-list view: One instance of a widget creator might get shared - * by several item-list view instances. + * The ownership of the widget creator is transferred to + * the item-list view. **/ void setWidgetCreator(KItemListWidgetCreatorBase* widgetCreator); KItemListWidgetCreatorBase* widgetCreator() const; + /** + * Sets the creator that creates a group header. Usually it is sufficient + * to implement a custom header widget X derived from KItemListGroupHeader and + * set the creator by: + * + * itemListView->setGroupHeaderCreator(new KItemListGroupHeaderCreator()); + * + * The ownership of the gropup header creator is transferred to + * the item-list view. + **/ void setGroupHeaderCreator(KItemListGroupHeaderCreatorBase* groupHeaderCreator); KItemListGroupHeaderCreatorBase* groupHeaderCreator() const; - void setStyleOption(const KItemListStyleOption& option); + QSizeF itemSize() const; + const KItemListStyleOption& styleOption() const; /** @reimp */ @@ -313,6 +310,33 @@ signals: void roleEditingFinished(int index, const QByteArray& role, const QVariant& value); protected: + void setItemSize(const QSizeF& size); + void setStyleOption(const KItemListStyleOption& option); + + /** + * If the scroll-orientation is vertical, the items are ordered + * from top to bottom (= default setting). If the scroll-orientation + * is horizontal, the items are ordered from left to right. + */ + void setScrollOrientation(Qt::Orientation orientation); + Qt::Orientation scrollOrientation() const; + + /** + * Factory method for creating a default widget-creator. The method will be used + * in case if setWidgetCreator() has not been set by the application. + * @return New instance of the widget-creator that should be used per + * default. + */ + virtual KItemListWidgetCreatorBase* defaultWidgetCreator() const; + + /** + * Factory method for creating a default group-header-creator. The method will be used + * in case if setGroupHeaderCreator() has not been set by the application. + * @return New instance of the group-header-creator that should be used per + * default. + */ + virtual KItemListGroupHeaderCreatorBase* defaultGroupHeaderCreator() const; + /** * Is called when creating a new KItemListWidget instance and allows derived * classes to do a custom initialization. @@ -656,8 +680,8 @@ private: KItemListController* m_controller; KItemModelBase* m_model; QList m_visibleRoles; - KItemListWidgetCreatorBase* m_widgetCreator; - KItemListGroupHeaderCreatorBase* m_groupHeaderCreator; + mutable KItemListWidgetCreatorBase* m_widgetCreator; + mutable KItemListGroupHeaderCreatorBase* m_groupHeaderCreator; KItemListStyleOption m_styleOption; QHash m_visibleItems; @@ -747,18 +771,12 @@ public: /** * @brief Template class for creating KItemListWidgets. - * - * The template class must provide the following two static methods: - * - QSizeF itemSizeHint(int index, const KItemListView* view) - * - preferredRoleColumnWidth(const QByteArray& role, int index, const KItemListView* view) - * Those static methods are used as implementation for - * KItemListWidgetCreatorBase::itemSizeHint() and - * KItemListWidgetCreatorBase::preferedRoleColumnWidth(). */ template class KItemListWidgetCreator : public KItemListWidgetCreatorBase { public: + KItemListWidgetCreator(); virtual ~KItemListWidgetCreator(); virtual KItemListWidget* create(KItemListView* view); @@ -768,11 +786,20 @@ public: virtual qreal preferredRoleColumnWidth(const QByteArray& role, int index, const KItemListView* view) const; +private: + KItemListWidgetInformant* m_informant; }; +template +KItemListWidgetCreator::KItemListWidgetCreator() : + m_informant(T::createInformant()) +{ +} + template KItemListWidgetCreator::~KItemListWidgetCreator() { + delete m_informant; } template @@ -780,7 +807,7 @@ KItemListWidget* KItemListWidgetCreator::create(KItemListView* view) { KItemListWidget* widget = static_cast(popRecycleableWidget()); if (!widget) { - widget = new T(view); + widget = new T(m_informant, view); addCreatedWidget(widget); } return widget; @@ -789,7 +816,7 @@ KItemListWidget* KItemListWidgetCreator::create(KItemListView* view) template QSizeF KItemListWidgetCreator::itemSizeHint(int index, const KItemListView* view) const { - return T::itemSizeHint(index, view); + return m_informant->itemSizeHint(index, view); } template @@ -797,7 +824,7 @@ qreal KItemListWidgetCreator::preferredRoleColumnWidth(const QByteArray& role int index, const KItemListView* view) const { - return T::preferredRoleColumnWidth(role, index, view); + return m_informant->preferredRoleColumnWidth(role, index, view); } /** -- cgit v1.3.1