Skip to content

Commit

Permalink
Fix highlights disappear after changing canvas CRS
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 18, 2021
1 parent c6a4331 commit 797a060
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/gui/qgshighlight.cpp
Expand Up @@ -68,30 +68,54 @@ QgsHighlight::QgsHighlight( QgsMapCanvas *mapCanvas, const QgsFeature &feature,
}

void QgsHighlight::init()
{
mOriginalGeometry = mGeometry.isNull() ? mFeature.geometry() : mGeometry;
setColor( QColor( Qt::lightGray ) );

connect( mMapCanvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsHighlight::updateTransformedGeometry );
updateTransformedGeometry();

if ( mGeometry.type() == QgsWkbTypes::PointGeometry )
{
mRenderContext = createRenderContext();
}
}

void QgsHighlight::updateTransformedGeometry()
{
QgsCoordinateTransform ct = mMapCanvas->mapSettings().layerTransform( mLayer );
if ( ct.isValid() )
{
// reset to original geometry and transform
if ( !mGeometry.isNull() )
{
mGeometry.transform( ct );

if ( mGeometry.type() == QgsWkbTypes::PointGeometry )
mGeometry = mOriginalGeometry;
try
{
mGeometry.transform( ct );
}
catch ( QgsCsException & )
{
mRenderContext = createRenderContext();
QgsDebugMsg( QStringLiteral( "Could not transform highlight geometry to canvas CRS" ) );
}
}
else if ( mFeature.hasGeometry() )
{
mFeature.setGeometry( mOriginalGeometry );
QgsGeometry g = mFeature.geometry();
g.transform( ct );
mFeature.setGeometry( g );
try
{
g.transform( ct );
mFeature.setGeometry( g );
}
catch ( QgsCsException & )
{
QgsDebugMsg( QStringLiteral( "Could not transform highlight geometry to canvas CRS" ) );
}
}
}

updateRect();
update();
setColor( QColor( Qt::lightGray ) );
}

QgsHighlight::~QgsHighlight() = default;
Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgshighlight.h
Expand Up @@ -168,6 +168,9 @@ class GUI_EXPORT QgsHighlight : public QgsMapCanvasItem
//! recalculates needed rectangle
void updateRect();

private slots:
void updateTransformedGeometry();

private:
enum PointSymbol
{
Expand All @@ -190,6 +193,7 @@ class GUI_EXPORT QgsHighlight : public QgsMapCanvasItem
QColor mFillColor; // line / stroke fillColor property
QBrush mBrush;
QPen mPen;
QgsGeometry mOriginalGeometry;
QgsGeometry mGeometry;
QgsMapLayer *mLayer = nullptr;
QgsFeature mFeature;
Expand Down

0 comments on commit 797a060

Please sign in to comment.