/
qgsmaptooladvanceddigitizing.cpp
182 lines (157 loc) · 5.98 KB
/
qgsmaptooladvanceddigitizing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/***************************************************************************
qgsmaptooladvanceddigitizing.cpp - map tool with event in map coordinates
----------------------
begin : October 2014
copyright : (C) Denis Rouzaud
email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsmapmouseevent.h"
#include "qgsmaptooladvanceddigitizing.h"
#include "qgsmapcanvas.h"
#include "qgsadvanceddigitizingdockwidget.h"
#include "qgsvectorlayer.h"
#include "qgsgeometryoptions.h"
#include "qgssnaptogridcanvasitem.h"
QgsMapToolAdvancedDigitizing::QgsMapToolAdvancedDigitizing( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
: QgsMapToolEdit( canvas )
, mCadDockWidget( cadDockWidget )
{
connect( canvas, &QgsMapCanvas::currentLayerChanged, this, &QgsMapToolAdvancedDigitizing::onCurrentLayerChanged );
}
void QgsMapToolAdvancedDigitizing::canvasPressEvent( QgsMapMouseEvent *e )
{
if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
{
mCadDockWidget->applyConstraints( e ); // updates event's map point
if ( mCadDockWidget->constructionMode() )
return; // decided to eat the event and not pass it to the map tool (construction mode)
}
else if ( isAutoSnapEnabled() )
{
e->snapPoint();
}
QgsVectorLayer *layer = currentVectorLayer();
if ( mSnapToLayerGridEnabled && layer )
{
e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
}
cadCanvasPressEvent( e );
}
void QgsMapToolAdvancedDigitizing::canvasReleaseEvent( QgsMapMouseEvent *e )
{
if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
{
if ( e->button() == Qt::RightButton )
{
mCadDockWidget->clear();
}
else
{
mCadDockWidget->applyConstraints( e ); // updates event's map point
if ( mCadDockWidget->alignToSegment( e ) )
{
// Parallel or perpendicular mode and snapped to segment: do not pass the event to map tool
return;
}
mCadDockWidget->addPoint( e->mapPoint() );
mCadDockWidget->releaseLocks( false );
if ( mCadDockWidget->constructionMode() )
return; // decided to eat the event and not pass it to the map tool (construction mode)
}
}
else if ( isAutoSnapEnabled() )
{
e->snapPoint();
}
QgsVectorLayer *layer = currentVectorLayer();
if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
{
e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
}
cadCanvasReleaseEvent( e );
}
void QgsMapToolAdvancedDigitizing::canvasMoveEvent( QgsMapMouseEvent *e )
{
if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
{
mCadDockWidget->applyConstraints( e ); // updates event's map point
// perpendicular/parallel constraint
// do a soft lock when snapping to a segment
mCadDockWidget->alignToSegment( e, QgsAdvancedDigitizingDockWidget::CadConstraint::SoftLock );
mCadDockWidget->updateCadPaintItem();
}
else if ( isAutoSnapEnabled() )
{
e->snapPoint();
}
QgsVectorLayer *layer = currentVectorLayer();
if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
{
e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
mSnapToGridCanvasItem->setPoint( e->mapPoint() );
}
cadCanvasMoveEvent( e );
}
void QgsMapToolAdvancedDigitizing::activate()
{
QgsMapToolEdit::activate();
connect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChanged, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
mCadDockWidget->enable();
mSnapToGridCanvasItem = new QgsSnapToGridCanvasItem( mCanvas );
QgsVectorLayer *layer = currentVectorLayer();
if ( layer )
{
mSnapToGridCanvasItem->setCrs( currentVectorLayer()->crs() );
mSnapToGridCanvasItem->setPrecision( currentVectorLayer()->geometryOptions()->geometryPrecision() );
}
mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
}
void QgsMapToolAdvancedDigitizing::deactivate()
{
QgsMapToolEdit::deactivate();
disconnect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChanged, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
mCadDockWidget->disable();
delete mSnapToGridCanvasItem;
mSnapToGridCanvasItem = nullptr;
}
void QgsMapToolAdvancedDigitizing::cadPointChanged( const QgsPointXY &point )
{
Q_UNUSED( point );
QMouseEvent *ev = new QMouseEvent( QEvent::MouseMove, mCanvas->mouseLastXY(), Qt::NoButton, Qt::NoButton, Qt::NoModifier );
qApp->postEvent( mCanvas->viewport(), ev ); // event queue will delete the event when processed
}
void QgsMapToolAdvancedDigitizing::onCurrentLayerChanged()
{
if ( mSnapToGridCanvasItem )
{
QgsVectorLayer *layer = currentVectorLayer();
if ( layer && mSnapToLayerGridEnabled )
{
mSnapToGridCanvasItem->setPrecision( layer->geometryOptions()->geometryPrecision() );
mSnapToGridCanvasItem->setCrs( layer->crs() );
}
if ( !layer )
mSnapToGridCanvasItem->setEnabled( false );
else
mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
}
}
bool QgsMapToolAdvancedDigitizing::snapToLayerGridEnabled() const
{
return mSnapToLayerGridEnabled;
}
void QgsMapToolAdvancedDigitizing::setSnapToLayerGridEnabled( bool snapToGridEnabled )
{
mSnapToLayerGridEnabled = snapToGridEnabled;
if ( mSnapToGridCanvasItem )
{
mSnapToGridCanvasItem->setEnabled( snapToGridEnabled );
}
}