[QGIS Commit] r15315 - in trunk/qgis/src: app core gui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Mar 2 21:04:42 EST 2011


Author: jef
Date: 2011-03-02 18:04:42 -0800 (Wed, 02 Mar 2011)
New Revision: 15315

Added:
   trunk/qgis/src/app/qgshighlight.cpp
   trunk/qgis/src/app/qgshighlight.h
Modified:
   trunk/qgis/src/app/CMakeLists.txt
   trunk/qgis/src/app/qgsattributedialog.cpp
   trunk/qgis/src/app/qgsattributedialog.h
   trunk/qgis/src/app/qgsfeatureaction.cpp
   trunk/qgis/src/app/qgsfeatureaction.h
   trunk/qgis/src/app/qgsidentifyresults.cpp
   trunk/qgis/src/app/qgsidentifyresults.h
   trunk/qgis/src/core/qgspoint.h
   trunk/qgis/src/gui/qgsmapcanvas.h
Log:
fix #926

Modified: trunk/qgis/src/app/CMakeLists.txt
===================================================================
--- trunk/qgis/src/app/CMakeLists.txt	2011-03-03 02:04:22 UTC (rev 15314)
+++ trunk/qgis/src/app/CMakeLists.txt	2011-03-03 02:04:42 UTC (rev 15315)
@@ -91,6 +91,7 @@
   qgsuniquevaluedialog.cpp
   qgsvectorlayerproperties.cpp
   qgsquerybuilder.cpp
+  qgshighlight.cpp
 
   qgsmanageconnectionsdialog.cpp
 

Modified: trunk/qgis/src/app/qgsattributedialog.cpp
===================================================================
--- trunk/qgis/src/app/qgsattributedialog.cpp	2011-03-03 02:04:22 UTC (rev 15314)
+++ trunk/qgis/src/app/qgsattributedialog.cpp	2011-03-03 02:04:42 UTC (rev 15315)
@@ -24,7 +24,7 @@
 #include "qgsuniquevaluerenderer.h"
 #include "qgssymbol.h"
 #include "qgsattributeeditor.h"
-#include "qgsrubberband.h"
+#include "qgshighlight.h"
 #include "qgssearchstring.h"
 #include "qgssearchtreenode.h"
 
@@ -52,7 +52,7 @@
     , mLayer( vl )
     , mFeature( thepFeature )
     , mFeatureOwner( featureOwner )
-    , mRubberBand( 0 )
+    , mHighlight( 0 )
     , mFormNr( -1 )
 {
   if ( !mFeature || !vl->dataProvider() )
@@ -299,10 +299,10 @@
 
 QgsAttributeDialog::~QgsAttributeDialog()
 {
-  if ( mRubberBand )
+  if ( mHighlight )
   {
-    mRubberBand->hide();
-    delete mRubberBand;
+    mHighlight->hide();
+    delete mHighlight;
   }
 
   if ( mFeatureOwner )
@@ -381,14 +381,14 @@
   }
 }
 
-void QgsAttributeDialog::setHighlight( QgsRubberBand *rb )
+void QgsAttributeDialog::setHighlight( QgsHighlight *h )
 {
-  if ( mRubberBand )
+  if ( mHighlight )
   {
-    delete mRubberBand;
+    delete mHighlight;
   }
 
-  mRubberBand = rb;
+  mHighlight = h;
 }
 
 
