/
qgsmaptoolreshape.cpp
151 lines (133 loc) · 4.81 KB
/
qgsmaptoolreshape.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
/***************************************************************************
qgsmaptoolreshape.cpp
---------------------------
begin : Juli 2009
copyright : (C) 2009 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 "qgsmaptoolreshape.h"
#include "qgsfeatureiterator.h"
#include "qgsgeometry.h"
#include "qgslinestring.h"
#include "qgsmapcanvas.h"
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgisapp.h"
#include <QMouseEvent>
QgsMapToolReshape::QgsMapToolReshape( QgsMapCanvas *canvas )
: QgsMapToolCapture( canvas, QgisApp::instance()->cadDockWidget(), QgsMapToolCapture::CaptureLine )
{
}
QgsMapToolReshape::~QgsMapToolReshape()
{
}
void QgsMapToolReshape::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
{
//check if we operate on a vector layer //todo: move this to a function in parent class to avoid duplication
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
if ( !vlayer )
{
notifyNotVectorLayer();
return;
}
if ( !vlayer->isEditable() )
{
notifyNotEditableLayer();
return;
}
//add point to list and to rubber band
if ( e->button() == Qt::LeftButton )
{
int error = addVertex( e->mapPoint(), e->mapPointMatch() );
if ( error == 1 )
{
//current layer is not a vector layer
return;
}
else if ( error == 2 )
{
//problem with coordinate transformation
emit messageEmitted( tr( "Cannot transform the point to the layers coordinate system" ), QgsMessageBar::WARNING );
return;
}
startCapturing();
}
else if ( e->button() == Qt::RightButton )
{
deleteTempRubberBand();
//find out bounding box of mCaptureList
if ( size() < 1 )
{
stopCapturing();
return;
}
QgsPointXY firstPoint = points().at( 0 );
QgsRectangle bbox( firstPoint.x(), firstPoint.y(), firstPoint.x(), firstPoint.y() );
for ( int i = 1; i < size(); ++i )
{
bbox.combineExtentWith( points().at( i ).x(), points().at( i ).y() );
}
QgsLineString reshapeLineString( points() );
if ( QgsWkbTypes::hasZ( vlayer->wkbType() ) )
reshapeLineString.addZValue( defaultZValue() );
//query all the features that intersect bounding box of capture line
QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( bbox ).setSubsetOfAttributes( QgsAttributeList() ) );
QgsFeature f;
int reshapeReturn;
bool reshapeDone = false;
vlayer->beginEditCommand( tr( "Reshape" ) );
while ( fit.nextFeature( f ) )
{
//query geometry
//call geometry->reshape(mCaptureList)
//register changed geometry in vector layer
QgsGeometry geom = f.geometry();
if ( !geom.isNull() )
{
reshapeReturn = geom.reshapeGeometry( reshapeLineString );
if ( reshapeReturn == 0 )
{
//avoid intersections on polygon layers
if ( vlayer->geometryType() == QgsWkbTypes::PolygonGeometry )
{
//ignore all current layer features as they should be reshaped too
QHash<QgsVectorLayer *, QSet<QgsFeatureId> > ignoreFeatures;
ignoreFeatures.insert( vlayer, vlayer->allFeatureIds() );
if ( geom.avoidIntersections( QgsProject::instance()->avoidIntersectionsLayers(), ignoreFeatures ) != 0 )
{
emit messageEmitted( tr( "An error was reported during intersection removal" ), QgsMessageBar::CRITICAL );
vlayer->destroyEditCommand();
stopCapturing();
return;
}
if ( geom.isEmpty() ) //intersection removal might have removed the whole geometry
{
emit messageEmitted( tr( "The feature cannot be reshaped because the resulting geometry is empty" ), QgsMessageBar::CRITICAL );
vlayer->destroyEditCommand();
stopCapturing();
return;
}
}
vlayer->changeGeometry( f.id(), geom );
reshapeDone = true;
}
}
}
if ( reshapeDone )
{
vlayer->endEditCommand();
}
else
{
vlayer->destroyEditCommand();
}
stopCapturing();
}
}