Skip to content

Commit

Permalink
identify tool improvements
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@11577 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Sep 6, 2009
1 parent 0f11b58 commit 56e45d6
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 27 deletions.
161 changes: 142 additions & 19 deletions src/app/qgsidentifyresults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ void QgsIdentifyResults::addFeature( QgsMapLayer *layer, int fid,
lstResults->addTopLevelItem( item );

connect( layer, SIGNAL( destroyed() ), this, SLOT( layerDestroyed() ) );

QgsVectorLayer *vlayer = dynamic_cast<QgsVectorLayer*>( layer );
if ( vlayer )
connect( vlayer, SIGNAL( featureDeleted( int ) ), this, SLOT( featureDeleted( int ) ) );
}

QTreeWidgetItem *featItem = new QTreeWidgetItem( QStringList() << displayField << displayValue );
Expand Down Expand Up @@ -183,9 +187,13 @@ void QgsIdentifyResults::contextMenuEvent( QContextMenuEvent* event )
{
a = mActionPopup->addAction( tr( "Edit feature" ) );
a->setEnabled( true );
a->setData( QVariant::fromValue( -3 ) );
a->setData( QVariant::fromValue( -4 ) );
}

a = mActionPopup->addAction( tr( "Zoom to feature" ) );
a->setEnabled( true );
a->setData( QVariant::fromValue( -3 ) );

a = mActionPopup->addAction( tr( "Copy attribute value" ) );
a->setEnabled( true );
a->setData( QVariant::fromValue( -2 ) );
Expand Down Expand Up @@ -252,30 +260,37 @@ void QgsIdentifyResults::popupItemSelected( QAction* menuAction )

