Skip to content

Commit 81f523a

Browse files
committed
browser: fix mModel vs. mProxyModel index
1 parent 86456f3 commit 81f523a

File tree

2 files changed

+18
-34
lines changed

2 files changed

+18
-34
lines changed

src/app/qgsbrowserdockwidget.cpp

+16-33
Original file line numberDiff line numberDiff line change
@@ -269,18 +269,9 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
269269
{
270270
mModel = new QgsBrowserModel( mBrowserView );
271271

272-
bool useFilter = true;
273-
if ( useFilter ) // enable proxy model
274-
{
275-
// mBrowserView->setModel( mModel );
276-
mProxyModel = new QgsBrowserTreeFilterProxyModel( this );
277-
mProxyModel->setBrowserModel( mModel );
278-
mBrowserView->setModel( mProxyModel );
279-
}
280-
else
281-
{
282-
mBrowserView->setModel( mModel );
283-
}
272+
mProxyModel = new QgsBrowserTreeFilterProxyModel( this );
273+
mProxyModel->setBrowserModel( mModel );
274+
mBrowserView->setModel( mProxyModel );
284275
// provide a horizontal scroll bar instead of using ellipse (...) for longer items
285276
mBrowserView->setTextElideMode( Qt::ElideNone );
286277
mBrowserView->header()->setResizeMode( 0, QHeaderView::ResizeToContents );
@@ -301,8 +292,8 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
301292

302293
void QgsBrowserDockWidget::showContextMenu( const QPoint & pt )
303294
{
304-
QModelIndex idx = mBrowserView->indexAt( pt );
305-
QgsDataItem* item = dataItem( idx );
295+
QModelIndex index = mProxyModel->mapToSource( mBrowserView->indexAt( pt ) );
296+
QgsDataItem* item = mModel->dataItem( index );
306297
if ( !item )
307298
return;
308299

@@ -358,7 +349,8 @@ void QgsBrowserDockWidget::showContextMenu( const QPoint & pt )
358349

359350
void QgsBrowserDockWidget::addFavourite()
360351
{
361-
QgsDataItem* item = dataItem( mBrowserView->currentIndex() );
352+
QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
353+
QgsDataItem* item = mModel->dataItem( index );
362354
if ( !item )
363355
return;
364356

@@ -390,7 +382,8 @@ void QgsBrowserDockWidget::addFavouriteDirectory( QString favDir )
390382

391383
void QgsBrowserDockWidget::removeFavourite()
392384
{
393-
QgsDataItem* item = dataItem( mBrowserView->currentIndex() );
385+
QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
386+
QgsDataItem* item = mModel->dataItem( index );
394387

395388
if ( !item )
396389
return;
@@ -420,7 +413,7 @@ void QgsBrowserDockWidget::refreshModel( const QModelIndex& index )
420413
QgsDebugMsg( "Entered" );
421414
if ( index.isValid() )
422415
{
423-
QgsDataItem *item = dataItem( index );
416+
QgsDataItem *item = mModel->dataItem( index );
424417
if ( item )
425418
{
426419
QgsDebugMsg( "path = " + item->path() );
@@ -436,7 +429,8 @@ void QgsBrowserDockWidget::refreshModel( const QModelIndex& index )
436429
for ( int i = 0 ; i < mModel->rowCount( index ); i++ )
437430
{
438431
QModelIndex idx = mModel->index( i, 0, index );
439-
if ( mBrowserView->isExpanded( idx ) || !mModel->hasChildren( idx ) )
432+
QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
433+
if ( mBrowserView->isExpanded( proxyIdx ) || !mModel->hasChildren( proxyIdx ) )
440434
{
441435
refreshModel( idx );
442436
}
@@ -468,7 +462,7 @@ void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem )
468462

469463
void QgsBrowserDockWidget::addLayerAtIndex( const QModelIndex& index )
470464
{
471-
QgsDataItem *item = dataItem( index );
465+
QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( index ) );
472466

473467
if ( item != NULL && item->type() == QgsDataItem::Layer )
474468
{
@@ -498,8 +492,7 @@ void QgsBrowserDockWidget::addSelectedLayers()
498492
// add items in reverse order so they are in correct order in the layers dock
499493
for ( int i = list.size() - 1; i >= 0; i-- )
500494
{
501-
QModelIndex index = list[i];
502-
QgsDataItem *item = dataItem( index );
495+
QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( list[i] ) );
503496
if ( item && item->type() == QgsDataItem::Layer )
504497
{
505498
QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*>( item );
@@ -513,9 +506,8 @@ void QgsBrowserDockWidget::addSelectedLayers()
513506

514507
void QgsBrowserDockWidget::showProperties( )
515508
{
516-
QgsDebugMsg( "Entered" );
517-
QModelIndex index = mBrowserView->currentIndex();
518-
QgsDataItem* item = dataItem( index );
509+
QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
510+
QgsDataItem* item = mModel->dataItem( index );
519511
if ( ! item )
520512
return;
521513

@@ -635,12 +627,3 @@ void QgsBrowserDockWidget::setFilterSyntax( QAction * action )
635627
return;
636628
mProxyModel->setFilterSyntax(( QRegExp::PatternSyntax ) action->data().toInt() );
637629
}
638-
639-
QgsDataItem* QgsBrowserDockWidget::dataItem( const QModelIndex& index )
640-
{
641-
if ( ! mProxyModel )
642-
return mModel->dataItem( index );
643-
else
644-
return mModel->dataItem( mProxyModel->mapToSource( index ) );
645-
}
646-

src/app/qgsbrowserdockwidget.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class QgsBrowserDockWidget : public QDockWidget, private Ui::QgsBrowserDockWidge
6161

6262
void addLayer( QgsLayerItem *layerItem );
6363

64-
QgsDataItem* dataItem( const QModelIndex& index );
64+
// removed dataItem(), call mModel->dataItem directly (to avoid passing index from the wrong model)
65+
6566
QgsBrowserTreeView* mBrowserView;
6667
QgsBrowserModel* mModel;
6768
QgsBrowserTreeFilterProxyModel* mProxyModel;

0 commit comments

Comments
 (0)