@@ -108,8 +108,37 @@ QModelIndex QgsMapLayerModel::indexFromLayer( QgsMapLayer *layer ) const
108108 return index ( r, 0 );
109109}
110110
111+ void QgsMapLayerModel::setAdditionalItems ( const QStringList& items )
112+ {
113+ if ( items == mAdditionalItems )
114+ return ;
115+
116+ int offset = 0 ;
117+ if ( mAllowEmpty )
118+ offset++;
119+
120+ offset += mLayers .count ();
121+
122+ // remove existing
123+ if ( !mAdditionalItems .isEmpty () )
124+ {
125+ beginRemoveRows ( QModelIndex (), offset, offset + mAdditionalItems .count () - 1 );
126+ mAdditionalItems .clear ();
127+ endRemoveRows ();
128+ }
129+
130+ // add new
131+ beginInsertRows ( QModelIndex (), offset, offset + items.count () - 1 );
132+ mAdditionalItems = items;
133+ endInsertRows ();
134+ }
135+
111136void QgsMapLayerModel::removeLayers ( const QStringList& layerIds )
112137{
138+ int offset = 0 ;
139+ if ( mAllowEmpty )
140+ offset++;
141+
113142 Q_FOREACH ( const QString& layerId, layerIds )
114143 {
115144 QModelIndex startIndex = index ( 0 , 0 );
@@ -119,7 +148,7 @@ void QgsMapLayerModel::removeLayers( const QStringList& layerIds )
119148 QModelIndex index = list[0 ];
120149 beginRemoveRows ( QModelIndex (), index.row (), index.row () );
121150 mLayersChecked .remove ( layerId );
122- mLayers .removeAt ( index.row () );
151+ mLayers .removeAt ( index.row () - offset );
123152 endRemoveRows ();
124153 }
125154 }
@@ -171,7 +200,7 @@ int QgsMapLayerModel::rowCount( const QModelIndex &parent ) const
171200 if ( parent.isValid () )
172201 return 0 ;
173202
174- return ( mAllowEmpty ? 1 : 0 ) + mLayers .length ();
203+ return ( mAllowEmpty ? 1 : 0 ) + mLayers .length () + mAdditionalItems . count () ;
175204}
176205
177206int QgsMapLayerModel::columnCount ( const QModelIndex &parent ) const
@@ -183,16 +212,20 @@ int QgsMapLayerModel::columnCount( const QModelIndex &parent ) const
183212
184213QVariant QgsMapLayerModel::data ( const QModelIndex &index, int role ) const
185214{
186- bool isEmpty = index.row () == 0 && mAllowEmpty ;
187-
188215 if ( !index.isValid () )
189216 return QVariant ();
190217
218+ bool isEmpty = index.row () == 0 && mAllowEmpty ;
219+ int additionalIndex = index.row () - ( mAllowEmpty ? 1 : 0 ) - mLayers .count ();
220+
191221 if ( role == Qt::DisplayRole )
192222 {
193223 if ( index.row () == 0 && mAllowEmpty )
194224 return QVariant ();
195225
226+ if ( additionalIndex >= 0 )
227+ return mAdditionalItems .at ( additionalIndex );
228+
196229 QgsMapLayer* layer = static_cast <QgsMapLayer*>( index.internalPointer () );
197230 if ( !layer )
198231 return QVariant ();
@@ -209,7 +242,7 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
209242
210243 if ( role == LayerIdRole )
211244 {
212- if ( isEmpty )
245+ if ( isEmpty || additionalIndex >= 0 )
213246 return QVariant ();
214247
215248 QgsMapLayer* layer = static_cast <QgsMapLayer*>( index.internalPointer () );
@@ -218,7 +251,7 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
218251
219252 if ( role == LayerRole )
220253 {
221- if ( isEmpty )
254+ if ( isEmpty || additionalIndex >= 0 )
222255 return QVariant ();
223256
224257 return QVariant::fromValue<QgsMapLayer*>( static_cast <QgsMapLayer*>( index.internalPointer () ) );
@@ -227,9 +260,12 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
227260 if ( role == IsEmptyRole )
228261 return isEmpty;
229262
263+ if ( role == IsAdditionalRole )
264+ return additionalIndex >= 0 ;
265+
230266 if ( role == Qt::CheckStateRole && mItemCheckable )
231267 {
232- if ( isEmpty )
268+ if ( isEmpty || additionalIndex >= 0 )
233269 return QVariant ();
234270
235271 QgsMapLayer* layer = static_cast <QgsMapLayer*>( index.internalPointer () );
@@ -238,7 +274,7 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
238274
239275 if ( role == Qt::DecorationRole )
240276 {
241- if ( isEmpty )
277+ if ( isEmpty || additionalIndex >= 0 )
242278 return QVariant ();
243279
244280 QgsMapLayer* layer = static_cast <QgsMapLayer*>( index.internalPointer () );
@@ -315,9 +351,10 @@ Qt::ItemFlags QgsMapLayerModel::flags( const QModelIndex &index ) const
315351 }
316352
317353 bool isEmpty = index.row () == 0 && mAllowEmpty ;
354+ int additionalIndex = index.row () - ( mAllowEmpty ? 1 : 0 ) - mLayers .count ();
318355
319356 Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
320- if ( mItemCheckable && !isEmpty )
357+ if ( mItemCheckable && !isEmpty && additionalIndex < 0 )
321358 {
322359 flags |= Qt::ItemIsUserCheckable;
323360 }
@@ -328,8 +365,9 @@ Qt::ItemFlags QgsMapLayerModel::flags( const QModelIndex &index ) const
328365bool QgsMapLayerModel::setData ( const QModelIndex &index, const QVariant &value, int role )
329366{
330367 bool isEmpty = index.row () == 0 && mAllowEmpty ;
368+ int additionalIndex = index.row () - ( mAllowEmpty ? 1 : 0 ) - mLayers .count ();
331369
332- if ( role == Qt::CheckStateRole && !isEmpty )
370+ if ( role == Qt::CheckStateRole && !isEmpty && additionalIndex < 0 )
333371 {
334372 QgsMapLayer* layer = static_cast <QgsMapLayer*>( index.internalPointer () );
335373 mLayersChecked [layer->id ()] = ( Qt::CheckState )value.toInt ();
0 commit comments