/
qgsmaptooladdpart.cpp
203 lines (177 loc) · 5.84 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
/***************************************************************************
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 "qgsgeometry.h"
#include "qgsmapcanvas.h"
#include "qgsproject.h"
#include "qgsrubberband.h"
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#include <QMessageBox>
#include <QMouseEvent>
QgsMapToolAddPart::QgsMapToolAddPart( QgsMapCanvas* canvas )
: QgsMapToolCapture( canvas )
{
}
QgsMapToolAddPart::~QgsMapToolAddPart()
{
}
void QgsMapToolAddPart::canvasReleaseEvent( QMouseEvent * e )
{
//check if we operate on a vector layer
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
if ( !vlayer )
{
QMessageBox::information( 0,
tr( "Not a vector layer" ),
tr( "The current layer is not a vector layer" ) );
return;
}
if ( !vlayer->isEditable() )
{
QMessageBox::information( 0,
tr( "Layer not editable" ),
tr( "Cannot edit the vector layer. Use 'Toggle Editing' to make it editable." )
);
return;
}
//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() )
{
QMessageBox::critical( 0, tr( "Error. Could not add part." ), selectionErrorMsg );
stopCapturing();
return;
}
int errorCode;
switch ( mode() )
{
case CapturePoint:
{
QgsPoint layerPoint;
QgsPoint mapPoint;
if ( nextPoint( e->pos(), layerPoint, mapPoint ) != 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
int error = addVertex( e->pos() );
if ( error == 1 )
{
QgsDebugMsg( "current layer is not a vector layer" );
return;
}
else if ( error == 2 )
{
//problem with coordinate transformation
QMessageBox::information( 0,
tr( "Coordinate transform error" ),
tr( "Cannot transform the point to the layers coordinate system" ) );
return;
}
if ( e->button() == Qt::LeftButton )
{
startCapturing();
return;
}
else if ( e->button() != Qt::RightButton )
{
return;
}
if ( mode() == CapturePolygon )
{
//close polygon
closePolygon();
//avoid intersections
QgsGeometry* geom = QgsGeometry::fromPolygon( QgsPolygon() << points().toVector() );
if ( geom )
{
geom->avoidIntersections();
QgsPolygon poly = geom->asPolygon();
if ( poly.size() < 1 )
{
stopCapturing();
delete geom;
vlayer->destroyEditCommand();
return;
}
setPoints( geom->asPolygon()[0].toList() );
delete geom;
}
}
vlayer->beginEditCommand( tr( "Part added" ) );
errorCode = vlayer->addPart( points() );
stopCapturing();
}
break;
default:
Q_ASSERT( !"invalid capture mode" );
errorCode = 6;
break;
}
QString errorMessage;
switch ( errorCode )
{
case 0:
{
//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;
}
QMessageBox::critical( 0, tr( "Error, could not add part" ), errorMessage );
vlayer->destroyEditCommand();
}