Skip to content

Commit

Permalink
[backport] Fix raster change datasource
Browse files Browse the repository at this point in the history
Fixes #44929

Cherry-picked from master 30f3a5d.
  • Loading branch information
elpaso authored and nyalldawson committed Nov 1, 2021
1 parent acdcd02 commit 286f8dd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/core/qgsmaplayer.cpp
Expand Up @@ -1792,7 +1792,7 @@ void QgsMapLayer::setLegend( QgsMapLayerLegend *legend )
if ( mLegend )
{
mLegend->setParent( this );
connect( mLegend, &QgsMapLayerLegend::itemsChanged, this, &QgsMapLayer::legendChanged );
connect( mLegend, &QgsMapLayerLegend::itemsChanged, this, &QgsMapLayer::legendChanged, Qt::UniqueConnection );
}

emit legendChanged();
Expand Down
2 changes: 1 addition & 1 deletion src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -601,7 +601,7 @@ void QgsRasterLayer::init()
{
mRasterType = QgsRasterLayer::GrayOrUndefined;

setLegend( QgsMapLayerLegend::defaultRasterLegend( this ) );
whileBlocking( this )->setLegend( QgsMapLayerLegend::defaultRasterLegend( this ) );

setRendererForDrawingStyle( QgsRaster::UndefinedDrawingStyle );

Expand Down
26 changes: 24 additions & 2 deletions src/core/raster/qgsrasterpipe.cpp
Expand Up @@ -96,7 +96,11 @@ bool QgsRasterPipe::insert( int idx, QgsRasterInterface *interface )
success = true;
mInterfaces.insert( idx, interface );
setRole( interface, idx );
QgsDebugMsgLevel( QStringLiteral( "inserted OK" ), 4 );
QgsDebugMsgLevel( QStringLiteral( "Pipe %1 inserted OK" ).arg( idx ), 4 );
}
else
{
QgsDebugMsgLevel( QStringLiteral( "Error inserting pipe %1" ).arg( idx ), 4 );
}

// Connect or reconnect (after the test) interfaces
Expand Down Expand Up @@ -158,6 +162,19 @@ void QgsRasterPipe::unsetRole( QgsRasterInterface *interface )
Role role = interfaceRole( interface );
if ( role == UnknownRole ) return;
mRoleMap.remove( role );

// Decrease all indexes greater than the removed one
const auto roleMapValues {mRoleMap.values()};
if ( roleIdx < *std::max_element( roleMapValues.begin(), roleMapValues.end() ) )
{
for ( auto it = mRoleMap.cbegin(); it != mRoleMap.cend(); ++it )
{
if ( it.value() > roleIdx )
{
mRoleMap[it.key()] = it.value() - 1;
}
}
}
}

bool QgsRasterPipe::set( QgsRasterInterface *interface )
Expand Down Expand Up @@ -283,11 +300,16 @@ bool QgsRasterPipe::remove( int idx )
unsetRole( mInterfaces.at( idx ) );
delete mInterfaces.at( idx );
mInterfaces.remove( idx );
QgsDebugMsgLevel( QStringLiteral( "removed OK" ), 4 );
QgsDebugMsgLevel( QStringLiteral( "Pipe %1 removed OK" ).arg( idx ), 4 );
}
else
{
QgsDebugMsgLevel( QStringLiteral( "Error removing pipe %1" ).arg( idx ), 4 );
}

// Connect or reconnect (after the test) interfaces
connect( mInterfaces );

return success;
}

Expand Down

0 comments on commit 286f8dd

Please sign in to comment.