Skip to content
Permalink
Browse files

Use switch instead of multiple if blocks for layer type logic

  • Loading branch information
nyalldawson committed Jan 28, 2019
1 parent b57c0fa commit 919e677d56315968fa6a2bf3debb5aaef3801be6

Large diffs are not rendered by default.

@@ -465,13 +465,19 @@ QTreeWidgetItem *QgsIdentifyResultsDialog::layerItem( QObject *object )

void QgsIdentifyResultsDialog::addFeature( const QgsMapToolIdentify::IdentifyResult &result )
{
if ( result.mLayer->type() == QgsMapLayer::VectorLayer )
switch ( result.mLayer->type() )
{
addFeature( qobject_cast<QgsVectorLayer *>( result.mLayer ), result.mFeature, result.mDerivedAttributes );
}
else if ( result.mLayer->type() == QgsMapLayer::RasterLayer )
{
addFeature( qobject_cast<QgsRasterLayer *>( result.mLayer ), result.mLabel, result.mAttributes, result.mDerivedAttributes, result.mFields, result.mFeature, result.mParams );
case QgsMapLayer::VectorLayer:
addFeature( qobject_cast<QgsVectorLayer *>( result.mLayer ), result.mFeature, result.mDerivedAttributes );
break;

case QgsMapLayer::RasterLayer:
addFeature( qobject_cast<QgsRasterLayer *>( result.mLayer ), result.mLabel, result.mAttributes, result.mDerivedAttributes, result.mFields, result.mFeature, result.mParams );
break;

case QgsMapLayer::MeshLayer:
case QgsMapLayer::PluginLayer:
break;
}
}

Large diffs are not rendered by default.

@@ -195,13 +195,17 @@ QVariant QgsLayerTreeModel::data( const QModelIndex &index, int role ) const
return QVariant();

// icons possibly overriding default icon
if ( layer->type() == QgsMapLayer::RasterLayer )
switch ( layer->type() )
{
return QgsLayerItem::iconRaster();
}
else if ( layer->type() == QgsMapLayer::MeshLayer )
{
return QgsLayerItem::iconMesh();
case QgsMapLayer::RasterLayer:
return QgsLayerItem::iconRaster();

case QgsMapLayer::MeshLayer:
return QgsLayerItem::iconMesh();

case QgsMapLayer::VectorLayer:
case QgsMapLayer::PluginLayer:
break;
}

QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
@@ -347,28 +347,37 @@ void QgsMapRendererJob::drawLabeling( const QgsMapSettings &settings, QgsRenderC

bool QgsMapRendererJob::needTemporaryImage( QgsMapLayer *ml )
{
if ( ml->type() == QgsMapLayer::VectorLayer )
switch ( ml->type() )
{
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
if ( vl->renderer() && vl->renderer()->forceRasterRender() )
case QgsMapLayer::VectorLayer:
{
//raster rendering is forced for this layer
return true;
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
if ( vl->renderer() && vl->renderer()->forceRasterRender() )
{
//raster rendering is forced for this layer
return true;
}
if ( mSettings.testFlag( QgsMapSettings::UseAdvancedEffects ) &&
( ( vl->blendMode() != QPainter::CompositionMode_SourceOver )
|| ( vl->featureBlendMode() != QPainter::CompositionMode_SourceOver )
|| ( !qgsDoubleNear( vl->opacity(), 1.0 ) ) ) )
{
//layer properties require rasterization
return true;
}
break;
}
if ( mSettings.testFlag( QgsMapSettings::UseAdvancedEffects ) &&
( ( vl->blendMode() != QPainter::CompositionMode_SourceOver )
|| ( vl->featureBlendMode() != QPainter::CompositionMode_SourceOver )
|| ( !qgsDoubleNear( vl->opacity(), 1.0 ) ) ) )
case QgsMapLayer::RasterLayer:
{
//layer properties require rasterization
return true;
// preview of intermediate raster rendering results requires a temporary output image
if ( mSettings.testFlag( QgsMapSettings::RenderPartialOutput ) )
return true;
break;
}
}
else if ( ml->type() == QgsMapLayer::RasterLayer )
{
// preview of intermediate raster rendering results requires a temporary output image
if ( mSettings.testFlag( QgsMapSettings::RenderPartialOutput ) )
return true;

case QgsMapLayer::MeshLayer:
case QgsMapLayer::PluginLayer:
break;
}

return false;
@@ -171,25 +171,33 @@ static void _addLayerTreeNodeToUriList( QgsLayerTreeNode *node, QgsMimeDataUtils
uri.name = layer->name();
uri.uri = layer->dataProvider()->dataSourceUri();
uri.providerKey = layer->dataProvider()->name();
if ( layer->type() == QgsMapLayer::VectorLayer )

switch ( layer->type() )
{
uri.layerType = QStringLiteral( "vector" );
if ( uri.providerKey == QStringLiteral( "memory" ) )
case QgsMapLayer::VectorLayer:
{
QUrl url = QUrl::fromEncoded( uri.uri.toUtf8() );
url.addQueryItem( QStringLiteral( "pid" ), QString::number( QCoreApplication::applicationPid() ) );
url.addQueryItem( QStringLiteral( "layerid" ), layer->id() );
uri.uri = QString( url.toEncoded() );
uri.layerType = QStringLiteral( "vector" );
if ( uri.providerKey == QStringLiteral( "memory" ) )
{
QUrl url = QUrl::fromEncoded( uri.uri.toUtf8() );
url.addQueryItem( QStringLiteral( "pid" ), QString::number( QCoreApplication::applicationPid() ) );
url.addQueryItem( QStringLiteral( "layerid" ), layer->id() );
uri.uri = QString( url.toEncoded() );
}
break;
}
case QgsMapLayer::RasterLayer:
{
uri.layerType = QStringLiteral( "raster" );
break;
}

case QgsMapLayer::MeshLayer:
case QgsMapLayer::PluginLayer:
{
// plugin layers do not have a standard way of storing their URI...
return;
}
}
else if ( layer->type() == QgsMapLayer::RasterLayer )
{
uri.layerType = QStringLiteral( "raster" );
}
else
{
// plugin layers do not have a standard way of storing their URI...
return;
}
uris << uri;
}
@@ -48,16 +48,27 @@ QgsLayerTreeOpacityWidget::QgsLayerTreeOpacityWidget( QgsMapLayer *layer )
connect( mSlider, &QAbstractSlider::valueChanged, this, &QgsLayerTreeOpacityWidget::sliderValueChanged );

// init from layer
if ( mLayer->type() == QgsMapLayer::VectorLayer )
switch ( mLayer->type() )
{
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayer );
mSlider->setValue( vl->opacity() * 1000.0 );
connect( vl, &QgsVectorLayer::opacityChanged, this, &QgsLayerTreeOpacityWidget::layerTrChanged );
}
else if ( mLayer->type() == QgsMapLayer::RasterLayer )
{
mSlider->setValue( 1000 - qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->opacity() * 1000 );
// TODO: there is no signal for raster layers
case QgsMapLayer::VectorLayer:
{
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayer );
mSlider->setValue( vl->opacity() * 1000.0 );
connect( vl, &QgsVectorLayer::opacityChanged, this, &QgsLayerTreeOpacityWidget::layerTrChanged );
break;
}

case QgsMapLayer::RasterLayer:
{
mSlider->setValue( 1000 - qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->opacity() * 1000 );
// TODO: there is no signal for raster layers
break;
}

case QgsMapLayer::PluginLayer:
case QgsMapLayer::MeshLayer:
break;

}
}

