Skip to content

Commit 24ca94b

Browse files
committedFeb 7, 2013
return results directly from the identify method instead of using a private attribute
1 parent cb7f7bf commit 24ca94b

File tree

4 files changed

+37
-57
lines changed

4 files changed

+37
-57
lines changed
 

‎python/gui/qgsmaptoolidentify.sip

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class QgsMapToolIdentify : QgsMapTool
7474
@param layerList Performs the identification within the given list of layers. Default value is an empty list, i.e. uses all the layers.
7575
@param mode Identification mode. Can use Qgis default settings or a defined mode. Default mode is DefaultQgsSetting.
7676
@return true if identification succeeded and a feature has been found, false otherwise.*/
77-
bool identify(int x, int y, QList<QgsMapLayer*> layerList = QList<QgsMapLayer*>(), IdentifyMode mode = DefaultQgsSetting);
77+
IdentifyResults identify(int x, int y, QList<QgsMapLayer*> layerList = QList<QgsMapLayer*>(), IdentifyMode mode = DefaultQgsSetting);
7878

7979
/** Performs the identification.
8080
To avoid beeing forced to specify IdentifyMode with a list of layers
@@ -84,10 +84,7 @@ class QgsMapToolIdentify : QgsMapTool
8484
@param mode Identification mode. Can use Qgis default settings or a defined mode.
8585
@param layerType Only performs identification in a certain type of layers (raster, vector). Default value is AllLayers.
8686
@return true if identification succeeded and a feature has been found, false otherwise.*/
87-
bool identify(int x, int y, IdentifyMode mode, LayerType layerType = AllLayers);
88-
89-
/** Access to results */
90-
IdentifyResults &results();
87+
IdentifyResults identify(int x, int y, IdentifyMode mode, LayerType layerType = AllLayers);
9188

9289
signals:
9390
void identifyProgress( int, int );

‎src/app/qgsmaptoolidentifyaction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,23 @@ void QgsMapToolIdentifyAction::canvasReleaseEvent( QMouseEvent *e )
9393

9494
connect( this, SIGNAL( identifyProgress( int, int ) ), QgisApp::instance(), SLOT( showProgress( int, int ) ) );
9595
connect( this, SIGNAL( identifyMessage( QString ) ), QgisApp::instance(), SLOT( showStatusMessage( QString ) ) );
96-
bool res = QgsMapToolIdentify::identify( e->x(), e->y() );
96+
IdentifyResults results = QgsMapToolIdentify::identify( e->x(), e->y() );
9797
disconnect( this, SIGNAL( identifyProgress( int, int ) ), QgisApp::instance(), SLOT( showProgress( int, int ) ) );
9898
disconnect( this, SIGNAL( identifyMessage( QString ) ), QgisApp::instance(), SLOT( showStatusMessage( QString ) ) );
9999

100100

101101
QList<VectorResult>::const_iterator vresult;
102-
for ( vresult = results().mVectorResults.begin(); vresult != results().mVectorResults.end(); ++vresult )
102+
for ( vresult = results.mVectorResults.begin(); vresult != results.mVectorResults.end(); ++vresult )
103103
{
104104
resultsDialog()->addFeature( vresult->mLayer, vresult->mFeature, vresult->mDerivedAttributes );
105105
}
106106
QList<RasterResult>::const_iterator rresult;
107-
for ( rresult = results().mRasterResults.begin(); rresult != results().mRasterResults.end(); ++rresult )
107+
for ( rresult = results.mRasterResults.begin(); rresult != results.mRasterResults.end(); ++rresult )
108108
{
109109
resultsDialog()->addFeature( rresult->mLayer, rresult->mLabel, rresult->mAttributes, rresult->mDerivedAttributes, rresult->mFields, rresult->mFeature );
110110
}
111111

112-
if ( res )
112+
if ( !results.mRasterResults.isEmpty() || !results.mVectorResults.isEmpty() )
113113
{
114114
resultsDialog()->show();
115115
}

‎src/gui/qgsmaptoolidentify.cpp

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,29 @@ void QgsMapToolIdentify::canvasReleaseEvent( QMouseEvent *e )
6868
Q_UNUSED( e );
6969
}
7070

