Skip to content
Permalink
Browse files
Add different point symbols for QgsRubberband.
  • Loading branch information
m-kuhn committed Dec 25, 2012
1 parent a4a8b4c commit f2328962df15271343063c22d0667bdf009a9ab6
Showing with 111 additions and 23 deletions.
  1. +23 −0 python/gui/qgsrubberband.sip
  2. +55 −23 src/gui/qgsrubberband.cpp
  3. +33 −0 src/gui/qgsrubberband.h
@@ -5,13 +5,35 @@ class QgsRubberBand: QgsMapCanvasItem
%End

public:
/** Icons
* Added in 1.9 */
enum IconType
{
ICON_NONE,
ICON_CROSS,
ICON_X,
ICON_BOX,
ICON_CIRCLE
};

QgsRubberBand( QgsMapCanvas* mapCanvas /TransferThis/, QGis::GeometryType geometryType = QGis::Line );
QgsRubberBand( QgsMapCanvas* mapCanvas /TransferThis/, bool isPolygon );
~QgsRubberBand();

/** Set the color for the rubberband */
void setColor( const QColor & color );

/** Set the width of the line. Outline width for polygon. */
void setWidth( int width );

/** Set the icon type to highlight point geometries.
* Added in 1.9 */
void setIcon( IconType icon );

/** Set the size of the point icons
* Added in 1.9 */
void setIconSize ( int iconSize );

void reset( QGis::GeometryType geometryType = QGis::Line );
void reset( bool isPolygon );

@@ -30,6 +52,7 @@ class QgsRubberBand: QgsMapCanvasItem

/**Sets this rubber band to the geometry of an existing feature.
This is useful for feature highlighting.
In contrast to addGeometry, this method does also change the geometry type of the rubberband.
@param geom the geometry object
@param layer the layer containing the feature, used for coord transformation to map
crs. In case of 0 pointer, the coordinates are not going to be transformed.
@@ -30,6 +30,8 @@
QgsRubberBand::QgsRubberBand( QgsMapCanvas* mapCanvas, QGis::GeometryType geometryType )
: QgsMapCanvasItem( mapCanvas )
, mWidth( 1 )
, mIconSize( 5 )
, mIconType( ICON_CIRCLE )
, mGeometryType( geometryType )
, mTranslationOffsetX( 0.0 )
, mTranslationOffsetY( 0.0 )
@@ -41,6 +43,8 @@ QgsRubberBand::QgsRubberBand( QgsMapCanvas* mapCanvas, QGis::GeometryType geomet
QgsRubberBand::QgsRubberBand( QgsMapCanvas* mapCanvas, bool isPolygon )
: QgsMapCanvasItem( mapCanvas )
, mWidth( 1 )
, mIconSize( 5 )
, mIconType( ICON_CIRCLE )
, mTranslationOffsetX( 0.0 )
, mTranslationOffsetY( 0.0 )
{
@@ -75,6 +79,16 @@ void QgsRubberBand::setWidth( int width )
mWidth = width;
}

void QgsRubberBand::setIcon( IconType icon )
{
mIconType = icon;
}

void QgsRubberBand::setIconSize( int iconSize )
{
mIconSize = iconSize;
}

/*!
Remove all points from the shape being created.
*/
@@ -189,7 +203,7 @@ void QgsRubberBand::movePoint( int index, const QgsPoint& p, int geometryIndex )

void QgsRubberBand::setToGeometry( QgsGeometry* geom, QgsVectorLayer* layer )
{
reset( mGeometryType );
reset( geom->type() );
addGeometry( geom, layer );
}