@@ -410,15 +410,15 @@
 
 bool QgsAttributeDialog::eventFilter( QObject *obj, QEvent *e )
 {
-  if ( mRubberBand && obj == mDialog )
+  if ( mHighlight && obj == mDialog )
   {
     switch ( e->type() )
     {
       case QEvent::WindowActivate:
-        mRubberBand->show();
+        mHighlight->show();
         break;
       case QEvent::WindowDeactivate:
-        mRubberBand->hide();
+        mHighlight->hide();
         break;
       default:
         break;

Modified: trunk/qgis/src/app/qgsattributedialog.h
===================================================================
--- trunk/qgis/src/app/qgsattributedialog.h	2011-03-03 02:04:22 UTC (rev 15314)
+++ trunk/qgis/src/app/qgsattributedialog.h	2011-03-03 02:04:42 UTC (rev 15315)
@@ -25,7 +25,7 @@
 class QLayout;
 class QgsField;
 class QgsVectorLayer;
-class QgsRubberBand;
+class QgsHighlight;
 
 class QgsAttributeDialog : public QObject
 {
@@ -46,7 +46,7 @@
      */
     void restoreGeometry();
 
-    void setHighlight( QgsRubberBand *rb );
+    void setHighlight( QgsHighlight *h );
 
     QDialog *dialog() { return mDialog; }
 
@@ -68,7 +68,7 @@
     QgsVectorLayer *mLayer;
     QgsFeature *mFeature;
     bool mFeatureOwner;
-    QgsRubberBand *mRubberBand;
+    QgsHighlight *mHighlight;
     int mFormNr;
     static int smFormCounter;
 };

Modified: trunk/qgis/src/app/qgsfeatureaction.cpp
===================================================================
--- trunk/qgis/src/app/qgsfeatureaction.cpp	2011-03-03 02:04:22 UTC (rev 15314)
+++ trunk/qgis/src/app/qgsfeatureaction.cpp	2011-03-03 02:04:42 UTC (rev 15315)
@@ -72,13 +72,13 @@
   return dialog;
 }
 
-bool QgsFeatureAction::viewFeatureForm( QgsRubberBand *rb )
+bool QgsFeatureAction::viewFeatureForm( QgsHighlight *h )
 {
   if ( !mLayer )
     return false;
 
   QgsAttributeDialog *dialog = newDialog( true );
-  dialog->setHighlight( rb );
+  dialog->setHighlight( h );
   dialog->show();
 
   return true;

Modified: trunk/qgis/src/app/qgsfeatureaction.h
===================================================================
--- trunk/qgis/src/app/qgsfeatureaction.h	2011-03-03 02:04:22 UTC (rev 15314)
+++ trunk/qgis/src/app/qgsfeatureaction.h	2011-03-03 02:04:42 UTC (rev 15315)
@@ -26,7 +26,7 @@
 
 class QgsIdentifyResults;
 class QgsVectorLayer;
-class QgsRubberBand;
+class QgsHighlight;
 class QgsAttributeDialog;
 
 class QgsFeatureAction : public QAction
@@ -38,7 +38,7 @@
 
   public slots:
     void execute();
-    bool viewFeatureForm( QgsRubberBand *rb = 0 );
+    bool viewFeatureForm( QgsHighlight *h = 0 );
     bool editFeature();
     bool addFeature();
 

Added: trunk/qgis/src/app/qgshighlight.cpp
===================================================================
--- trunk/qgis/src/app/qgshighlight.cpp	                        (rev 0)
+++ trunk/qgis/src/app/qgshighlight.cpp	2011-03-03 02:04:42 UTC (rev 15315)
@@ -0,0 +1,244 @@
+/***************************************************************************
+    qgshighlight.cpp - widget to highlight features on the map
+     --------------------------------------
+    Date                 : 02-03-2011
+    Copyright            : (C) 2011 by Juergen E. Fischer, norBIT GmbH
+    Email                : jef at norbit dot de
+ ***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+/* $Id$ */
+
+#include "qgshighlight.h"
+#include "qgsgeometry.h"
+#include "qgslogger.h"
+#include "qgsmapcanvas.h"
+#include "qgsmaprenderer.h"
+#include "qgsvectorlayer.h"
+#include <QPainter>
+
+/*!
+  \class QgsHighlight
+  \brief The QgsHighlight class provides a transparent overlay widget
+  for highlightng features on the map.
+*/
+QgsHighlight::QgsHighlight( QgsMapCanvas* mapCanvas, QgsGeometry *geom, QgsVectorLayer *layer )
+    : QgsMapCanvasItem( mapCanvas )
+    , mLayer( layer )
+{
+  QgsDebugMsg( geom ? "geometry!" : "no geometry" );
+  mGeometry = geom ? new QgsGeometry( *geom ) : 0;
+  updateRect();
+  update();
+  setColor( QColor( Qt::lightGray ) );
+}
+
+QgsHighlight::~QgsHighlight()
+{
+  delete mGeometry;
+}
+
+/*!
+  Set the outline and fill color.
+  */
+void QgsHighlight::setColor( const QColor & color )
+{
+  mPen.setColor( color );
+  QColor fillColor( color.red(), color.green(), color.blue(), 63 );
+  mBrush.setColor( fillColor );
+  mBrush.setStyle( Qt::SolidPattern );
+}
+
+/*!
+  Set the outline width.
+  */
+void QgsHighlight::setWidth( int width )
+{
+  mPen.setWidth( width );
+}
+
+void QgsHighlight::paintPoint( QPainter *p, QgsPoint point )
+{
+  QPolygonF r( 5 );
+
+  double d = mMapCanvas->extent().width() * 0.005;
+
+  if ( mLayer )
+  {
+    point = mMapCanvas->mapRenderer()->layerToMapCoordinates( mLayer, point );
+  }
+
+  r[0] = toCanvasCoordinates( point + QgsVector( -d, -d ) ) - pos();
+  r[1] = toCanvasCoordinates( point + QgsVector( d, -d ) ) - pos();
+  r[2] = toCanvasCoordinates( point + QgsVector( d, d ) ) - pos();
+  r[3] = toCanvasCoordinates( point + QgsVector( -d, d ) ) - pos();
+  r[4] = r[0];
+
+  p->drawPolygon( r );
+}
+
+void QgsHighlight::paintLine( QPainter *p, QgsPolyline line )
+{
+  QPolygonF polygon( line.size() );
+
+  for ( int i = 0; i < line.size(); i++ )
+  {
+    if ( mLayer )
+    {
+      line[i] = mMapCanvas->mapRenderer()->layerToMapCoordinates( mLayer, line[i] );
+    }
+
+    polygon[i] = toCanvasCoordinates( line[i] ) - pos();
+  }
+
+  p->drawPolyline( polygon );
+}
+
+void QgsHighlight::paintPolygon( QPainter *p, QgsPolygon polygon )
+{
+  QPolygonF poly;
+
+  // just ring outlines, no fill
+  p->setPen( mPen );
+  p->setBrush( Qt::NoBrush );
+
+  for ( int i = 0; i < polygon.size(); i++ )
+  {
+    QPolygonF ring( polygon[i].size() + 1 );
+
+    for ( int j = 0; j < polygon[i].size(); j++ )
+    {
+      if ( mLayer )
+      {
+        polygon[i][j] = mMapCanvas->mapRenderer()->layerToMapCoordinates( mLayer, polygon[i][j] );
+      }
+
+      ring[ j ] = toCanvasCoordinates( polygon[i][j] ) - pos();
+    }
+
+    ring[ polygon[i].size()] = ring[ 0 ];
+
+    p->drawPolygon( ring );
+
+    if ( i == 0 )
+      poly = ring;
+    else
+      poly = poly.subtracted( ring );
+
+  }
+
+  // just fill, no outline
+  p->setPen( Qt::NoPen );
+  p->setBrush( mBrush );
+  p->drawPolygon( poly );
+}
+
+/*!
+  Draw the shape in response to an update event.
+  */
+void QgsHighlight::paint( QPainter* p )
+{
+  QgsDebugMsg( "entered." );
+
+  if ( !mGeometry )
+  {
+    QgsDebugMsg( "no geometry." );
+    return;
+  }
+
+  p->setPen( mPen );
+  p->setBrush( mBrush );
+
+  switch ( mGeometry->wkbType() )
+  {
+    case QGis::WKBPoint:
+    case QGis::WKBPoint25D:
+    {
+      paintPoint( p, mGeometry->asPoint() );
+    }
+    break;
+
+    case QGis::WKBMultiPoint:
+    case QGis::WKBMultiPoint25D:
+    {
+      QgsMultiPoint m = mGeometry->asMultiPoint();
+      for ( int i = 0; i < m.size(); i++ )
+      {
+        paintPoint( p, m[i] );
+      }
+    }
+    break;
+
+    case QGis::WKBLineString:
+    case QGis::WKBLineString25D:
+    {
+      paintLine( p, mGeometry->asPolyline() );
+    }
+    break;
+
+    case QGis::WKBMultiLineString:
+    case QGis::WKBMultiLineString25D:
+    {
+      QgsMultiPolyline m = mGeometry->asMultiPolyline();
+
+      for ( int i = 0; i < m.size(); i++ )
+      {
+        paintLine( p, m[i] );
+      }
+    }
+    break;
+
+    case QGis::WKBPolygon:
+    case QGis::WKBPolygon25D:
+    {
+      paintPolygon( p, mGeometry->asPolygon() );
+    }
+    break;
+
+    case QGis::WKBMultiPolygon:
+    case QGis::WKBMultiPolygon25D:
+    {
+      QgsMultiPolygon m = mGeometry->asMultiPolygon();
+      for ( int i = 0; i < m.size(); i++ )
+      {
+        paintPolygon( p, m[i] );
+      }
+    }
+    break;
+
+    case QGis::WKBUnknown:
+    default:
+      return;
+  }
+}
+
+void QgsHighlight::updateRect()
+{
+  if ( mGeometry )
+  {
+    QgsRectangle r = mGeometry->boundingBox();
+
+    if ( r.isEmpty() )
+    {
+      double d = mMapCanvas->extent().width() * 0.005;
+      r.setXMinimum( r.xMinimum() - d );
+      r.setYMinimum( r.yMinimum() - d );
+      r.setXMaximum( r.xMaximum() + d );
+      r.setXMaximum( r.yMaximum() + d );
+    }
+
+    setRect( r );
+    setVisible( mGeometry );
+  }
+  else
+  {
+    setRect( QgsRectangle() );
+  }
+
+}
+

Added: trunk/qgis/src/app/qgshighlight.h
===================================================================
--- trunk/qgis/src/app/qgshighlight.h	                        (rev 0)
+++ trunk/qgis/src/app/qgshighlight.h	2011-03-03 02:04:42 UTC (rev 15315)
@@ -0,0 +1,58 @@
+/***************************************************************************
+    qgshighlight.h - widget to highlight geometries
+     --------------------------------------
+    Date                 : 02-Mar-2011
+    Copyright            : (C) 2011 by Juergen E. Fischer, norBIT GmbH
+    Email                : jef at norbit dot de
+ ***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+/* $Id$ */
+#ifndef QGSHIGHLIGHT_H
+#define QGSHIGHLIGHT_H
+
+#include "qgsmapcanvasitem.h"
+#include "qgsgeometry.h"
+#include <QBrush>
+#include <QList>
+#include <QPen>
+#include <QPainter>
+
+class QgsVectorLayer;
+
+/** A class for highlight features on the map.
+ */
+class QgsHighlight: public QgsMapCanvasItem
+{
+  public:
+    QgsHighlight( QgsMapCanvas *mapCanvas, QgsGeometry *geom, QgsVectorLayer *layer );
+    ~QgsHighlight();
+
+    void setColor( const QColor & color );
+    void setWidth( int width );
+
+  protected:
+    virtual void paint( QPainter* p );
+
+    //! recalculates needed rectangle
+    void updateRect();
+
+  private:
+    void paintPoint( QPainter *p, QgsPoint point );
+    void paintLine( QPainter *p, QgsPolyline line );
+    void paintPolygon( QPainter *p, QgsPolygon polygon );
+
+    QgsHighlight();
+
+    QBrush mBrush;
+    QPen mPen;
+    QgsGeometry *mGeometry;
+    QgsVectorLayer *mLayer;
+};
+
+#endif

Modified: trunk/qgis/src/app/qgsidentifyresults.cpp
===================================================================
--- trunk/qgis/src/app/qgsidentifyresults.cpp	2011-03-03 02:04:22 UTC (rev 15314)
+++ trunk/qgis/src/app/qgsidentifyresults.cpp	2011-03-03 02:04:42 UTC (rev 15315)
@@ -23,7 +23,7 @@
 #include "qgsmaplayer.h"
 #include "qgsvectorlayer.h"
 #include "qgsrasterlayer.h"
-#include "qgsrubberband.h"
+#include "qgshighlight.h"
 #include "qgsgeometry.h"
 #include "qgsattributedialog.h"
 #include "qgsmapcanvas.h"
@@ -114,7 +114,7 @@
 
 QgsIdentifyResults::~QgsIdentifyResults()
 {
-  clearRubberbands();
+  clearHighlights();
   if ( mActionPopup )
     delete mActionPopup;
 }
@@ -441,7 +441,7 @@
   }
 
   mActionPopup->addAction( tr( "Clear results" ), this, SLOT( clear() ) );
-  mActionPopup->addAction( tr( "Clear highlights" ), this, SLOT( clearRubberbands() ) );
+  mActionPopup->addAction( tr( "Clear highlights" ), this, SLOT( clearHighlights() ) );
   mActionPopup->addAction( tr( "Highlight all" ), this, SLOT( highlightAll() ) );
   mActionPopup->addAction( tr( "Highlight layer" ), this, SLOT( highlightLayer() ) );
   mActionPopup->addSeparator();
@@ -491,14 +491,14 @@
   lstResults->resizeColumnToContents( 1 );
 }
 
-void QgsIdentifyResults::clearRubberbands()
+void QgsIdentifyResults::clearHighlights()
 {
-  foreach( QgsRubberBand *rb, mRubberBands )
+  foreach( QgsHighlight *h, mHighlights )
   {
-    delete rb;
+    delete h;
   }
 
-  mRubberBands.clear();
+  mHighlights.clear();
 }
 
 void QgsIdentifyResults::clear()
@@ -509,7 +509,7 @@
   }
 
   lstResults->clear();
