Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'info_layer_selection'
  • Loading branch information
mhugent committed Sep 23, 2013
2 parents f758597 + 1bc397d commit 6c2fa21
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 4 deletions.
134 changes: 132 additions & 2 deletions src/app/qgsmaptoolidentifyaction.cpp
Expand Up @@ -13,11 +13,13 @@
* *
***************************************************************************/

#include "qgsapplication.h"
#include "qgscursors.h"
#include "qgsdistancearea.h"
#include "qgsfeature.h"
#include "qgsfield.h"
#include "qgsgeometry.h"
#include "qgshighlight.h"
#include "qgslogger.h"
#include "qgsidentifyresultsdialog.h"
#include "qgsmapcanvas.h"
Expand Down Expand Up @@ -57,6 +59,7 @@ QgsMapToolIdentifyAction::~QgsMapToolIdentifyAction()
{
mResultsDialog->done( 0 );
}
deleteRubberBands();
}

QgsIdentifyResultsDialog *QgsMapToolIdentifyAction::resultsDialog()
Expand Down Expand Up @@ -90,10 +93,26 @@ void QgsMapToolIdentifyAction::canvasReleaseEvent( QMouseEvent *e )
}

resultsDialog()->clear();

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

QList<IdentifyResult> results;
QSettings settings;
IdentifyMode mode = static_cast<IdentifyMode>( settings.value( "/Map/identifyMode", 0 ).toInt() );
if ( mode == LayerSelection )
{
fillLayerIdResults( e->x(), e->y() ); //get id results from all layers into mLayerIdResults
QMenu layerSelectionMenu;
fillLayerSelectionMenu( layerSelectionMenu ); //fill selection menu with entries from mLayerIdResults
execLayerSelectionMenu( layerSelectionMenu, e->globalPos(), results );
mLayerIdResults.clear();
deleteRubberBands();
}
else
{
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 ) ) );

Expand Down Expand Up @@ -148,6 +167,7 @@ void QgsMapToolIdentifyAction::deactivate()
{
resultsDialog()->deactivate();
QgsMapTool::deactivate();
deleteRubberBands();
}

QGis::UnitType QgsMapToolIdentifyAction::displayUnits()
Expand All @@ -163,3 +183,113 @@ void QgsMapToolIdentifyAction::handleCopyToClipboard( QgsFeatureStore & featureS
emit copyToClipboard( featureStore );
}

void QgsMapToolIdentifyAction::handleMenuHover()
{
if ( !mCanvas )
{
return;
}

deleteRubberBands();
QAction* senderAction = qobject_cast<QAction*>( sender() );
if ( senderAction )
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( senderAction->data().toString() ) );
if ( vl )
{
QMap< QgsMapLayer*, QList<IdentifyResult> >::const_iterator lIt = mLayerIdResults.find( vl );
if ( lIt != mLayerIdResults.constEnd() )
{
const QList<IdentifyResult>& idList = lIt.value();
QList<IdentifyResult>::const_iterator idListIt = idList.constBegin();
for ( ; idListIt != idList.constEnd(); ++idListIt )
{
QgsHighlight* hl = new QgsHighlight( mCanvas, idListIt->mFeature.geometry(), vl );
hl->setColor( QColor( 255, 0, 0 ) );
hl->setWidth( 2 );
mRubberBands.append( hl );
}
}
}
}
}

void QgsMapToolIdentifyAction::deleteRubberBands()
{
QList<QgsHighlight*>::const_iterator it = mRubberBands.constBegin();
for ( ; it != mRubberBands.constEnd(); ++it )
{
delete *it;
}
mRubberBands.clear();
}

void QgsMapToolIdentifyAction::fillLayerIdResults( int x, int y )
{
mLayerIdResults.clear();
if ( !mCanvas )
{
return;
}

QList<QgsMapLayer*> canvasLayers = mCanvas->layers();
QList<QgsMapLayer*>::iterator it = canvasLayers.begin();
for ( ; it != canvasLayers.end(); ++it )
{
QList<IdentifyResult> idResult = QgsMapToolIdentify::identify( x, y, QList<QgsMapLayer*>() << *it );
if ( !idResult.isEmpty() )
{
mLayerIdResults.insert( *it, idResult );
}
}
}

