Skip to content

Commit

Permalink
Add QgsRubberBand::IconType::ICON_SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
manisandro authored and nyalldawson committed Jul 16, 2019
1 parent bc4bd91 commit 7af22a8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
15 changes: 15 additions & 0 deletions python/gui/auto_generated/qgsrubberband.sip.in
Expand Up @@ -43,6 +43,8 @@ for tracking the mouse while drawing polylines or polygons.
ICON_DIAMOND,

ICON_FULL_DIAMOND,

ICON_SVG
};

QgsRubberBand( QgsMapCanvas *mapCanvas /TransferThis/, QgsWkbTypes::GeometryType geometryType = QgsWkbTypes::LineGeometry );
Expand All @@ -56,6 +58,8 @@ Creates a new RubberBand.
QgsWkbTypes.LineGeometry, QgsWkbTypes.PolygonGeometry or QgsWkbTypes.PointGeometry
%End

~QgsRubberBand();

void setColor( const QColor &color );
%Docstring
Sets the color for the rubberband.
Expand Down Expand Up @@ -126,6 +130,17 @@ Sets the icon type to highlight point geometries.
:param icon: The icon to visualize point geometries
%End

void setSvgIcon( const QString &path, const QPoint &drawOffset );
%Docstring
Set the path to the svg file to use to draw points.
Calling this function automatically calls setIcon(ICON_SVG)

:param path: The path to the svg
:param drawOffset: The offset where to draw the image origin

.. versionadded:: 3.10
%End


IconType icon() const;
%Docstring
Expand Down
30 changes: 30 additions & 0 deletions src/gui/qgsrubberband.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgsproject.h"
#include "qgsrectangle.h"
#include <QPainter>
#include <QSvgRenderer>

QgsRubberBand::QgsRubberBand( QgsMapCanvas *mapCanvas, QgsWkbTypes::GeometryType geometryType )
: QObject( nullptr )
Expand All @@ -43,6 +44,11 @@ QgsRubberBand::QgsRubberBand()
{
}

QgsRubberBand::~QgsRubberBand()
{
delete mSvgRenderer;
}

void QgsRubberBand::setColor( const QColor &color )
{
setStrokeColor( color );
Expand Down Expand Up @@ -74,9 +80,18 @@ void QgsRubberBand::setWidth( int width )

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

void QgsRubberBand::setSvgIcon( const QString &path, const QPoint &drawOffset )
{
setIcon( ICON_SVG );
mSvgRenderer = new QSvgRenderer( path );;
mSvgOffset = drawOffset;
}

void QgsRubberBand::setIconSize( int iconSize )
{
mIconSize = iconSize;
Expand Down Expand Up @@ -489,6 +504,14 @@ void QgsRubberBand::drawShape( QPainter *p, const QVector<QPointF> &pts )
else
p->drawPolyline( pts, 4 );
}
case ICON_SVG:
QRectF viewBox = mSvgRenderer->viewBoxF();
QRectF r( mSvgOffset.x(), mSvgOffset.y(), viewBox.width(), viewBox.height() );
p->save();
p->translate( pt );
mSvgRenderer->render( p, r );
p->restore();
break;
}
}
}
Expand All @@ -514,6 +537,13 @@ void QgsRubberBand::updateRect()

const QgsMapToPixel &m2p = *( mMapCanvas->getCoordinateTransform() );

double iconSize = ( mIconSize + 1 ) / 2.;
if ( mSvgRenderer )
{
QRectF viewBox = mSvgRenderer->viewBoxF();
iconSize = qMax( qAbs( mSvgOffset.x() ) + .5 * viewBox.width(), qAbs( mSvgOffset.y() ) + .5 * viewBox.height() );
}

qreal w = ( ( mIconSize - 1 ) / 2 + mPen.width() ); // in canvas units

QgsRectangle r; // in canvas units
Expand Down
20 changes: 20 additions & 0 deletions src/gui/qgsrubberband.h
Expand Up @@ -29,6 +29,7 @@

class QgsVectorLayer;
class QPaintEvent;
class QSvgRenderer;

/**
* \ingroup gui
Expand Down Expand Up @@ -93,6 +94,12 @@ class GUI_EXPORT QgsRubberBand : public QObject, public QgsMapCanvasItem
* \since QGIS 3.0
*/
ICON_FULL_DIAMOND,

/**
* An svg image is used to highlight points
* \since QGIS 3.10
*/
ICON_SVG
};

/**
Expand All @@ -105,6 +112,8 @@ class GUI_EXPORT QgsRubberBand : public QObject, public QgsMapCanvasItem
*/
QgsRubberBand( QgsMapCanvas *mapCanvas SIP_TRANSFERTHIS, QgsWkbTypes::GeometryType geometryType = QgsWkbTypes::LineGeometry );

~QgsRubberBand();

/**
* Sets the color for the rubberband.
* Shorthand method to set fill and stroke color with a single call.
Expand Down Expand Up @@ -166,6 +175,15 @@ class GUI_EXPORT QgsRubberBand : public QObject, public QgsMapCanvasItem
*/
void setIcon( IconType icon );

/**
* Set the path to the svg file to use to draw points.
* Calling this function automatically calls setIcon(ICON_SVG)
* \param path The path to the svg
* \param drawOffset The offset where to draw the image origin
* \since QGIS 3.10
*/
void setSvgIcon( const QString &path, const QPoint &drawOffset );


/**
* Returns the current icon type to highlight point geometries.
Expand Down Expand Up @@ -362,6 +380,8 @@ class GUI_EXPORT QgsRubberBand : public QObject, public QgsMapCanvasItem

//! Icon to be shown.
IconType mIconType = ICON_CIRCLE;
QSvgRenderer *mSvgRenderer = nullptr;
QPoint mSvgOffset;

/**
* Nested lists used for multitypes
Expand Down

0 comments on commit 7af22a8

Please sign in to comment.