71-
bool QgsMapToolIdentify::identify( int x, int y, QList<QgsMapLayer *> layerList, IdentifyMode mode )
71+
QgsMapToolIdentify::IdentifyResults QgsMapToolIdentify::identify ( int x, int y, QList<QgsMapLayer *> layerList, IdentifyMode mode )
7272
{
7373
return identify( x, y, mode, layerList, AllLayers );
7474
}
7575

76-
bool QgsMapToolIdentify::identify( int x, int y, IdentifyMode mode, LayerType layerType )
76+
QgsMapToolIdentify::IdentifyResults QgsMapToolIdentify::identify( int x, int y, IdentifyMode mode, LayerType layerType )
7777
{
7878
return identify( x, y, mode, QList<QgsMapLayer*>(), layerType );
7979
}
8080

81-
bool QgsMapToolIdentify::identify( int x, int y, IdentifyMode mode, QList<QgsMapLayer*> layerList, LayerType layerType )
81+
QgsMapToolIdentify::IdentifyResults QgsMapToolIdentify::identify( int x, int y, IdentifyMode mode, QList<QgsMapLayer*> layerList, LayerType layerType )
8282
{
83+
IdentifyResults results;
84+
8385
mLastPoint = mCanvas->getCoordinateTransform()->toMapCoordinates( x, y );
8486
mLastExtent = mCanvas->extent();
8587
mLastMapUnitsPerPixel = mCanvas->mapUnitsPerPixel();
86-
return identify( mLastPoint, mLastExtent, mLastMapUnitsPerPixel, mode, layerList, layerType );
87-
}
8888

