Skip to content

Commit

Permalink
move LayerSelection identification to gui
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Oct 28, 2013
1 parent d4027bb commit 2286f0a
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 144 deletions.
128 changes: 1 addition & 127 deletions src/app/qgsmaptoolidentifyaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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();
}
}
}

16 changes: 0 additions & 16 deletions src/app/qgsmaptoolidentifyaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
118 changes: 117 additions & 1 deletion src/gui/qgsmaptoolidentify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -41,6 +43,7 @@
#include <QPixmap>
#include <QStatusBar>
#include <QVariant>
#include <QMenu>

QgsMapToolIdentify::QgsMapToolIdentify( QgsMapCanvas* canvas )
: QgsMapTool( canvas )
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();
}





15 changes: 15 additions & 0 deletions src/gui/qgsmaptoolidentify.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class QgsRasterLayer;
class QgsVectorLayer;
class QgsMapLayer;
class QgsMapCanvas;
class QgsHighlight;

/**
\brief Map tool for identifying features in layers
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe this commit a18b4a3 @blazek

@3nids
Copy link
Member Author

@3nids 3nids commented on 2286f0a Jan 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@slarosa
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

@blazek blazek commented on 2286f0a Jan 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be a18b4a3 -> moving discussion there.

@blazek
Copy link
Member

@blazek blazek commented on 2286f0a Feb 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be fixed in b05c93c.

Please sign in to comment.