Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add different point symbols for QgsRubberband.
  • Loading branch information
m-kuhn committed Dec 25, 2012
1 parent a4a8b4c commit f232896
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 23 deletions.
23 changes: 23 additions & 0 deletions python/gui/qgsrubberband.sip
Expand Up @@ -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 );

Expand All @@ -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.
Expand Down
78 changes: 55 additions & 23 deletions src/gui/qgsrubberband.cpp
Expand Up @@ -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 )
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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 );
}

Expand All @@ -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 )
{
Expand All @@ -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 );
}
}
}
Expand Down Expand Up @@ -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 )
{
Expand All @@ -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;
Expand All @@ -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 );
Expand Down
33 changes: 33 additions & 0 deletions src/gui/qgsrubberband.h
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f232896

Please sign in to comment.