Skip to content

Commit 343ebe5

Browse files
committedMay 16, 2013
Implement rubberbands for layer selection mode
1 parent b093cfa commit 343ebe5

File tree

2 files changed

+111
-29
lines changed

2 files changed

+111
-29
lines changed
 

‎src/app/qgsmaptoolidentifyaction.cpp

+96-29
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "qgsmaplayerregistry.h"
3333
#include "qgisapp.h"
3434
#include "qgsrendererv2.h"
35+
#include "qgsrubberband.h"
3536

3637
#include <QSettings>
3738
#include <QMessageBox>
@@ -57,6 +58,7 @@ QgsMapToolIdentifyAction::~QgsMapToolIdentifyAction()
5758
{
5859
mResultsDialog->done( 0 );
5960
}
61+
deleteRubberBands();
6062
}
6163

6264
QgsIdentifyResultsDialog *QgsMapToolIdentifyAction::resultsDialog()
@@ -98,36 +100,12 @@ void QgsMapToolIdentifyAction::canvasReleaseEvent( QMouseEvent *e )
98100
IdentifyMode mode = static_cast<IdentifyMode>( settings.value( "/Map/identifyMode", 0 ).toInt() );
99101
if ( mode == LayerSelection )
100102
{
101-
QMap< QgsMapLayer*, QList<IdentifyResult> > layerIdResults;
102-
103-
QList<QgsMapLayer*> canvasLayers = mCanvas->layers();
104-
QList<QgsMapLayer*>::iterator it = canvasLayers.begin();
105-
for ( ; it != canvasLayers.end(); ++it )
106-
{
107-
QList<IdentifyResult> idResult = QgsMapToolIdentify::identify( e->x(), e->y(), QList<QgsMapLayer*>() << *it );
108-
if ( !idResult.isEmpty() )
109-
{
110-
layerIdResults.insert( *it, idResult );
111-
}
112-
}
113-
114-
//show QMenu with available layers
103+
fillLayerIdResults( e->x(), e->y() ); //get id results from all layers into mLayerIdResults
115104
QMenu layerSelectionMenu;
116-
QMap< QgsMapLayer*, QList<IdentifyResult> >::const_iterator resultIt = layerIdResults.constBegin();
117-
for ( ; resultIt != layerIdResults.constEnd(); ++resultIt )
118-
{
119-
QAction* action = new QAction( resultIt.key()->name(), 0 );
120-
action->setData( resultIt.key()->id() );
121-
//QObject::connect( action, SIGNAL( hovered() ), this, SLOT( handleMenuHover() ) );
122-
layerSelectionMenu.addAction( action );
123-
}
124-
125-
QAction* selectedAction = layerSelectionMenu.exec( e->globalPos() );
126-
if ( selectedAction )
127-
{
128-
QgsMapLayer* selectedLayer = QgsMapLayerRegistry::instance()->mapLayer( selectedAction->data().toString() );
129-
results = layerIdResults[ selectedLayer ];
130-
}
105+
fillLayerSelectionMenu( layerSelectionMenu ); //fill selection menu with entries from mLayerIdResults
106+
execLayerSelectionMenu( layerSelectionMenu, e->globalPos(), results );
107+
mLayerIdResults.clear();
108+
deleteRubberBands();
131109
}
132110
else
133111
{
@@ -188,6 +166,7 @@ void QgsMapToolIdentifyAction::deactivate()
188166
{
189167
resultsDialog()->deactivate();
190168
QgsMapTool::deactivate();
169+
deleteRubberBands();
191170
}
192171

193172
QGis::UnitType QgsMapToolIdentifyAction::displayUnits()
@@ -203,3 +182,91 @@ void QgsMapToolIdentifyAction::handleCopyToClipboard( QgsFeatureStore & featureS
203182
emit copyToClipboard( featureStore );
204183
}
205184

185+
void QgsMapToolIdentifyAction::handleMenuHover()
186+
{
187+
if ( !mCanvas )
188+
{
189+
return;
190+
}
191+
192+
deleteRubberBands();
193+
QAction* senderAction = qobject_cast<QAction*>( sender() );
194+
if ( senderAction )
195+
{
196+
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( senderAction->data().toString() ) );
197+
if ( vl )
198+
{
199+
QMap< QgsMapLayer*, QList<IdentifyResult> >::const_iterator lIt = mLayerIdResults.find( vl );
200+
if ( lIt != mLayerIdResults.constEnd() )
201+
{
202+
const QList<IdentifyResult>& idList = lIt.value();
203+
QList<IdentifyResult>::const_iterator idListIt = idList.constBegin();
204+
for ( ; idListIt != idList.constEnd(); ++idListIt )
205+
{
206+
QgsRubberBand* rb = new QgsRubberBand( mCanvas );
207+
rb->setColor( QColor( 255, 0, 0 ) );
208+
rb->setWidth( 2 );
209+
rb->setToGeometry( idListIt->mFeature.geometry(), vl );
210+
mRubberBands.append( rb );
211+
}
212+
}
213+
}
214+
}
215+
}
216+
217+
void QgsMapToolIdentifyAction::deleteRubberBands()
218+
{
219+
QList<QgsRubberBand*>::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+
QObject::connect( action, SIGNAL( hovered() ), this, SLOT( handleMenuHover() ) );
255+
menu.addAction( action );
256+
}
257+
}
258+
259+
void QgsMapToolIdentifyAction::execLayerSelectionMenu( QMenu& menu, const QPoint& pos, QList<IdentifyResult>& resultList )
260+
{
261+
QAction* selectedAction = menu.exec( pos );
262+
if ( selectedAction )
263+
{
264+
QgsMapLayer* selectedLayer = QgsMapLayerRegistry::instance()->mapLayer( selectedAction->data().toString() );
265+
QMap< QgsMapLayer*, QList<IdentifyResult> >::const_iterator sIt = mLayerIdResults.find( selectedLayer );
266+
if ( sIt != mLayerIdResults.constEnd() )
267+
{
268+
resultList = sIt.value();
269+
}
270+
}
271+
}
272+

‎src/app/qgsmaptoolidentifyaction.h

+15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class QgsMapLayer;
3232
class QgsRasterLayer;
3333
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 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<QgsRubberBand*> 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

0 commit comments

Comments
 (0)
Please sign in to comment.