Skip to content

Commit f00bbe4

Browse files
author
wonder
committedApr 4, 2006
Created map tool for changing grass region, so QgsMapToolEmitPoint is not used anymore and can be finally removed.
git-svn-id: http://svn.osgeo.org/qgis/trunk@5168 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent cf776ee commit f00bbe4

File tree

2 files changed

+91
-48
lines changed

2 files changed

+91
-48
lines changed
 

‎src/plugins/grass/qgsgrassregion.cpp

Lines changed: 87 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,88 @@ extern "C" {
6464

6565
bool QgsGrassRegion::mRunning = false;
6666

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+
67149
QgsGrassRegion::QgsGrassRegion ( QgsGrassPlugin *plugin, QgisApp *qgisApp, QgisIface *iface,
68150
QWidget * parent, Qt::WFlags f )
69151
:QDialog(parent, f), QgsGrassRegionBase ( )
@@ -80,7 +162,6 @@ QgsGrassRegion::QgsGrassRegion ( QgsGrassPlugin *plugin, QgisApp *qgisApp, Qgis
80162
mInterface = iface;
81163
mCanvas = mInterface->getMapCanvas();
82164
restorePosition();
83-
mDraw = false;
84165
mUpdatingGui = false;
85166
mDisplayed = false;
86167

@@ -131,8 +212,6 @@ QgsGrassRegion::QgsGrassRegion ( QgsGrassPlugin *plugin, QgisApp *qgisApp, Qgis
131212

132213
setGuiValues();
133214

134-
connect( mCanvas, SIGNAL(xyClickCoordinates(QgsPoint &)), this, SLOT(mouseEventReceiverClick(QgsPoint &)));
135-
connect( mCanvas, SIGNAL(xyCoordinates(QgsPoint &)), this, SLOT(mouseEventReceiverMove(QgsPoint &)));
136215
connect( mCanvas, SIGNAL(renderComplete(QPainter *)), this, SLOT(postRender(QPainter *)));
137216

138217
// Connect entries
@@ -145,8 +224,6 @@ QgsGrassRegion::QgsGrassRegion ( QgsGrassPlugin *plugin, QgisApp *qgisApp, Qgis
145224
connect( mRows, SIGNAL(textChanged(const QString &)), this, SLOT(rowsChanged(const QString &)));
146225
connect( mCols, SIGNAL(textChanged(const QString &)), this, SLOT(colsChanged(const QString &)));
147226

148-
mCanvas->setMapTool(new QgsMapToolEmitPoint(mCanvas));
149-
150227
// Symbology
151228
QPen pen = mPlugin->regionPen();
152229
QPalette palette = mColorButton->palette();
@@ -157,7 +234,8 @@ QgsGrassRegion::QgsGrassRegion ( QgsGrassPlugin *plugin, QgisApp *qgisApp, Qgis
157234
mWidthSpinBox->setValue( pen.width() );
158235
connect( mWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeWidth()));
159236

160-
mRubberBand = new QRubberBand ( QRubberBand::Rectangle, mCanvas );
237+
mRegionEdit = new QgsGrassRegionEdit(this); // will be deleted by map canvas
238+
mCanvas->setMapTool(mRegionEdit);
161239

162240
setAttribute ( Qt::WA_DeleteOnClose );
163241
displayRegion();
@@ -215,7 +293,6 @@ void QgsGrassRegion::setGuiValues( bool north, bool south, bool east, bool west,
215293

216294
QgsGrassRegion::~QgsGrassRegion ()
217295
{
218-
delete mRubberBand;
219296
mRunning = false;
220297
}
221298

@@ -325,33 +402,7 @@ void QgsGrassRegion::radioChanged()
325402
mCols->setEnabled(false);
326403
}
327404
}
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-
}
346405

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-
}
355406

356407
void QgsGrassRegion::draw ( double x1, double y1, double x2, double y2 )
357408
{
@@ -390,15 +441,7 @@ void QgsGrassRegion::displayRegion()
390441
QgsPoint ul(mWindow.west, mWindow.north);
391442
QgsPoint lr(mWindow.east, mWindow.south);
392443

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);
402445

403446
mDisplayed = true;
404447

@@ -438,7 +481,7 @@ void QgsGrassRegion::accept()
438481
}
439482

440483
saveWindowLocation();
441-
mRubberBand->hide();
484+
mQgisApp->pan(); // change to pan tool
442485
mRunning = false;
443486
close();
444487
//delete this;
@@ -447,7 +490,7 @@ void QgsGrassRegion::accept()
447490
void QgsGrassRegion::reject()
448491
{
449492
saveWindowLocation();
450-
mRubberBand->hide();
493+
mQgisApp->pan(); // change to pan tool
451494
mRunning = false;
452495
close();
453496
//delete this;

‎src/plugins/grass/qgsgrassregion.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class QgisIface;
2525
class QgsMapCanvas;
2626
class Q3ButtonGroup;
2727
class QgsPoint;
28+
class QgsGrassRegionEdit;
2829
#include "ui_qgsgrassregionbase.h"
2930
#include <QDialog>
3031
#include <QRubberBand>
@@ -119,9 +120,6 @@ public slots:
119120
//! Draw region
120121
void draw ( double x1, double y1, double x2, double y2 );
121122

122-
//! Status of input from canvas
123-
bool mDraw;
124-
125123
//! First corner coordinates
126124
double mX;
127125
double mY;
@@ -141,7 +139,9 @@ public slots:
141139
// Format N, S, E, W value
142140
QString formatEdge ( double v );
143141

144-
QRubberBand *mRubberBand;
142+
QgsGrassRegionEdit* mRegionEdit;
143+
144+
friend class QgsGrassRegionEdit;
145145
};
146146

147147
#endif // QGSGRASSREGION_H

0 commit comments

Comments
 (0)
Please sign in to comment.