/
qgsmaptooladdpart.cpp
262 lines (225 loc) · 6.96 KB
/
qgsmaptooladdpart.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/***************************************************************************
qgsmaptooladdpart.cpp - map tool to add new parts to multipart features
-----------------------
begin : Mai 2007
copyright : (C) 2007 by Marco Hugentobler
email : marco dot hugentobler at karto dot baug dot ethz dot ch
***************************************************************************
* *
* 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 "qgsmaptooladdpart.h"
#include "qgscurvepolygonv2.h"
#include "qgsgeometry.h"
#include "qgslinestringv2.h"
#include "qgsmapcanvas.h"
#include "qgsproject.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#include "qgisapp.h"
#include <QMouseEvent>
QgsMapToolAddPart::QgsMapToolAddPart( QgsMapCanvas* canvas )
: QgsMapToolCapture( canvas, QgisApp::instance()->cadDockWidget() )
{
mToolName = tr( "Add part" );
}
QgsMapToolAddPart::~QgsMapToolAddPart()
{
}
void QgsMapToolAddPart::canvasReleaseEvent( QgsMapMouseEvent * e )
{
if ( checkSelection() )
{
QgsMapToolAdvancedDigitizing::canvasReleaseEvent( e );
}
else
{
cadDockWidget()->clear();
}
}
void QgsMapToolAddPart::cadCanvasReleaseEvent( QgsMapMouseEvent * e )
{
//check if we operate on a vector layer
QgsVectorLayer *vlayer = currentVectorLayer();
if ( !vlayer )
{
notifyNotVectorLayer();
return;
}
if ( !vlayer->isEditable() )
{
notifyNotEditableLayer();
return;
}
if ( !checkSelection() )
{
stopCapturing();
return;
}
int errorCode = 0;
switch ( mode() )
{
case CapturePoint:
{
QgsPoint layerPoint;
QgsPoint mapPoint = e->mapPoint();
if ( nextPoint( mapPoint, layerPoint ) != 0 )
{
QgsDebugMsg( "nextPoint failed" );
return;
}
vlayer->beginEditCommand( tr( "Part added" ) );
errorCode = vlayer->addPart( QList<QgsPoint>() << layerPoint );
}
break;
case CaptureLine:
case CapturePolygon:
{
//add point to list and to rubber band
if ( e->button() == Qt::LeftButton )
{
int error = addVertex( e->mapPoint() );
if ( error == 1 )
{
QgsDebugMsg( "current layer is not a vector layer" );
return;
}
else if ( error == 2 )
{
//problem with coordinate transformation
emit messageEmitted( tr( "Coordinate transform error. Cannot transform the point to the layers coordinate system" ), QgsMessageBar::WARNING );
return;
}
startCapturing();
return;
}
else if ( e->button() != Qt::RightButton )
{
deleteTempRubberBand();
return;
}
if ( !isCapturing() )
return;
if ( mode() == CapturePolygon )
{
closePolygon();
}
//does compoundcurve contain circular strings?
//does provider support circular strings?
bool hasCurvedSegments = captureCurve()->hasCurvedSegments();
bool providerSupportsCurvedSegments = vlayer->dataProvider()->capabilities() & QgsVectorDataProvider::CircularGeometries;
QgsCurveV2* curveToAdd = nullptr;
if ( hasCurvedSegments && providerSupportsCurvedSegments )
{
curveToAdd = captureCurve()->clone();
}
else
{
curveToAdd = captureCurve()->curveToLine();
}
vlayer->beginEditCommand( tr( "Part added" ) );
if ( mode() == CapturePolygon )
{
//avoid intersections
QgsCurvePolygonV2* cp = new QgsCurvePolygonV2();
cp->setExteriorRing( curveToAdd );
QgsGeometry* geom = new QgsGeometry( cp );
geom->avoidIntersections();
const QgsCurvePolygonV2* cpGeom = dynamic_cast<const QgsCurvePolygonV2*>( geom->geometry() );
if ( !cpGeom )
{
stopCapturing();
delete geom;
vlayer->destroyEditCommand();
return;
}
errorCode = vlayer->addPart( cpGeom->exteriorRing()->clone() );
delete geom;
}
else
{
errorCode = vlayer->addPart( curveToAdd );
}
stopCapturing();
}
break;
default:
Q_ASSERT( !"invalid capture mode" );
errorCode = 6;
break;
}
QString errorMessage;
switch ( errorCode )
{
case 0:
{
// remove previous message
emit messageDiscarded();
//add points to other features to keep topology up-to-date
int topologicalEditing = QgsProject::instance()->readNumEntry( "Digitizing", "/TopologicalEditing", 0 );
if ( topologicalEditing )
{
addTopologicalPoints( points() );
}
vlayer->endEditCommand();
mCanvas->refresh();
return;
}
case 1:
errorMessage = tr( "Selected feature is not multi part." );
break;
case 2:
errorMessage = tr( "New part's geometry is not valid." );
break;
case 3:
errorMessage = tr( "New polygon ring not disjoint with existing polygons." );
break;
case 4:
errorMessage = tr( "No feature selected. Please select a feature with the selection tool or in the attribute table" );
break;
case 5:
errorMessage = tr( "Several features are selected. Please select only one feature to which an island should be added." );
break;
case 6:
errorMessage = tr( "Selected geometry could not be found" );
break;
}
emit messageEmitted( errorMessage, QgsMessageBar::WARNING );
vlayer->destroyEditCommand();
}
void QgsMapToolAddPart::activate()
{
checkSelection();
QgsMapToolCapture::activate();
}
bool QgsMapToolAddPart::checkSelection()
{
//check if we operate on a vector layer
QgsVectorLayer *vlayer = currentVectorLayer();
if ( !vlayer )
{
notifyNotVectorLayer();
return false;
}
//inform user at the begin of the digitising action that the island tool only works if exactly one feature is selected
int nSelectedFeatures = vlayer->selectedFeatureCount();
QString selectionErrorMsg;
if ( nSelectedFeatures < 1 )
{
selectionErrorMsg = tr( "No feature selected. Please select a feature with the selection tool or in the attribute table" );
}
else if ( nSelectedFeatures > 1 )
{
selectionErrorMsg = tr( "Several features are selected. Please select only one feature to which an part should be added." );
}
if ( !selectionErrorMsg.isEmpty() )
{
emit messageEmitted( tr( "Could not add part. %1" ).arg( selectionErrorMsg ), QgsMessageBar::WARNING );
}
return selectionErrorMsg.isEmpty();
}