Skip to content

Commit e1f3079

Browse files
author
jef
committed
[FEATURE] identify tool highlighting changed
- identified features are highlighted by default [fixed #2169] - clicking on a feature or attribute highlights the feature - clicking on a layer highlights all identified features on that layer - context menu now also has "highlight all" and "highlight layer" git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12307 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 43d196e commit e1f3079

File tree

2 files changed

+93
-45
lines changed

2 files changed

+93
-45
lines changed

src/app/qgsidentifyresults.cpp

+87-41
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ class QgsIdentifyResultsDock : public QDockWidget
9292
QgsIdentifyResults::QgsIdentifyResults( QgsMapCanvas *canvas, QWidget *parent, Qt::WFlags f )
9393
: QDialog( parent, f ),
9494
mActionPopup( 0 ),
95-
mRubberBand( 0 ),
9695
mCanvas( canvas ),
9796
mDock( NULL )
9897
{
@@ -215,6 +214,8 @@ void QgsIdentifyResults::addFeature( QgsMapLayer *layer, int fid,
215214
}
216215

217216
layItem->addChild( featItem );
217+
218+
highlightFeature( featItem );
218219
}
219220

220221
void QgsIdentifyResults::editingToggled()
@@ -263,13 +264,11 @@ void QgsIdentifyResults::show()
263264
QTreeWidgetItem *layItem = lstResults->topLevelItem( 0 );
264265
QTreeWidgetItem *featItem = layItem->child( 0 );
265266

266-
if ( layItem->childCount() == 1 && QSettings().value( "/Map/identifyAutoFeatureForm", false ).toBool() )
267+
if ( lstResults->topLevelItemCount() == 1 && layItem->childCount() == 1 && QSettings().value( "/Map/identifyAutoFeatureForm", false ).toBool() )
267268
{
268269
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( layItem->data( 0, Qt::UserRole ).value<QObject *>() );
269270
if ( layer )
270271
{
271-
highlightFeature( featItem );
272-
273272
// if this is the only feature and it's on a vector layer
274273
// don't show the form dialog instead of the results window
275274
lstResults->setCurrentItem( featItem );
@@ -341,6 +340,8 @@ void QgsIdentifyResults::contextMenuEvent( QContextMenuEvent* event )
341340
mActionPopup->addAction( tr( "Copy attribute value" ), this, SLOT( copyAttributeValue() ) );
342341
mActionPopup->addAction( tr( "Copy feature attributes" ), this, SLOT( copyFeatureAttributes() ) );
343342
mActionPopup->addSeparator();
343+
mActionPopup->addAction( tr( "Highlight all" ), this, SLOT( highlightAll() ) );
344+
mActionPopup->addAction( tr( "Highlight layer" ), this, SLOT( highlightLayer() ) );
344345
mActionPopup->addAction( tr( "Expand all" ), this, SLOT( expandAll() ) );
345346
mActionPopup->addAction( tr( "Collapse all" ), this, SLOT( collapseAll() ) );
346347

@@ -405,14 +406,14 @@ void QgsIdentifyResults::clear()
405406
}
406407

407408
lstResults->clear();
408-
clearRubberBand();
409+
mRubberBands.clear();
409410
}
410411

411412
void QgsIdentifyResults::activate()
412413
{
413-
if ( mRubberBand )
414+
foreach( QSharedPointer<QgsRubberBand> rb, mRubberBands )
414415
{
415-
mRubberBand->show();
416+
rb->show();
416417
}
417418

418419
if ( lstResults->topLevelItemCount() > 0 )
@@ -424,23 +425,12 @@ void QgsIdentifyResults::activate()
424425

425426
void QgsIdentifyResults::deactivate()
426427
{
427-
if ( mRubberBand )
428+
foreach( QSharedPointer<QgsRubberBand> rb, mRubberBands )
428429
{
429-
mRubberBand->hide();
430+
rb->hide();
430431
}
431432
}
432433

433-
void QgsIdentifyResults::clearRubberBand()
434-
{
435-
if ( !mRubberBand )
436-
return;
437-
438-
delete mRubberBand;
439-
mRubberBand = 0;
440-
mRubberBandLayer = 0;
441-
mRubberBandFid = 0;
442-
}
443-
444434
void QgsIdentifyResults::doAction( QTreeWidgetItem *item, int action )
445435
{
446436
int idx;
@@ -507,13 +497,22 @@ QTreeWidgetItem *QgsIdentifyResults::featureItem( QTreeWidgetItem *item )
507497
return featItem;
508498
}
509499

510-
QgsVectorLayer *QgsIdentifyResults::vectorLayer( QTreeWidgetItem *item )
500+
QTreeWidgetItem *QgsIdentifyResults::layerItem( QTreeWidgetItem *item )
511501
{
512-
if ( item->parent() )
502+
if ( item && item->parent() )
513503
{
514504
item = featureItem( item )->parent();
515505
}
516506

507+
return item;
508+
}
509+
510+
511+
QgsVectorLayer *QgsIdentifyResults::vectorLayer( QTreeWidgetItem *item )
512+
{
513+
item = layerItem( item );
514+
if ( !item )
515+
return NULL;
517516
return qobject_cast<QgsVectorLayer *>( item->data( 0, Qt::UserRole ).value<QObject *>() );
518517
}
519518

@@ -543,24 +542,42 @@ void QgsIdentifyResults::itemExpanded( QTreeWidgetItem* item )
543542
expandColumnsToFit();
544543
}
545544

546-
void QgsIdentifyResults::handleCurrentItemChanged( QTreeWidgetItem* current, QTreeWidgetItem* previous )
545+
void QgsIdentifyResults::handleCurrentItemChanged( QTreeWidgetItem *current, QTreeWidgetItem *previous )
547546
{
548547
if ( current == NULL )
549548
{
550549
emit selectedFeatureChanged( 0, 0 );
551550
return;
552551
}
553552

554-
highlightFeature( current );
553+
QTreeWidgetItem *layItem = layerItem( current );
554+
555+
if ( current == layItem )
556+
{
557+
highlightLayer( layItem );
558+
}
559+
else
560+
{
561+
mRubberBands.clear();
562+
highlightFeature( current );
563+
}
555564
}
556565

557566
void QgsIdentifyResults::layerDestroyed()
558567
{
559568
QObject *theSender = sender();
560569

561-
if ( mRubberBandLayer == theSender )
570+
for ( int i = 0; i < lstResults->topLevelItemCount(); i++ )
562571
{
563-
clearRubberBand();
572+
QTreeWidgetItem *layItem = lstResults->topLevelItem( i );
573+
574+
if ( layItem->data( 0, Qt::UserRole ).value<QObject *>() == sender() )
575+
{
576+
for ( int j = 0; j < layItem->childCount(); j++ )
577+
{
578+
mRubberBands.remove( layItem->child( i ) );
579+
}
580+
}
564581
}
565582

566583
disconnectLayer( theSender );
@@ -604,17 +621,13 @@ void QgsIdentifyResults::featureDeleted( int fid )
604621

605622
if ( featItem && featItem->data( 0, Qt::UserRole ).toInt() == fid )
606623
{
607-
if ( mRubberBandLayer == sender() && mRubberBandFid == fid )
608-
clearRubberBand();
609-
delete featItem;
624+
mRubberBands.remove( featItem );
610625
break;
611626
}
612627
}
613628

614629
if ( layItem->childCount() == 0 )
615630
{
616-
if ( mRubberBandLayer == sender() )
617-
clearRubberBand();
618631
delete layItem;
619632
}
620633

@@ -634,9 +647,10 @@ void QgsIdentifyResults::highlightFeature( QTreeWidgetItem *item )
634647
if ( !featItem )
635648
return;
636649

637-
int fid = featItem->data( 0, Qt::UserRole ).toInt();
650+
if ( mRubberBands.contains( featItem ) )
651+
return;
638652

639-
clearRubberBand();
653+
int fid = featItem->data( 0, Qt::UserRole ).toInt();
640654

641655
QgsFeature feat;
642656
if ( ! layer->featureAtId( fid, feat, true, false ) )
@@ -649,15 +663,14 @@ void QgsIdentifyResults::highlightFeature( QTreeWidgetItem *item )
649663
return;
650664
}
651665

