4141QgsMapToolIdentify::QgsMapToolIdentify ( QgsMapCanvas* canvas )
4242 : QgsMapTool( canvas ),
4343 mResults( 0 ),
44- mRubberBand( 0 )
44+ mRubberBand( 0 ),
45+ mLayer( 0 )
4546{
4647 // set cursor
4748 QPixmap myIdentifyQPixmap = QPixmap (( const char ** ) identify_cursor );
4849 mCursor = QCursor ( myIdentifyQPixmap, 1 , 1 );
49-
50- mLayer = 0 ; // Initialize mLayer, useful in removeLayer SLOT
5150}
5251
5352QgsMapToolIdentify::~QgsMapToolIdentify ()
@@ -75,63 +74,61 @@ void QgsMapToolIdentify::canvasReleaseEvent( QMouseEvent * e )
7574 return ;
7675 }
7776
78- mLayer = mCanvas ->currentLayer ();
79-
8077 // delete rubber band if there was any
8178 delete mRubberBand ;
8279 mRubberBand = 0 ;
8380
81+ mLayer = mCanvas ->currentLayer ();
82+
83+ if ( !mLayer )
84+ {
85+ QMessageBox::warning ( mCanvas ,
86+ tr ( " No active layer" ),
87+ tr ( " To identify features, you must choose an active layer by clicking on its name in the legend" ) );
88+ return ;
89+ }
90+
91+ // cleanup, when layer is removed
92+ connect ( mLayer , SIGNAL ( destroyed () ), this , SLOT ( layerDestroyed () ) );
93+
8494 // call identify method for selected layer
8595
86- if ( mLayer )
96+ // In the special case of the WMS provider,
97+ // coordinates are sent back to the server as pixel coordinates
98+ // not the layer's native CRS. So identify on screen coordinates!
99+ if ( mLayer ->type () == QgsMapLayer::RasterLayer &&
100+ dynamic_cast <QgsRasterLayer*>( mLayer )->providerKey () == " wms" )
101+ {
102+ identifyRasterWmsLayer ( QgsPoint ( e->x (), e->y () ) );
103+ }
104+ else
87105 {
88- // In the special case of the WMS provider,
89- // coordinates are sent back to the server as pixel coordinates
90- // not the layer's native CRS. So identify on screen coordinates!
91- if (
92- ( mLayer ->type () == QgsMapLayer::RasterLayer )
93- &&
94- ( dynamic_cast <QgsRasterLayer*>( mLayer )->providerKey () == " wms" )
95- )
106+ // convert screen coordinates to map coordinates
107+ QgsPoint idPoint = mCanvas ->getCoordinateTransform ()->toMapCoordinates ( e->x (), e->y () );
108+
109+ if ( mLayer ->type () == QgsMapLayer::VectorLayer )
96110 {
97- identifyRasterWmsLayer ( QgsPoint ( e->x (), e->y () ) );
111+ identifyVectorLayer ( idPoint );
112+ }
113+ else if ( mLayer ->type () == QgsMapLayer::RasterLayer )
114+ {
115+ identifyRasterLayer ( idPoint );
98116 }
99117 else
100118 {
101- // convert screen coordinates to map coordinates
102- QgsPoint idPoint = mCanvas ->getCoordinateTransform ()->toMapCoordinates ( e->x (), e->y () );
103-
104- if ( mLayer ->type () == QgsMapLayer::VectorLayer )
105- {
106- identifyVectorLayer ( idPoint );
107- }
108- else if ( mLayer ->type () == QgsMapLayer::RasterLayer )
109- {
110- identifyRasterLayer ( idPoint );
111- }
112- else
113- {
114- QgsDebugMsg ( " unknown layer type!" );
115- }
119+ QgsDebugMsg ( " unknown layer type!" );
116120 }
117-
118121 }
119- else
120- {
121- QMessageBox::warning ( mCanvas ,
122- tr ( " No active layer" ),
123- tr ( " To identify features, you must choose an active layer by clicking on its name in the legend" ) );
124- }
125-
126-
127122}
128123
129124
130125void QgsMapToolIdentify::identifyRasterLayer ( const QgsPoint& point )
131126{
132127 QgsRasterLayer *layer = dynamic_cast <QgsRasterLayer*>( mLayer );
133128 if ( !layer )
129+ {
134130 return ;
131+ }
135132
136133 QMap<QString, QString> attributes;
137134 layer->identify ( point, attributes );
@@ -232,7 +229,9 @@ void QgsMapToolIdentify::identifyVectorLayer( const QgsPoint& point )
232229{
233230 QgsVectorLayer *layer = dynamic_cast <QgsVectorLayer*>( mLayer );
234231 if ( !layer )
232+ {
235233 return ;
234+ }
236235
237236 // load identify radius from settings
238237 QSettings settings;
@@ -421,9 +420,19 @@ void QgsMapToolIdentify::showError()
421420#endif
422421
423422 QgsMessageViewer * mv = new QgsMessageViewer ();
424- mv->setWindowTitle ( mLayer ->lastErrorTitle () );
425- mv->setMessageAsPlainText ( tr ( " Could not identify objects on %1 because:\n %2" )
426- .arg ( mLayer ->name () ).arg ( mLayer ->lastError () ) );
423+
424+ if ( mLayer )
425+ {
426+ mv->setWindowTitle ( mLayer ->lastErrorTitle () );
427+ mv->setMessageAsPlainText ( tr ( " Could not identify objects on %1 because:\n %2" )
428+ .arg ( mLayer ->name () ).arg ( mLayer ->lastError () ) );
429+ }
430+ else
431+ {
432+ mv->setWindowTitle ( tr ( " Layer was removed" ) );
433+ mv->setMessageAsPlainText ( tr ( " Layer to identify objects on was removed" ) );
434+ }
435+
427436 mv->exec (); // deletes itself on close
428437}
429438
@@ -488,11 +497,10 @@ void QgsMapToolIdentify::editFeature( int featureId )
488497void QgsMapToolIdentify::editFeature ( QgsFeature &f )
489498{
490499 QgsVectorLayer* layer = dynamic_cast <QgsVectorLayer*>( mLayer );
491- if ( !layer )
492- return ;
493-
494- if ( !layer->isEditable () )
500+ if ( !layer || !layer->isEditable () )
501+ {
495502 return ;
503+ }
496504
497505 QgsAttributeMap src = f.attributeMap ();
498506
@@ -519,23 +527,19 @@ void QgsMapToolIdentify::editFeature( QgsFeature &f )
519527 mCanvas ->refresh ();
520528}
521529
522- void QgsMapToolIdentify::removeLayer ( QString layerID )
530+ void QgsMapToolIdentify::layerDestroyed ( )
523531{
524- if ( mLayer )
532+ mLayer = 0 ;
533+
534+ if ( mResults )
525535 {
526- if ( mLayer ->type () == QgsMapLayer::VectorLayer )
527- {
528- if ( mLayer ->getLayerID () == layerID )
529- {
530- if ( mResults )
531- {
532- mResults ->clear ();
533- delete mRubberBand ;
534- mRubberBand = 0 ;
535- }
536- mLayer = 0 ;
537- }
538- }
536+ mResults ->clear ();
537+ }
538+
539+ if ( mRubberBand )
540+ {
541+ delete mRubberBand ;
542+ mRubberBand = 0 ;
539543 }
540544}
541545
0 commit comments