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