66 changes: 27 additions & 39 deletions src/gui/qgsmaptoolidentify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,50 +53,44 @@ QgsMapToolIdentify::~QgsMapToolIdentify()
{
}

void QgsMapToolIdentify::canvasMoveEvent( QMouseEvent *e )
void QgsMapToolIdentify::canvasMoveEvent( QMouseEvent * e )
{
Q_UNUSED( e );
}

void QgsMapToolIdentify::canvasPressEvent( QMouseEvent *e )
void QgsMapToolIdentify::canvasPressEvent( QMouseEvent * e )
{
Q_UNUSED( e );
}

void QgsMapToolIdentify::canvasReleaseEvent( QMouseEvent *e )
void QgsMapToolIdentify::canvasReleaseEvent( QMouseEvent * e )
{
Q_UNUSED( e );
}

bool QgsMapToolIdentify::identify( int x, int y, QList<QgsMapLayer *> layerList, IdentifyMode mode )
QList<QgsMapToolIdentify::IdentifyResult> QgsMapToolIdentify::identify( int x, int y, QList<QgsMapLayer *> layerList, IdentifyMode mode )
{
return identify( x, y, mode, layerList, AllLayers );
}

bool QgsMapToolIdentify::identify( int x, int y, IdentifyMode mode, LayerType layerType )
QList<QgsMapToolIdentify::IdentifyResult> QgsMapToolIdentify::identify( int x, int y, IdentifyMode mode, LayerType layerType )
{
return identify( x, y, mode, QList<QgsMapLayer*>(), layerType );
}

bool QgsMapToolIdentify::identify( int x, int y, IdentifyMode mode, QList<QgsMapLayer*> layerList, LayerType layerType )
QList<QgsMapToolIdentify::IdentifyResult> QgsMapToolIdentify::identify( int x, int y, IdentifyMode mode, QList<QgsMapLayer*> layerList, LayerType layerType )
{
QList<IdentifyResult> results;

mLastPoint = mCanvas->getCoordinateTransform()->toMapCoordinates( x, y );
mLastExtent = mCanvas->extent();
mLastMapUnitsPerPixel = mCanvas->mapUnitsPerPixel();
return identify( mLastPoint, mLastExtent, mLastMapUnitsPerPixel, mode, layerList, layerType );
}

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

mResultData.mVectorResults.clear();
mResultData.mRasterResults.clear();

if ( mode == DefaultQgsSetting )
{
QSettings settings;
Expand All @@ -110,12 +104,12 @@ bool QgsMapToolIdentify::identify( QgsPoint point, QgsRectangle viewExtent, doub
if ( !layer )
{
emit identifyMessage( tr( "No active layer. To identify features, you must choose an active layer." ) );
return res;
return results;
}

QApplication::setOverrideCursor( Qt::WaitCursor );

res = identifyLayer( layer, point, viewExtent, mapUnitsPerPixel, layerType );
identifyLayer( &results, layer, mLastPoint, mLastExtent, mLastMapUnitsPerPixel, layerType );
}
else
{
Expand Down Expand Up @@ -145,9 +139,8 @@ bool QgsMapToolIdentify::identify( QgsPoint point, QgsRectangle viewExtent, doub
if ( noIdentifyLayerIdList.contains( layer->id() ) )
continue;

if ( identifyLayer( layer, point, viewExtent, mapUnitsPerPixel, layerType ) )
if ( identifyLayer( &results, layer, mLastPoint, mLastExtent, mLastMapUnitsPerPixel, layerType ) )
{
res = true;
if ( mode == TopDownStopAtFirst )
break;
}
Expand All @@ -159,7 +152,7 @@ bool QgsMapToolIdentify::identify( QgsPoint point, QgsRectangle viewExtent, doub

QApplication::restoreOverrideCursor();

return res;
return results;
}

void QgsMapToolIdentify::activate()
Expand All @@ -172,25 +165,24 @@ void QgsMapToolIdentify::deactivate()
QgsMapTool::deactivate();
}

bool QgsMapToolIdentify::identifyLayer( QgsMapLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, LayerType layerType )
bool QgsMapToolIdentify::identifyLayer( QList<IdentifyResult> *results, QgsMapLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, LayerType layerType )
{
if ( layer->type() == QgsMapLayer::RasterLayer && ( layerType == AllLayers || layerType == RasterLayer ) )
{
return identifyRasterLayer( qobject_cast<QgsRasterLayer *>( layer ), point, viewExtent, mapUnitsPerPixel, mResultData.mRasterResults );
return identifyRasterLayer( results, qobject_cast<QgsRasterLayer *>( layer ), point, viewExtent, mapUnitsPerPixel );
}
else if ( layer->type() == QgsMapLayer::VectorLayer && ( layerType == AllLayers || layerType == VectorLayer ) )
{
return identifyVectorLayer( qobject_cast<QgsVectorLayer *>( layer ), point );
return identifyVectorLayer( results, qobject_cast<QgsVectorLayer *>( layer ), point );
}
else
{
return false;
}
}

bool QgsMapToolIdentify::identifyVectorLayer( QgsVectorLayer *layer, QgsPoint point )
bool QgsMapToolIdentify::identifyVectorLayer( QList<IdentifyResult> *results, QgsVectorLayer *layer, QgsPoint point )
{
QgsDebugMsg( "point = " + point.toString() );
if ( !layer )
return false;

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


if ( identifyValue <= 0.0 )
identifyValue = QGis::DEFAULT_IDENTIFY_RADIUS;

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

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

mResultData.mVectorResults.append( VectorResult( layer, *f_it, derivedAttributes ) );
results->append( IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), *f_it, derivedAttributes ) );
}

