QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgssnapindicator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssnapindicator.cpp
3 --------------------------------------
4 Date : October 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgssnapindicator.h"
17
18#include "qgsguiutils.h"
19#include "qgsmapcanvas.h"
21#include "qgsvectorlayer.h"
22#include "qgsvertexmarker.h"
24
25#include <QToolTip>
26
27
29 : mCanvas( canvas )
30{
31 mSnappingMarker.setParentOwner( canvas );
32}
33
35
37{
38 mMatch = match;
39
40 if ( !mMatch.isValid() )
41 {
42 if ( mSnappingMarker )
43 {
44 mCanvas->scene()->removeItem( mSnappingMarker );
45 mSnappingMarker.reset(); // need to delete since QGraphicsSene::removeItem transfers back ownership
46 }
47 QToolTip::hideText();
48 }
49 else
50 {
51 if ( !mSnappingMarker )
52 {
53 mSnappingMarker.reset( new QgsVertexMarker( mCanvas ) );
54 mSnappingMarker->setIconSize( QgsGuiUtils::scaleIconSize( 10 ) );
55 mSnappingMarker->setPenWidth( QgsGuiUtils::scaleIconSize( 3 ) );
56 }
57
59 mSnappingMarker->setColor( color );
60
61 int iconType;
62 if ( match.hasLineEndpoint() )
63 {
64 iconType = QgsVertexMarker::ICON_INVERTED_TRIANGLE; // line endpoint snap
65 }
66 else if ( match.hasVertex() )
67 {
68 if ( match.layer() )
69 iconType = QgsVertexMarker::ICON_BOX; // vertex snap
70 else
71 iconType = QgsVertexMarker::ICON_X; // intersection snap
72 }
73 else if ( match.hasMiddleSegment() )
74 {
75 iconType = QgsVertexMarker::ICON_TRIANGLE; // middle snap
76 }
77 else if ( match.hasCentroid() )
78 {
79 iconType = QgsVertexMarker::ICON_CIRCLE; // centroid snap
80 }
81 else if ( match.hasArea() )
82 {
83 iconType = QgsVertexMarker::ICON_RHOMBUS; // area snap
84 }
85 else // must be segment snap
86 {
88 }
89
90 mSnappingMarker->setIconType( iconType );
91
92 mSnappingMarker->setCenter( match.point() );
93
94 // tooltip
96 {
97 const QPoint ptCanvas = mSnappingMarker->toCanvasCoordinates( match.point() ).toPoint();
98 const QPoint ptGlobal = mCanvas->mapToGlobal( ptCanvas );
99 const QRect rect( ptCanvas.x(), ptCanvas.y(), 1, 1 ); // area where is the tooltip valid
100 const QString layerName = match.layer() ? match.layer()->name() : QString();
101 QToolTip::showText( ptGlobal, layerName, mCanvas, rect );
102 }
103 }
104}
105
107{
108 if ( mSnappingMarker )
109 mSnappingMarker->setVisible( visible );
110}
111
113{
114 if ( mSnappingMarker )
115 return mSnappingMarker->isVisible();
116
117 return false;
118}
void setParentOwner(QObject *parent)
Sets the parent object.
void reset(T *p=nullptr)
Will reset the managed pointer to p.
QPointF toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:93
QString name
Definition: qgsmaplayer.h:78
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
static const QgsSettingsEntryBool * settingsDigitizingSnapTooltip
Settings entry digitizing snap tooltip.
static const QgsSettingsEntryColor * settingsDigitizingSnapColor
Settings entry digitizing snap color.
QgsPointLocator::Match match() const
Returns currently displayed snapping match.
void setVisible(bool visible=true)
Sets whether the snapping indicator is visible.
void setMatch(const QgsPointLocator::Match &match)
Sets snapping match that should be displayed in map canvas. Invalid match hides the indicator.
QgsSnapIndicator(QgsMapCanvas *canvas)
Constructs an indicator for the given map canvas.
bool isVisible() const
Returns whether the snapping indicator is visible.
A class for marking vertices of features using e.g.
void setPenWidth(int width)
@ ICON_TRIANGLE
Added in QGIS 3.12.
@ ICON_DOUBLE_TRIANGLE
Added in QGIS 3.0.
@ ICON_INVERTED_TRIANGLE
Added in QGIS 3.20.
@ ICON_RHOMBUS
Added in QGIS 3.12.
void setIconSize(int iconSize)
void setCenter(const QgsPointXY &point)
Sets the center point of the marker, in map coordinates.
void setIconType(int iconType)
void setColor(const QColor &color)
Sets the stroke color for the marker.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
bool hasCentroid() const
Returns true if the Match is a centroid.
QgsVectorLayer * layer() const
The vector layer where the snap occurred.
QgsPointXY point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords,...
bool hasArea() const
Returns true if the Match is an area.
bool hasLineEndpoint() const
Returns true if the Match is a line endpoint (start or end vertex).
bool hasMiddleSegment() const
Returns true if the Match is the middle of a segment.
bool hasVertex() const
Returns true if the Match is a vertex.