if ( id < 0 )
{
QClipboard *clipboard = QApplication::clipboard();
QString text;

if ( id == -3 )
if ( id == -4 )
{
editFeature( item );
}
else if ( id == -2 )
else if ( id == -3 )
{
text = item->data( 1, Qt::DisplayRole ).toString();
zoomToFeature( item );
}
else
{
std::vector< std::pair<QString, QString> > attributes;
retrieveAttributes( item, attributes );
QClipboard *clipboard = QApplication::clipboard();
QString text;

for ( std::vector< std::pair<QString, QString> >::iterator it = attributes.begin(); it != attributes.end(); it++ )
if ( id == -2 )
{
text += QString( "%1: %2\n" ).arg( it->first ).arg( it->second );
text = item->data( 1, Qt::DisplayRole ).toString();
}
}
else
{
std::vector< std::pair<QString, QString> > attributes;
retrieveAttributes( item, attributes );

QgsDebugMsg( QString( "set clipboard: %1" ).arg( text ) );
clipboard->setText( text );
for ( std::vector< std::pair<QString, QString> >::iterator it = attributes.begin(); it != attributes.end(); it++ )
{
text += QString( "%1: %2\n" ).arg( it->first ).arg( it->second );
}
}

QgsDebugMsg( QString( "set clipboard: %1" ).arg( text ) );
clipboard->setText( text );
}
}
else
{
Expand All @@ -292,14 +307,42 @@ void QgsIdentifyResults::expandColumnsToFit()
void QgsIdentifyResults::clear()
{
lstResults->clear();
clearRubberBand();
}

void QgsIdentifyResults::activate()
{
if ( mRubberBand )
{
mRubberBand->show();
}

if ( lstResults->topLevelItemCount() > 0 )
{
show();
raise();
}
}

void QgsIdentifyResults::deactivate()
{
if ( mRubberBand )
{
delete mRubberBand;
mRubberBand = 0;
mRubberBand->hide();
}
}

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

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

void QgsIdentifyResults::doAction( QTreeWidgetItem *item )
{
std::vector< std::pair<QString, QString> > attributes;
Expand Down Expand Up @@ -394,7 +437,50 @@ void QgsIdentifyResults::handleCurrentItemChanged( QTreeWidgetItem* current, QTr

void QgsIdentifyResults::layerDestroyed()
{
if ( mRubberBandLayer == sender() )
{
clearRubberBand();
}

delete layerItem( sender() );

if ( lstResults->topLevelItemCount() == 0 )
{
hide();
}
}

void QgsIdentifyResults::featureDeleted( int fid )
{
QTreeWidgetItem *layItem = layerItem( sender() );

if ( !layItem )
return;

for ( int i = 0; i < layItem->childCount(); i++ )
{
QTreeWidgetItem *featItem = layItem->child( i );

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

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

if ( lstResults->topLevelItemCount() == 0 )
{
hide();
}
}

void QgsIdentifyResults::highlightFeature( QTreeWidgetItem *item )
Expand All @@ -409,8 +495,7 @@ void QgsIdentifyResults::highlightFeature( QTreeWidgetItem *item )

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

delete mRubberBand;
mRubberBand = 0;
clearRubberBand();

QgsFeature feat;
if ( ! layer->featureAtId( fid, feat, true, false ) )
Expand All @@ -424,16 +509,54 @@ void QgsIdentifyResults::highlightFeature( QTreeWidgetItem *item )
}

mRubberBand = new QgsRubberBand( mCanvas, feat.geometry()->type() == QGis::Polygon );

if ( mRubberBand )
{
mRubberBandLayer = layer;
mRubberBandFid = fid;
mRubberBand->setToGeometry( feat.geometry(), layer );
mRubberBand->setWidth( 2 );
mRubberBand->setColor( Qt::red );
mRubberBand->show();
}
}

void QgsIdentifyResults::zoomToFeature( QTreeWidgetItem *item )
{
QgsVectorLayer *layer = vectorLayer( item );
if ( !layer )
return;

QTreeWidgetItem *featItem = featureItem( item );
if ( !featItem )
return;

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

QgsFeature feat;
if ( ! layer->featureAtId( fid, feat, true, false ) )
{
return;
}

if ( !feat.geometry() )
{
return;
}

QgsRectangle rect = mCanvas->mapRenderer()->layerExtentToOutputExtent( layer, feat.geometry()->boundingBox() );

if ( rect.isEmpty() )
{
QgsPoint c = rect.center();
rect = mCanvas->extent();
rect.expand( 0.25, &c );
}

mCanvas->setExtent( rect );
mCanvas->refresh();
}


void QgsIdentifyResults::editFeature( QTreeWidgetItem *item )
{
QgsVectorLayer *layer = vectorLayer( item );
Expand Down
21 changes: 15 additions & 6 deletions src/app/qgsidentifyresults.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,22 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase

~QgsIdentifyResults();

/** Remove results */
void clear();

/** Add add feature */
void addFeature( QgsMapLayer *layer, int fid,
QString displayField, QString displayValue,
const QMap< QString, QString > &attributes,
const QMap< QString, QString > &derivedAttributes );

void closeEvent( QCloseEvent *e );
/** Remove results */
void clear();

/** map tool was deactivated */
void deactivate();

/** Set "No features ... " */
void setMessage( QString shortMsg, QString longMsg );
/** map tool was activated */
void activate();

void closeEvent( QCloseEvent *e );

signals:
void selectedFeatureChanged( QgsVectorLayer *, int featureId );
Expand All @@ -77,6 +80,8 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase

void layerDestroyed();

void featureDeleted( int fid );

//! Context help
void on_buttonHelp_clicked();

Expand All @@ -89,6 +94,8 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase

private:
QMenu *mActionPopup;
QgsVectorLayer *mRubberBandLayer;
int mRubberBandFid;
QgsRubberBand *mRubberBand;
QgsMapCanvas *mCanvas;

Expand All @@ -98,13 +105,15 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
QTreeWidgetItem *featureItem( QTreeWidgetItem *item );
QTreeWidgetItem *layerItem( QObject *layer );
QTreeWidgetItem *retrieveAttributes( QTreeWidgetItem *item, std::vector< std::pair<QString, QString> > &attributes );
void clearRubberBand();

void setColumnText( int column, const QString & label );
void expandColumnsToFit();
void saveWindowLocation();
void restorePosition();

void highlightFeature( QTreeWidgetItem *item );
void zoomToFeature( QTreeWidgetItem *item );
void editFeature( QTreeWidgetItem *item );

void doAction( QTreeWidgetItem *item );
Expand Down
12 changes: 12 additions & 0 deletions src/app/qgsmaptoolidentify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ void QgsMapToolIdentify::canvasReleaseEvent( QMouseEvent * e )
}
}

void QgsMapToolIdentify::activate()
{
mResults->activate();
QgsMapTool::activate();
}

void QgsMapToolIdentify::deactivate()
{
mResults->deactivate();
QgsMapTool::deactivate();
}

bool QgsMapToolIdentify::identifyLayer( QgsMapLayer *layer, int x, int y )
{
bool res = false;
Expand Down
6 changes: 4 additions & 2 deletions src/app/qgsmaptoolidentify.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class QgsMapToolIdentify : public QgsMapTool
//! Overridden mouse release event
virtual void canvasReleaseEvent( QMouseEvent * e );

virtual void activate();

virtual void deactivate();

private:
bool identifyLayer( QgsMapLayer *layer, int x, int y );
bool identifyRasterLayer( QgsRasterLayer *layer, int x, int y );
Expand All @@ -73,8 +77,6 @@ class QgsMapToolIdentify : public QgsMapTool
const QMap< QString, QString > &attributes,
const QMap< QString, QString > &derivedAttributes );

/** Add an action to the feature display node */

private slots:
// Let us know when the QgsIdentifyResults dialog box has been closed
void resultsDialogGone();
Expand Down

0 comments on commit 56e45d6

Please sign in to comment.