@@ -80,13 +91,22 @@ void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
{
int value = mSlider->value();

if ( mLayer->type() == QgsMapLayer::VectorLayer )
{
qobject_cast<QgsVectorLayer *>( mLayer )->setOpacity( value / 1000.0 );
}
else if ( mLayer->type() == QgsMapLayer::RasterLayer )
switch ( mLayer->type() )
{
qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->setOpacity( 1 - value / 1000.0 );
case QgsMapLayer::VectorLayer:
{
qobject_cast<QgsVectorLayer *>( mLayer )->setOpacity( value / 1000.0 );
break;
}
case QgsMapLayer::RasterLayer:
{
qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->setOpacity( 1 - value / 1000.0 );
break;
}

case QgsMapLayer::PluginLayer:
case QgsMapLayer::MeshLayer:
break;
}

mLayer->triggerRepaint();
@@ -119,7 +139,17 @@ QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget( Qg

bool QgsLayerTreeOpacityWidget::Provider::supportsLayer( QgsMapLayer *layer )
{
return layer->type() == QgsMapLayer::VectorLayer || layer->type() == QgsMapLayer::RasterLayer;
switch ( layer->type() )
{
case QgsMapLayer::VectorLayer:
case QgsMapLayer::RasterLayer:
return true;

case QgsMapLayer::MeshLayer:
case QgsMapLayer::PluginLayer:
return false;
}
return false;
}

///@endcond
@@ -189,26 +189,35 @@ void QgsBrowserLayerProperties::setItem( QgsDataItem *item )
// we need to create a temporary layer to get metadata
// we could use a provider but the metadata is not as complete and "pretty" and this is easier
QgsDebugMsg( QStringLiteral( "creating temporary layer using path %1" ).arg( layerItem->path() ) );
if ( type == QgsMapLayer::RasterLayer )
switch ( type )
{
QgsDebugMsg( QStringLiteral( "creating raster layer" ) );
// should copy code from addLayer() to split uri ?
mLayer = qgis::make_unique< QgsRasterLayer >( layerItem->uri(), layerItem->name(), layerItem->providerKey() );
}
else if ( type == QgsMapLayer::MeshLayer )
{
QgsDebugMsg( QStringLiteral( "creating mesh layer" ) );
mLayer = qgis::make_unique < QgsMeshLayer >( layerItem->uri(), layerItem->name(), layerItem->providerKey() );
}
else if ( type == QgsMapLayer::VectorLayer )
{
QgsDebugMsg( QStringLiteral( "creating vector layer" ) );
mLayer = qgis::make_unique < QgsVectorLayer>( layerItem->uri(), layerItem->name(), layerItem->providerKey() );
}
else if ( type == QgsMapLayer::PluginLayer )
{
// TODO: support display of properties for plugin layers
return;
case QgsMapLayer::RasterLayer:
{
QgsDebugMsg( QStringLiteral( "creating raster layer" ) );
// should copy code from addLayer() to split uri ?
mLayer = qgis::make_unique< QgsRasterLayer >( layerItem->uri(), layerItem->name(), layerItem->providerKey() );
break;
}

case QgsMapLayer::MeshLayer:
{
QgsDebugMsg( QStringLiteral( "creating mesh layer" ) );
mLayer = qgis::make_unique < QgsMeshLayer >( layerItem->uri(), layerItem->name(), layerItem->providerKey() );
break;
}

case QgsMapLayer::VectorLayer:
{
QgsDebugMsg( QStringLiteral( "creating vector layer" ) );
mLayer = qgis::make_unique < QgsVectorLayer>( layerItem->uri(), layerItem->name(), layerItem->providerKey() );
break;
}

case QgsMapLayer::PluginLayer:
{
// TODO: support display of properties for plugin layers
return;
}
}

mAttributeTable->setModel( nullptr );
@@ -108,16 +108,25 @@ QList<QgsMapToolIdentify::IdentifyResult> QgsIdentifyMenu::exec( const QList<Qgs
++count;
it.next();
QgsMapLayer *layer = it.key();
if ( layer->type() == QgsMapLayer::RasterLayer )
switch ( layer->type() )
{
addRasterLayer( layer );
}
else if ( layer->type() == QgsMapLayer::VectorLayer )
{
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
if ( !vl )
continue;
addVectorLayer( vl, it.value(), singleLayer );
case QgsMapLayer::RasterLayer:
{
addRasterLayer( layer );
break;
}
case QgsMapLayer::VectorLayer:
{
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
if ( !vl )
continue;
addVectorLayer( vl, it.value(), singleLayer );
break;
}

case QgsMapLayer::PluginLayer:
case QgsMapLayer::MeshLayer:
break;
}
}

0 comments on commit 919e677

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