QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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"
20 #include "qgssettings.h"
21 #include "qgsvectorlayer.h"
22 #include "qgsvertexmarker.h"
23 
24 #include <QToolTip>
25 
26 
28  : mCanvas( canvas )
29 {
30  // We need to make sure that the internal pointers are invalidated if the canvas is deleted before this
31  // indicator.
32  // The canvas is specified again as the "receiver", just to silence clazy (official clazy recommendation
33  // for false positives).
34  mCanvasDestroyedConnection = QObject::connect( canvas, &QgsMapCanvas::destroyed, canvas, [ = ]()
35  {
36  mCanvas = nullptr;
37  mSnappingMarker = nullptr;
38  } );
39 }
40 
42 {
43  if ( mSnappingMarker && mCanvas )
44  {
45  mCanvas->scene()->removeItem( mSnappingMarker );
46  delete mSnappingMarker;
47  }
48 
49  QObject::disconnect( mCanvasDestroyedConnection );
50 };
51 
53 {
54  mMatch = match;
55 
56  if ( !mMatch.isValid() )
57  {
58  if ( mSnappingMarker )
59  {
60  mCanvas->scene()->removeItem( mSnappingMarker );
61  delete mSnappingMarker; // need to delete since QGraphicsSene::removeItem transfers back ownership
62  }
63  mSnappingMarker = nullptr;
64  QToolTip::hideText();
65  }
66  else
67  {
68  if ( !mSnappingMarker )
69  {
70  mSnappingMarker = new QgsVertexMarker( mCanvas ); // ownership of the marker is transferred to QGraphicsScene
71  mSnappingMarker->setIconSize( QgsGuiUtils::scaleIconSize( 10 ) );
72  mSnappingMarker->setPenWidth( QgsGuiUtils::scaleIconSize( 3 ) );
73  }
74 
75  QgsSettings s;
76 
77  QColor color = s.value( QStringLiteral( "/qgis/digitizing/snap_color" ), QColor( Qt::magenta ) ).value<QColor>();
78  mSnappingMarker->setColor( color );
79 
80  int iconType;
81  if ( match.hasVertex() )
82  {
83  if ( match.layer() )
84  iconType = QgsVertexMarker::ICON_BOX; // vertex snap
85  else
86  iconType = QgsVertexMarker::ICON_X; // intersection snap
87  }
88  else if ( match.hasMiddleSegment() )
89  {
90  iconType = QgsVertexMarker::ICON_TRIANGLE; // middle snap
91  }
92  else if ( match.hasCentroid() )
93  {
94  iconType = QgsVertexMarker::ICON_CIRCLE; // centroid snap
95  }
96  else if ( match.hasArea() )
97  {
98  iconType = QgsVertexMarker::ICON_RHOMBUS; // area snap
99  }
100  else // must be segment snap
101  {
103  }
104 
105  mSnappingMarker->setIconType( iconType );
106 
107  mSnappingMarker->setCenter( match.point() );
108 
109  // tooltip
110  if ( s.value( QStringLiteral( "/qgis/digitizing/snap_tooltip" ), false ).toBool() )
111  {
112  QPoint ptCanvas = mSnappingMarker->toCanvasCoordinates( match.point() ).toPoint();
113  QPoint ptGlobal = mCanvas->mapToGlobal( ptCanvas );
114  QRect rect( ptCanvas.x(), ptCanvas.y(), 1, 1 ); // area where is the tooltip valid
115  QString layerName = match.layer() ? match.layer()->name() : QString();
116  QToolTip::showText( ptGlobal, layerName, mCanvas, rect );
117  }
118  }
119 }
120 
121 void QgsSnapIndicator::setVisible( bool visible )
122 {
123  if ( mSnappingMarker )
124  mSnappingMarker->setVisible( visible );
125 }
126 
128 {
129  if ( mSnappingMarker )
130  return mSnappingMarker->isVisible();
131 
132  return false;
133 }
QgsSnapIndicator::setVisible
void setVisible(bool visible=true)
Sets whether the snapping indicator is visible.
Definition: qgssnapindicator.cpp:121
QgsPointLocator::Match::hasMiddleSegment
bool hasMiddleSegment() const
Returns true if the Match is the middle of a segment.
Definition: qgspointlocator.h:216
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsVertexMarker::setIconType
void setIconType(int iconType)
Definition: qgsvertexmarker.cpp:25
qgsmapcanvas.h
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
Definition: qgsmapcanvas.h:83
QgsVertexMarker::ICON_CIRCLE
@ ICON_CIRCLE
Definition: qgsvertexmarker.h:56
QgsSettings
Definition: qgssettings.h:61
QgsPointLocator::Match::point
QgsPointXY point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords,...
Definition: qgspointlocator.h:228
QgsVertexMarker::setIconSize
void setIconSize(int iconSize)
Definition: qgsvertexmarker.cpp:30
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:208
qgssnapindicator.h
QgsPointLocator::Match::hasCentroid
bool hasCentroid() const
Returns true if the Match is a centroid.
Definition: qgspointlocator.h:212
QgsVertexMarker
Definition: qgsvertexmarker.h:36
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:52
QgsVertexMarker::ICON_BOX
@ ICON_BOX
Definition: qgsvertexmarker.h:55
qgsvertexmarker.h
QgsVertexMarker::setPenWidth
void setPenWidth(int width)
Definition: qgsvertexmarker.cpp:54
QgsVertexMarker::setCenter
void setCenter(const QgsPointXY &point)
Definition: qgsvertexmarker.cpp:35
QgsPointLocator::Match::isValid
bool isValid() const
Definition: qgspointlocator.h:206
QgsVertexMarker::setColor
void setColor(const QColor &color)
Sets the stroke color for the marker.
Definition: qgsvertexmarker.cpp:42
QgsPointLocator::Match::layer
QgsVectorLayer * layer() const
The vector layer where the snap occurred.
Definition: qgspointlocator.h:237
QgsPointLocator::Match
Definition: qgspointlocator.h:184
qgsvectorlayer.h
qgssettings.h
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:85
qgsguiutils.h
QgsSnapIndicator::isVisible
bool isVisible() const
Returns whether the snapping indicator is visible.
Definition: qgssnapindicator.cpp:127
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:257
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()
Definition: qgssnapindicator.cpp:41
QgsPointLocator::Match::hasArea
bool hasArea() const
Returns true if the Match is an area.
Definition: qgspointlocator.h:214