89-
bool QgsMapToolIdentify::identify( QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, IdentifyMode mode, QList<QgsMapLayer*> layerList, LayerType layerType )
90-
{
91-
bool res = false;
9289
if ( !mCanvas || mCanvas->isDrawing() )
9390
{
94-
return res;
91+
return results;
9592
}
9693

97-
mResultData.mVectorResults.clear();
98-
mResultData.mRasterResults.clear();
99-
10094
if ( mode == DefaultQgsSetting )
10195
{
10296
QSettings settings;
@@ -110,12 +104,12 @@ bool QgsMapToolIdentify::identify( QgsPoint point, QgsRectangle viewExtent, doub
110104
if ( !layer )
111105
{
112106
emit identifyMessage( tr( "No active layer. To identify features, you must choose an active layer." ) );
113-
return res;
107+
return results;
114108
}
115109

116110
QApplication::setOverrideCursor( Qt::WaitCursor );
117111

118-
res = identifyLayer( layer, point, viewExtent, mapUnitsPerPixel, layerType );
112+
identifyLayer( &results, layer, mLastPoint, mLastExtent, mLastMapUnitsPerPixel, layerType );
119113
}
120114
else
121115
{
@@ -145,9 +139,8 @@ bool QgsMapToolIdentify::identify( QgsPoint point, QgsRectangle viewExtent, doub
145139
if ( noIdentifyLayerIdList.contains( layer->id() ) )
146140
continue;
147141

148-
if ( identifyLayer( layer, point, viewExtent, mapUnitsPerPixel, layerType ) )
142+
if ( identifyLayer( &results, layer, mLastPoint, mLastExtent, mLastMapUnitsPerPixel, layerType ) )
149143
{
150-
res = true;
151144
if ( mode == TopDownStopAtFirst )
152145
break;
153146
}
@@ -159,7 +152,7 @@ bool QgsMapToolIdentify::identify( QgsPoint point, QgsRectangle viewExtent, doub
159152

160153
QApplication::restoreOverrideCursor();
161154

162-
return res;
155+
return results;
163156
}
164157

165158
void QgsMapToolIdentify::activate()
@@ -172,25 +165,24 @@ void QgsMapToolIdentify::deactivate()
172165
QgsMapTool::deactivate();
173166
}
174167

175-
bool QgsMapToolIdentify::identifyLayer( QgsMapLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, LayerType layerType )
168+
bool QgsMapToolIdentify::identifyLayer( IdentifyResults *results, QgsMapLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, LayerType layerType )
176169
{
177170
if ( layer->type() == QgsMapLayer::RasterLayer && ( layerType == AllLayers || layerType == RasterLayer ) )
178171
{
179-
return identifyRasterLayer( qobject_cast<QgsRasterLayer *>( layer ), point, viewExtent, mapUnitsPerPixel, mResultData.mRasterResults );
172+
return identifyRasterLayer( results, qobject_cast<QgsRasterLayer *>( layer ), point, viewExtent, mapUnitsPerPixel );
180173
}
181174
else if ( layer->type() == QgsMapLayer::VectorLayer && ( layerType == AllLayers || layerType == VectorLayer ) )
182175
{
183-
return identifyVectorLayer( qobject_cast<QgsVectorLayer *>( layer ), point );
176+
return identifyVectorLayer( results, qobject_cast<QgsVectorLayer *>( layer ), point );
184177
}
185178
else
186179
{
187180
return false;
188181
}
189182
}
190183

191-
bool QgsMapToolIdentify::identifyVectorLayer( QgsVectorLayer *layer, QgsPoint point )
184+
bool QgsMapToolIdentify::identifyVectorLayer( IdentifyResults *results, QgsVectorLayer *layer, QgsPoint point )
192185
{
193-
QgsDebugMsg( "point = " + point.toString() );
194186
if ( !layer )
195187
return false;
196188

@@ -210,7 +202,6 @@ bool QgsMapToolIdentify::identifyVectorLayer( QgsVectorLayer *layer, QgsPoint po
210202
QSettings settings;
211203
double identifyValue = settings.value( "/Map/identifyRadius", QGis::DEFAULT_IDENTIFY_RADIUS ).toDouble();
212204

213-
214205
if ( identifyValue <= 0.0 )
215206
identifyValue = QGis::DEFAULT_IDENTIFY_RADIUS;
216207

@@ -271,7 +262,7 @@ bool QgsMapToolIdentify::identifyVectorLayer( QgsVectorLayer *layer, QgsPoint po
271262

272263
derivedAttributes.insert( tr( "feature id" ), fid < 0 ? tr( "new feature" ) : FID_TO_STRING( fid ) );
273264

274-
mResultData.mVectorResults.append( VectorResult( layer, *f_it, derivedAttributes ) );
265+
results->mVectorResults.append( VectorResult( layer, *f_it, derivedAttributes ) );
275266
}
276267

277268
if ( renderer && renderer->capabilities() & QgsFeatureRendererV2::ScaleDependent )
@@ -348,7 +339,7 @@ QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( QgsFeatur
348339
return derivedAttributes;
349340
}
350341

351-
bool QgsMapToolIdentify::identifyRasterLayer( QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, QList<RasterResult>& rasterResults )
342+
bool QgsMapToolIdentify::identifyRasterLayer( IdentifyResults *results, QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel )
352343
{
353344
QgsDebugMsg( "point = " + point.toString() );
354345
if ( !layer ) return false;
@@ -442,7 +433,7 @@ bool QgsMapToolIdentify::identifyRasterLayer( QgsRasterLayer *layer, QgsPoint po
442433
attributes.insert( dprovider->generateBandName( bandNo ), valueString );
443434
}
444435
QString label = layer->name();
445-
rasterResults.append( RasterResult( layer, label, attributes, derivedAttributes ) );
436+
results->mRasterResults.append( RasterResult( layer, label, attributes, derivedAttributes ) );
446437
}
447438
else if ( format == QgsRasterDataProvider::IdentifyFormatFeature )
448439
{
@@ -476,7 +467,7 @@ bool QgsMapToolIdentify::identifyRasterLayer( QgsRasterLayer *layer, QgsPoint po
476467
QMap< QString, QString > derAttributes = derivedAttributes;
477468
derAttributes.unite( featureDerivedAttributes( &feature, layer ) );
478469

479-
rasterResults.append( RasterResult( layer, labels.join( " / " ), featureStore.fields(), feature, derAttributes ) );
470+
results->mRasterResults.append( RasterResult( layer, labels.join( " / " ), featureStore.fields(), feature, derAttributes ) );
480471
}
481472
}
482473
}
@@ -491,7 +482,7 @@ bool QgsMapToolIdentify::identifyRasterLayer( QgsRasterLayer *layer, QgsPoint po
491482
attributes.insert( "", value );
492483

493484
QString label = layer->subLayers().value( bandNo );
494-
rasterResults.append( RasterResult( layer, label, attributes, derivedAttributes ) );
485+
results->mRasterResults.append( RasterResult( layer, label, attributes, derivedAttributes ) );
495486
}
496487
}
497488

@@ -515,16 +506,13 @@ QGis::UnitType QgsMapToolIdentify::displayUnits()
515506
return mCanvas->mapUnits();
516507
}
517508

518-
QgsMapToolIdentify::IdentifyResults &QgsMapToolIdentify::results()
519-
{
520-
return mResultData;
521-
}
522-
523509
void QgsMapToolIdentify::formatChanged( QgsRasterLayer *layer )
524510
{
525511
QgsDebugMsg( "Entered" );
526-
QList<RasterResult> rasterResults;
527-
identifyRasterLayer( layer, mLastPoint, mLastExtent, mLastMapUnitsPerPixel, rasterResults );
528-
emit changedRasterResults( rasterResults );
512+
IdentifyResults results;
513+
if ( identifyRasterLayer( &results, layer, mLastPoint, mLastExtent, mLastMapUnitsPerPixel ) )
514+
{
515+
emit changedRasterResults( results.mRasterResults );
516+
}
529517
}
530518

‎src/gui/qgsmaptoolidentify.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
121121
@param layerList Performs the identification within the given list of layers. Default value is an empty list, i.e. uses all the layers.
122122
@param mode Identification mode. Can use Qgis default settings or a defined mode. Default mode is DefaultQgsSetting.
123123
@return true if identification succeeded and a feature has been found, false otherwise.*/
124-
bool identify( int x, int y, QList<QgsMapLayer*> layerList = QList<QgsMapLayer*>(), IdentifyMode mode = DefaultQgsSetting );
124+
IdentifyResults identify( int x, int y, QList<QgsMapLayer*> layerList = QList<QgsMapLayer*>(), IdentifyMode mode = DefaultQgsSetting );
125125

126126
/** Performs the identification.
127127
To avoid beeing forced to specify IdentifyMode with a list of layers
@@ -131,10 +131,7 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
131131
@param mode Identification mode. Can use Qgis default settings or a defined mode.
132132
@param layerType Only performs identification in a certain type of layers (raster, vector). Default value is AllLayers.
133133
@return true if identification succeeded and a feature has been found, false otherwise.*/
134-
bool identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );
135-
136-
/** Access to results */
137-
IdentifyResults &results();
134+
IdentifyResults identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );
138135

