Skip to content

Commit

Permalink
Switch line endpoint snap to a distinct vertex marker (inverted
Browse files Browse the repository at this point in the history
triangle)

And minor cleanup/optimisation to QgsVertexMarker code
  • Loading branch information
nyalldawson committed Mar 4, 2021
1 parent b8baabf commit f51e38b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
1 change: 1 addition & 0 deletions python/gui/auto_generated/qgsvertexmarker.sip.in
Expand Up @@ -41,6 +41,7 @@ A class for marking vertices of features using e.g. circles or 'x'.
ICON_DOUBLE_TRIANGLE,
ICON_TRIANGLE,
ICON_RHOMBUS,
ICON_INVERTED_TRIANGLE,
};

QgsVertexMarker( QgsMapCanvas *mapCanvas /TransferThis/ );
Expand Down
10 changes: 5 additions & 5 deletions src/gui/qgssnapindicator.cpp
Expand Up @@ -78,7 +78,11 @@ void QgsSnapIndicator::setMatch( const QgsPointLocator::Match &match )
mSnappingMarker->setColor( color );

int iconType;
if ( match.hasVertex() )
if ( match.hasLineEndpoint() )
{
iconType = QgsVertexMarker::ICON_INVERTED_TRIANGLE; // line endpoint snap
}
else if ( match.hasVertex() )
{
if ( match.layer() )
iconType = QgsVertexMarker::ICON_BOX; // vertex snap
Expand All @@ -97,10 +101,6 @@ void QgsSnapIndicator::setMatch( const QgsPointLocator::Match &match )
{
iconType = QgsVertexMarker::ICON_RHOMBUS; // area snap
}
else if ( match.hasLineEndpoint() )
{
iconType = QgsVertexMarker::ICON_BOX; // line endpoint snap
}
else // must be segment snap
{
iconType = QgsVertexMarker::ICON_DOUBLE_TRIANGLE;
Expand Down
69 changes: 45 additions & 24 deletions src/gui/qgsvertexmarker.cpp
Expand Up @@ -20,16 +20,20 @@

QgsVertexMarker::QgsVertexMarker( QgsMapCanvas *mapCanvas )
: QgsMapCanvasItem( mapCanvas )
{}
{
updatePath();
}

void QgsVertexMarker::setIconType( int type )
{
mIconType = type;
updatePath();
}

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

void QgsVertexMarker::setCenter( const QgsPointXY &point )
Expand Down Expand Up @@ -58,62 +62,79 @@ void QgsVertexMarker::setPenWidth( int width )

void QgsVertexMarker::paint( QPainter *p )
{
qreal s = ( mIconSize - 1 ) / 2.0;

QPen pen( mColor );
pen.setWidth( mPenWidth );
p->setPen( pen );
QBrush brush( mFillColor );
p->setBrush( brush );
p->drawPath( mPath );
}

void QgsVertexMarker::updatePath()
{
mPath.clear();

const qreal s = ( mIconSize - 1 ) / 2.0;

switch ( mIconType )
{
case ICON_NONE:
break;

case ICON_CROSS:
p->drawLine( QLineF( -s, 0, s, 0 ) );
p->drawLine( QLineF( 0, -s, 0, s ) );
mPath.moveTo( QPointF( -s, 0 ) );
mPath.lineTo( QPointF( s, 0 ) );
mPath.moveTo( QPointF( 0, -s ) );
mPath.lineTo( QPointF( 0, s ) );
break;

case ICON_X:
p->drawLine( QLineF( -s, -s, s, s ) );
p->drawLine( QLineF( -s, s, s, -s ) );
mPath.moveTo( QPointF( -s, -s ) );
mPath.lineTo( QPointF( s, s ) );
mPath.moveTo( QPointF( -s, s ) );
mPath.lineTo( QPointF( s, -s ) );
break;

case ICON_BOX:
p->drawLine( QLineF( -s, -s, s, -s ) );
p->drawLine( QLineF( s, -s, s, s ) );
p->drawLine( QLineF( s, s, -s, s ) );
p->drawLine( QLineF( -s, s, -s, -s ) );
mPath.addRect( QRectF( -s, -s, s * 2, s * 2 ) );
break;

case ICON_CIRCLE:
p->drawEllipse( QPointF( 0, 0 ), s, s );
mPath.addEllipse( QPointF( 0, 0 ), s, s );
break;

case ICON_DOUBLE_TRIANGLE:
p->drawLine( QLineF( -s, -s, s, -s ) );
p->drawLine( QLineF( -s, s, s, s ) );
p->drawLine( QLineF( -s, -s, s, s ) );
p->drawLine( QLineF( s, -s, -s, s ) );
mPath.moveTo( QPointF( -s, -s ) );
mPath.lineTo( QPointF( s, -s ) );
mPath.lineTo( QPointF( -s, s ) );
mPath.lineTo( QPointF( s, s ) );
mPath.lineTo( QPointF( -s, -s ) );
break;

case ICON_TRIANGLE:
p->drawLine( QLineF( -s, s, s, s ) );
p->drawLine( QLineF( s, s, 0, -s ) );
p->drawLine( QLineF( 0, -s, -s, s ) );
mPath.moveTo( QPointF( -s, s ) );
mPath.lineTo( QPointF( s, s ) );
mPath.lineTo( QPointF( 0, -s ) );
mPath.lineTo( QPointF( -s, s ) );
break;

case ICON_RHOMBUS:
p->drawLine( QLineF( 0, -s, -s, 0 ) );
p->drawLine( QLineF( -s, 0, 0, s ) );
p->drawLine( QLineF( 0, s, s, 0 ) );
p->drawLine( QLineF( s, 0, 0, -s ) );
mPath.moveTo( QPointF( 0, -s ) );
mPath.lineTo( QPointF( -s, 0 ) );
mPath.lineTo( QPointF( 0, s ) );
mPath.lineTo( QPointF( s, 0 ) );
mPath.lineTo( QPointF( 0, -s ) );
break;

case ICON_INVERTED_TRIANGLE:
mPath.moveTo( QPointF( -s, -s ) );
mPath.lineTo( QPointF( s, -s ) );
mPath.lineTo( QPointF( 0, s ) );
mPath.lineTo( QPointF( -s, -s ) );
break;
}
}


QRectF QgsVertexMarker::boundingRect() const
{
qreal s = qreal( mIconSize + mPenWidth ) / 2.0;
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsvertexmarker.h
Expand Up @@ -57,6 +57,7 @@ class GUI_EXPORT QgsVertexMarker : public QgsMapCanvasItem
ICON_DOUBLE_TRIANGLE, //!< Added in QGIS 3.0
ICON_TRIANGLE, //!< Added in QGIS 3.12
ICON_RHOMBUS, //!< Added in QGIS 3.12
ICON_INVERTED_TRIANGLE, //!< Added in QGIS 3.20
};

QgsVertexMarker( QgsMapCanvas *mapCanvas SIP_TRANSFERTHIS );
Expand Down Expand Up @@ -123,9 +124,13 @@ class GUI_EXPORT QgsVertexMarker : public QgsMapCanvasItem

private:

void updatePath();

//! icon to be shown
int mIconType = ICON_X;

QPainterPath mPath;

//! size
int mIconSize = 10;

Expand Down

0 comments on commit f51e38b

Please sign in to comment.