Skip to content
Permalink
Browse files

Fix crash in attribute table when layer has no fields

  • Loading branch information
nyalldawson committed May 24, 2016
1 parent dadc9d2 commit eba4d03d09e80d55f6d351147194d66e3087b630
@@ -69,7 +69,7 @@ void QgsAttributeTableFilterModel::sort( int column, Qt::SortOrder order )

QVariant QgsAttributeTableFilterModel::data( const QModelIndex& index, int role ) const
{
if ( mColumnMapping.at( index.column() ) == -1 ) // actions
if ( mapColumnToSource( index.column() ) == -1 ) // actions
{
if ( role == TypeRole )
return ColumnTypeActionButton;
@@ -355,6 +355,14 @@ void QgsAttributeTableFilterModel::onColumnsChanged()
setAttributeTableConfig( mConfig );
}

int QgsAttributeTableFilterModel::mapColumnToSource( int column ) const
{
if ( mColumnMapping.isEmpty() )
return 0;
else
return mColumnMapping.at( column );
}

void QgsAttributeTableFilterModel::generateListOfVisibleFeatures()
{
if ( !layer() )
@@ -466,26 +474,29 @@ QModelIndex QgsAttributeTableFilterModel::mapToSource( const QModelIndex& proxyI
if ( !proxyIndex.isValid() )
return QModelIndex();

int sourceColumn = mColumnMapping.at( proxyIndex.column() );
int sourceColumn = mapColumnToSource( proxyIndex.column() );

// For the action column there is no matching column in the source model: invalid
if ( sourceColumn == -1 )
return QModelIndex();

return QSortFilterProxyModel::mapToSource( index( proxyIndex.row(), mColumnMapping.at( proxyIndex.column() ), proxyIndex.parent() ) );
return QSortFilterProxyModel::mapToSource( index( proxyIndex.row(), sourceColumn, proxyIndex.parent() ) );
}

QModelIndex QgsAttributeTableFilterModel::mapFromSource( const QModelIndex& sourceIndex ) const
{
QModelIndex proxyIndex = QSortFilterProxyModel::mapFromSource( sourceIndex );

return index( proxyIndex.row(), mColumnMapping.indexOf( proxyIndex.column() ), proxyIndex.parent() );
if ( proxyIndex.column() < 0 )
return QModelIndex();

return index( proxyIndex.row(), mapColumnToSource( proxyIndex.column() ), proxyIndex.parent() );
}

Qt::ItemFlags QgsAttributeTableFilterModel::flags( const QModelIndex& index ) const
{
// Handle the action column flags here, the master model doesn't know it
if ( mColumnMapping.at( index.column() ) == -1 )
if ( mapColumnToSource( index.column() ) == -1 )
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;

QModelIndex source_index = mapToSource( index );
@@ -255,6 +255,8 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub

QgsAttributeTableConfig mConfig;
QVector<int> mColumnMapping;
int mapColumnToSource( int column ) const;

};

#endif

0 comments on commit eba4d03

Please sign in to comment.
You can’t perform that action at this time.