Skip to content

Commit 6c2fa21

Browse files
committed
Merge branch 'info_layer_selection'
2 parents f758597 + 1bc397d commit 6c2fa21

File tree

4 files changed

+151
-4
lines changed

4 files changed

+151
-4
lines changed

src/app/qgsmaptoolidentifyaction.cpp

Lines changed: 132 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
* *
1414
***************************************************************************/
1515

16+
#include "qgsapplication.h"
1617
#include "qgscursors.h"
1718
#include "qgsdistancearea.h"
1819
#include "qgsfeature.h"
1920
#include "qgsfield.h"
2021
#include "qgsgeometry.h"
22+
#include "qgshighlight.h"
2123
#include "qgslogger.h"
2224
#include "qgsidentifyresultsdialog.h"
2325
#include "qgsmapcanvas.h"
@@ -57,6 +59,7 @@ QgsMapToolIdentifyAction::~QgsMapToolIdentifyAction()
5759
{
5860
mResultsDialog->done( 0 );
5961
}
62+
deleteRubberBands();
6063
}
6164

6265
QgsIdentifyResultsDialog *QgsMapToolIdentifyAction::resultsDialog()
@@ -90,10 +93,26 @@ void QgsMapToolIdentifyAction::canvasReleaseEvent( QMouseEvent *e )
9093
}
9194

9295
resultsDialog()->clear();
93-
9496
connect( this, SIGNAL( identifyProgress( int, int ) ), QgisApp::instance(), SLOT( showProgress( int, int ) ) );
9597
connect( this, SIGNAL( identifyMessage( QString ) ), QgisApp::instance(), SLOT( showStatusMessage( QString ) ) );
96-
QList<IdentifyResult> results = QgsMapToolIdentify::identify( e->x(), e->y() );
98+
99+
QList<IdentifyResult> results;
100+
QSettings settings;
101+
IdentifyMode mode = static_cast<IdentifyMode>( settings.value( "/Map/identifyMode", 0 ).toInt() );
102+
if ( mode == LayerSelection )
103+
{
104+
fillLayerIdResults( e->x(), e->y() ); //get id results from all layers into mLayerIdResults
105+
QMenu layerSelectionMenu;
106+
fillLayerSelectionMenu( layerSelectionMenu ); //fill selection menu with entries from mLayerIdResults
107+
execLayerSelectionMenu( layerSelectionMenu, e->globalPos(), results );
108+
mLayerIdResults.clear();
109+
deleteRubberBands();
110+
}
111+
else
112+
{
113+
results = QgsMapToolIdentify::identify( e->x(), e->y() );
114+
}
115+
97116
disconnect( this, SIGNAL( identifyProgress( int, int ) ), QgisApp::instance(), SLOT( showProgress( int, int ) ) );
98117
disconnect( this, SIGNAL( identifyMessage( QString ) ), QgisApp::instance(), SLOT( showStatusMessage( QString ) ) );
99118

@@ -148,6 +167,7 @@ void QgsMapToolIdentifyAction::deactivate()
148167
{
149168
resultsDialog()->deactivate();
150169
QgsMapTool::deactivate();
170+
deleteRubberBands();
151171
}
152172

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

