Skip to content

Commit

Permalink
qgisapp: Update raster layer style when render details have changed
Browse files Browse the repository at this point in the history
`QgsRasterLayer` updates the GUI (the renderer widget and the legend)
when the rendered values are updated. This does not work when multiple
canvas are displayed because each canvas causes a renderer update.

This issue is fixed by removing the GUI update from the raster layer
code. With this change, when the rendered values are updated, these
statistics are propagated to the canvas by the use of a
`QgsRenderedItemDetails`: `QgsRenderedCalculatedResults`. Then, only
the main canvas is notified of this change and updates the GUI
accordingly.
  • Loading branch information
ptitjano committed Nov 14, 2024
1 parent 8dcabd9 commit b444c01
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
38 changes: 38 additions & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@
#include "devtools/querylogger/qgsappquerylogger.h"
#include "devtools/querylogger/qgsqueryloggerwidgetfactory.h"
#include "devtools/profiler/qgsprofilerwidgetfactory.h"
#include "qgsrendereditemresults.h"
#include "qgsrenderedcalculatedresults.h"

#include "browser/qgsinbuiltdataitemproviders.h"

Expand Down Expand Up @@ -1071,6 +1073,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipBadLayers
mMapCanvas = new QgsMapCanvas( centralWidget );
mMapCanvas->setObjectName( QStringLiteral( "theMapCanvas" ) );
mMapCanvas->setFlags( Qgis::MapCanvasFlag::ShowMainAnnotationLayer );
connect( mMapCanvas, &QgsMapCanvas::calculatedRenderDetailsChanged, this, &QgisApp::onCalculatedRenderDetailsChanged );

// before anything, let's freeze canvas redraws
QgsCanvasRefreshBlocker refreshBlocker;
Expand Down Expand Up @@ -17588,3 +17591,38 @@ void QgisApp::showEvent( QShowEvent *event )
}
} );
}

void QgisApp::onCalculatedRenderDetailsChanged()
{
const QgsRenderedItemResults *renderedItemResults = mMapCanvas->renderedItemResults( false );
if ( !renderedItemResults )
{
return;
}

const QList<QgsRenderedItemDetails *> items = renderedItemResults->renderedItems();
QList<QString> computedLayerIds;
for ( const QgsRenderedItemDetails *item : renderedItemResults->renderedItems() )
{
if ( const QgsRenderedCalculatedResults *calculatedResults = dynamic_cast< const QgsRenderedCalculatedResults *>( item ) )
{
computedLayerIds.append( calculatedResults->layerId() );
}
}

if ( !computedLayerIds.empty() )
{
for ( QgsMapLayer *layer : mMapCanvas->layers() )
{
// refresh the legend and style of the raster layer
if ( QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( layer ) )
{
if ( computedLayerIds.contains( rasterLayer->id() ) )
{
rasterLayer->emitStyleChanged();
emit rasterLayer->rendererChanged();
}
}
}
}
}
2 changes: 2 additions & 0 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

void onSnappingConfigChanged();

void onCalculatedRenderDetailsChanged();

/**
* Triggers validation of the specified \a crs.
*/
Expand Down
5 changes: 0 additions & 5 deletions src/core/raster/qgsrasterlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1613,9 +1613,6 @@ bool QgsRasterLayer::refreshRenderer( QgsRasterRenderer *rasterRenderer, const Q
}
}

emit repaintRequested();
emitStyleChanged();
emit rendererChanged();
return true;
}

Expand Down Expand Up @@ -1650,8 +1647,6 @@ bool QgsRasterLayer::refreshRenderer( QgsRasterRenderer *rasterRenderer, const Q
}
}

emitStyleChanged();
emit rendererChanged();
refreshed = true;
}
}
Expand Down

0 comments on commit b444c01

Please sign in to comment.