-  clearRubberbands();
+  clearHighlights();
 }
 
 void QgsIdentifyResults::activate()
@@ -670,7 +670,7 @@
   }
   else
   {
-    clearRubberbands();
+    clearHighlights();
     highlightFeature( current );
   }
 }
@@ -687,7 +687,7 @@
     {
       for ( int j = 0; j < layItem->childCount(); j++ )
       {
-        delete mRubberBands.take( layItem->child( i ) );
+        delete mHighlights.take( layItem->child( i ) );
       }
     }
   }
@@ -734,7 +734,7 @@
 
     if ( featItem && featItem->data( 0, Qt::UserRole ).toInt() == fid )
     {
-      delete mRubberBands.take( featItem );
+      delete mHighlights.take( featItem );
       delete featItem;
       break;
     }
@@ -794,7 +794,7 @@
   if ( !featItem )
     return;
 
-  if ( mRubberBands.contains( featItem ) )
+  if ( mHighlights.contains( featItem ) )
     return;
 
   int fid = featItem->data( 0, Qt::UserRole ).toInt();
@@ -810,14 +810,13 @@
     return;
   }
 
-  QgsRubberBand *rb = new QgsRubberBand( mCanvas, feat.geometry()->type() == QGis::Polygon );
-  if ( rb )
+  QgsHighlight *h = new QgsHighlight( mCanvas, feat.geometry(), layer );
+  if ( h )
   {
-    rb->setToGeometry( feat.geometry(), layer );
-    rb->setWidth( 2 );
-    rb->setColor( Qt::red );
-    rb->show();
-    mRubberBands.insert( featItem, rb );
+    h->setWidth( 2 );
+    h->setColor( Qt::red );
+    h->show();
+    mHighlights.insert( featItem, h );
   }
 }
 
