/
qgsmaptoolcircularstringradius.cpp
181 lines (165 loc) · 5.9 KB
/
qgsmaptoolcircularstringradius.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
/***************************************************************************
qgsmaptoolcircularstringradius.h - map tool for adding circular strings
---------------------
begin : Feb 2015
copyright : (C) 2015 by Marco Hugentobler
email : marco dot hugentobler at sourcepole 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 "qgsmaptoolcircularstringradius.h"
#include "qgisapp.h"
#include "qgscircularstringv2.h"
#include "qgscompoundcurvev2.h"
#include "qgsgeometryutils.h"
#include "qgsgeometryrubberband.h"
#include "qgsmapcanvas.h"
#include "qgspointv2.h"
#include <QDoubleSpinBox>
#include <QMouseEvent>
#include <cmath>
QgsMapToolCircularStringRadius::QgsMapToolCircularStringRadius( QgsMapToolCapture* parentTool, QgsMapCanvas* canvas, CaptureMode mode )
: QgsMapToolAddCircularString( parentTool, canvas, mode ),
mTemporaryEndPointX( 0.0 ),
mTemporaryEndPointY( 0.0 ),
mRadius( 0.0 ),
mRadiusSpinBox( nullptr )
{
}
QgsMapToolCircularStringRadius::~QgsMapToolCircularStringRadius()
{
}
void QgsMapToolCircularStringRadius::deactivate()
{
deleteRadiusSpinBox();
QgsMapToolAddCircularString::deactivate();
}
void QgsMapToolCircularStringRadius::cadCanvasReleaseEvent( QgsMapMouseEvent* e )
{
QgsPointV2 mapPoint( e->mapPoint().x(), e->mapPoint().y() );
if ( e->button() == Qt::LeftButton )
{
if ( mPoints.isEmpty() )
{
mPoints.append( mapPoint );
}
else
{
if ( mPoints.size() % 2 )
{
mTemporaryEndPointX = mapPoint.x();
mTemporaryEndPointY = mapPoint.y();
//initial radius is distance( tempPoint - mPoints.last ) / 2.0
double minRadius = sqrt( QgsGeometryUtils::sqrDistance2D( mPoints.last(), QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ) ) ) / 2.0;
mRadius = minRadius + minRadius / 10.0;
QgsPointV2 result;
if ( QgsGeometryUtils::segmentMidPoint( mPoints.last(), QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ), result, mRadius, QgsPointV2( mapPoint.x(), mapPoint.y() ) ) )
{
mPoints.append( result );
createRadiusSpinBox();
if ( mRadiusSpinBox )
{
mRadiusSpinBox->setMinimum( minRadius );
}
}
}
else
{
mPoints.append( QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ) );
deleteRadiusSpinBox();
}
recalculateCircularString();
}
}
else if ( e->button() == Qt::RightButton )
{
if ( !( mPoints.size() % 2 ) )
mPoints.removeLast();
deactivate();
if ( mParentTool )
{
mParentTool->canvasReleaseEvent( e );
}
}
}
void QgsMapToolCircularStringRadius::cadCanvasMoveEvent( QgsMapMouseEvent* e )
{
if ( !mPoints.isEmpty() )
{
mLastMouseMapPos.setX( e->mapPoint().x() );
mLastMouseMapPos.setY( e->mapPoint().y() );
recalculateCircularString();
updateCenterPointRubberBand( QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ) );
}
}
void QgsMapToolCircularStringRadius::recalculateCircularString()
{
if ( mPoints.size() >= 3 )
{
QgsCircularStringV2* cString = new QgsCircularStringV2();
int rubberBandSize = mPoints.size() - ( mPoints.size() + 1 ) % 2;
cString->setPoints( mPoints.mid( 0, rubberBandSize ) );
delete mRubberBand;
mRubberBand = createGeometryRubberBand(( mode() == CapturePolygon ) ? QGis::Polygon : QGis::Line );
mRubberBand->setGeometry( cString );
mRubberBand->show();
}
QList<QgsPointV2> rubberBandPoints;
if ( !( mPoints.size() % 2 ) )
{
//recalculate midpoint on circle segment
QgsPointV2 midPoint;
if ( !QgsGeometryUtils::segmentMidPoint( mPoints.at( mPoints.size() - 2 ), QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ), midPoint, mRadius,
mLastMouseMapPos ) )
{
return;
}
mPoints.replace( mPoints.size() - 1, midPoint );
rubberBandPoints.append( mPoints.at( mPoints.size() - 2 ) );
rubberBandPoints.append( mPoints.last() );
rubberBandPoints.append( QgsPointV2( mTemporaryEndPointX, mTemporaryEndPointY ) );
}
else
{
rubberBandPoints.append( mPoints.last() );
rubberBandPoints.append( mLastMouseMapPos );
}
QgsCircularStringV2* cString = new QgsCircularStringV2();
cString->setPoints( rubberBandPoints );
delete mTempRubberBand;
mTempRubberBand = createGeometryRubberBand(( mode() == CapturePolygon ) ? QGis::Polygon : QGis::Line, true );
mTempRubberBand->setGeometry( cString );
mTempRubberBand->show();
}
void QgsMapToolCircularStringRadius::createRadiusSpinBox()
{
deleteRadiusSpinBox();
mRadiusSpinBox = new QDoubleSpinBox();
mRadiusSpinBox->setMaximum( 99999999 );
mRadiusSpinBox->setDecimals( 2 );
mRadiusSpinBox->setPrefix( tr( "Radius: " ) );
mRadiusSpinBox->setValue( mRadius );
QgisApp::instance()->addUserInputWidget( mRadiusSpinBox );
QObject::connect( mRadiusSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( updateRadiusFromSpinBox( double ) ) );
mRadiusSpinBox->setFocus( Qt::TabFocusReason );
}
void QgsMapToolCircularStringRadius::deleteRadiusSpinBox()
{
if ( !mRadiusSpinBox )
{
return;
}
QgisApp::instance()->statusBar()->removeWidget( mRadiusSpinBox );
delete mRadiusSpinBox;
mRadiusSpinBox = nullptr;
}
void QgsMapToolCircularStringRadius::updateRadiusFromSpinBox( double radius )
{
mRadius = radius;
recalculateCircularString();
}