Skip to content

Commit

Permalink
unifying identify results in a single struct
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Feb 7, 2013
1 parent e37e1cc commit 66696ba
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 99 deletions.
40 changes: 15 additions & 25 deletions python/gui/qgsmaptoolidentify.sip
Expand Up @@ -20,36 +20,26 @@ class QgsMapToolIdentify : QgsMapTool
VectorLayer,
RasterLayer,
};

struct VectorResult
{
VectorResult();
VectorResult(QgsVectorLayer* layer, QgsFeature feature, QMap< QString, QString > derivedAttributes);
QgsVectorLayer* mLayer;
QgsFeature mFeature;
QMap< QString, QString > mDerivedAttributes;
};

struct RasterResult

struct IdentifyResult
{
RasterResult();
RasterResult( QgsRasterLayer * layer, QString label, QgsFields fields, QgsFeature feature, QMap< QString, QString > derivedAttributes );
QgsRasterLayer* mLayer;
IdentifyResult();

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

IdentifyResult( QgsMapLayer * layer, QString label, QMap< QString, QString > attributes, QMap< QString, QString > derivedAttributes );

IdentifyResult( QgsMapLayer * layer, QString label, QgsFields fields, QgsFeature feature, QMap< QString, QString > derivedAttributes );

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

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

//! constructor
QgsMapToolIdentify( QgsMapCanvas* canvas );

Expand All @@ -73,8 +63,8 @@ class QgsMapToolIdentify : 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.*/
IdentifyResults identify(int x, int y, QList<QgsMapLayer*> layerList = QList<QgsMapLayer*>(), IdentifyMode mode = DefaultQgsSetting);
@return a list of IdentifyResult*/
QList<QgsMapToolIdentify::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 @@ -83,8 +73,8 @@ class QgsMapToolIdentify : 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.*/
IdentifyResults identify(int x, int y, IdentifyMode mode, LayerType layerType = AllLayers);
@return a list of IdentifyResult*/
QList<QgsMapToolIdentify::IdentifyResult> identify(int x, int y, IdentifyMode mode, LayerType layerType = AllLayers);

signals:
void identifyProgress( int, int );
Expand Down
16 changes: 13 additions & 3 deletions src/app/qgsidentifyresultsdialog.cpp
Expand Up @@ -292,9 +292,19 @@ QTreeWidgetItem *QgsIdentifyResultsDialog::layerItem( QObject *layer )
return 0;
}

void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer,
const QgsFeature &f,
const QMap<QString, QString> &derivedAttributes )
void QgsIdentifyResultsDialog::addFeature( QgsMapToolIdentify::IdentifyResult result )
{
if ( result.mLayer->type() == QgsMapLayer::VectorLayer )
{
addFeature( qobject_cast<QgsVectorLayer *>( result.mLayer ), result.mFeature, result.mDerivedAttributes );
}
else if ( result.mLayer->type() == QgsMapLayer::RasterLayer )
{
addFeature( qobject_cast<QgsRasterLayer *>( result.mLayer ), result.mLabel, result.mAttributes, result.mDerivedAttributes, result.mFields, result.mFeature );
}
}

