Skip to content

Commit

Permalink
browser: fix mModel vs. mProxyModel index
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennesky committed Nov 29, 2012
1 parent 86456f3 commit 81f523a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
49 changes: 16 additions & 33 deletions src/app/qgsbrowserdockwidget.cpp
Expand Up @@ -269,18 +269,9 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
{
mModel = new QgsBrowserModel( mBrowserView );

bool useFilter = true;
if ( useFilter ) // enable proxy model
{
// mBrowserView->setModel( mModel );
mProxyModel = new QgsBrowserTreeFilterProxyModel( this );
mProxyModel->setBrowserModel( mModel );
mBrowserView->setModel( mProxyModel );
}
else
{
mBrowserView->setModel( mModel );
}
mProxyModel = new QgsBrowserTreeFilterProxyModel( this );
mProxyModel->setBrowserModel( mModel );
mBrowserView->setModel( mProxyModel );
// provide a horizontal scroll bar instead of using ellipse (...) for longer items
mBrowserView->setTextElideMode( Qt::ElideNone );
mBrowserView->header()->setResizeMode( 0, QHeaderView::ResizeToContents );
Expand All @@ -301,8 +292,8 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )

void QgsBrowserDockWidget::showContextMenu( const QPoint & pt )
{
QModelIndex idx = mBrowserView->indexAt( pt );
QgsDataItem* item = dataItem( idx );
QModelIndex index = mProxyModel->mapToSource( mBrowserView->indexAt( pt ) );
QgsDataItem* item = mModel->dataItem( index );
if ( !item )
return;

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

void QgsBrowserDockWidget::addFavourite()
{
QgsDataItem* item = dataItem( mBrowserView->currentIndex() );
QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
QgsDataItem* item = mModel->dataItem( index );
if ( !item )
return;

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

void QgsBrowserDockWidget::removeFavourite()
{
QgsDataItem* item = dataItem( mBrowserView->currentIndex() );
QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
QgsDataItem* item = mModel->dataItem( index );

if ( !item )
return;
Expand Down Expand Up @@ -420,7 +413,7 @@ void QgsBrowserDockWidget::refreshModel( const QModelIndex& index )
QgsDebugMsg( "Entered" );
if ( index.isValid() )
{
QgsDataItem *item = dataItem( index );
QgsDataItem *item = mModel->dataItem( index );
if ( item )
{
QgsDebugMsg( "path = " + item->path() );
Expand All @@ -436,7 +429,8 @@ void QgsBrowserDockWidget::refreshModel( const QModelIndex& index )
for ( int i = 0 ; i < mModel->rowCount( index ); i++ )
{
QModelIndex idx = mModel->index( i, 0, index );
if ( mBrowserView->isExpanded( idx ) || !mModel->hasChildren( idx ) )
QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
if ( mBrowserView->isExpanded( proxyIdx ) || !mModel->hasChildren( proxyIdx ) )
{
refreshModel( idx );
}
Expand Down Expand Up @@ -468,7 +462,7 @@ void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem )

void QgsBrowserDockWidget::addLayerAtIndex( const QModelIndex& index )
{
QgsDataItem *item = dataItem( index );
QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( index ) );

if ( item != NULL && item->type() == QgsDataItem::Layer )
{
Expand Down Expand Up @@ -498,8 +492,7 @@ void QgsBrowserDockWidget::addSelectedLayers()
// add items in reverse order so they are in correct order in the layers dock
for ( int i = list.size() - 1; i >= 0; i-- )
{
QModelIndex index = list[i];
QgsDataItem *item = dataItem( index );
QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( list[i] ) );
if ( item && item->type() == QgsDataItem::Layer )
{
QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*>( item );
Expand All @@ -513,9 +506,8 @@ void QgsBrowserDockWidget::addSelectedLayers()

void QgsBrowserDockWidget::showProperties( )
{
QgsDebugMsg( "Entered" );
QModelIndex index = mBrowserView->currentIndex();
QgsDataItem* item = dataItem( index );
QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
QgsDataItem* item = mModel->dataItem( index );
if ( ! item )
return;

Expand Down Expand Up @@ -635,12 +627,3 @@ void QgsBrowserDockWidget::setFilterSyntax( QAction * action )
return;
mProxyModel->setFilterSyntax(( QRegExp::PatternSyntax ) action->data().toInt() );
}

QgsDataItem* QgsBrowserDockWidget::dataItem( const QModelIndex& index )
{
if ( ! mProxyModel )
return mModel->dataItem( index );
else
return mModel->dataItem( mProxyModel->mapToSource( index ) );
}

3 changes: 2 additions & 1 deletion src/app/qgsbrowserdockwidget.h
Expand Up @@ -61,7 +61,8 @@ class QgsBrowserDockWidget : public QDockWidget, private Ui::QgsBrowserDockWidge

void addLayer( QgsLayerItem *layerItem );

QgsDataItem* dataItem( const QModelIndex& index );
// removed dataItem(), call mModel->dataItem directly (to avoid passing index from the wrong model)

QgsBrowserTreeView* mBrowserView;
QgsBrowserModel* mModel;
QgsBrowserTreeFilterProxyModel* mProxyModel;
Expand Down

0 comments on commit 81f523a

Please sign in to comment.