13
13
* (at your option) any later version. *
14
14
* *
15
15
***************************************************************************/
16
+ /* $Id$ */
17
+
16
18
#include " qgsmeasure.h"
17
19
18
20
#include " qgscontexthelp.h"
19
21
#include " qgsdistancearea.h"
20
22
#include " qgsmapcanvas.h"
23
+ #include " qgsmaptopixel.h"
24
+ #include " qgsrubberband.h"
21
25
22
26
#include < QSettings>
23
27
#include < iostream>
@@ -27,14 +31,11 @@ QgsMeasure::QgsMeasure(bool measureArea, QgsMapCanvas *mc, QWidget *parent, cons
27
31
: QWidget(parent, name, f)
28
32
{
29
33
setupUi (this );
30
- connect (btnHelp, SIGNAL (clicked ()), this , SLOT (showHelp ()));
31
34
connect (mRestartButton , SIGNAL (clicked ()), this , SLOT (restart ()));
32
35
connect (mCloseButton , SIGNAL (clicked ()), this , SLOT (close ()));
33
36
34
37
mMeasureArea = measureArea;
35
38
mMapCanvas = mc;
36
- mDynamic = false ;
37
- mPixmap = mMapCanvas ->canvasPixmap ();
38
39
mTotal = 0 .;
39
40
40
41
mTable ->setLeftMargin (0 ); // hide row labels
@@ -58,45 +59,40 @@ QgsMeasure::QgsMeasure(bool measureArea, QgsMapCanvas *mc, QWidget *parent, cons
58
59
59
60
updateUi ();
60
61
61
- connect ( mMapCanvas , SIGNAL (renderComplete (QPainter*)), this , SLOT (draw (QPainter* )) );
62
+ connect ( mMapCanvas , SIGNAL (renderComplete (QPainter*)), this , SLOT (mapCanvasChanged ( )) );
62
63
restorePosition ();
63
64
64
65
mCalc = new QgsDistanceArea;
65
-
66
+
67
+ mRubberBand = new QgsRubberBand (mMapCanvas , mMeasureArea );
68
+ mRubberBand ->show ();
66
69
}
67
70
68
71
69
72
void QgsMeasure::setMeasureArea (bool measureArea)
70
73
{
71
74
saveWindowLocation ();
72
- restart ();
73
75
mMeasureArea = measureArea;
74
- updateUi ();
76
+ restart ();
75
77
restorePosition ();
76
78
}
77
79
78
80
79
81
QgsMeasure::~QgsMeasure ()
80
82
{
81
83
delete mCalc ;
84
+ delete mRubberBand ;
82
85
}
83
86
84
87
void QgsMeasure::restart (void )
85
88
{
86
- // Delete old line
87
- drawLine ();
88
-
89
89
mPoints .resize (0 );
90
90
mTable ->setNumRows (0 );
91
91
mTotal = 0 .;
92
92
93
93
updateUi ();
94
-
95
- if ( mDynamic ) {
96
- drawDynamicLine ();
97
- mDynamic = false ;
98
- }
99
-
94
+
95
+ mRubberBand ->reset (mMeasureArea );
100
96
}
101
97
102
98
void QgsMeasure::addPoint (QgsPoint &point)
@@ -105,15 +101,6 @@ void QgsMeasure::addPoint(QgsPoint &point)
105
101
std::cout << " QgsMeasure::addPoint" << point.x () << " , " << point.y () << std::endl;
106
102
#endif
107
103
108
- // Delete dynamic
109
- if ( mDynamic ) {
110
- drawDynamicLine ();
111
- mDynamic = false ;
112
- }
113
-
114
- // Delete old line
115
- drawLine (true );
116
-
117
104
// don't add points with the same coordinates
118
105
if (mPoints .size () > 0 && point == mPoints [0 ])
119
106
return ;
@@ -153,8 +140,9 @@ void QgsMeasure::addPoint(QgsPoint &point)
153
140
mTable ->ensureCellVisible (row,0 );
154
141
}
155
142
156
- // Draw new line
157
- drawLine ();
143
+ QgsMapToPixel *trans = mMapCanvas ->getCoordinateTransform ();
144
+ QgsPoint ppnt = trans->transform (point);
145
+ mRubberBand ->addPoint (QPoint (int (ppnt.x ()), int (ppnt.y ())));
158
146
}
159
147
160
148
void QgsMeasure::mousePress (QgsPoint &point)
@@ -166,29 +154,6 @@ void QgsMeasure::mousePress(QgsPoint &point)
166
154
}
167
155
168
156
mouseMove (point);
169
-
170
- if (mMeasureArea && mPoints .size () > 2 )
171
- {
172
- // delete old line which connect 1. and last point
173
-
174
- // TODO: Qt4 uses "QRubberBand"s - need to refactor.
175
- #if QT_VERSION < 0x040000
176
- QPainter p;
177
- p.begin (mPixmap );
178
- QPen pen (Qt::gray, 2 );
179
- p.setPen (pen);
180
- p.setRasterOp (Qt::XorROP);
181
-
182
- QgsMapToPixel *trans = mMapCanvas ->getCoordinateTransform ();
183
- QgsPoint ppnt = trans->transform (mPoints [mPoints .size ()-1 ]);
184
- p.moveTo (static_cast <int >(ppnt.x ()), static_cast <int >(ppnt.y ()));
185
- ppnt = trans->transform (mPoints [0 ]);
186
- p.lineTo (static_cast <int >(ppnt.x ()), static_cast <int >(ppnt.y ()));
187
- p.end ();
188
- #endif
189
- mMapCanvas ->repaint (false );
190
- }
191
-
192
157
}
193
158
194
159
void QgsMeasure::mouseMove (QgsPoint &point)
@@ -197,92 +162,24 @@ void QgsMeasure::mouseMove(QgsPoint &point)
197
162
// std::cout << "QgsMeasure::mouseMove" << point.x() << ", " << point.y() << std::endl;
198
163
#endif
199
164
200
- if ( mDynamic ) {
201
- drawDynamicLine (); // delete old
202
- }
203
-
204
- if ( mPoints .size () > 0 ) {
205
- mDynamicPoints [0 ] = mPoints [mPoints .size ()-1 ];
206
- mDynamicPoints [1 ] = point;
207
- drawDynamicLine ();
208
- mDynamic = true ;
209
- }
165
+ QgsMapToPixel *trans = mMapCanvas ->getCoordinateTransform ();
166
+ QgsPoint ppnt = trans->transform (point);
167
+ mRubberBand ->movePoint (QPoint (int (ppnt.x ()), int (ppnt.y ())));
210
168
}
211
169
212
- void QgsMeasure::draw (QPainter *p )
170
+ void QgsMeasure::mapCanvasChanged ( )
213
171
{
214
172
#ifdef QGISDEBUG
215
- std::cout << " QgsMeasure::draw " << std::endl;
173
+ std::cout << " QgsMeasure::mapCanvasChanged " << std::endl;
216
174
#endif
217
-
218
- drawLine ();
219
- mDynamic = false ;
220
- }
221
-
222
- void QgsMeasure::drawLine (bool erase)
223
- {
224
- #ifdef QGISDEBUG
225
- std::cout << " QgsMeasure::drawLine" << std::endl;
226
- #endif
227
-
228
- // TODO: Qt4 uses "QRubberBand"s - need to refactor.
229
- #if QT_VERSION < 0x040000
230
- QPainter p;
231
- p.begin (mPixmap );
232
- QPen pen (Qt::gray, 2 );
233
- p.setPen (pen);
234
- p.setRasterOp (Qt::XorROP);
235
-
236
- QgsMapToPixel *trans = mMapCanvas ->getCoordinateTransform ();
237
- for ( int i = 0 ; i < mPoints .size (); i++ ) {
238
- QgsPoint ppnt = trans->transform (mPoints [i]);
239
- if ( i == 0 ) {
240
- p.moveTo (static_cast <int >(ppnt.x ()), static_cast <int >(ppnt.y ()));
241
- } else {
242
- p.lineTo (static_cast <int >(ppnt.x ()), static_cast <int >(ppnt.y ()));
243
- }
244
- }
245
-
246
- if (!erase && mMeasureArea && mPoints .size () > 2 ) // draw the last point of the polygon
247
- {
248
- QgsPoint ppnt = trans->transform (mPoints [0 ]);
249
- p.lineTo (static_cast <int >(ppnt.x ()), static_cast <int >(ppnt.y ()));
250
- }
251
-
252
- p.end ();
253
- #endif
254
- mMapCanvas ->repaint (false );
255
- }
256
-
257
- void QgsMeasure::drawDynamicLine ( void )
258
- {
259
- #ifdef QGISDEBUG
260
- // std::cout << "QgsMeasure::drawDynamicLine" << std::endl;
261
- #endif
262
-
263
- // TODO: Qt4 uses "QRubberBand"s and "QPainterPath"s - need to refactor.
264
- #if QT_VERSION < 0x040000
265
- QPainter p;
266
- p.begin (mPixmap );
267
- QPen pen (Qt::gray, 2 );
268
- p.setPen (pen);
269
- p.setRasterOp (Qt::XorROP);
270
-
271
- QgsMapToPixel *trans = mMapCanvas ->getCoordinateTransform ();
272
- QgsPoint ppnt = trans->transform (mDynamicPoints [0 ]);
273
- p.moveTo (static_cast <int >(ppnt.x ()), static_cast <int >(ppnt.y ()));
274
- QgsPoint ppnt2 = trans->transform (mDynamicPoints [1 ]);
275
- p.lineTo (static_cast <int >(ppnt2.x ()), static_cast <int >(ppnt2.y ()));
276
-
277
- if (mMeasureArea && mPoints .size () >= 2 )
278
- {
279
- ppnt = trans->transform (mPoints [0 ]);
280
- p.lineTo (static_cast <int >(ppnt.x ()), static_cast <int >(ppnt.y ()));
281
- }
282
-
283
- p.end ();
284
- #endif
285
- mMapCanvas ->repaint (false );
175
+ mRubberBand ->setGeometry (mMapCanvas ->rect ());
176
+ mRubberBand ->reset (mMeasureArea );
177
+ QgsMapToPixel *trans = mMapCanvas ->getCoordinateTransform ();
178
+ for (std::vector<QgsPoint>::iterator it = mPoints .begin (); it != mPoints .end (); ++it)
179
+ {
180
+ QgsPoint ppnt = trans->transform (*it);
181
+ mRubberBand ->addPoint (QPoint (int (ppnt.x ()), int (ppnt.y ())));
182
+ }
286
183
}
287
184
288
185
void QgsMeasure::close (void )
@@ -331,7 +228,7 @@ void QgsMeasure::saveWindowLocation()
331
228
settings.writeEntry (" /Windows/Measure/h" , s.height ());
332
229
}
333
230
334
- void QgsMeasure::showHelp ()
231
+ void QgsMeasure::on_btnHelp_clicked ()
335
232
{
336
233
QgsContextHelp::run (context_id);
337
234
}
0 commit comments