void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeature &f, const QMap<QString, QString> &derivedAttributes )
{
QTreeWidgetItem *layItem = layerItem( vlayer );

Expand Down
8 changes: 6 additions & 2 deletions src/app/qgsidentifyresultsdialog.h
Expand Up @@ -24,6 +24,7 @@
#include "qgsfeature.h"
#include "qgsfeaturestore.h"
#include "qgsfield.h"
#include "qgsmaptoolidentify.h"
#include "qgscoordinatereferencesystem.h"

#include <QWidget>
Expand Down Expand Up @@ -101,18 +102,21 @@ class QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdentifyResultsBa
~QgsIdentifyResultsDialog();

/** Add add feature from vector layer */
void addFeature( QgsVectorLayer *layer,
void addFeature( QgsVectorLayer * layer,
const QgsFeature &f,
const QMap< QString, QString > &derivedAttributes );

/** Add add feature from other layer */
void addFeature( QgsRasterLayer *layer,
void addFeature( QgsRasterLayer * layer,
QString label,
const QMap< QString, QString > &attributes,
const QMap< QString, QString > &derivedAttributes,
const QgsFields &fields = QgsFields(),
const QgsFeature &feature = QgsFeature() );

/** Add feature from identify results */
void addFeature( QgsMapToolIdentify::IdentifyResult result );

/** map tool was deactivated */
void deactivate();

Expand Down
32 changes: 15 additions & 17 deletions src/app/qgsmaptoolidentifyaction.cpp
Expand Up @@ -41,14 +41,14 @@
#include <QStatusBar>
#include <QVariant>

QgsMapToolIdentifyAction::QgsMapToolIdentifyAction( QgsMapCanvas* canvas )
QgsMapToolIdentifyAction::QgsMapToolIdentifyAction( QgsMapCanvas * canvas )
: QgsMapToolIdentify( canvas )
{
// set cursor
QPixmap myIdentifyQPixmap = QPixmap(( const char ** ) identify_cursor );
mCursor = QCursor( myIdentifyQPixmap, 1, 1 );

connect( this, SIGNAL( changedRasterResults( QList<RasterResult>& ) ), this, SLOT( handleChangedRasterResults( QList<RasterResult>& ) ) );
connect( this, SIGNAL( changedRasterResults( QList<IdentifyResult>& ) ), this, SLOT( handleChangedRasterResults( QList<RasterResult>& ) ) );
}

QgsMapToolIdentifyAction::~QgsMapToolIdentifyAction()
Expand Down Expand Up @@ -93,23 +93,18 @@ void QgsMapToolIdentifyAction::canvasReleaseEvent( QMouseEvent *e )

connect( this, SIGNAL( identifyProgress( int, int ) ), QgisApp::instance(), SLOT( showProgress( int, int ) ) );
connect( this, SIGNAL( identifyMessage( QString ) ), QgisApp::instance(), SLOT( showStatusMessage( QString ) ) );
IdentifyResults results = QgsMapToolIdentify::identify( e->x(), e->y() );
QList<IdentifyResult> results = QgsMapToolIdentify::identify( e->x(), e->y() );
disconnect( this, SIGNAL( identifyProgress( int, int ) ), QgisApp::instance(), SLOT( showProgress( int, int ) ) );
disconnect( this, SIGNAL( identifyMessage( QString ) ), QgisApp::instance(), SLOT( showStatusMessage( QString ) ) );


QList<VectorResult>::const_iterator vresult;
for ( vresult = results.mVectorResults.begin(); vresult != results.mVectorResults.end(); ++vresult )
QList<IdentifyResult>::const_iterator result;
for ( result = results.begin(); result != results.end(); ++result )
{
resultsDialog()->addFeature( vresult->mLayer, vresult->mFeature, vresult->mDerivedAttributes );
}
QList<RasterResult>::const_iterator rresult;
for ( rresult = results.mRasterResults.begin(); rresult != results.mRasterResults.end(); ++rresult )
{
resultsDialog()->addFeature( rresult->mLayer, rresult->mLabel, rresult->mAttributes, rresult->mDerivedAttributes, rresult->mFields, rresult->mFeature );
resultsDialog()->addFeature( *result );
}

if ( !results.mRasterResults.isEmpty() || !results.mVectorResults.isEmpty() )
if ( !results.isEmpty() )
{
resultsDialog()->show();
}
Expand All @@ -129,14 +124,17 @@ void QgsMapToolIdentifyAction::canvasReleaseEvent( QMouseEvent *e )
}
}

void QgsMapToolIdentifyAction::handleChangedRasterResults( QList<RasterResult>& rasterResults )
void QgsMapToolIdentifyAction::handleChangedRasterResults( QList<IdentifyResult> &results )
{
// Add new result after raster format change
QgsDebugMsg( QString( "%1 raster results" ).arg( rasterResults.size() ) );
QList<RasterResult>::const_iterator rresult;
for ( rresult = rasterResults.begin(); rresult != rasterResults.end(); ++rresult )
QgsDebugMsg( QString( "%1 raster results" ).arg( results.size() ) );
QList<IdentifyResult>::const_iterator rresult;
for ( rresult = results.begin(); rresult != results.end(); ++rresult )
{
resultsDialog()->addFeature( rresult->mLayer, rresult->mLabel, rresult->mAttributes, rresult->mDerivedAttributes, rresult->mFields, rresult->mFeature );
if ( rresult->mLayer->type() == QgsMapLayer::RasterLayer )
{
resultsDialog()->addFeature( qobject_cast<QgsRasterLayer *>( rresult->mLayer ), rresult->mLabel, rresult->mAttributes, rresult->mDerivedAttributes, rresult->mFields, rresult->mFeature );
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsmaptoolidentifyaction.h
Expand Up @@ -46,7 +46,7 @@ class QgsMapToolIdentifyAction : public QgsMapToolIdentify
Q_OBJECT

public:
QgsMapToolIdentifyAction( QgsMapCanvas* canvas );
QgsMapToolIdentifyAction( QgsMapCanvas * canvas );

~QgsMapToolIdentifyAction();

Expand All @@ -65,7 +65,7 @@ class QgsMapToolIdentifyAction : public QgsMapToolIdentify

public slots:
void handleCopyToClipboard( QgsFeatureStore & );
void handleChangedRasterResults( QList<RasterResult>& rasterResults );
void handleChangedRasterResults( QList<IdentifyResult>& results );

signals:
void identifyProgress( int, int );
Expand Down
32 changes: 16 additions & 16 deletions src/gui/qgsmaptoolidentify.cpp
Expand Up @@ -53,34 +53,34 @@ 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 );
}

QgsMapToolIdentify::IdentifyResults 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 );
}

