Skip to content
Permalink
Browse files

move LayerSelection identification to gui

  • Loading branch information
3nids committed Oct 28, 2013
1 parent d4027bb commit 2286f0a803592112f394c8eaa1abaf52465f528e
Showing with 133 additions and 144 deletions.
  1. +1 −127 src/app/qgsmaptoolidentifyaction.cpp
  2. +0 −16 src/app/qgsmaptoolidentifyaction.h
  3. +117 −1 src/gui/qgsmaptoolidentify.cpp
  4. +15 −0 src/gui/qgsmaptoolidentify.h
@@ -19,7 +19,6 @@
#include "qgsfeature.h"
#include "qgsfield.h"
#include "qgsgeometry.h"
#include "qgshighlight.h"
#include "qgslogger.h"
#include "qgsidentifyresultsdialog.h"
#include "qgsmapcanvas.h"
@@ -96,27 +95,11 @@ 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 ) ) );

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() );
}
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<IdentifyResult>::const_iterator result;
for ( result = results.begin(); result != results.end(); ++result )
{
@@ -183,113 +166,4 @@ 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();
}
}
}

@@ -27,12 +27,10 @@
#include <QObject>
#include <QPointer>

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

/**
\brief Map tool for identifying features layers and showing results
@@ -77,23 +75,9 @@ 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
@@ -13,12 +13,14 @@
* *
***************************************************************************/

#include "qgsapplication.h"
#include "qgscursors.h"
#include "qgsdistancearea.h"
#include "qgsfeature.h"
#include "qgsfeaturestore.h"
#include "qgsfield.h"
#include "qgsgeometry.h"
#include "qgshighlight.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaptoolidentify.h"
@@ -41,6 +43,7 @@
#include <QPixmap>
#include <QStatusBar>
#include <QVariant>
#include <QMenu>

QgsMapToolIdentify::QgsMapToolIdentify( QgsMapCanvas* canvas )
: QgsMapTool( canvas )
@@ -98,7 +101,75 @@ QList<QgsMapToolIdentify::IdentifyResult> QgsMapToolIdentify::identify( int x, i
mode = static_cast<IdentifyMode>( settings.value( "/Map/identifyMode", 0 ).toInt() );
}

if ( mode == ActiveLayer && layerList.isEmpty() )
if ( mode == LayerSelection )
{
// fill map of layer / identify results
mLayerIdResults.clear();
QList<IdentifyResult> idResult = identify( x, y, TopDownAll );
QList<IdentifyResult>::const_iterator it = idResult.constBegin();
for ( ; it != idResult.constEnd(); it++ )
{
QgsMapLayer *layer = it->mLayer;
if ( mLayerIdResults.contains( layer ) )
{
mLayerIdResults[layer].append( idResult );
}
else
{
mLayerIdResults.insert( layer, idResult );
}
}

//fill selection menu with entries from mmLayerIdResults
QMenu layerSelectionMenu;
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() ) );
layerSelectionMenu.addAction( action );
}

// exec layer selection menu
QPoint globalPos = mCanvas->mapToGlobal( QPoint( x + 5, y + 5 ) );
QAction* selectedAction = layerSelectionMenu.exec( globalPos );
if ( selectedAction )
{
QgsMapLayer* selectedLayer = QgsMapLayerRegistry::instance()->mapLayer( selectedAction->data().toString() );
QMap< QgsMapLayer*, QList<IdentifyResult> >::const_iterator sIt = mLayerIdResults.find( selectedLayer );
if ( sIt != mLayerIdResults.constEnd() )
{
results = sIt.value();
}
}

deleteRubberBands();
}
else if ( mode == ActiveLayer && layerList.isEmpty() )
{
QgsMapLayer *layer = mCanvas->currentLayer();

@@ -559,3 +630,48 @@ void QgsMapToolIdentify::formatChanged( QgsRasterLayer *layer )
}
}

void QgsMapToolIdentify::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 QgsMapToolIdentify::deleteRubberBands()
{
QList<QgsHighlight*>::const_iterator it = mRubberBands.constBegin();
for ( ; it != mRubberBands.constEnd(); ++it )
{
delete *it;
}
mRubberBands.clear();
}





@@ -31,6 +31,7 @@ class QgsRasterLayer;
class QgsVectorLayer;
class QgsMapLayer;
class QgsMapCanvas;
class QgsHighlight;

/**
\brief Map tool for identifying features in layers
@@ -120,6 +121,10 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
@return a list of IdentifyResult*/
QList<IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );

protected:
//! rubber bands for layer select mode
void deleteRubberBands();

public slots:
void formatChanged( QgsRasterLayer *layer );

@@ -154,12 +159,22 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool

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

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

// Last point in canvas CRS
QgsPoint mLastPoint;

double mLastMapUnitsPerPixel;

QgsRectangle mLastExtent;

private slots:
//! menu for layer selection
void handleMenuHover();
};

#endif

6 comments on commit 2286f0a

@slarosa

This comment has been minimized.

Copy link
Member

@slarosa slarosa replied Jan 27, 2014

the highlighting of the geometry covers everything in map canvas (see image below), I am not sure if this commit has introduced this issue, @3nids please could you check out it ?

schermata del 2014-01-23 09 18 47schermata del 2014-01-23 09 18 43

@slarosa

This comment has been minimized.

Copy link
Member

@slarosa slarosa replied Jan 27, 2014

or maybe this commit a18b4a3 @blazek

@3nids

This comment has been minimized.

Copy link
Member Author

@3nids 3nids replied Jan 27, 2014

I just moved the code from app to gui, so I don't think I am at the origin of this...

@slarosa

This comment has been minimized.

Copy link
Member

@slarosa slarosa replied Jan 27, 2014

I just moved the code from app to gui, so I don't think I am at the origin of this...

sorry @3nids I think the issue might be generated by @blazek's commit.

@blazek

This comment has been minimized.

Copy link
Member

@blazek blazek replied Jan 27, 2014

It will be a18b4a3 -> moving discussion there.

@blazek

This comment has been minimized.

Copy link
Member

@blazek blazek replied Feb 9, 2014

It should be fixed in b05c93c.

Please sign in to comment.
You can’t perform that action at this time.