186+
void QgsMapToolIdentifyAction::handleMenuHover()
187+
{
188+
if ( !mCanvas )
189+
{
190+
return;
191+
}
192+
193+
deleteRubberBands();
194+
QAction* senderAction = qobject_cast<QAction*>( sender() );
195+
if ( senderAction )
196+
{
197+
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( senderAction->data().toString() ) );
198+
if ( vl )
199+
{
200+
QMap< QgsMapLayer*, QList<IdentifyResult> >::const_iterator lIt = mLayerIdResults.find( vl );
201+
if ( lIt != mLayerIdResults.constEnd() )
202+
{
203+
const QList<IdentifyResult>& idList = lIt.value();
204+
QList<IdentifyResult>::const_iterator idListIt = idList.constBegin();
205+
for ( ; idListIt != idList.constEnd(); ++idListIt )
206+
{
207+
QgsHighlight* hl = new QgsHighlight( mCanvas, idListIt->mFeature.geometry(), vl );
208+
hl->setColor( QColor( 255, 0, 0 ) );
209+
hl->setWidth( 2 );
210+
mRubberBands.append( hl );
211+
}
212+
}
213+
}
214+
}
215+
}
216+
217+
void QgsMapToolIdentifyAction::deleteRubberBands()
218+
{
219+
QList<QgsHighlight*>::const_iterator it = mRubberBands.constBegin();
220+
for ( ; it != mRubberBands.constEnd(); ++it )
221+
{
222+
delete *it;
223+
}
224+
mRubberBands.clear();
225+
}
226+
227+
void QgsMapToolIdentifyAction::fillLayerIdResults( int x, int y )
228+
{
229+
mLayerIdResults.clear();
230+
if ( !mCanvas )
231+
{
232+
return;
233+
}
234+
235+
QList<QgsMapLayer*> canvasLayers = mCanvas->layers();
236+
QList<QgsMapLayer*>::iterator it = canvasLayers.begin();
237+
for ( ; it != canvasLayers.end(); ++it )
238+
{
239+
QList<IdentifyResult> idResult = QgsMapToolIdentify::identify( x, y, QList<QgsMapLayer*>() << *it );
240+
if ( !idResult.isEmpty() )
241+
{
242+
mLayerIdResults.insert( *it, idResult );
243+
}
244+
}
245+
}
246+
247+
void QgsMapToolIdentifyAction::fillLayerSelectionMenu( QMenu& menu )
248+
{
249+
QMap< QgsMapLayer*, QList<IdentifyResult> >::const_iterator resultIt = mLayerIdResults.constBegin();
250+
for ( ; resultIt != mLayerIdResults.constEnd(); ++resultIt )
251+
{
252+
QAction* action = new QAction( resultIt.key()->name(), 0 );
253+
action->setData( resultIt.key()->id() );
254+
//add point/line/polygon icon
255+
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( resultIt.key() );
256+
if ( vl )
257+
{
258+
switch ( vl->geometryType() )
259+
{
260+
case QGis::Point:
261+
action->setIcon( QgsApplication::getThemeIcon( "/mIconPointLayer.png" ) );
262+
break;
263+
case QGis::Line:
264+
action->setIcon( QgsApplication::getThemeIcon( "/mIconLineLayer.png" ) );
265+
break;
266+
case QGis::Polygon:
267+
action->setIcon( QgsApplication::getThemeIcon( "/mIconPolygonLayer.png" ) );
268+
break;
269+
default:
270+
break;
271+
}
272+
}
273+
else if ( resultIt.key()->type() == QgsMapLayer::RasterLayer )
274+
{
275+
action->setIcon( QgsApplication::getThemeIcon( "/mIconRaster.png" ) );
276+
}
277+
QObject::connect( action, SIGNAL( hovered() ), this, SLOT( handleMenuHover() ) );
278+
menu.addAction( action );
279+
}
280+
}
281+
282+
void QgsMapToolIdentifyAction::execLayerSelectionMenu( QMenu& menu, const QPoint& pos, QList<IdentifyResult>& resultList )
283+
{
284+
QAction* selectedAction = menu.exec( QPoint( pos.x() + 5, pos.y() + 5 ) );
285+
if ( selectedAction )
286+
{
287+
QgsMapLayer* selectedLayer = QgsMapLayerRegistry::instance()->mapLayer( selectedAction->data().toString() );
288+
QMap< QgsMapLayer*, QList<IdentifyResult> >::const_iterator sIt = mLayerIdResults.find( selectedLayer );
289+
if ( sIt != mLayerIdResults.constEnd() )
290+
{
291+
resultList = sIt.value();
292+
}
293+
}
294+
}
295+

src/app/qgsmaptoolidentifyaction.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@
2727
#include <QObject>
2828
#include <QPointer>
2929

30+
class QgsHighlight;
3031
class QgsIdentifyResultsDialog;
3132
class QgsMapLayer;
3233
class QgsRasterLayer;
33-
class QgsRubberBand;
3434
class QgsVectorLayer;
35+
class QMenu;
3536

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

80+
//! layer id map for layer select mode
81+
QMap< QgsMapLayer*, QList<IdentifyResult> > mLayerIdResults;
82+
//! rubber bands for layer select mode
83+
QList<QgsHighlight*> mRubberBands;
84+
7985
QgsIdentifyResultsDialog *resultsDialog();
8086

8187
virtual QGis::UnitType displayUnits();
88+
89+
//helper functions for layer selection mode
90+
void deleteRubberBands();
91+
void fillLayerIdResults( int x, int y );
92+
void fillLayerSelectionMenu( QMenu& menu );
93+
void execLayerSelectionMenu( QMenu& menu, const QPoint& pos, QList<IdentifyResult>& resultList );
94+
95+
private slots:
96+
void handleMenuHover();
8297
};
8398

8499
#endif

src/app/qgsoptions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
109109
cmbIdentifyMode->addItem( tr( "Current layer" ), 0 );
110110
cmbIdentifyMode->addItem( tr( "Top down, stop at first" ), 1 );
111111
cmbIdentifyMode->addItem( tr( "Top down" ), 2 );
112+
cmbIdentifyMode->addItem( tr( "Layer selection" ), 3 );
112113

113114
// read the current browser and set it
114115
QSettings settings;

src/gui/qgsmaptoolidentify.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
5151
DefaultQgsSetting = -1,
5252
ActiveLayer,
5353
TopDownStopAtFirst,
54-
TopDownAll
54+
TopDownAll,
55+
LayerSelection
5556
};
5657

5758
enum LayerType

0 commit comments

Comments
 (0)