Skip to content
Permalink
Browse files

Show layer path tooltips in QgsMapLayerComboBox

Handy for differentiating between multiple layers with the
same name
  • Loading branch information
nyalldawson committed Nov 16, 2016
1 parent 1da400a commit 089b663996b95f5205f55bd1b061b299914f170a
Showing with 102 additions and 79 deletions.
  1. +102 −79 src/core/qgsmaplayermodel.cpp
@@ -218,114 +218,137 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
bool isEmpty = index.row() == 0 && mAllowEmpty;
int additionalIndex = index.row() - ( mAllowEmpty ? 1 : 0 ) - mLayers.count();

if ( role == Qt::DisplayRole )
switch ( role )
{
if ( index.row() == 0 && mAllowEmpty )
return QVariant();
case Qt::DisplayRole:
{
if ( index.row() == 0 && mAllowEmpty )
return QVariant();

if ( additionalIndex >= 0 )
return mAdditionalItems.at( additionalIndex );
if ( additionalIndex >= 0 )
return mAdditionalItems.at( additionalIndex );

QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
if ( !layer )
return QVariant();
QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
if ( !layer )
return QVariant();

if ( !mShowCrs )
{
return layer->name();
}
else
{
return tr( "%1 [%2]" ).arg( layer->name(), layer->crs().authid() );
if ( !mShowCrs )
{
return layer->name();
}
else
{
return tr( "%1 [%2]" ).arg( layer->name(), layer->crs().authid() );
}
}
}

if ( role == LayerIdRole )
{
if ( isEmpty || additionalIndex >= 0 )
return QVariant();
case LayerIdRole:
{
if ( isEmpty || additionalIndex >= 0 )
return QVariant();

QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
return layer ? layer->id() : QVariant();
}
QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
return layer ? layer->id() : QVariant();
}

if ( role == LayerRole )
{
if ( isEmpty || additionalIndex >= 0 )
return QVariant();
case LayerRole:
{
if ( isEmpty || additionalIndex >= 0 )
return QVariant();

return QVariant::fromValue<QgsMapLayer*>( static_cast<QgsMapLayer*>( index.internalPointer() ) );
}
return QVariant::fromValue<QgsMapLayer*>( static_cast<QgsMapLayer*>( index.internalPointer() ) );
}

if ( role == EmptyRole )
return isEmpty;
case EmptyRole:
return isEmpty;

if ( role == AdditionalRole )
return additionalIndex >= 0;
case AdditionalRole:
return additionalIndex >= 0;

if ( role == Qt::CheckStateRole && mItemCheckable )
{
if ( isEmpty || additionalIndex >= 0 )
return QVariant();
case Qt::CheckStateRole:
{
if ( mItemCheckable )
{
if ( isEmpty || additionalIndex >= 0 )
return QVariant();

QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
return layer ? mLayersChecked[layer->id()] : QVariant();
}
QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
return layer ? mLayersChecked[layer->id()] : QVariant();
}

if ( role == Qt::DecorationRole )
{
if ( isEmpty || additionalIndex >= 0 )
return QVariant();
}

QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
if ( !layer )
case Qt::ToolTipRole:
{
QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
if ( layer )
{
QString tooltip = "<b>" +
( layer->title().isEmpty() ? layer->shortName() : layer->title() ) + "</b>";
if ( !layer->abstract().isEmpty() )
tooltip += "<br/>" + layer->abstract().replace( QLatin1String( "\n" ), QLatin1String( "<br/>" ) );
tooltip += "<br/><i>" + layer->publicSource() + "</i>";
return tooltip;
}
return QVariant();
}

QgsMapLayer::LayerType type = layer->type();
if ( role == Qt::DecorationRole )
case Qt::DecorationRole:
{
switch ( type )
{
case QgsMapLayer::RasterLayer:
{
return QgsLayerItem::iconRaster();
}
if ( isEmpty || additionalIndex >= 0 )
return QVariant();

case QgsMapLayer::VectorLayer:
QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
if ( !layer )
return QVariant();

QgsMapLayer::LayerType type = layer->type();
if ( role == Qt::DecorationRole )
{
switch ( type )
{
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );
if ( !vl )
case QgsMapLayer::RasterLayer:
{
return QIcon();
return QgsLayerItem::iconRaster();
}
QgsWkbTypes::GeometryType geomType = vl->geometryType();
switch ( geomType )

case QgsMapLayer::VectorLayer:
{
case QgsWkbTypes::PointGeometry:
{
return QgsLayerItem::iconPoint();
}
case QgsWkbTypes::PolygonGeometry :
{
return QgsLayerItem::iconPolygon();
}
case QgsWkbTypes::LineGeometry :
{
return QgsLayerItem::iconLine();
}
case QgsWkbTypes::NullGeometry :
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );
if ( !vl )
{
return QgsLayerItem::iconTable();
return QIcon();
}
default:
QgsWkbTypes::GeometryType geomType = vl->geometryType();
switch ( geomType )
{
return QIcon();
case QgsWkbTypes::PointGeometry:
{
return QgsLayerItem::iconPoint();
}
case QgsWkbTypes::PolygonGeometry :
{
return QgsLayerItem::iconPolygon();
}
case QgsWkbTypes::LineGeometry :
{
return QgsLayerItem::iconLine();
}
case QgsWkbTypes::NullGeometry :
{
return QgsLayerItem::iconTable();
}
default:
{
return QIcon();
}
}
}
}
default:
{
return QIcon();
default:
{
return QIcon();
}
}
}
}

0 comments on commit 089b663

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