Skip to content

Commit 04d392b

Browse files
committed
Allow QgsMapLayer::triggerRepaint to defer updates
By calling QgsMapLayer::triggerRepaint( true ) any cached version of the layer will be invalidated, but a map canvas refresh won't automatically be triggered This allows invalidation of cached images while deferring the actual map update until the next canvas refresh.
1 parent 92091c5 commit 04d392b

10 files changed

+34
-24
lines changed

python/core/qgsmaplayer.sip

+2-11
Original file line numberDiff line numberDiff line change
@@ -634,13 +634,7 @@ class QgsMapLayer : QObject
634634
*/
635635
void setScaleBasedVisibility( const bool enabled );
636636

637-
/**
638-
* Will advice the map canvas (and any other interested party) that this layer requires to be repainted.
639-
* Will emit a repaintRequested() signal.
640-
*
641-
* @note in 2.6 function moved from vector/raster subclasses to QgsMapLayer
642-
*/
643-
void triggerRepaint();
637+
void triggerRepaint( bool deferredUpdate = false );
644638

645639
/** \brief Obtain Metadata for this layer */
646640
virtual QString metadata() const;
@@ -687,10 +681,7 @@ class QgsMapLayer : QObject
687681
/** Emit a signal that layer's CRS has been reset */
688682
void crsChanged();
689683

690-
/** By emitting this signal the layer tells that either appearance or content have been changed
691-
* and any view showing the rendered layer should refresh itself.
692-
*/
693-
void repaintRequested();
684+
void repaintRequested( bool deferredUpdate = false );
694685

695686
/** This is used to send a request that any mapcanvas using this layer update its extents */
696687
void recalculateExtents() const;

src/app/qgisapp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10408,7 +10408,7 @@ void QgisApp::layersWereAdded( const QList<QgsMapLayer *>& theLayers )
1040810408

1040910409
if ( provider )
1041010410
{
10411-
connect( provider, &QgsDataProvider::dataChanged, layer, &QgsMapLayer::triggerRepaint );
10411+
connect( provider, &QgsDataProvider::dataChanged, layer, [layer] { layer->triggerRepaint(); } );
1041210412
connect( provider, &QgsDataProvider::dataChanged, mMapCanvas, &QgsMapCanvas::refresh );
1041310413
}
1041410414
}

src/browser/qgsbrowser.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ void QgsBrowser::updateCurrentTab()
439439
QgsRasterLayer *rlayer = qobject_cast< QgsRasterLayer * >( mLayer );
440440
if ( rlayer )
441441
{
442-
connect( rlayer->dataProvider(), &QgsRasterDataProvider::dataChanged, rlayer, &QgsRasterLayer::triggerRepaint );
442+
connect( rlayer->dataProvider(), &QgsRasterDataProvider::dataChanged, rlayer, [rlayer] { rlayer->triggerRepaint(); } );
443443
connect( rlayer->dataProvider(), &QgsRasterDataProvider::dataChanged, mapCanvas, &QgsMapCanvas::refresh );
444444
}
445445
}

src/core/qgsmaplayer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1591,9 +1591,9 @@ QgsMapLayerStyleManager* QgsMapLayer::styleManager() const
15911591
return mStyleManager;
15921592
}
15931593

1594-
void QgsMapLayer::triggerRepaint()
1594+
void QgsMapLayer::triggerRepaint( bool deferredUpdate )
15951595
{
1596-
emit repaintRequested();
1596+
emit repaintRequested( deferredUpdate );
15971597
}
15981598

15991599
QString QgsMapLayer::metadata() const

src/core/qgsmaplayer.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -678,12 +678,14 @@ class CORE_EXPORT QgsMapLayer : public QObject
678678
void setScaleBasedVisibility( const bool enabled );
679679

680680
/**
681-
* Will advice the map canvas (and any other interested party) that this layer requires to be repainted.
681+
* Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
682682
* Will emit a repaintRequested() signal.
683+
* If \a deferredUpdate is true then the layer will only be repainted when the canvas is next
684+
* re-rendered, and will not trigger any canvas redraws itself.
683685
*
684686
* @note in 2.6 function moved from vector/raster subclasses to QgsMapLayer
685687
*/
686-
void triggerRepaint();
688+
void triggerRepaint( bool deferredUpdate = false );
687689