139136
public slots:
140137
void formatChanged( QgsRasterLayer *layer );
@@ -154,13 +151,13 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
154151
@param layerList Performs the identification within the given list of layers.
155152
@param layerType Only performs identification in a certain type of layers (raster, vector).
156153
@return true if identification succeeded and a feature has been found, false otherwise.*/
157-
bool identify( int x, int y, IdentifyMode mode, QList<QgsMapLayer*> layerList, LayerType layerType = AllLayers );
154+
IdentifyResults identify( int x, int y, IdentifyMode mode, QList<QgsMapLayer*> layerList, LayerType layerType = AllLayers );
158155

159-
bool identify( QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, IdentifyMode mode, QList<QgsMapLayer*> layerList, LayerType layerType = AllLayers );
156+
/** call the right method depending on layer type */
157+
bool identifyLayer( IdentifyResults *results, QgsMapLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, LayerType layerType = AllLayers );
160158

161-
bool identifyLayer( QgsMapLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, LayerType layerType = AllLayers );
162-
bool identifyRasterLayer( QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, QList<RasterResult>& rasterResults );
163-
bool identifyVectorLayer( QgsVectorLayer *layer, QgsPoint point );
159+
bool identifyRasterLayer( IdentifyResults *results, QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel );
160+
bool identifyVectorLayer( IdentifyResults *results, QgsVectorLayer *layer, QgsPoint point );
164161

165162
//! Private helper
166163
virtual void convertMeasurement( QgsDistanceArea &calc, double &measure, QGis::UnitType &u, bool isArea );
@@ -170,8 +167,6 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
170167

171168
QMap< QString, QString > featureDerivedAttributes( QgsFeature *feature, QgsMapLayer *layer );
172169

173-
IdentifyResults mResultData;
174-
175170
// Last point in canvas CRS
176171
QgsPoint mLastPoint;
177172

0 commit comments

Comments
 (0)
Please sign in to comment.