QgsMapToolIdentify::IdentifyResults 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 );
}

QgsMapToolIdentify::IdentifyResults 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 )
{
IdentifyResults results;
QList<IdentifyResult> results;

mLastPoint = mCanvas->getCoordinateTransform()->toMapCoordinates( x, y );
mLastExtent = mCanvas->extent();
Expand Down Expand Up @@ -165,7 +165,7 @@ void QgsMapToolIdentify::deactivate()
QgsMapTool::deactivate();
}

bool QgsMapToolIdentify::identifyLayer( IdentifyResults *results, 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 ) )
{
Expand All @@ -181,7 +181,7 @@ bool QgsMapToolIdentify::identifyLayer( IdentifyResults *results, QgsMapLayer *l
}
}

bool QgsMapToolIdentify::identifyVectorLayer( IdentifyResults *results, QgsVectorLayer *layer, QgsPoint point )
bool QgsMapToolIdentify::identifyVectorLayer( QList<IdentifyResult> *results, QgsVectorLayer *layer, QgsPoint point )
{
if ( !layer )
return false;
Expand Down Expand Up @@ -262,7 +262,7 @@ bool QgsMapToolIdentify::identifyVectorLayer( IdentifyResults *results, QgsVecto

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

results->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 @@ -339,7 +339,7 @@ QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( QgsFeatur
return derivedAttributes;
}

bool QgsMapToolIdentify::identifyRasterLayer( IdentifyResults *results, QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel )
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 @@ -433,7 +433,7 @@ bool QgsMapToolIdentify::identifyRasterLayer( IdentifyResults *results, QgsRaste
attributes.insert( dprovider->generateBandName( bandNo ), valueString );
}
QString label = layer->name();
results->mRasterResults.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 @@ -467,7 +467,7 @@ bool QgsMapToolIdentify::identifyRasterLayer( IdentifyResults *results, QgsRaste
QMap< QString, QString > derAttributes = derivedAttributes;
derAttributes.unite( featureDerivedAttributes( &feature, layer ) );

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

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

Expand All @@ -509,10 +509,10 @@ QGis::UnitType QgsMapToolIdentify::displayUnits()
void QgsMapToolIdentify::formatChanged( QgsRasterLayer *layer )
{
QgsDebugMsg( "Entered" );
IdentifyResults results;
QList<IdentifyResult> results;
if ( identifyRasterLayer( &results, layer, mLastPoint, mLastExtent, mLastMapUnitsPerPixel ) )
{
emit changedRasterResults( results.mRasterResults );
emit changedRasterResults( results );
}
}

0 comments on commit 66696ba

Please sign in to comment.