@@ -215,7 +229,6 @@ void QgsRubberBand::addGeometry( QgsGeometry* geom, QgsVectorLayer* layer )
case QGis::WKBPoint:
case QGis::WKBPoint25D:
{
double d = mMapCanvas->extent().width() * 0.005;
QgsPoint pt;
if ( layer )
{
@@ -225,34 +238,24 @@ void QgsRubberBand::addGeometry( QgsGeometry* geom, QgsVectorLayer* layer )
{
pt = geom->asPoint();
}
addPoint( QgsPoint( pt.x() - d, pt.y() - d ), false, idx );
addPoint( QgsPoint( pt.x() + d, pt.y() - d ), false, idx );
addPoint( QgsPoint( pt.x() + d, pt.y() + d ), false, idx );
addPoint( QgsPoint( pt.x() - d, pt.y() + d ), false, idx );
addPoint( pt, false, idx );
}
break;

case QGis::WKBMultiPoint:
case QGis::WKBMultiPoint25D:
{
double d = mMapCanvas->extent().width() * 0.005;
QgsMultiPoint mpt = geom->asMultiPoint();
for ( int i = 0; i < mpt.size(); ++i, ++idx )
{
QgsPoint pt = mpt[i];
if ( layer )
{
addPoint( mr->layerToMapCoordinates( layer, QgsPoint( pt.x() - d, pt.y() - d ) ), false, idx );
addPoint( mr->layerToMapCoordinates( layer, QgsPoint( pt.x() + d, pt.y() - d ) ), false, idx );
addPoint( mr->layerToMapCoordinates( layer, QgsPoint( pt.x() + d, pt.y() + d ) ), false, idx );
addPoint( mr->layerToMapCoordinates( layer, QgsPoint( pt.x() - d, pt.y() + d ) ), false, idx );
addPoint( mr->layerToMapCoordinates( layer, pt ), false, idx );
}
else
{
addPoint( QgsPoint( pt.x() - d, pt.y() - d ), false, idx );
addPoint( QgsPoint( pt.x() + d, pt.y() - d ), false, idx );
addPoint( QgsPoint( pt.x() + d, pt.y() + d ), false, idx );
addPoint( QgsPoint( pt.x() - d, pt.y() + d ), false, idx );
addPoint( pt, false, idx );
}
}
}
@@ -386,6 +389,8 @@ void QgsRubberBand::paint( QPainter* p )
if ( mPoints.size() > 0 )
{
p->setBrush( mBrush );
mPen.setWidth( mWidth );
p->setPen( mPen );

for ( int i = 0; i < mPoints.size(); ++i )
{
@@ -400,29 +405,53 @@ void QgsRubberBand::paint( QPainter* p )
{
case QGis::Polygon:
{
mPen.setWidth( mWidth );
p->setPen( mPen );
p->drawPolygon( pts );
}
break;

case QGis::Point:
{
mPen.setWidth( 1 );
p->setPen( mPen );
QVector<QPointF>::const_iterator ptIt = pts.constBegin();
for ( ; ptIt != pts.constEnd(); ++ptIt )
{
p->drawEllipse(( *ptIt ).x() - mWidth / 2, ( *ptIt ).y() - mWidth / 2, mWidth, mWidth );
double x = (*ptIt).x();
double y = (*ptIt).y();

qreal s = ( mIconSize - 1 ) / 2;

switch ( mIconType )
{
case ICON_NONE:
break;

case ICON_CROSS:
p->drawLine( QLineF( x - s, y, x + s, y ) );
p->drawLine( QLineF( x, y - s, x, y + s ) );
break;

case ICON_X:
p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
break;

case ICON_BOX:
p->drawLine( QLineF( x - s, y - s, x + s, y - s ) );
p->drawLine( QLineF( x + s, y - s, x + s, y + s ) );
p->drawLine( QLineF( x + s, y + s, x - s, y + s ) );
p->drawLine( QLineF( x - s, y + s, x - s, y - s ) );
break;

case ICON_CIRCLE:
p->drawEllipse( x - s, y - s, mIconSize, mIconSize );
break;
}
}
}
break;

case QGis::Line:
default:
{
mPen.setWidth( mWidth );
p->setPen( mPen );
p->drawPolyline( pts );
}
break;
@@ -449,7 +478,10 @@ void QgsRubberBand::updateRect()
QList<QgsPoint>::const_iterator it = mPoints.at( i ).constBegin();
for ( ; it != mPoints.at( i ).constEnd(); ++it )
{
r.combineExtentWith( it->x() + mTranslationOffsetX, it->y() + mTranslationOffsetY );
qreal s = ( mIconSize - 1 ) / 2;
QgsRectangle rect = QgsRectangle( it->x() + mTranslationOffsetX - s, it->y() + mTranslationOffsetY - s,
it->x() + mTranslationOffsetX + s, it->y() + mTranslationOffsetY + s );
r.combineExtentWith( &rect );
}
}
setRect( r );
@@ -31,6 +31,18 @@ class QPaintEvent;
class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
{
public:

/** Icons
* Added in 1.9 */
enum IconType
{
ICON_NONE,
ICON_CROSS,
ICON_X,
ICON_BOX,
ICON_CIRCLE
};

/**
* Creates a new RubberBand.
* @param mapCanvas The map canvas to draw onto. It's CRS will be used map points onto screen coordinates.
@@ -47,9 +59,20 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
QgsRubberBand( QgsMapCanvas* mapCanvas, bool isPolygon );
~QgsRubberBand();

/** Set the color for the rubberband */
void setColor( const QColor & color );

/** Set the width of the line. Outline width for polygon. */
void setWidth( int width );

/** Set the icon type to highlight point geometries.
* Added in 1.9 */
void setIcon( IconType icon );

/** Set the size of the point icons
* Added in 1.9 */
void setIconSize ( int iconSize );

/**
* Clears all the geometries in this rubberband.
* Sets the representation type according to geometryType.
@@ -80,6 +103,7 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem

/**Sets this rubber band to the geometry of an existing feature.
This is useful for feature highlighting.
In contrast to addGeometry, this method does also change the geometry type of the rubberband.
@param geom the geometry object
@param layer the layer containing the feature, used for coord transformation to map
crs. In case of 0 pointer, the coordinates are not going to be transformed.
@@ -127,8 +151,17 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
QBrush mBrush;
QPen mPen;

/** The width of any line within the rubberband. */
int mWidth;

/** The size of the icon for points.
* Added in 1.9 */
int mIconSize;

/** Icon to be shown.
* Added in 1.9 */
IconType mIconType ;

/**Nested lists used for multitypes*/
QList< QList <QgsPoint> > mPoints;
QGis::GeometryType mGeometryType;

0 comments on commit f232896

Please sign in to comment.