Skip to content

Commit

Permalink
fix crash when removing layers with highlighed features
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Apr 6, 2014
1 parent 8e8019a commit c91d470
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
10 changes: 10 additions & 0 deletions python/gui/qgshighlight.sip
Expand Up @@ -7,17 +7,27 @@ class QgsHighlight : QgsMapCanvasItem
QgsHighlight( QgsMapCanvas *mapCanvas, QgsGeometry *geom, QgsVectorLayer *layer ); QgsHighlight( QgsMapCanvas *mapCanvas, QgsGeometry *geom, QgsVectorLayer *layer );
~QgsHighlight(); ~QgsHighlight();


/** Set line/outline to color, polygon fill to color with alpha = 63.
* This is legacy function, use setFillColor() after setColor() if different fill color is required. */
void setColor( const QColor & color ); void setColor( const QColor & color );


/** Set polygons fill color.
* @note: added in version 2.3 */
void setFillColor( const QColor & fillColor ); void setFillColor( const QColor & fillColor );


/** Set width. Ignored in feature mode. */ /** Set width. Ignored in feature mode. */
void setWidth( int width ); void setWidth( int width );


/** Set line / outline buffer in millimeters.
* @note: added in version 2.3 */
void setBuffer( double buffer ); void setBuffer( double buffer );


/** Set minimum line / outline width in millimeters.
* @note: added in version 2.3 */
void setMinWidth( double width ); void setMinWidth( double width );


const QgsMapLayer *layer() const;

protected: protected:
virtual void paint( QPainter* p ); virtual void paint( QPainter* p );


Expand Down
14 changes: 4 additions & 10 deletions src/gui/qgshighlight.cpp
Expand Up @@ -13,8 +13,6 @@
* * * *
***************************************************************************/ ***************************************************************************/


#include <typeinfo>

#include <QImage> #include <QImage>


#include "qgsmarkersymbollayerv2.h" #include "qgsmarkersymbollayerv2.h"
Expand Down Expand Up @@ -134,7 +132,7 @@ void QgsHighlight::setFillColor( const QColor & fillColor )
QgsFeatureRendererV2 * QgsHighlight::getRenderer( const QgsRenderContext & context, const QColor & color, const QColor & fillColor ) QgsFeatureRendererV2 * QgsHighlight::getRenderer( const QgsRenderContext & context, const QColor & color, const QColor & fillColor )
{ {
QgsFeatureRendererV2 *renderer = 0; QgsFeatureRendererV2 *renderer = 0;
QgsVectorLayer *layer = vectorLayer(); QgsVectorLayer *layer = qobject_cast<QgsVectorLayer*>( mLayer );
if ( layer && layer->rendererV2() ) if ( layer && layer->rendererV2() )
{ {
renderer = layer->rendererV2()->clone(); renderer = layer->rendererV2()->clone();
Expand Down Expand Up @@ -345,7 +343,9 @@ void QgsHighlight::paint( QPainter* p )
} }
else if ( mFeature.geometry() ) else if ( mFeature.geometry() )
{ {
QgsVectorLayer *layer = vectorLayer(); QgsVectorLayer *layer = qobject_cast<QgsVectorLayer*>( mLayer );
if( !layer )
return;
QgsMapSettings mapSettings = mMapCanvas->mapSettings(); QgsMapSettings mapSettings = mMapCanvas->mapSettings();
QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings ); QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );


Expand Down Expand Up @@ -438,9 +438,3 @@ void QgsHighlight::updateRect()
setRect( QgsRectangle() ); setRect( QgsRectangle() );
} }
} }

QgsVectorLayer * QgsHighlight::vectorLayer()
{
return dynamic_cast<QgsVectorLayer *>( mLayer );
}

6 changes: 2 additions & 4 deletions src/gui/qgshighlight.h
Expand Up @@ -66,6 +66,8 @@ class GUI_EXPORT QgsHighlight: public QgsMapCanvasItem
* @note: added in version 2.3 */ * @note: added in version 2.3 */
void setMinWidth( double width ) { mMinWidth = width; } void setMinWidth( double width ) { mMinWidth = width; }


const QgsMapLayer *layer() const { return mLayer; }

protected: protected:
virtual void paint( QPainter* p ); virtual void paint( QPainter* p );


Expand All @@ -82,10 +84,6 @@ class GUI_EXPORT QgsHighlight: public QgsMapCanvasItem
void paintLine( QPainter *p, QgsPolyline line ); void paintLine( QPainter *p, QgsPolyline line );
void paintPolygon( QPainter *p, QgsPolygon polygon ); void paintPolygon( QPainter *p, QgsPolygon polygon );


QgsVectorLayer *vectorLayer();

QgsHighlight();

QBrush mBrush; QBrush mBrush;
QPen mPen; QPen mPen;
QgsGeometry *mGeometry; QgsGeometry *mGeometry;
Expand Down
23 changes: 18 additions & 5 deletions src/gui/qgsmaptoolidentify.cpp
Expand Up @@ -650,10 +650,11 @@ void QgsMapToolIdentify::handleMenuHover()
QList<IdentifyResult>::const_iterator idListIt = idList.constBegin(); QList<IdentifyResult>::const_iterator idListIt = idList.constBegin();
for ( ; idListIt != idList.constEnd(); ++idListIt ) for ( ; idListIt != idList.constEnd(); ++idListIt )
{ {
QgsHighlight* hl = new QgsHighlight( mCanvas, idListIt->mFeature.geometry(), vl ); QgsHighlight *hl = new QgsHighlight( mCanvas, idListIt->mFeature.geometry(), vl );
hl->setColor( QColor( 255, 0, 0 ) ); hl->setColor( QColor( 255, 0, 0 ) );
hl->setWidth( 2 ); hl->setWidth( 2 );
mRubberBands.append( hl ); mRubberBands.append( hl );
connect( vl, SIGNAL( destroyed() ), this, SLOT( layerDestroyed() ) );
} }
} }
} }
Expand All @@ -662,10 +663,22 @@ void QgsMapToolIdentify::handleMenuHover()


void QgsMapToolIdentify::deleteRubberBands() void QgsMapToolIdentify::deleteRubberBands()
{ {
QList<QgsHighlight*>::const_iterator it = mRubberBands.constBegin(); qDeleteAll( mRubberBands );
for ( ; it != mRubberBands.constEnd(); ++it ) }

void QgsMapToolIdentify::layerDestroyed()
{
QList<QgsHighlight*>::iterator it = mRubberBands.begin();
while ( it != mRubberBands.end() )
{ {
delete *it; if (( *it )->layer() == sender() )
{
delete *it;
it = mRubberBands.erase( it );
}
else
{
++it;
}
} }
mRubberBands.clear();
} }
1 change: 1 addition & 0 deletions src/gui/qgsmaptoolidentify.h
Expand Up @@ -127,6 +127,7 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool


public slots: public slots:
void formatChanged( QgsRasterLayer *layer ); void formatChanged( QgsRasterLayer *layer );
void layerDestroyed();


signals: signals:
void identifyProgress( int, int ); void identifyProgress( int, int );
Expand Down

0 comments on commit c91d470

Please sign in to comment.