[QGIS Commit] r11577 - trunk/qgis/src/app

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sun Sep 6 10:03:41 EDT 2009


Author: jef
Date: 2009-09-06 10:03:39 -0400 (Sun, 06 Sep 2009)
New Revision: 11577

Modified:
   trunk/qgis/src/app/qgsidentifyresults.cpp
   trunk/qgis/src/app/qgsidentifyresults.h
   trunk/qgis/src/app/qgsmaptoolidentify.cpp
   trunk/qgis/src/app/qgsmaptoolidentify.h
Log:
identify tool improvements

Modified: trunk/qgis/src/app/qgsidentifyresults.cpp
===================================================================
--- trunk/qgis/src/app/qgsidentifyresults.cpp	2009-09-05 19:57:18 UTC (rev 11576)
+++ trunk/qgis/src/app/qgsidentifyresults.cpp	2009-09-06 14:03:39 UTC (rev 11577)
@@ -107,6 +107,10 @@
     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 );
@@ -183,9 +187,13 @@
     {
       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 ) );
@@ -252,30 +260,37 @@
 
   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 );
+
+        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 );
     }
-
-    QgsDebugMsg( QString( "set clipboard: %1" ).arg( text ) );
-    clipboard->setText( text );
   }
   else
   {
@@ -292,14 +307,42 @@
 void QgsIdentifyResults::clear()
 {
   lstResults->clear();
+  clearRubberBand();
+}
 
+void QgsIdentifyResults::activate()
+{
   if ( mRubberBand )
   {
-    delete mRubberBand;
-    mRubberBand = 0;
+    mRubberBand->show();
   }
+
+  if ( lstResults->topLevelItemCount() > 0 )
+  {
+    show();
+    raise();
+  }
 }
 
+void QgsIdentifyResults::deactivate()
+{
+  if ( mRubberBand )
+  {
+    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;
@@ -394,9 +437,52 @@
 
 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 )
 {
   QgsVectorLayer *layer = vectorLayer( item );
@@ -409,8 +495,7 @@
 
   int fid = featItem->data( 0, Qt::UserRole ).toInt();
 
-  delete mRubberBand;
-  mRubberBand = 0;
+  clearRubberBand();
 
   QgsFeature feat;
   if ( ! layer->featureAtId( fid, feat, true, false ) )
@@ -424,9 +509,10 @@
   }
 
   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 );
@@ -434,6 +520,43 @@
   }
 }
 
+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 );

Modified: trunk/qgis/src/app/qgsidentifyresults.h
===================================================================
--- trunk/qgis/src/app/qgsidentifyresults.h	2009-09-05 19:57:18 UTC (rev 11576)
+++ trunk/qgis/src/app/qgsidentifyresults.h	2009-09-06 14:03:39 UTC (rev 11577)
@@ -50,20 +50,23 @@
 
     ~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 );
 
+    /** Remove results */
+    void clear();
+
+    /** map tool was deactivated */
+    void deactivate();
+
+    /** map tool was activated */
+    void activate();
+
     void closeEvent( QCloseEvent *e );
 
-    /** Set "No features ... " */
-    void setMessage( QString shortMsg, QString longMsg );
-
   signals:
     void selectedFeatureChanged( QgsVectorLayer *, int featureId );
 
@@ -77,6 +80,8 @@
 
     void layerDestroyed();
 
+    void featureDeleted( int fid );
+
     //! Context help
     void on_buttonHelp_clicked();
 
@@ -89,6 +94,8 @@
 
   private:
     QMenu *mActionPopup;
+    QgsVectorLayer *mRubberBandLayer;
+    int mRubberBandFid;
     QgsRubberBand *mRubberBand;
     QgsMapCanvas *mCanvas;
 
@@ -98,6 +105,7 @@
     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();
@@ -105,6 +113,7 @@
     void restorePosition();
 
     void highlightFeature( QTreeWidgetItem *item );
+    void zoomToFeature( QTreeWidgetItem *item );
     void editFeature( QTreeWidgetItem *item );
 
     void doAction( QTreeWidgetItem *item );

Modified: trunk/qgis/src/app/qgsmaptoolidentify.cpp
===================================================================
--- trunk/qgis/src/app/qgsmaptoolidentify.cpp	2009-09-05 19:57:18 UTC (rev 11576)
+++ trunk/qgis/src/app/qgsmaptoolidentify.cpp	2009-09-06 14:03:39 UTC (rev 11577)
@@ -124,6 +124,18 @@
   }
 }
 
+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;

Modified: trunk/qgis/src/app/qgsmaptoolidentify.h
===================================================================
--- trunk/qgis/src/app/qgsmaptoolidentify.h	2009-09-05 19:57:18 UTC (rev 11576)
+++ trunk/qgis/src/app/qgsmaptoolidentify.h	2009-09-06 14:03:39 UTC (rev 11577)
@@ -57,6 +57,10 @@
     //! 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 );
@@ -73,8 +77,6 @@
                      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();



More information about the QGIS-commit mailing list