if ( renderer && renderer->capabilities() & QgsFeatureRendererV2::ScaleDependent )
Expand Down Expand Up @@ -348,7 +339,7 @@ QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( QgsFeatur
return derivedAttributes;
}

bool QgsMapToolIdentify::identifyRasterLayer( QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, QList<RasterResult>& rasterResults )
bool QgsMapToolIdentify::identifyRasterLayer( QList<IdentifyResult> *results, QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel )
{
QgsDebugMsg( "point = " + point.toString() );
if ( !layer ) return false;
Expand Down Expand Up @@ -442,7 +433,7 @@ bool QgsMapToolIdentify::identifyRasterLayer( QgsRasterLayer *layer, QgsPoint po
attributes.insert( dprovider->generateBandName( bandNo ), valueString );
}
QString label = layer->name();
rasterResults.append( RasterResult( layer, label, attributes, derivedAttributes ) );
results->append( IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), label, attributes, derivedAttributes ) );
}
else if ( format == QgsRasterDataProvider::IdentifyFormatFeature )
{
Expand Down Expand Up @@ -476,7 +467,7 @@ bool QgsMapToolIdentify::identifyRasterLayer( QgsRasterLayer *layer, QgsPoint po
QMap< QString, QString > derAttributes = derivedAttributes;
derAttributes.unite( featureDerivedAttributes( &feature, layer ) );

rasterResults.append( RasterResult( layer, labels.join( " / " ), featureStore.fields(), feature, derAttributes ) );
results->append( IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), labels.join( " / " ), featureStore.fields(), feature, derAttributes ) );
}
}
}
Expand All @@ -491,7 +482,7 @@ bool QgsMapToolIdentify::identifyRasterLayer( QgsRasterLayer *layer, QgsPoint po
attributes.insert( "", value );

QString label = layer->subLayers().value( bandNo );
rasterResults.append( RasterResult( layer, label, attributes, derivedAttributes ) );
results->append( IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), label, attributes, derivedAttributes ) );
}
}

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

QgsMapToolIdentify::IdentifyResults &QgsMapToolIdentify::results()
{
return mResultData;
}

void QgsMapToolIdentify::formatChanged( QgsRasterLayer *layer )
{
QgsDebugMsg( "Entered" );
QList<RasterResult> rasterResults;
identifyRasterLayer( layer, mLastPoint, mLastExtent, mLastMapUnitsPerPixel, rasterResults );
emit changedRasterResults( rasterResults );
QList<IdentifyResult> results;
if ( identifyRasterLayer( &results, layer, mLastPoint, mLastExtent, mLastMapUnitsPerPixel ) )
{
emit changedRasterResults( results );
}
}

60 changes: 20 additions & 40 deletions src/gui/qgsmaptoolidentify.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

class QgsRasterLayer;
class QgsVectorLayer;
class QgsMapLayer;
class QgsMapCanvas;

