32
32
#include " qgsmaplayerregistry.h"
33
33
#include " qgisapp.h"
34
34
#include " qgsrendererv2.h"
35
+ #include " qgsrubberband.h"
35
36
36
37
#include < QSettings>
37
38
#include < QMessageBox>
@@ -57,6 +58,7 @@ QgsMapToolIdentifyAction::~QgsMapToolIdentifyAction()
57
58
{
58
59
mResultsDialog ->done ( 0 );
59
60
}
61
+ deleteRubberBands ();
60
62
}
61
63
62
64
QgsIdentifyResultsDialog *QgsMapToolIdentifyAction::resultsDialog ()
@@ -98,36 +100,12 @@ void QgsMapToolIdentifyAction::canvasReleaseEvent( QMouseEvent *e )
98
100
IdentifyMode mode = static_cast <IdentifyMode>( settings.value ( " /Map/identifyMode" , 0 ).toInt () );
99
101
if ( mode == LayerSelection )
100
102
{
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
115
104
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 ();
131
109
}
132
110
else
133
111
{
@@ -188,6 +166,7 @@ void QgsMapToolIdentifyAction::deactivate()
188
166
{
189
167
resultsDialog ()->deactivate ();
190
168
QgsMapTool::deactivate ();
169
+ deleteRubberBands ();
191
170
}
192
171
193
172
QGis::UnitType QgsMapToolIdentifyAction::displayUnits ()
@@ -203,3 +182,91 @@ void QgsMapToolIdentifyAction::handleCopyToClipboard( QgsFeatureStore & featureS
203
182
emit copyToClipboard ( featureStore );
204
183
}
205
184
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
+
0 commit comments