Skip to content

Commit

Permalink
Fix regeneration of cached maps when zoom level changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 7, 2017
1 parent 6f59965 commit 7924aa1
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 34 deletions.
5 changes: 5 additions & 0 deletions python/core/layout/qgslayoutitem.sip
Expand Up @@ -551,6 +551,11 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
recalculation of its position and size.
%End

virtual void invalidateCache();
%Docstring
Forces a deferred update of any cached image the item uses.
%End

virtual void redraw();
%Docstring
Triggers a redraw (update) of the item.
Expand Down
6 changes: 2 additions & 4 deletions python/core/layout/qgslayoutitemmap.sip
Expand Up @@ -447,10 +447,8 @@ Is emitted when the map has been prepared for atlas rendering, just before actua

public slots:

void invalidateCache();
%Docstring
Forces a deferred update of the cached map image on next paint.
%End
virtual void invalidateCache();


void updateBoundingRect();
%Docstring
Expand Down
10 changes: 10 additions & 0 deletions src/core/layout/qgslayoutitem.cpp
Expand Up @@ -945,6 +945,16 @@ void QgsLayoutItem::refresh()
refreshDataDefinedProperty();
}

void QgsLayoutItem::invalidateCache()
{
if ( !mItemCachedImage.isNull() )
{
mItemCachedImage = QImage();
mItemCacheDpi = -1;
update();
}
}

void QgsLayoutItem::redraw()
{
update();
Expand Down
5 changes: 5 additions & 0 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -548,6 +548,11 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
*/
void refresh() override;

/**
* Forces a deferred update of any cached image the item uses.
*/
virtual void invalidateCache();

/**
* Triggers a redraw (update) of the item.
*/
Expand Down
5 changes: 1 addition & 4 deletions src/core/layout/qgslayoutitemmap.h
Expand Up @@ -495,10 +495,7 @@ class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem

public slots:

/**
* Forces a deferred update of the cached map image on next paint.
*/
void invalidateCache();
void invalidateCache() override;

//! Updates the bounding rect of this item. Call this function before doing any changes related to annotation out of the map rectangle
void updateBoundingRect();
Expand Down
28 changes: 2 additions & 26 deletions src/gui/layout/qgslayoutitemwidget.cpp
Expand Up @@ -327,20 +327,8 @@ void QgsLayoutItemPropertiesWidget::mBackgroundColorButton_colorChanged( const Q
}
mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Background Color" ), QgsLayoutItem::UndoBackgroundColor );
mItem->setBackgroundColor( newBackgroundColor );

#if 0 //TODO
//if the item is a composer map, we need to regenerate the map image
//because it usually is cached
if ( QgsComposerMap *cm = qobject_cast<QgsComposerMap *>( mItem ) )
{
cm->invalidateCache();
}
else
{
mItem->updateItem();
}
#endif
mItem->layout()->undoStack()->endCommand();
mItem->invalidateCache();
}

void QgsLayoutItemPropertiesWidget::changeItemPosition()
Expand Down Expand Up @@ -477,20 +465,8 @@ void QgsLayoutItemPropertiesWidget::mBackgroundGroupBox_toggled( bool state )

mItem->layout()->undoStack()->beginCommand( mItem, state ? tr( "Enable Background" ) : tr( "Disable Background" ) );
mItem->setBackgroundEnabled( state );

#if 0 //TODO
//if the item is a composer map, we need to regenerate the map image
//because it usually is cached
if ( QgsComposerMap *cm = qobject_cast<QgsComposerMap *>( mItem ) )
{
cm->invalidateCache();
}
else
{
mItem->updateItem();
}
#endif
mItem->layout()->undoStack()->endCommand();
mItem->invalidateCache();
}


Expand Down
17 changes: 17 additions & 0 deletions src/gui/layout/qgslayoutview.cpp
Expand Up @@ -58,6 +58,8 @@ QgsLayoutView::QgsLayoutView( QWidget *parent )

mPreviewEffect = new QgsPreviewEffect( this );
viewport()->setGraphicsEffect( mPreviewEffect );

connect( this, &QgsLayoutView::zoomLevelChanged, this, &QgsLayoutView::invalidateCachedRenders );
}

QgsLayout *QgsLayoutView::currentLayout()
Expand Down Expand Up @@ -899,6 +901,21 @@ void QgsLayoutView::scrollContentsBy( int dx, int dy )
viewChanged();
}

void QgsLayoutView::invalidateCachedRenders()
{
if ( !currentLayout() )
return;

//redraw cached map items
QList< QgsLayoutItem *> items;
currentLayout()->layoutItems( items );

for ( QgsLayoutItem *item : qgis::as_const( items ) )
{
item->invalidateCache();
}
}

void QgsLayoutView::viewChanged()
{
if ( mHorizontalRuler )
Expand Down
2 changes: 2 additions & 0 deletions src/gui/layout/qgslayoutview.h
Expand Up @@ -451,6 +451,8 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView

private slots:

void invalidateCachedRenders();

private:

//! Zoom layout from a mouse wheel event
Expand Down

0 comments on commit 7924aa1

Please sign in to comment.