Skip to content

Commit

Permalink
Invalidate cached image when layer's data or appearance is modified
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Dec 10, 2013
1 parent 9164ae9 commit e814e8d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
4 changes: 4 additions & 0 deletions python/gui/qgsmapcanvas.sip
Expand Up @@ -79,6 +79,10 @@ class QgsMapCanvas : QGraphicsView
//! @note added in 2.1
bool isCachingEnabled() const;

//! Make sure to remove any rendered images from cache (does nothing if cache is not enabled)
//! @note added in 2.1
void clearCache();

//! Set whether the layers are rendered in parallel or sequentially
//! @note added in 2.1
void setParallelRenderingEnabled( bool enabled );
Expand Down
4 changes: 3 additions & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -5681,6 +5681,8 @@ void QgisApp::refreshMapCanvas()
{
//reload cached provider data
QgsMapLayerRegistry::instance()->reloadAllLayers();

mMapCanvas->clearCache();
//then refresh
mMapCanvas->refresh();
}
Expand Down Expand Up @@ -6726,7 +6728,7 @@ void QgisApp::adjustBrightnessContrast( int delta, bool updateBrightness )
brightnessFilter->setContrast( brightnessFilter->contrast() + delta );
}

mMapCanvas->refresh();
myRasterLayer->triggerRepaint();
}

void QgisApp::helpContents()
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsmaplayer.h
Expand Up @@ -407,8 +407,8 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
void layerCrsChanged();

/** This signal should be connected with the slot QgsMapCanvas::refresh()
* \todo to be removed - GUI dependency
/** By emitting this signal the layer tells that either appearance or content have been changed
* and any view showing the rendered layer should refresh itself.
*/
void repaintRequested();

Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -168,6 +168,8 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,

connect( this, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SIGNAL( selectionChanged() ) );

connect( this, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SIGNAL( repaintRequested() ) );

connect( QgsProject::instance()->relationManager(), SIGNAL( relationsLoaded() ), this, SLOT( onRelationsLoaded() ) );
} // QgsVectorLayer ctor

Expand Down Expand Up @@ -888,6 +890,9 @@ bool QgsVectorLayer::setSubsetString( QString subset )
mDataSource = mDataProvider->dataSourceUri();
updateExtents();

if ( res )
emit repaintRequested();

return res;
}

Expand Down Expand Up @@ -2325,6 +2330,8 @@ bool QgsVectorLayer::commitChanges()
updateFields();
mDataProvider->updateExtents();

emit repaintRequested();

return success;
}

Expand Down Expand Up @@ -2366,6 +2373,7 @@ bool QgsVectorLayer::rollBack( bool deleteBuffer )
mCache->deleteCachedGeometries();
}

emit repaintRequested();
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -931,6 +931,8 @@ void QgsRasterLayer::setContrastEnhancement( QgsContrastEnhancement::ContrastEnh
if ( myEnhancements.value( 1 ) ) myMultiBandRenderer->setGreenContrastEnhancement( myEnhancements.value( 1 ) );
if ( myEnhancements.value( 2 ) ) myMultiBandRenderer->setBlueContrastEnhancement( myEnhancements.value( 2 ) );
}

emit repaintRequested();
}

void QgsRasterLayer::setDefaultContrastEnhancement()
Expand Down
23 changes: 21 additions & 2 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -370,7 +370,7 @@ void QgsMapCanvas::setLayerSet( QList<QgsMapCanvasLayer> &layers )
// Add check if vector layer when disconnecting from selectionChanged slot
// Ticket #811 - racicot
QgsMapLayer *currentLayer = layer( i );
disconnect( currentLayer, SIGNAL( repaintRequested() ), this, SLOT( refresh() ) );
disconnect( currentLayer, SIGNAL( repaintRequested() ), this, SLOT( layerRequestedRepaint() ) );
QgsVectorLayer *isVectLyr = qobject_cast<QgsVectorLayer *>( currentLayer );
if ( isVectLyr )
{
Expand All @@ -385,7 +385,7 @@ void QgsMapCanvas::setLayerSet( QList<QgsMapCanvasLayer> &layers )
// Add check if vector layer when connecting to selectionChanged slot
// Ticket #811 - racicot
QgsMapLayer *currentLayer = layer( i );
connect( currentLayer, SIGNAL( repaintRequested() ), this, SLOT( refresh() ) );
connect( currentLayer, SIGNAL( repaintRequested() ), this, SLOT( layerRequestedRepaint() ) );
QgsVectorLayer *isVectLyr = qobject_cast<QgsVectorLayer *>( currentLayer );
if ( isVectLyr )
{
Expand Down Expand Up @@ -509,6 +509,12 @@ bool QgsMapCanvas::isCachingEnabled() const
return mCache != 0;
}

void QgsMapCanvas::clearCache()
{
if ( mCache )
mCache->clear();
}

void QgsMapCanvas::setParallelRenderingEnabled( bool enabled )
{
mUseParallelRendering = enabled;
Expand Down Expand Up @@ -640,6 +646,19 @@ void QgsMapCanvas::refreshMap()
mMapUpdateTimer.start();
}

void QgsMapCanvas::layerRequestedRepaint()
{
// make sure to clear the cached image
if ( mCache )
{
QgsMapLayer* layer = qobject_cast<QgsMapLayer*>( sender() );
if ( layer )
mCache->setCacheImage( layer->id(), QImage() );
}

refresh();
}

void QgsMapCanvas::rendererJobFinished()
{
qDebug("CANVAS finish! %d", !mJobCancelled );
Expand Down
7 changes: 7 additions & 0 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -150,6 +150,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! @note added in 2.1
bool isCachingEnabled() const;

//! Make sure to remove any rendered images from cache (does nothing if cache is not enabled)
//! @note added in 2.1
void clearCache();

//! Set whether the layers are rendered in parallel or sequentially
//! @note added in 2.1
void setParallelRenderingEnabled( bool enabled );
Expand Down Expand Up @@ -367,6 +371,9 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView

void refreshMap();

//! Layer says something has changed that affects its appearance
void layerRequestedRepaint();

signals:
/** Let the owner know how far we are with render operations */
//! @deprecated since 2.1 - already unused in 2.0 anyway
Expand Down

0 comments on commit e814e8d

Please sign in to comment.