void QgsMapToolIdentifyAction::fillLayerSelectionMenu( QMenu& menu )
{
QMap< QgsMapLayer*, QList<IdentifyResult> >::const_iterator resultIt = mLayerIdResults.constBegin();
for ( ; resultIt != mLayerIdResults.constEnd(); ++resultIt )
{
QAction* action = new QAction( resultIt.key()->name(), 0 );
action->setData( resultIt.key()->id() );
//add point/line/polygon icon
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( resultIt.key() );
if ( vl )
{
switch ( vl->geometryType() )
{
case QGis::Point:
action->setIcon( QgsApplication::getThemeIcon( "/mIconPointLayer.png" ) );
break;
case QGis::Line:
action->setIcon( QgsApplication::getThemeIcon( "/mIconLineLayer.png" ) );
break;
case QGis::Polygon:
action->setIcon( QgsApplication::getThemeIcon( "/mIconPolygonLayer.png" ) );
break;
default:
break;
}
}
else if ( resultIt.key()->type() == QgsMapLayer::RasterLayer )
{
action->setIcon( QgsApplication::getThemeIcon( "/mIconRaster.png" ) );
}
QObject::connect( action, SIGNAL( hovered() ), this, SLOT( handleMenuHover() ) );
menu.addAction( action );
}
}

void QgsMapToolIdentifyAction::execLayerSelectionMenu( QMenu& menu, const QPoint& pos, QList<IdentifyResult>& resultList )
{
QAction* selectedAction = menu.exec( QPoint( pos.x() + 5, pos.y() + 5 ) );
if ( selectedAction )
{
QgsMapLayer* selectedLayer = QgsMapLayerRegistry::instance()->mapLayer( selectedAction->data().toString() );
QMap< QgsMapLayer*, QList<IdentifyResult> >::const_iterator sIt = mLayerIdResults.find( selectedLayer );
if ( sIt != mLayerIdResults.constEnd() )
{
resultList = sIt.value();
}
}
}

17 changes: 16 additions & 1 deletion src/app/qgsmaptoolidentifyaction.h
Expand Up @@ -27,11 +27,12 @@
#include <QObject>
#include <QPointer>

class QgsHighlight;
class QgsIdentifyResultsDialog;
class QgsMapLayer;
class QgsRasterLayer;
class QgsRubberBand;
class QgsVectorLayer;
class QMenu;

/**
\brief Map tool for identifying features layers and showing results
Expand Down Expand Up @@ -76,9 +77,23 @@ class APP_EXPORT QgsMapToolIdentifyAction : public QgsMapToolIdentify
//! Pointer to the identify results dialog for name/value pairs
QPointer<QgsIdentifyResultsDialog> mResultsDialog;

//! layer id map for layer select mode
QMap< QgsMapLayer*, QList<IdentifyResult> > mLayerIdResults;
//! rubber bands for layer select mode
QList<QgsHighlight*> mRubberBands;

QgsIdentifyResultsDialog *resultsDialog();

virtual QGis::UnitType displayUnits();

//helper functions for layer selection mode
void deleteRubberBands();
void fillLayerIdResults( int x, int y );
void fillLayerSelectionMenu( QMenu& menu );
void execLayerSelectionMenu( QMenu& menu, const QPoint& pos, QList<IdentifyResult>& resultList );

private slots:
void handleMenuHover();
};

#endif
1 change: 1 addition & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -109,6 +109,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
cmbIdentifyMode->addItem( tr( "Current layer" ), 0 );
cmbIdentifyMode->addItem( tr( "Top down, stop at first" ), 1 );
cmbIdentifyMode->addItem( tr( "Top down" ), 2 );
cmbIdentifyMode->addItem( tr( "Layer selection" ), 3 );

// read the current browser and set it
QSettings settings;
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsmaptoolidentify.h
Expand Up @@ -51,7 +51,8 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
DefaultQgsSetting = -1,
ActiveLayer,
TopDownStopAtFirst,
TopDownAll
TopDownAll,
LayerSelection
};

enum LayerType
Expand Down

0 comments on commit 6c2fa21

Please sign in to comment.