Skip to content

Commit c91d470

Browse files
committedApr 6, 2014
fix crash when removing layers with highlighed features
1 parent 8e8019a commit c91d470

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed
 

‎python/gui/qgshighlight.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,27 @@ class QgsHighlight : QgsMapCanvasItem
77
QgsHighlight( QgsMapCanvas *mapCanvas, QgsGeometry *geom, QgsVectorLayer *layer );
88
~QgsHighlight();
99

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

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

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

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

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

29+
const QgsMapLayer *layer() const;
30+
2131
protected:
2232
virtual void paint( QPainter* p );
2333

‎src/gui/qgshighlight.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* *
1414
***************************************************************************/
1515

16-
#include <typeinfo>
17-
1816
#include <QImage>
1917

2018
#include "qgsmarkersymbollayerv2.h"
@@ -134,7 +132,7 @@ void QgsHighlight::setFillColor( const QColor & fillColor )
134132
QgsFeatureRendererV2 * QgsHighlight::getRenderer( const QgsRenderContext & context, const QColor & color, const QColor & fillColor )
135133
{
136134
QgsFeatureRendererV2 *renderer = 0;
137-
QgsVectorLayer *layer = vectorLayer();
135+
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer*>( mLayer );
138136
if ( layer && layer->rendererV2() )
139137
{
140138
renderer = layer->rendererV2()->clone();
@@ -345,7 +343,9 @@ void QgsHighlight::paint( QPainter* p )
345343
}
346344
else if ( mFeature.geometry() )
347345
{
348-
QgsVectorLayer *layer = vectorLayer();
346+
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer*>( mLayer );
347+
if( !layer )
348+
return;
349349
QgsMapSettings mapSettings = mMapCanvas->mapSettings();
350350
QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
351351

@@ -438,9 +438,3 @@ void QgsHighlight::updateRect()
438438
setRect( QgsRectangle() );
439439
}
440440
}
441-
442-
QgsVectorLayer * QgsHighlight::vectorLayer()
443-
{
444-
return dynamic_cast<QgsVectorLayer *>( mLayer );
445-
}
446-

‎src/gui/qgshighlight.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class GUI_EXPORT QgsHighlight: public QgsMapCanvasItem
6666
* @note: added in version 2.3 */
6767
void setMinWidth( double width ) { mMinWidth = width; }
6868

69+
const QgsMapLayer *layer() const { return mLayer; }
70+
6971
protected:
7072
virtual void paint( QPainter* p );
7173

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

85-
QgsVectorLayer *vectorLayer();
86-
87-
QgsHighlight();
88-
8987
QBrush mBrush;
9088
QPen mPen;
9189
QgsGeometry *mGeometry;

‎src/gui/qgsmaptoolidentify.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -650,10 +650,11 @@ void QgsMapToolIdentify::handleMenuHover()
650650
QList<IdentifyResult>::const_iterator idListIt = idList.constBegin();
651651
for ( ; idListIt != idList.constEnd(); ++idListIt )
652652
{
653-
QgsHighlight* hl = new QgsHighlight( mCanvas, idListIt->mFeature.geometry(), vl );
653+
QgsHighlight *hl = new QgsHighlight( mCanvas, idListIt->mFeature.geometry(), vl );
654654
hl->setColor( QColor( 255, 0, 0 ) );
655655
hl->setWidth( 2 );
656656
mRubberBands.append( hl );
657+
connect( vl, SIGNAL( destroyed() ), this, SLOT( layerDestroyed() ) );
657658
}
658659
}
659660
}
@@ -662,10 +663,22 @@ void QgsMapToolIdentify::handleMenuHover()
662663

663664
void QgsMapToolIdentify::deleteRubberBands()
664665
{
665-
QList<QgsHighlight*>::const_iterator it = mRubberBands.constBegin();
666-
for ( ; it != mRubberBands.constEnd(); ++it )
666+
qDeleteAll( mRubberBands );
667+
}
668+
669+
void QgsMapToolIdentify::layerDestroyed()
670+
{
671+
QList<QgsHighlight*>::iterator it = mRubberBands.begin();
672+
while ( it != mRubberBands.end() )
667673
{
668-
delete *it;
674+
if (( *it )->layer() == sender() )
675+
{
676+
delete *it;
677+
it = mRubberBands.erase( it );
678+
}
679+
else
680+
{
681+
++it;
682+
}
669683
}
670-
mRubberBands.clear();
671684
}

‎src/gui/qgsmaptoolidentify.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
127127

128128
public slots:
129129
void formatChanged( QgsRasterLayer *layer );
130+
void layerDestroyed();
130131

131132
signals:
132133
void identifyProgress( int, int );

0 commit comments

Comments
 (0)
Please sign in to comment.