QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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"
23 
24 #include <QToolTip>
25 
26 
28  : mCanvas( canvas )
29 {
30  mSnappingMarker.setParentOwner( canvas );
31 }
32 
34 
36 {
37  mMatch = match;
38 
39  if ( !mMatch.isValid() )
40  {
41  if ( mSnappingMarker )
42  {
43  mCanvas->scene()->removeItem( mSnappingMarker );
44  mSnappingMarker.reset(); // need to delete since QGraphicsSene::removeItem transfers back ownership
45  }
46  QToolTip::hideText();
47  }
48  else
49  {
50  if ( !mSnappingMarker )
51  {
52  mSnappingMarker.reset( new QgsVertexMarker( mCanvas ) );
53  mSnappingMarker->setIconSize( QgsGuiUtils::scaleIconSize( 10 ) );
54  mSnappingMarker->setPenWidth( QgsGuiUtils::scaleIconSize( 3 ) );
55  }
56 
58  mSnappingMarker->setColor( color );
59 
60  int iconType;
61  if ( match.hasLineEndpoint() )
62  {
63  iconType = QgsVertexMarker::ICON_INVERTED_TRIANGLE; // line endpoint snap
64  }
65  else if ( match.hasVertex() )
66  {
67  if ( match.layer() )
68  iconType = QgsVertexMarker::ICON_BOX; // vertex snap
69  else
70  iconType = QgsVertexMarker::ICON_X; // intersection snap
71  }
72  else if ( match.hasMiddleSegment() )
73  {
74  iconType = QgsVertexMarker::ICON_TRIANGLE; // middle snap
75  }
76  else if ( match.hasCentroid() )
77  {
78  iconType = QgsVertexMarker::ICON_CIRCLE; // centroid snap
79  }
80  else if ( match.hasArea() )
81  {
82  iconType = QgsVertexMarker::ICON_RHOMBUS; // area snap
83  }
84  else // must be segment snap
85  {
87  }
88 
89  mSnappingMarker->setIconType( iconType );
90 
91  mSnappingMarker->setCenter( match.point() );
92 
93  // tooltip
95  {
96  const QPoint ptCanvas = mSnappingMarker->toCanvasCoordinates( match.point() ).toPoint();
97  const QPoint ptGlobal = mCanvas->mapToGlobal( ptCanvas );
98  const QRect rect( ptCanvas.x(), ptCanvas.y(), 1, 1 ); // area where is the tooltip valid
99  const QString layerName = match.layer() ? match.layer()->name() : QString();
100  QToolTip::showText( ptGlobal, layerName, mCanvas, rect );
101  }
102  }
103 }
104 
105 void QgsSnapIndicator::setVisible( bool visible )
106 {
107  if ( mSnappingMarker )
108  mSnappingMarker->setVisible( visible );
109 }
110 
112 {
113  if ( mSnappingMarker )
114  return mSnappingMarker->isVisible();
115 
116  return false;
117 }
qgssettingsregistrycore.h
QObjectParentUniquePtr::reset
void reset(T *p=nullptr)
Will reset the managed pointer to p.
Definition: qobjectuniqueptr.h:441
QgsSnapIndicator::setVisible
void setVisible(bool visible=true)
Sets whether the snapping indicator is visible.
Definition: qgssnapindicator.cpp:105
QgsPointLocator::Match::hasMiddleSegment
bool hasMiddleSegment() const
Returns true if the Match is the middle of a segment.
Definition: qgspointlocator.h:219
QgsVertexMarker::setIconType
void setIconType(int iconType)
Definition: qgsvertexmarker.cpp:27
qgsmapcanvas.h
QgsVertexMarker::ICON_INVERTED_TRIANGLE
@ ICON_INVERTED_TRIANGLE
Added in QGIS 3.20.
Definition: qgsvertexmarker.h:60
QgsVertexMarker::ICON_TRIANGLE
@ ICON_TRIANGLE
Added in QGIS 3.12.
Definition: qgsvertexmarker.h:58
QgsSnapIndicator::match
QgsPointLocator::Match match() const
Returns currently displayed snapping match.
Definition: qgssnapindicator.h:42
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsVertexMarker::ICON_CIRCLE
@ ICON_CIRCLE
Definition: qgsvertexmarker.h:56
QgsPointLocator::Match::point
QgsPointXY point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords,...
Definition: qgspointlocator.h:238
QgsVertexMarker::setIconSize
void setIconSize(int iconSize)
Definition: qgsvertexmarker.cpp:33
QgsVertexMarker::ICON_DOUBLE_TRIANGLE
@ ICON_DOUBLE_TRIANGLE
Added in QGIS 3.0.
Definition: qgsvertexmarker.h:57
QgsPointLocator::Match::hasVertex
bool hasVertex() const
Returns true if the Match is a vertex.
Definition: qgspointlocator.h:211
qgssnapindicator.h
QgsPointLocator::Match::hasCentroid
bool hasCentroid() const
Returns true if the Match is a centroid.
Definition: qgspointlocator.h:215
QgsVertexMarker
A class for marking vertices of features using e.g. circles or 'x'.
Definition: qgsvertexmarker.h:36
QObjectParentUniquePtr::setParentOwner
void setParentOwner(QObject *parent)
Sets the parent object.
Definition: qobjectuniqueptr.h:320
QgsVertexMarker::ICON_RHOMBUS
@ ICON_RHOMBUS
Added in QGIS 3.12.
Definition: qgsvertexmarker.h:59
QgsMapCanvasItem::toCanvasCoordinates
QPointF toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
Definition: qgsmapcanvasitem.cpp:61
QgsSnapIndicator::setMatch
void setMatch(const QgsPointLocator::Match &match)
Sets snapping match that should be displayed in map canvas. Invalid match hides the indicator.
Definition: qgssnapindicator.cpp:35
QgsPointLocator::Match::hasLineEndpoint
bool hasLineEndpoint() const
Returns true if the Match is a line endpoint (start or end vertex).
Definition: qgspointlocator.h:226
QgsVertexMarker::ICON_BOX
@ ICON_BOX
Definition: qgsvertexmarker.h:55
qgsvertexmarker.h
QgsVertexMarker::setPenWidth
void setPenWidth(int width)
Definition: qgsvertexmarker.cpp:58
QgsVertexMarker::setCenter
void setCenter(const QgsPointXY &point)
Sets the center point of the marker, in map coordinates.
Definition: qgsvertexmarker.cpp:39
QgsPointLocator::Match::isValid
bool isValid() const
Definition: qgspointlocator.h:209
QgsSettingsRegistryCore::settingsDigitizingSnapTooltip
static const QgsSettingsEntryBool settingsDigitizingSnapTooltip
Settings entry digitizing snap tooltip.
Definition: qgssettingsregistrycore.h:121
QgsVertexMarker::setColor
void setColor(const QColor &color)
Sets the stroke color for the marker.
Definition: qgsvertexmarker.cpp:46
QgsPointLocator::Match::layer
QgsVectorLayer * layer() const
The vector layer where the snap occurred.
Definition: qgspointlocator.h:247
QgsPointLocator::Match
Definition: qgspointlocator.h:187
qgsvectorlayer.h
QgsSettingsEntryByReference::value
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
Definition: qgssettingsentry.h:379
QgsSettingsRegistryCore::settingsDigitizingSnapColor
static const QgsSettingsEntryColor settingsDigitizingSnapColor
Settings entry digitizing snap color.
Definition: qgssettingsregistrycore.h:118
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:76
qgsguiutils.h
QgsSnapIndicator::isVisible
bool isVisible() const
Returns whether the snapping indicator is visible.
Definition: qgssnapindicator.cpp:111
QgsGuiUtils::scaleIconSize
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
Definition: qgsguiutils.cpp:259
QgsVertexMarker::ICON_X
@ ICON_X
Definition: qgsvertexmarker.h:54
QgsSnapIndicator::QgsSnapIndicator
QgsSnapIndicator(QgsMapCanvas *canvas)
Constructs an indicator for the given map canvas.
Definition: qgssnapindicator.cpp:27
QgsSnapIndicator::~QgsSnapIndicator
~QgsSnapIndicator()
QgsPointLocator::Match::hasArea
bool hasArea() const
Returns true if the Match is an area.
Definition: qgspointlocator.h:217