Skip to content

Commit

Permalink
[FEATURE] identify tool highlighting changed
Browse files Browse the repository at this point in the history
- 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@12307 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Dec 1, 2009
1 parent b8d7b48 commit f2c0df2
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 45 deletions.
128 changes: 87 additions & 41 deletions src/app/qgsidentifyresults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class QgsIdentifyResultsDock : public QDockWidget
QgsIdentifyResults::QgsIdentifyResults( QgsMapCanvas *canvas, QWidget *parent, Qt::WFlags f )
: QDialog( parent, f ),
mActionPopup( 0 ),
mRubberBand( 0 ),
mCanvas( canvas ),
mDock( NULL )
{
Expand Down Expand Up @@ -215,6 +214,8 @@ void QgsIdentifyResults::addFeature( QgsMapLayer *layer, int fid,
}

layItem->addChild( featItem );

highlightFeature( featItem );
}

void QgsIdentifyResults::editingToggled()
Expand Down Expand Up @@ -263,13 +264,11 @@ void QgsIdentifyResults::show()
QTreeWidgetItem *layItem = lstResults->topLevelItem( 0 );
QTreeWidgetItem *featItem = layItem->child( 0 );

if ( layItem->childCount() == 1 && QSettings().value( "/Map/identifyAutoFeatureForm", false ).toBool() )
if ( lstResults->topLevelItemCount() == 1 && layItem->childCount() == 1 && QSettings().value( "/Map/identifyAutoFeatureForm", false ).toBool() )
{
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( layItem->data( 0, Qt::UserRole ).value<QObject *>() );
if ( layer )
{
highlightFeature( featItem );

// if this is the only feature and it's on a vector layer
// don't show the form dialog instead of the results window
lstResults->setCurrentItem( featItem );
Expand Down Expand Up @@ -341,6 +340,8 @@ void QgsIdentifyResults::contextMenuEvent( QContextMenuEvent* event )
mActionPopup->addAction( tr( "Copy attribute value" ), this, SLOT( copyAttributeValue() ) );
mActionPopup->addAction( tr( "Copy feature attributes" ), this, SLOT( copyFeatureAttributes() ) );
mActionPopup->addSeparator();
mActionPopup->addAction( tr( "Highlight all" ), this, SLOT( highlightAll() ) );
mActionPopup->addAction( tr( "Highlight layer" ), this, SLOT( highlightLayer() ) );
mActionPopup->addAction( tr( "Expand all" ), this, SLOT( expandAll() ) );
mActionPopup->addAction( tr( "Collapse all" ), this, SLOT( collapseAll() ) );

Expand Down Expand Up @@ -405,14 +406,14 @@ void QgsIdentifyResults::clear()
}

lstResults->clear();
clearRubberBand();
mRubberBands.clear();
}

void QgsIdentifyResults::activate()
{
if ( mRubberBand )
foreach( QSharedPointer<QgsRubberBand> rb, mRubberBands )
{
mRubberBand->show();
rb->show();
}

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

void QgsIdentifyResults::deactivate()
{
if ( mRubberBand )
foreach( QSharedPointer<QgsRubberBand> rb, mRubberBands )
{
mRubberBand->hide();
rb->hide();
}
}

void QgsIdentifyResults::clearRubberBand()
{
if ( !mRubberBand )
return;

delete mRubberBand;
mRubberBand = 0;
mRubberBandLayer = 0;
mRubberBandFid = 0;
}

void QgsIdentifyResults::doAction( QTreeWidgetItem *item, int action )
{
int idx;
Expand Down Expand Up @@ -507,13 +497,22 @@ QTreeWidgetItem *QgsIdentifyResults::featureItem( QTreeWidgetItem *item )
return featItem;
}

QgsVectorLayer *QgsIdentifyResults::vectorLayer( QTreeWidgetItem *item )
QTreeWidgetItem *QgsIdentifyResults::layerItem( QTreeWidgetItem *item )
{
if ( item->parent() )
if ( item && item->parent() )
{
item = featureItem( item )->parent();
}

return item;
}


QgsVectorLayer *QgsIdentifyResults::vectorLayer( QTreeWidgetItem *item )
{
item = layerItem( item );
if ( !item )
return NULL;
return qobject_cast<QgsVectorLayer *>( item->data( 0, Qt::UserRole ).value<QObject *>() );
}

Expand Down Expand Up @@ -543,24 +542,42 @@ void QgsIdentifyResults::itemExpanded( QTreeWidgetItem* item )
expandColumnsToFit();
}