@@ -888,7 +887,7 @@
   }
   else
   {
-    action.viewFeatureForm( mRubberBands.take( featItem ) );
+    action.viewFeatureForm( mHighlights.take( featItem ) );
   }
 }
 
@@ -916,7 +915,7 @@
   if ( !layItem )
     return;
 
-  clearRubberbands();
+  clearHighlights();
 
   for ( int i = 0; i < layItem->childCount(); i++ )
   {

Modified: trunk/qgis/src/app/qgsidentifyresults.h
===================================================================
--- trunk/qgis/src/app/qgsidentifyresults.h	2011-03-03 02:04:22 UTC (rev 15314)
+++ trunk/qgis/src/app/qgsidentifyresults.h	2011-03-03 02:04:42 UTC (rev 15315)
@@ -34,7 +34,7 @@
 
 class QgsVectorLayer;
 class QgsRasterLayer;
-class QgsRubberBand;
+class QgsHighlight;
 class QgsMapCanvas;
 class QDockWidget;
 
@@ -95,7 +95,7 @@
     void copyFeatureAttributes();
     void highlightAll();
     void highlightLayer();
-    void clearRubberbands();
+    void clearHighlights();
     void expandAll();
     void collapseAll();
 
@@ -115,7 +115,7 @@
 
   private:
     QMenu *mActionPopup;
-    QMap<QTreeWidgetItem *, QgsRubberBand * > mRubberBands;
+    QMap<QTreeWidgetItem *, QgsHighlight * > mHighlights;
     QgsMapCanvas *mCanvas;
     QList<QgsFeature> mFeatures;
 

Modified: trunk/qgis/src/core/qgspoint.h
===================================================================
--- trunk/qgis/src/core/qgspoint.h	2011-03-03 02:04:22 UTC (rev 15314)
+++ trunk/qgis/src/core/qgspoint.h	2011-03-03 02:04:42 UTC (rev 15315)
@@ -21,6 +21,7 @@
 
 #include <iostream>
 #include <QString>
+#include <QPoint>
 
 /** \ingroup core
  * A class to represent a vector.

Modified: trunk/qgis/src/gui/qgsmapcanvas.h
===================================================================
--- trunk/qgis/src/gui/qgsmapcanvas.h	2011-03-03 02:04:22 UTC (rev 15314)
+++ trunk/qgis/src/gui/qgsmapcanvas.h	2011-03-03 02:04:42 UTC (rev 15315)
@@ -48,7 +48,7 @@
 class QgsMapLayer;
 class QgsLegend;
 class QgsLegendView;
-class QgsRubberBand;
+class QgsHighlight;
 class QgsVectorLayer;
 
 class QgsMapRenderer;



More information about the QGIS-commit mailing list