QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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  mCanvasDestroyedConnection = QObject::connect( canvas, &QgsMapCanvas::destroyed, [ = ]()
31  {
32  mCanvas = nullptr;
33  mSnappingMarker = nullptr;
34  } );
35 }
36 
38 {
39  if ( mSnappingMarker && mCanvas )
40  {
41  mCanvas->scene()->removeItem( mSnappingMarker );
42  delete mSnappingMarker;
43  }
44 
45  QObject::disconnect( mCanvasDestroyedConnection );
46 };
47 
49 {
50  mMatch = match;
51 
52  if ( !mMatch.isValid() )
53  {
54  if ( mSnappingMarker )
55  {
56  mCanvas->scene()->removeItem( mSnappingMarker );
57  delete mSnappingMarker; // need to delete since QGraphicsSene::removeItem transfers back ownership
58  }
59  mSnappingMarker = nullptr;
60  QToolTip::hideText();
61  }
62  else
63  {
64  if ( !mSnappingMarker )
65  {
66  mSnappingMarker = new QgsVertexMarker( mCanvas ); // ownership of the marker is transferred to QGraphicsScene
67  mSnappingMarker->setIconSize( QgsGuiUtils::scaleIconSize( 10 ) );
68  mSnappingMarker->setPenWidth( QgsGuiUtils::scaleIconSize( 3 ) );
69  }
70 
71  QgsSettings s;
72 
73  QColor color = s.value( QStringLiteral( "/qgis/digitizing/snap_color" ), QColor( Qt::magenta ) ).value<QColor>();
74  mSnappingMarker->setColor( color );
75 
76  int iconType;
77  if ( match.hasVertex() )
78  {
79  if ( match.layer() )
80  iconType = QgsVertexMarker::ICON_BOX; // vertex snap
81  else
82  iconType = QgsVertexMarker::ICON_X; // intersection snap
83  }
84  else // must be segment snap
85  {
87  }
88 
89  mSnappingMarker->setIconType( iconType );
90 
91  mSnappingMarker->setCenter( match.point() );
92 
93  // tooltip
94  if ( s.value( QStringLiteral( "/qgis/digitizing/snap_tooltip" ), false ).toBool() )
95  {
96  QPoint ptCanvas = mSnappingMarker->toCanvasCoordinates( match.point() ).toPoint();
97  QPoint ptGlobal = mCanvas->mapToGlobal( ptCanvas );
98  QRect rect( ptCanvas.x(), ptCanvas.y(), 1, 1 ); // area where is the tooltip valid
99  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 }
QgsPointXY point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords...
QgsPointLocator::Match match() const
Returns currently displayed snapping match.
void setMatch(const QgsPointLocator::Match &match)
Sets snapping match that should be displayed in map canvas. Invalid match hides the indicator...
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly...
void setPenWidth(int width)
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
void setCenter(const QgsPointXY &point)
bool isVisible() const
Returns whether the snapping indicator is visible.
A class for marking vertices of features using e.g.
void setIconSize(int iconSize)
QgsVectorLayer * layer() const
The vector layer where the snap occurred.
QString name
Definition: qgsmaplayer.h:67
QPointF toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
void setVisible(bool visible=true)
Sets whether the snapping indicator is visible.
void setColor(const QColor &color)
Sets the stroke color for the marker.
QgsSnapIndicator(QgsMapCanvas *canvas)
Constructs an indicator for the given map canvas.
void setIconType(int iconType)