void QgsIdentifyResults::handleCurrentItemChanged( QTreeWidgetItem* current, QTreeWidgetItem* previous )
void QgsIdentifyResults::handleCurrentItemChanged( QTreeWidgetItem *current, QTreeWidgetItem *previous )
{
if ( current == NULL )
{
emit selectedFeatureChanged( 0, 0 );
return;
}

highlightFeature( current );
QTreeWidgetItem *layItem = layerItem( current );

if ( current == layItem )
{
highlightLayer( layItem );
}
else
{
mRubberBands.clear();
highlightFeature( current );
}
}

void QgsIdentifyResults::layerDestroyed()
{
QObject *theSender = sender();

if ( mRubberBandLayer == theSender )
for ( int i = 0; i < lstResults->topLevelItemCount(); i++ )
{
clearRubberBand();
QTreeWidgetItem *layItem = lstResults->topLevelItem( i );

if ( layItem->data( 0, Qt::UserRole ).value<QObject *>() == sender() )
{
for ( int j = 0; j < layItem->childCount(); j++ )
{
mRubberBands.remove( layItem->child( i ) );
}
}
}

disconnectLayer( theSender );
Expand Down Expand Up @@ -604,17 +621,13 @@ void QgsIdentifyResults::featureDeleted( int fid )

if ( featItem && featItem->data( 0, Qt::UserRole ).toInt() == fid )
{
if ( mRubberBandLayer == sender() && mRubberBandFid == fid )
clearRubberBand();
delete featItem;
mRubberBands.remove( featItem );
break;
}
}

if ( layItem->childCount() == 0 )
{
if ( mRubberBandLayer == sender() )
clearRubberBand();
delete layItem;
}

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

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

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

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

mRubberBand = new QgsRubberBand( mCanvas, feat.geometry()->type() == QGis::Polygon );
if ( mRubberBand )
QgsRubberBand *rb = new QgsRubberBand( mCanvas, feat.geometry()->type() == QGis::Polygon );
if ( rb )
{
mRubberBandLayer = layer;
mRubberBandFid = fid;
mRubberBand->setToGeometry( feat.geometry(), layer );
mRubberBand->setWidth( 2 );
mRubberBand->setColor( Qt::red );
mRubberBand->show();
rb->setToGeometry( feat.geometry(), layer );
rb->setWidth( 2 );
rb->setColor( Qt::red );
rb->show();
mRubberBands.insert( featItem, QSharedPointer<QgsRubberBand>( rb ) );
}
}

Expand Down Expand Up @@ -768,6 +781,39 @@ void QgsIdentifyResults::featureForm()
mCanvas->refresh();
}

void QgsIdentifyResults::highlightAll()
{
for ( int i = 0; i < lstResults->topLevelItemCount(); i++ )
{
QTreeWidgetItem *layItem = lstResults->topLevelItem( i );

for ( int j = 0; j < layItem->childCount(); j++ )
{
highlightFeature( layItem->child( j ) );
}
}
}

void QgsIdentifyResults::highlightLayer()
{
highlightLayer( lstResults->currentItem() );
}

void QgsIdentifyResults::highlightLayer( QTreeWidgetItem *item )
{
QTreeWidgetItem *layItem = layerItem( item );
if ( !layItem )
return;

mRubberBands.clear();

for ( int i = 0; i < layItem->childCount(); i++ )
{
highlightFeature( layItem->child( i ) );
}
}


void QgsIdentifyResults::expandAll()
{
lstResults->expandAll();
Expand Down
10 changes: 6 additions & 4 deletions src/app/qgsidentifyresults.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <QWidget>
#include <QList>
#include <QSharedPointer>

class QCloseEvent;
class QTreeWidgetItem;
Expand Down Expand Up @@ -88,6 +89,8 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
void zoomToFeature();
void copyAttributeValue();
void copyFeatureAttributes();
void highlightAll();
void highlightLayer();
void expandAll();
void collapseAll();

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

private:
QMenu *mActionPopup;
QgsVectorLayer *mRubberBandLayer;
int mRubberBandFid;
QgsRubberBand *mRubberBand;
QMap<QTreeWidgetItem *, QSharedPointer<QgsRubberBand> > mRubberBands;
QgsMapCanvas *mCanvas;

QgsVectorLayer *vectorLayer( QTreeWidgetItem *item );
QTreeWidgetItem *featureItem( QTreeWidgetItem *item );
QTreeWidgetItem *layerItem( QTreeWidgetItem *item );
QTreeWidgetItem *layerItem( QObject *layer );

void clearRubberBand();
void highlightLayer( QTreeWidgetItem *object );
void disconnectLayer( QObject *object );

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

0 comments on commit f2c0df2

Please sign in to comment.