2 Copyright (C) 2010 Klarälvdalens Datakonsult AB,
3 a KDAB Group company, info@kdab.net,
4 author Stephen Kelly <stephen@kdab.com>
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include "breadcrumbnavigationcontext.h"
24 #include "breadcrumbnavigation.h"
25 #include "kbreadcrumbselectionmodel.h"
26 #include "klinkitemselectionmodel.h"
27 #include "kmodelindexproxymapper.h"
28 #include <kdescendantsproxymodel.h>
29 #include "kselectionproxymodel.h"
30 #include "qmlcheckableproxymodel.h"
31 #include "qmllistselectionmodel.h"
33 #include <QtCore/QAbstractItemModel>
34 #include <QtDeclarative/QDeclarativeContext>
36 class KBreadcrumbNavigationFactoryPrivate
38 KBreadcrumbNavigationFactoryPrivate( KBreadcrumbNavigationFactory
*qq
)
40 m_breadcrumbSelectionModel( 0 ),
41 m_selectionModel( 0 ),
42 m_childItemsSelectionModel( 0 ),
43 m_breadcrumbModel( 0 ),
44 m_selectedItemModel( 0 ),
45 m_unfilteredChildItemsModel( 0 ),
46 m_childItemsModel( 0 ),
47 m_breadcrumbDepth( -1 ),
48 m_modelIndexProxyMapper( 0 ),
50 m_qmlBreadcrumbSelectionModel( 0 ),
51 m_qmlSelectedItemSelectionModel( 0 ),
52 m_qmlChildSelectionModel( 0 ),
53 m_qmlBreadcrumbCheckModel( 0 ),
54 m_qmlSelectedItemCheckModel( 0 ),
55 m_qmlChildCheckModel( 0 ),
56 m_checkedItemsModel( 0 ),
57 m_checkedItemsCheckModel( 0 ),
58 m_qmlCheckedItemsCheckModel( 0 )
62 Q_DECLARE_PUBLIC( KBreadcrumbNavigationFactory
)
63 KBreadcrumbNavigationFactory
* const q_ptr
;
65 QItemSelectionModel
*m_breadcrumbSelectionModel
;
66 QItemSelectionModel
*m_selectionModel
;
67 QItemSelectionModel
*m_childItemsSelectionModel
;
69 QAbstractItemModel
*m_breadcrumbModel
;
70 QAbstractItemModel
*m_selectedItemModel
;
71 QAbstractItemModel
*m_unfilteredChildItemsModel
;
72 QAbstractItemModel
*m_childItemsModel
;
73 int m_breadcrumbDepth
;
74 KModelIndexProxyMapper
*m_modelIndexProxyMapper
;
76 QItemSelectionModel
*m_checkModel
;
78 QMLListSelectionModel
*m_qmlBreadcrumbSelectionModel
;
79 QMLListSelectionModel
*m_qmlSelectedItemSelectionModel
;
80 QMLListSelectionModel
*m_qmlChildSelectionModel
;
82 QMLListSelectionModel
*m_qmlBreadcrumbCheckModel
;
83 QMLListSelectionModel
*m_qmlSelectedItemCheckModel
;
84 QMLListSelectionModel
*m_qmlChildCheckModel
;
86 KSelectionProxyModel
*m_checkedItemsModel
;
87 QItemSelectionModel
*m_checkedItemsCheckModel
;
88 QMLListSelectionModel
*m_qmlCheckedItemsCheckModel
;
92 KBreadcrumbNavigationFactory::KBreadcrumbNavigationFactory( QObject
*parent
)
94 d_ptr( new KBreadcrumbNavigationFactoryPrivate( this ) )
98 KBreadcrumbNavigationFactory::~KBreadcrumbNavigationFactory()
103 void KBreadcrumbNavigationFactory::createCheckableBreadcrumbContext( QAbstractItemModel
*model
, QObject
*parent
)
105 Q_D( KBreadcrumbNavigationFactory
);
107 d
->m_checkModel
= new QItemSelectionModel( model
, parent
);
109 QMLCheckableItemProxyModel
*checkableProxy
= new QMLCheckableItemProxyModel( parent
);
110 checkableProxy
->setSourceModel( model
);
111 checkableProxy
->setSelectionModel( d
->m_checkModel
);
113 KDescendantsProxyModel
*descProxy
= new KDescendantsProxyModel( parent
);
114 descProxy
->setDisplayAncestorData( true );
115 descProxy
->setSourceModel( checkableProxy
);
117 createBreadcrumbContext( checkableProxy
, parent
);
119 KLinkItemSelectionModel
*breadcrumbLinkSelectionModel
= new KLinkItemSelectionModel( d
->m_breadcrumbModel
, d
->m_checkModel
, parent
);
120 KLinkItemSelectionModel
*childLinkSelectionModel
= new KLinkItemSelectionModel( d
->m_childItemsModel
, d
->m_checkModel
, parent
);
121 KLinkItemSelectionModel
*selectedItemLinkSelectionModel
= new KLinkItemSelectionModel( d
->m_selectedItemModel
, d
->m_checkModel
, parent
);
123 d
->m_qmlBreadcrumbCheckModel
= new QMLListSelectionModel( breadcrumbLinkSelectionModel
, parent
);
124 d
->m_qmlSelectedItemCheckModel
= new QMLListSelectionModel( selectedItemLinkSelectionModel
, parent
);
125 d
->m_qmlChildCheckModel
= new QMLListSelectionModel( childLinkSelectionModel
, parent
);
127 d
->m_checkedItemsModel
= new KSelectionProxyModel( d
->m_checkModel
, parent
);
128 d
->m_checkedItemsModel
->setFilterBehavior( KSelectionProxyModel::ExactSelection
);
129 d
->m_checkedItemsModel
->setSourceModel( descProxy
);
131 d
->m_checkedItemsCheckModel
= new KLinkItemSelectionModel( d
->m_checkedItemsModel
, d
->m_checkModel
, parent
);
133 d
->m_qmlCheckedItemsCheckModel
= new QMLListSelectionModel( d
->m_checkedItemsCheckModel
, parent
);
136 void KBreadcrumbNavigationFactory::createBreadcrumbContext( QAbstractItemModel
*model
, QObject
*parent
)
138 Q_D(KBreadcrumbNavigationFactory
);
140 d
->m_selectionModel
= new QItemSelectionModel( model
, parent
);
141 connect( d
->m_selectionModel
, SIGNAL(selectionChanged(QItemSelection
,QItemSelection
)), SIGNAL(collectionSelectionChanged()) );
143 KSelectionProxyModel
*currentCollectionSelectionModel
= new KSelectionProxyModel( d
->m_selectionModel
, parent
);
144 currentCollectionSelectionModel
->setFilterBehavior( KSelectionProxyModel::ExactSelection
);
145 currentCollectionSelectionModel
->setSourceModel( model
);
146 d
->m_selectedItemModel
= currentCollectionSelectionModel
;
148 KBreadcrumbSelectionModel
*breadcrumbCollectionSelection
149 = new KBreadcrumbSelectionModel( d
->m_selectionModel
, KBreadcrumbSelectionModel::MakeBreadcrumbSelectionInOther
, parent
);
150 breadcrumbCollectionSelection
->setActualSelectionIncluded( false );
151 breadcrumbCollectionSelection
->setBreadcrumbLength( d
->m_breadcrumbDepth
);
153 KBreadcrumbNavigationProxyModel
*breadcrumbNavigationModel
154 = new KBreadcrumbNavigationProxyModel( breadcrumbCollectionSelection
, parent
);
155 breadcrumbNavigationModel
->setSourceModel( model
);
156 breadcrumbNavigationModel
->setFilterBehavior( KSelectionProxyModel::ExactSelection
);
157 d
->m_breadcrumbModel
= getBreadcrumbNavigationModel( breadcrumbNavigationModel
);
159 KLinkItemSelectionModel
*proxyBreadcrumbCollectionSelection
160 = new KLinkItemSelectionModel( d
->m_breadcrumbModel
, d
->m_selectionModel
, parent
);
162 d
->m_breadcrumbSelectionModel
= new KForwardingItemSelectionModel( d
->m_breadcrumbModel
,
163 proxyBreadcrumbCollectionSelection
,
164 KForwardingItemSelectionModel::Reverse
,
167 // Breadcrumbs done. (phew!)
169 KForwardingItemSelectionModel
*oneway
= new KForwardingItemSelectionModel( model
, d
->m_selectionModel
, parent
);
171 KNavigatingProxyModel
*navigatingProxyModel
= new KNavigatingProxyModel( oneway
, parent
);
172 navigatingProxyModel
->setSourceModel( model
);
173 d
->m_unfilteredChildItemsModel
= navigatingProxyModel
;
175 d
->m_childItemsModel
= getChildItemsModel( d
->m_unfilteredChildItemsModel
);
177 d
->m_childItemsSelectionModel
= new KLinkItemSelectionModel( d
->m_childItemsModel
, d
->m_selectionModel
, parent
);
179 d
->m_modelIndexProxyMapper
= new KModelIndexProxyMapper( model
, d
->m_childItemsModel
, parent
);
181 // Navigation stuff for QML:
183 d
->m_qmlBreadcrumbSelectionModel
= new QMLListSelectionModel( d
->m_breadcrumbSelectionModel
, parent
);
184 d
->m_qmlSelectedItemSelectionModel
= new QMLListSelectionModel( d
->m_selectionModel
, parent
);
185 d
->m_qmlChildSelectionModel
= new QMLListSelectionModel( d
->m_childItemsSelectionModel
, parent
);
187 connect( d
->m_selectionModel
, SIGNAL(selectionChanged(QItemSelection
,QItemSelection
)), SIGNAL(selectedDisplayTextChanged()) );
190 QItemSelectionModel
* KBreadcrumbNavigationFactory::selectionModel() const
192 Q_D( const KBreadcrumbNavigationFactory
);
193 return d
->m_selectionModel
;
196 QAbstractItemModel
* KBreadcrumbNavigationFactory::selectedItemModel() const
198 Q_D( const KBreadcrumbNavigationFactory
);
199 return d
->m_selectedItemModel
;
202 int KBreadcrumbNavigationFactory::breadcrumbDepth() const
204 Q_D( const KBreadcrumbNavigationFactory
);
205 return d
->m_breadcrumbDepth
;
208 void KBreadcrumbNavigationFactory::setBreadcrumbDepth( int depth
)
210 Q_D( KBreadcrumbNavigationFactory
);
211 d
->m_breadcrumbDepth
= depth
;
214 QAbstractItemModel
* KBreadcrumbNavigationFactory::breadcrumbItemModel() const
216 Q_D( const KBreadcrumbNavigationFactory
);
217 return d
->m_breadcrumbModel
;
220 QItemSelectionModel
* KBreadcrumbNavigationFactory::breadcrumbSelectionModel() const
222 Q_D( const KBreadcrumbNavigationFactory
);
223 return d
->m_breadcrumbSelectionModel
;
226 QAbstractItemModel
* KBreadcrumbNavigationFactory::childItemModel() const
228 Q_D( const KBreadcrumbNavigationFactory
);
229 return d
->m_childItemsModel
;
232 QAbstractItemModel
* KBreadcrumbNavigationFactory::unfilteredChildItemModel() const
234 Q_D( const KBreadcrumbNavigationFactory
);
235 return d
->m_unfilteredChildItemsModel
;
238 QAbstractItemModel
* KBreadcrumbNavigationFactory::getBreadcrumbNavigationModel( QAbstractItemModel
*model
)
243 QAbstractItemModel
* KBreadcrumbNavigationFactory::getChildItemsModel( QAbstractItemModel
*model
)
248 QItemSelectionModel
* KBreadcrumbNavigationFactory::childSelectionModel() const
250 Q_D( const KBreadcrumbNavigationFactory
);
251 return d
->m_childItemsSelectionModel
;
254 void KBreadcrumbNavigationFactory::selectBreadcrumb( int row
)
256 Q_D( KBreadcrumbNavigationFactory
);
259 d
->m_selectionModel
->clearSelection();
263 const QModelIndex index
= d
->m_breadcrumbModel
->index( row
, 0 );
264 d
->m_breadcrumbSelectionModel
->select( QItemSelection( index
, index
), QItemSelectionModel::Rows
| QItemSelectionModel::ClearAndSelect
);
267 void KBreadcrumbNavigationFactory::selectChild( int row
)
269 Q_D( KBreadcrumbNavigationFactory
);
272 d
->m_selectionModel
->clearSelection();
276 const QModelIndex index
= d
->m_childItemsModel
->index( row
, 0 );
277 d
->m_childItemsSelectionModel
->select( QItemSelection( index
, index
), QItemSelectionModel::Rows
| QItemSelectionModel::ClearAndSelect
);
280 bool KBreadcrumbNavigationFactory::childCollectionHasChildren( int row
) const
285 Q_D( const KBreadcrumbNavigationFactory
);
287 static const int column
= 0;
288 const QModelIndex index
= d
->m_modelIndexProxyMapper
->mapRightToLeft( d
->m_childItemsModel
->index( row
, column
) );
289 if ( !index
.isValid() )
292 return (index
.model()->rowCount( index
) > 0);
295 QObject
* KBreadcrumbNavigationFactory::qmlBreadcrumbSelectionModel() const
297 Q_D( const KBreadcrumbNavigationFactory
);
298 return d
->m_qmlBreadcrumbSelectionModel
;
301 QObject
* KBreadcrumbNavigationFactory::qmlBreadcrumbsModel() const
303 Q_D( const KBreadcrumbNavigationFactory
);
304 return d
->m_breadcrumbModel
;
307 QObject
* KBreadcrumbNavigationFactory::qmlChildItemsModel() const
309 Q_D( const KBreadcrumbNavigationFactory
);
310 return d
->m_childItemsModel
;
313 QObject
* KBreadcrumbNavigationFactory::qmlChildSelectionModel() const
315 Q_D( const KBreadcrumbNavigationFactory
);
316 return d
->m_qmlChildSelectionModel
;
319 QObject
* KBreadcrumbNavigationFactory::qmlSelectedItemModel() const
321 Q_D( const KBreadcrumbNavigationFactory
);
322 return d
->m_selectedItemModel
;
325 QObject
* KBreadcrumbNavigationFactory::qmlSelectionModel() const
327 Q_D( const KBreadcrumbNavigationFactory
);
328 return d
->m_qmlSelectedItemSelectionModel
;
331 QObject
* KBreadcrumbNavigationFactory::qmlBreadcrumbCheckModel() const
333 Q_D( const KBreadcrumbNavigationFactory
);
334 return d
->m_qmlBreadcrumbCheckModel
;
337 QObject
* KBreadcrumbNavigationFactory::qmlChildCheckModel() const
339 Q_D( const KBreadcrumbNavigationFactory
);
340 return d
->m_qmlChildCheckModel
;
343 QObject
* KBreadcrumbNavigationFactory::qmlSelectedItemCheckModel() const
345 Q_D( const KBreadcrumbNavigationFactory
);
346 return d
->m_qmlSelectedItemCheckModel
;
349 QItemSelectionModel
* KBreadcrumbNavigationFactory::checkedItemsCheckModel() const
351 Q_D( const KBreadcrumbNavigationFactory
);
352 return d
->m_checkedItemsCheckModel
;
355 QAbstractItemModel
* KBreadcrumbNavigationFactory::checkedItemsModel() const
357 Q_D( const KBreadcrumbNavigationFactory
);
358 return d
->m_checkedItemsModel
;
361 QItemSelectionModel
* KBreadcrumbNavigationFactory::checkModel() const
363 Q_D( const KBreadcrumbNavigationFactory
);
364 return d
->m_checkModel
;
367 QObject
* KBreadcrumbNavigationFactory::qmlCheckedItemsCheckModel() const
369 Q_D( const KBreadcrumbNavigationFactory
);
370 return d
->m_qmlCheckedItemsCheckModel
;
373 QObject
* KBreadcrumbNavigationFactory::qmlCheckedItemsModel() const
375 Q_D( const KBreadcrumbNavigationFactory
);
376 return d
->m_checkedItemsModel
;
379 QString
KBreadcrumbNavigationFactory::selectedDisplayText() const
381 Q_D( const KBreadcrumbNavigationFactory
);
382 return d
->m_selectedItemModel
->index( 0, 0 ).data().toString();
385 #include "breadcrumbnavigationcontext.moc"