652-
mRubberBand = new QgsRubberBand( mCanvas, feat.geometry()->type() == QGis::Polygon );
653-
if ( mRubberBand )
666+
QgsRubberBand *rb = new QgsRubberBand( mCanvas, feat.geometry()->type() == QGis::Polygon );
667+
if ( rb )
654668
{
655-
mRubberBandLayer = layer;
656-
mRubberBandFid = fid;
657-
mRubberBand->setToGeometry( feat.geometry(), layer );
658-
mRubberBand->setWidth( 2 );
659-
mRubberBand->setColor( Qt::red );
660-
mRubberBand->show();
669+
rb->setToGeometry( feat.geometry(), layer );
670+
rb->setWidth( 2 );
671+
rb->setColor( Qt::red );
672+
rb->show();
673+
mRubberBands.insert( featItem, QSharedPointer<QgsRubberBand>( rb ) );
661674
}
662675
}
663676

@@ -768,6 +781,39 @@ void QgsIdentifyResults::featureForm()
768781
mCanvas->refresh();
769782
}
770783

784+
void QgsIdentifyResults::highlightAll()
785+
{
786+
for ( int i = 0; i < lstResults->topLevelItemCount(); i++ )
787+
{
788+
QTreeWidgetItem *layItem = lstResults->topLevelItem( i );
789+
790+
for ( int j = 0; j < layItem->childCount(); j++ )
791+
{
792+
highlightFeature( layItem->child( j ) );
793+
}
794+
}
795+
}
796+
797+
void QgsIdentifyResults::highlightLayer()
798+
{
799+
highlightLayer( lstResults->currentItem() );
800+
}
801+
802+
void QgsIdentifyResults::highlightLayer( QTreeWidgetItem *item )
803+
{
804+
QTreeWidgetItem *layItem = layerItem( item );
805+
if ( !layItem )
806+
return;
807+
808+
mRubberBands.clear();
809+
810+
for ( int i = 0; i < layItem->childCount(); i++ )
811+
{
812+
highlightFeature( layItem->child( i ) );
813+
}
814+
}
815+
816+
771817
void QgsIdentifyResults::expandAll()
772818
{
773819
lstResults->expandAll();

src/app/qgsidentifyresults.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <QWidget>
2727
#include <QList>
28+
#include <QSharedPointer>
2829

2930
class QCloseEvent;
3031
class QTreeWidgetItem;
@@ -88,6 +89,8 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
8889
void zoomToFeature();
8990
void copyAttributeValue();
9091
void copyFeatureAttributes();
92+
void highlightAll();
93+
void highlightLayer();
9194
void expandAll();
9295
void collapseAll();
9396

@@ -107,16 +110,15 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
107110

108111
private:
109112
QMenu *mActionPopup;
110-
QgsVectorLayer *mRubberBandLayer;
111-
int mRubberBandFid;
112-
QgsRubberBand *mRubberBand;
113+
QMap<QTreeWidgetItem *, QSharedPointer<QgsRubberBand> > mRubberBands;
113114
QgsMapCanvas *mCanvas;
114115

115116
QgsVectorLayer *vectorLayer( QTreeWidgetItem *item );
116117
QTreeWidgetItem *featureItem( QTreeWidgetItem *item );
118+
QTreeWidgetItem *layerItem( QTreeWidgetItem *item );
117119
QTreeWidgetItem *layerItem( QObject *layer );
118120

119-
void clearRubberBand();
121+
void highlightLayer( QTreeWidgetItem *object );
120122
void disconnectLayer( QObject *object );
121123

122124
void setColumnText( int column, const QString & label );

0 commit comments

Comments
 (0)