688690
//! \brief Obtain Metadata for this layer
689691
virtual QString metadata() const;
@@ -732,8 +734,10 @@ class CORE_EXPORT QgsMapLayer : public QObject
732734

733735
/** By emitting this signal the layer tells that either appearance or content have been changed
734736
* and any view showing the rendered layer should refresh itself.
737+
* If \a deferredUpdate is true then the layer will only be repainted when the canvas is next
738+
* re-rendered, and will not trigger any canvas redraws itself.
735739
*/
736-
void repaintRequested();
740+
void repaintRequested( bool deferredUpdate = false );
737741

738742
//! This is used to send a request that any mapcanvas using this layer update its extents
739743
void recalculateExtents() const;

src/core/qgsvectorlayer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ QgsVectorLayer::QgsVectorLayer( const QString& vectorLayerPath,
164164
setDataSource( vectorLayerPath, baseName, providerKey, loadDefaultStyleFlag );
165165
}
166166

167-
connect( this, &QgsVectorLayer::selectionChanged, this, &QgsVectorLayer::repaintRequested );
167+
connect( this, &QgsVectorLayer::selectionChanged, this, [=]{ emit repaintRequested(); } );
168168
connect( QgsProject::instance()->relationManager(), &QgsRelationManager::relationsLoaded, this, &QgsVectorLayer::onRelationsLoaded );
169169

170170
// Default simplify drawing settings

src/gui/qgsmapcanvas.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ void QgsMapCanvas::setLayers( const QList<QgsMapLayer*>& layers )
295295

296296
Q_FOREACH ( QgsMapLayer* layer, oldLayers )
297297
{
298-
disconnect( layer, &QgsMapLayer::repaintRequested, this, &QgsMapCanvas::refresh );
298+
disconnect( layer, &QgsMapLayer::repaintRequested, this, &QgsMapCanvas::layerRepaintRequested );
299299
disconnect( layer, &QgsMapLayer::crsChanged, this, &QgsMapCanvas::layerCrsChange );
300300
if ( QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( layer ) )
301301
{
@@ -307,7 +307,7 @@ void QgsMapCanvas::setLayers( const QList<QgsMapLayer*>& layers )
307307

308308
Q_FOREACH ( QgsMapLayer* layer, layers )
309309
{
310-
connect( layer, &QgsMapLayer::repaintRequested, this, &QgsMapCanvas::refresh );
310+
connect( layer, &QgsMapLayer::repaintRequested, this, &QgsMapCanvas::layerRepaintRequested );
311311
connect( layer, &QgsMapLayer::crsChanged, this, &QgsMapCanvas::layerCrsChange );
312312
if ( QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( layer ) )
313313
{
@@ -1668,6 +1668,12 @@ void QgsMapCanvas::updateDatumTransformEntries()
16681668
}
16691669
}
16701670

1671+
void QgsMapCanvas::layerRepaintRequested( bool deferred )
1672+
{
1673+
if ( !deferred )
1674+
refresh();
1675+
}
1676+
16711677

16721678

16731679
QgsMapTool* QgsMapCanvas::mapTool()

src/gui/qgsmapcanvas.h

+4
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
612612
//! Make sure the datum transform store is properly populated
613613
void updateDatumTransformEntries();
614614

615+
private slots:
616+
617+
void layerRepaintRequested( bool deferred );
618+
615619
private:
616620
/// this class is non-copyable
617621

src/gui/qgsmapoverviewcanvas.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,10 @@ void QgsMapOverviewCanvas::mapRenderingFinished()
207207
update();
208208
}
209209

210-
void QgsMapOverviewCanvas::layerRepaintRequested()
210+
void QgsMapOverviewCanvas::layerRepaintRequested( bool deferred )
211211
{
212-
refresh();
212+
if ( !deferred )
213+
refresh();
213214
}
214215

215216

src/gui/qgsmapoverviewcanvas.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ class GUI_EXPORT QgsMapOverviewCanvas : public QWidget
7575

7676
protected slots:
7777
void mapRenderingFinished();
78-
void layerRepaintRequested();
78+
79+
/**
80+
* Triggered when a layer in the overview requests a repaint.
81+
*/
82+
void layerRepaintRequested( bool deferred = false );
7983

8084
protected:
8185

0 commit comments

Comments
 (0)