@@ -64,6 +64,88 @@ extern "C" {
64
64
65
65
bool QgsGrassRegion::mRunning = false ;
66
66
67
+ /* * map tool which uses rubber band for changing grass region */
68
+ class QgsGrassRegionEdit : public QgsMapTool
69
+ {
70
+ public:
71
+ QgsGrassRegionEdit (QgsGrassRegion* reg)
72
+ : QgsMapTool(reg->mCanvas ), mRegion (reg)
73
+ {
74
+ mDraw = false ;
75
+ mRubberBand = new QRubberBand ( QRubberBand::Rectangle, mCanvas );
76
+ }
77
+
78
+ ~QgsGrassRegionEdit ()
79
+ {
80
+ delete mRubberBand ;
81
+ }
82
+
83
+ // ! mouse click in map canvas
84
+ void canvasPressEvent (QMouseEvent * event)
85
+ {
86
+ QgsPoint point = toMapCoords (event->pos ());
87
+ double x = point.x ();
88
+ double y = point.y ();
89
+
90
+ #ifdef QGISDEBUG
91
+ std::cerr << " QgsGrassRegionEdit::canvasPressEvent()" << std::endl;
92
+ #endif
93
+
94
+ if ( !mDraw ) { // first corner
95
+ mRegion ->mX = x;
96
+ mRegion ->mY = y;
97
+
98
+ mRegion ->draw ( x, y, x, y );
99
+ mDraw = true ;
100
+ } else {
101
+ mRegion ->draw ( mRegion ->mX , mRegion ->mY , x, y );
102
+ mDraw = false ;
103
+ }
104
+ mRubberBand ->show ();
105
+ }
106
+
107
+ // ! mouse movement in map canvas
108
+ void canvasMoveEvent (QMouseEvent * event)
109
+ {
110
+ QgsPoint point = toMapCoords (event->pos ());
111
+
112
+ #ifdef QGISDEBUG
113
+ std::cerr << " QgsGrassRegionEdit::canvasMoveEvent()" << std::endl;
114
+ #endif
115
+
116
+ if ( !mDraw ) return ;
117
+ mRegion ->draw ( mRegion ->mX , mRegion ->mY , point.x (), point.y () );
118
+ }
119
+
120
+ // ! called when map tool is about to get inactive
121
+ void deactivate ()
122
+ {
123
+ mRubberBand ->hide ();
124
+
125
+ QgsMapTool::deactivate ();
126
+ }
127
+
128
+ void setRegion (const QgsPoint& ul, const QgsPoint& lr)
129
+ {
130
+ QPoint qul = toCanvasCoords (ul);
131
+ QPoint qlr = toCanvasCoords (lr);
132
+ mRubberBand ->setGeometry ( QRect (qul,qlr));
133
+ }
134
+
135
+
136
+ private:
137
+ // ! Rubber band for selecting grass region
138
+ QRubberBand* mRubberBand ;
139
+
140
+ // ! Status of input from canvas
141
+ bool mDraw ;
142
+
143
+ QgsGrassRegion* mRegion ;
144
+ };
145
+
146
+
147
+
148
+
67
149
QgsGrassRegion::QgsGrassRegion ( QgsGrassPlugin *plugin, QgisApp *qgisApp, QgisIface *iface,
68
150
QWidget * parent, Qt::WFlags f )
69
151
:QDialog(parent, f), QgsGrassRegionBase ( )
@@ -80,7 +162,6 @@ QgsGrassRegion::QgsGrassRegion ( QgsGrassPlugin *plugin, QgisApp *qgisApp, Qgis
80
162
mInterface = iface;
81
163
mCanvas = mInterface ->getMapCanvas ();
82
164
restorePosition ();
83
- mDraw = false ;
84
165
mUpdatingGui = false ;
85
166
mDisplayed = false ;
86
167
@@ -131,8 +212,6 @@ QgsGrassRegion::QgsGrassRegion ( QgsGrassPlugin *plugin, QgisApp *qgisApp, Qgis
131
212
132
213
setGuiValues ();
133
214
134
- connect ( mCanvas , SIGNAL (xyClickCoordinates (QgsPoint &)), this , SLOT (mouseEventReceiverClick (QgsPoint &)));
135
- connect ( mCanvas , SIGNAL (xyCoordinates (QgsPoint &)), this , SLOT (mouseEventReceiverMove (QgsPoint &)));
136
215
connect ( mCanvas , SIGNAL (renderComplete (QPainter *)), this , SLOT (postRender (QPainter *)));
137
216
138
217
// Connect entries
@@ -145,8 +224,6 @@ QgsGrassRegion::QgsGrassRegion ( QgsGrassPlugin *plugin, QgisApp *qgisApp, Qgis
145
224
connect ( mRows , SIGNAL (textChanged (const QString &)), this , SLOT (rowsChanged (const QString &)));
146
225
connect ( mCols , SIGNAL (textChanged (const QString &)), this , SLOT (colsChanged (const QString &)));
147
226
148
- mCanvas ->setMapTool (new QgsMapToolEmitPoint (mCanvas ));
149
-
150
227
// Symbology
151
228
QPen pen = mPlugin ->regionPen ();
152
229
QPalette palette = mColorButton ->palette ();
@@ -157,7 +234,8 @@ QgsGrassRegion::QgsGrassRegion ( QgsGrassPlugin *plugin, QgisApp *qgisApp, Qgis
157
234
mWidthSpinBox ->setValue ( pen.width () );
158
235
connect ( mWidthSpinBox , SIGNAL (valueChanged (int )), this , SLOT (changeWidth ()));
159
236
160
- mRubberBand = new QRubberBand ( QRubberBand::Rectangle, mCanvas );
237
+ mRegionEdit = new QgsGrassRegionEdit (this ); // will be deleted by map canvas
238
+ mCanvas ->setMapTool (mRegionEdit );
161
239
162
240
setAttribute ( Qt::WA_DeleteOnClose );
163
241
displayRegion ();
@@ -215,7 +293,6 @@ void QgsGrassRegion::setGuiValues( bool north, bool south, bool east, bool west,
215
293
216
294
QgsGrassRegion::~QgsGrassRegion ()
217
295
{
218
- delete mRubberBand ;
219
296
mRunning = false ;
220
297
}
221
298
@@ -325,33 +402,7 @@ void QgsGrassRegion::radioChanged()
325
402
mCols ->setEnabled (false );
326
403
}
327
404
}
328
- void QgsGrassRegion::mouseEventReceiverClick ( QgsPoint & point )
329
- {
330
- #ifdef QGISDEBUG
331
- std::cerr << " QgsGrassRegion::mouseEventReceiverClick()" << std::endl;
332
- #endif
333
-
334
- if ( !mDraw ) { // first corner
335
- mX = point.x ();
336
- mY = point.y ();
337
-
338
- draw ( mX , mY , mX , mY );
339
- mDraw = true ;
340
- } else {
341
- draw ( mX , mY , point.x (), point.y () );
342
- mDraw = false ;
343
- }
344
- mRubberBand ->show ();
345
- }
346
405
347
- void QgsGrassRegion::mouseEventReceiverMove ( QgsPoint & point )
348
- {
349
- #ifdef QGISDEBUG
350
- std::cerr << " QgsGrassRegion::mouseEventReceiverMove()" << std::endl;
351
- #endif
352
- if ( !mDraw ) return ;
353
- draw ( mX , mY , point.x (), point.y () );
354
- }
355
406
356
407
void QgsGrassRegion::draw ( double x1, double y1, double x2, double y2 )
357
408
{
@@ -390,15 +441,7 @@ void QgsGrassRegion::displayRegion()
390
441
QgsPoint ul (mWindow .west , mWindow .north );
391
442
QgsPoint lr (mWindow .east , mWindow .south );
392
443
393
- QgsMapToPixel *transform = mCanvas ->getCoordinateTransform ();
394
-
395
- transform->transform ( &ul );
396
- transform->transform ( &lr );
397
-
398
- QPoint qul ( static_cast <int >(ul.x ()), static_cast <int >(ul.y ()) );
399
- QPoint qlr ( static_cast <int >(lr.x ()), static_cast <int >(lr.y ()) );
400
-
401
- mRubberBand ->setGeometry ( QRect (qul,qlr));
444
+ mRegionEdit ->setRegion (ul, lr);
402
445
403
446
mDisplayed = true ;
404
447
@@ -438,7 +481,7 @@ void QgsGrassRegion::accept()
438
481
}
439
482
440
483
saveWindowLocation ();
441
- mRubberBand -> hide ();
484
+ mQgisApp -> pan (); // change to pan tool
442
485
mRunning = false ;
443
486
close ();
444
487
// delete this;
@@ -447,7 +490,7 @@ void QgsGrassRegion::accept()
447
490
void QgsGrassRegion::reject ()
448
491
{
449
492
saveWindowLocation ();
450
- mRubberBand -> hide ();
493
+ mQgisApp -> pan (); // change to pan tool
451
494
mRunning = false ;
452
495
close ();
453
496
// delete this;
0 commit comments