/**
Expand Down Expand Up @@ -60,45 +61,29 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
RasterLayer
};

struct VectorResult
struct IdentifyResult
{
VectorResult() {}
VectorResult( QgsVectorLayer * layer, QgsFeature feature, QMap< QString, QString > derivedAttributes ):
IdentifyResult() {}

IdentifyResult( QgsMapLayer * layer, QgsFeature feature, QMap< QString, QString > derivedAttributes ):
mLayer( layer ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
QgsVectorLayer* mLayer;
QgsFeature mFeature;
QMap< QString, QString > mDerivedAttributes;
};

struct RasterResult
{
RasterResult() {}
RasterResult( QgsRasterLayer * layer, QString label, QMap< QString, QString > attributes, QMap< QString, QString > derivedAttributes ):
IdentifyResult( QgsMapLayer * layer, QString label, QMap< QString, QString > attributes, QMap< QString, QString > derivedAttributes ):
mLayer( layer ), mLabel( label ), mAttributes( attributes ), mDerivedAttributes( derivedAttributes ) {}

RasterResult( QgsRasterLayer * layer, QString label, QgsFields fields, QgsFeature feature, QMap< QString, QString > derivedAttributes ):
IdentifyResult( QgsMapLayer * layer, QString label, QgsFields fields, QgsFeature feature, QMap< QString, QString > derivedAttributes ):
mLayer( layer ), mLabel( label ), mFields( fields ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
QgsRasterLayer* mLayer;

QgsMapLayer* mLayer;
QString mLabel;
QgsFields mFields;
QgsFeature mFeature;
QMap< QString, QString > mAttributes;
QMap< QString, QString > mDerivedAttributes;
};

struct IdentifyResults
{
IdentifyResults() {}
IdentifyResults( QList<VectorResult> vectorResults , QList<RasterResult> rasterResults ) :
mVectorResults( vectorResults ),
mRasterResults( rasterResults )
{}
QList<VectorResult> mVectorResults;
QList<RasterResult> mRasterResults;
};

//! constructor
QgsMapToolIdentify( QgsMapCanvas* canvas );
QgsMapToolIdentify( QgsMapCanvas * canvas );

virtual ~QgsMapToolIdentify();

Expand All @@ -120,8 +105,8 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
@param y y coordinates of mouseEvent
@param layerList Performs the identification within the given list of layers. Default value is an empty list, i.e. uses all the layers.
@param mode Identification mode. Can use Qgis default settings or a defined mode. Default mode is DefaultQgsSetting.
@return true if identification succeeded and a feature has been found, false otherwise.*/
bool identify( int x, int y, QList<QgsMapLayer*> layerList = QList<QgsMapLayer*>(), IdentifyMode mode = DefaultQgsSetting );
@return a list of IdentifyResult*/
QList<IdentifyResult> identify( int x, int y, QList<QgsMapLayer*> layerList = QList<QgsMapLayer*>(), IdentifyMode mode = DefaultQgsSetting );

/** Performs the identification.
To avoid beeing forced to specify IdentifyMode with a list of layers
Expand All @@ -130,19 +115,16 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
@param y y coordinates of mouseEvent
@param mode Identification mode. Can use Qgis default settings or a defined mode.
@param layerType Only performs identification in a certain type of layers (raster, vector). Default value is AllLayers.
@return true if identification succeeded and a feature has been found, false otherwise.*/
bool identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );

/** Access to results */
IdentifyResults &results();
@return a list of IdentifyResult*/
QList<IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );

public slots:
void formatChanged( QgsRasterLayer *layer );

signals:
void identifyProgress( int, int );
void identifyMessage( QString );
void changedRasterResults( QList<RasterResult>& );
void changedRasterResults( QList<IdentifyResult>& );

private:
/** Performs the identification.
Expand All @@ -154,13 +136,13 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
@param layerList Performs the identification within the given list of layers.
@param layerType Only performs identification in a certain type of layers (raster, vector).
@return true if identification succeeded and a feature has been found, false otherwise.*/
bool identify( int x, int y, IdentifyMode mode, QList<QgsMapLayer*> layerList, LayerType layerType = AllLayers );
QList<IdentifyResult> identify( int x, int y, IdentifyMode mode, QList<QgsMapLayer*> layerList, LayerType layerType = AllLayers );

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

bool identifyLayer( QgsMapLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, LayerType layerType = AllLayers );
bool identifyRasterLayer( QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, QList<RasterResult>& rasterResults );
bool identifyVectorLayer( QgsVectorLayer *layer, QgsPoint point );
bool identifyRasterLayer( QList<IdentifyResult> *results, QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel );
bool identifyVectorLayer( QList<IdentifyResult> *results, QgsVectorLayer *layer, QgsPoint point );

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

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

IdentifyResults mResultData;

// Last point in canvas CRS
QgsPoint mLastPoint;

Expand Down