Skip to content

Commit 7e367f3

Browse files
author
wonder
committedApr 23, 2007
Solved problems with destruction of map tools - canvas mustn't be deleted before map tool,
however in Python the destruction had to be done explicitly to acheive right order. New solution is that QgsMapTool is derived from QObject and on construction, QgsMapCanvas is set as its parent. This way canvas is always deleted after deletion of map tools. Also, because of this, I've divided QgsMeasure to QgsMeasureTool and QgsMeasureDialog (problem with 2 parents with QObject as a base). git-svn-id: http://svn.osgeo.org/qgis/trunk@6908 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 4cfcc0d commit 7e367f3

12 files changed

+608
-489
lines changed
 

‎python/gui/qgsmaptool.sip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
class QgsMapTool
2+
class QgsMapTool : QObject
33
{
44
%TypeHeaderCode
55
#include <qgsmaptool.h>
@@ -54,7 +54,7 @@ class QgsMapTool
5454
protected:
5555

5656
//! constructor takes map canvas as a parameter
57-
QgsMapTool(QgsMapCanvas* canvas);
57+
QgsMapTool(QgsMapCanvas* canvas /TransferThis/);
5858

5959
//! transformation from screen coordinates to map coordinates
6060
QgsPoint toMapCoords(const QPoint& point);

‎src/app/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ qgsmaptoolidentify.cpp
3131
qgsmaptoolselect.cpp
3232
qgsmaptoolvertexedit.cpp
3333
qgsmarkerdialog.cpp
34-
qgsmeasure.cpp
34+
qgsmeasuredialog.cpp
35+
qgsmeasuretool.cpp
3536
qgsnewhttpconnection.cpp
3637
qgsnumericsortlistviewitem.cpp
3738
qgsoptions.cpp
@@ -100,7 +101,7 @@ qgsludialog.h
100101
qgsmaplayerinterface.h
101102
qgsmaptoolidentify.h
102103
qgsmarkerdialog.h
103-
qgsmeasure.h
104+
qgsmeasuredialog.h
104105
qgsnewhttpconnection.h
105106
qgsoptions.h
106107
qgspastetransformations.h

‎src/app/qgisapp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
#include "qgsmaptoolselect.h"
151151
#include "qgsmaptoolvertexedit.h"
152152
#include "qgsmaptoolzoom.h"
153-
#include "qgsmeasure.h"
153+
#include "qgsmeasuretool.h"
154154

155155
//
156156
// Conditional Includes
@@ -1203,9 +1203,9 @@ void QgisApp::createCanvas()
12031203
mMapTools.mPan->setAction(mActionPan);
12041204
mMapTools.mIdentify = new QgsMapToolIdentify(mMapCanvas);
12051205
mMapTools.mIdentify->setAction(mActionIdentify);
1206-
mMapTools.mMeasureDist = new QgsMeasure(FALSE /* area */, mMapCanvas);
1206+
mMapTools.mMeasureDist = new QgsMeasureTool(mMapCanvas, FALSE /* area */);
12071207
mMapTools.mMeasureDist->setAction(mActionMeasure);
1208-
mMapTools.mMeasureArea = new QgsMeasure(TRUE /* area */, mMapCanvas);
1208+
mMapTools.mMeasureArea = new QgsMeasureTool(mMapCanvas, TRUE /* area */);
12091209
mMapTools.mMeasureArea->setAction(mActionMeasureArea);
12101210
mMapTools.mCapturePoint = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CapturePoint);
12111211
mMapTools.mCapturePoint->setAction(mActionCapturePoint);

‎src/app/qgsmaptoolidentify.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class QgsVectorLayer;
3737
- for vector layers shows feature attributes within search radius
3838
(allows to edit values when vector layer is in editing mode)
3939
*/
40-
class QgsMapToolIdentify : public QObject, public QgsMapTool
40+
class QgsMapToolIdentify : public QgsMapTool
4141
{
4242
Q_OBJECT;
4343

‎src/app/qgsmeasure.cpp

Lines changed: 0 additions & 406 deletions
This file was deleted.

‎src/app/qgsmeasuredialog.cpp

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
/***************************************************************************
2+
qgsmeasure.h
3+
------------------
4+
begin : March 2005
5+
copyright : (C) 2005 by Radim Blazek
6+
email : blazek@itc.it
7+
***************************************************************************/
8+
/***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
/* $Id$ */
17+
18+
#include "qgsmeasuredialog.h"
19+
#include "qgsmeasuretool.h"
20+
21+
#include "qgscontexthelp.h"
22+
#include "qgsdistancearea.h"
23+
#include "qgsmapcanvas.h"
24+
#include "qgsmaprender.h"
25+
#include "qgsspatialrefsys.h"
26+
27+
#include <QSettings>
28+
#include <QLocale>
29+
30+
31+
QgsMeasureDialog::QgsMeasureDialog(QgsMeasureTool* tool, Qt::WFlags f)
32+
: QDialog(tool->canvas()->topLevelWidget(), f), mTool(tool)
33+
{
34+
setupUi(this);
35+
#ifdef Q_WS_MAC
36+
// Mac buttons are larger than X11 and require a larger minimum width to be drawn correctly
37+
frame4->setMinimumSize(QSize(224, 0));
38+
#endif
39+
connect(mRestartButton, SIGNAL(clicked()), this, SLOT(restart()));
40+
connect(mCloseButton, SIGNAL(clicked()), this, SLOT(close()));
41+
42+
mMeasureArea = tool->measureArea();
43+
mTotal = 0.;
44+
45+
mTable->setLeftMargin(0); // hide row labels
46+
47+
// Set one cell row where to update current distance
48+
// If measuring area, the table doesn't get shown
49+
mTable->setNumRows(1);
50+
mTable->setText(0, 0, QString::number(0, 'f',1));
51+
52+
//mTable->horizontalHeader()->setLabel( 0, tr("Segments (in meters)") );
53+
//mTable->horizontalHeader()->setLabel( 1, tr("Total") );
54+
//mTable->horizontalHeader()->setLabel( 2, tr("Azimuth") );
55+
56+
mTable->setColumnStretchable ( 0, true );
57+
//mTable->setColumnStretchable ( 1, true );
58+
//mTable->setColumnStretchable ( 2, true );
59+
60+
updateUi();
61+
}
62+
63+
64+
void QgsMeasureDialog::restart()
65+
{
66+
mTool->restart();
67+
68+
// Set one cell row where to update current distance
69+
// If measuring area, the table doesn't get shown
70+
mTable->setNumRows(1);
71+
mTable->setText(0, 0, QString::number(0, 'f',1));
72+
mTotal = 0.;
73+
74+
updateUi();
75+
}
76+
77+
78+
void QgsMeasureDialog::mousePress(QgsPoint &point)
79+
{
80+
if (mTool->points().size() == 0)
81+
{
82+
addPoint(point);
83+
this->show();
84+
}
85+
raise();
86+
87+
mouseMove(point);
88+
}
89+
90+
void QgsMeasureDialog::mouseMove(QgsPoint &point)
91+
{
92+
// show current distance/area while moving the point
93+
// by creating a temporary copy of point array
94+
// and adding moving point at the end
95+
QList<QgsPoint> tmpPoints = mTool->points();
96+
tmpPoints.append(point);
97+
if (mMeasureArea && tmpPoints.size() > 2)
98+
{
99+
double area = mTool->canvas()->mapRender()->distArea()->measurePolygon(tmpPoints);
100+
editTotal->setText(formatArea(area));
101+
}
102+
else if (!mMeasureArea && tmpPoints.size() > 1)
103+
{
104+
int last = tmpPoints.size()-2;
105+
QgsPoint p1 = tmpPoints[last], p2 = tmpPoints[last+1];
106+
107+
double d = mTool->canvas()->mapRender()->distArea()->measureLine(p1,p2);
108+
//mTable->setText(last, 0, QString::number(d, 'f',1));
109+
mTable->setText(last, 0, QLocale::system().toString(d, 'f', 2));
110+
editTotal->setText(formatDistance(mTotal + d));
111+
}
112+
}
113+
114+
void QgsMeasureDialog::addPoint(QgsPoint &point)
115+
{
116+
int numPoints = mTool->points().size();
117+
if (mMeasureArea && numPoints > 2)
118+
{
119+
double area = mTool->canvas()->mapRender()->distArea()->measurePolygon(mTool->points());
120+
editTotal->setText(formatArea(area));
121+
}
122+
else if (!mMeasureArea && numPoints > 1)
123+
{
124+
int last = numPoints-2;
125+
126+
QgsPoint p1 = mTool->points()[last], p2 = mTool->points()[last+1];
127+
128+
double d = mTool->canvas()->mapRender()->distArea()->measureLine(p1,p2);
129+
130+
mTotal += d;
131+
editTotal->setText(formatDistance(mTotal));
132+
133+
134+
int row = numPoints-2;
135+
mTable->setText(row, 0, QLocale::system().toString(d, 'f', 2));
136+
mTable->setNumRows ( numPoints );
137+
138+
mTable->setText(row + 1, 0, QLocale::system().toString(0.0, 'f', 2));
139+
mTable->ensureCellVisible(row + 1,0);
140+
}
141+
}
142+
143+
144+
void QgsMeasureDialog::close(void)
145+
{
146+
restart();
147+
saveWindowLocation();
148+
hide();
149+
}
150+
151+
void QgsMeasureDialog::closeEvent(QCloseEvent *e)
152+
{
153+
saveWindowLocation();
154+
e->accept();
155+
}
156+
157+
void QgsMeasureDialog::restorePosition()
158+
{
159+
QSettings settings;
160+
int ww = settings.readNumEntry("/Windows/Measure/w", 150);
161+
int wh;
162+
if (mMeasureArea)
163+
wh = settings.readNumEntry("/Windows/Measure/hNoTable", 70);
164+
else
165+
wh = settings.readNumEntry("/Windows/Measure/h", 200);
166+
int wx = settings.readNumEntry("/Windows/Measure/x", 100);
167+
int wy = settings.readNumEntry("/Windows/Measure/y", 100);
168+
// setUpdatesEnabled(false);
169+
adjustSize();
170+
resize(ww,wh);
171+
move(wx,wy);
172+
// setUpdatesEnabled(true);
173+
this->show();
174+
}
175+
176+
void QgsMeasureDialog::saveWindowLocation()
177+
{
178+
QSettings settings;
179+
QPoint p = this->pos();
180+
QSize s = this->size();
181+
settings.writeEntry("/Windows/Measure/x", p.x());
182+
settings.writeEntry("/Windows/Measure/y", p.y());
183+
settings.writeEntry("/Windows/Measure/w", s.width());
184+
if (mMeasureArea)
185+
settings.writeEntry("/Windows/Measure/hNoTable", s.height());
186+
else
187+
settings.writeEntry("/Windows/Measure/h", s.height());
188+
}
189+
190+
void QgsMeasureDialog::on_btnHelp_clicked()
191+
{
192+
QgsContextHelp::run(context_id);
193+
}
194+
195+
196+
QString QgsMeasureDialog::formatDistance(double distance)
197+
{
198+
QString txt;
199+
QString unitLabel;
200+
201+
QGis::units myMapUnits = mTool->canvas()->mapUnits();
202+
return QgsDistanceArea::textUnit(distance, 2, myMapUnits, false);
203+
}
204+
205+
QString QgsMeasureDialog::formatArea(double area)
206+
{
207+
QGis::units myMapUnits = mTool->canvas()->mapUnits();
208+
return QgsDistanceArea::textUnit(area, 2, myMapUnits, true);
209+
}
210+
211+
void QgsMeasureDialog::updateUi()
212+
{
213+
214+
QGis::units myMapUnits = mTool->canvas()->mapUnits();
215+
switch (myMapUnits)
216+
{
217+
case QGis::METERS:
218+
mTable->horizontalHeader()->setLabel( 0, tr("Segments (in meters)") );
219+
break;
220+
case QGis::FEET:
221+
mTable->horizontalHeader()->setLabel( 0, tr("Segments (in feet)") );
222+
break;
223+
case QGis::DEGREES:
224+
mTable->horizontalHeader()->setLabel( 0, tr("Segments (in degrees)") );
225+
break;
226+
case QGis::UNKNOWN:
227+
mTable->horizontalHeader()->setLabel( 0, tr("Segments") );
228+
};
229+
230+
if (mMeasureArea)
231+
{
232+
mTable->hide();
233+
editTotal->setText(formatArea(0));
234+
}
235+
else
236+
{
237+
mTable->show();
238+
editTotal->setText(formatDistance(0));
239+
}
240+
241+
}
242+

‎src/app/qgsmeasure.h renamed to ‎src/app/qgsmeasuredialog.h

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,25 @@
1313
* (at your option) any later version. *
1414
* *
1515
***************************************************************************/
16-
#ifndef QGSMEASURE_H
17-
#define QGSMEASURE_H
16+
17+
#ifndef QGSMEASUREDIALOG_H
18+
#define QGSMEASUREDIALOG_H
1819

1920
#include "ui_qgsmeasurebase.h"
20-
#include <QWidget>
21-
#include "qgsmaptool.h"
22-
#include "qgspoint.h"
2321

24-
class QgsDistanceArea;
25-
class QgsMapCanvas;
26-
class QgsRubberBand;
22+
#include "qgspoint.h"
2723

2824
class QCloseEvent;
25+
class QgsMeasureTool;
2926

30-
31-
class QgsMeasure:public QDialog, public QgsMapTool, private Ui::QgsMeasureBase
27+
class QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase
3228
{
3329
Q_OBJECT;
34-
public:
3530

36-
//! Constructor
37-
QgsMeasure(bool measureArea, QgsMapCanvas *mc, Qt::WFlags f = 0);
31+
public:
3832

39-
~QgsMeasure();
33+
//! Constructor
34+
QgsMeasureDialog(QgsMeasureTool* tool, Qt::WFlags f = 0);
4035

4136
//! Save position
4237
void saveWindowLocation(void);
@@ -53,43 +48,16 @@ class QgsMeasure:public QDialog, public QgsMapTool, private Ui::QgsMeasureBase
5348
//! Mouse press
5449
void mousePress(QgsPoint &point);
5550

56-
//! returns whether measuring distance or area
57-
bool measureArea() { return mMeasureArea; }
58-
//! sets whether we're measuring area (and restarts)
59-
void setMeasureArea(bool measureArea);
60-
61-
public:
62-
63-
// Inherited from QgsMapTool
64-
65-
//! Mouse move event for overriding
66-
virtual void canvasMoveEvent(QMouseEvent * e);
67-
68-
//! Mouse press event for overriding
69-
virtual void canvasPressEvent(QMouseEvent * e);
70-
71-
//! Mouse release event for overriding
72-
virtual void canvasReleaseEvent(QMouseEvent * e);
73-
74-
//! called when set as currently active map tool
75-
virtual void activate();
76-
77-
//! called when map tool is being deactivated
78-
virtual void deactivate();
79-
8051
public slots:
8152
//! Close
8253
void close ( void);
8354

8455
//! Reset and start new
85-
void restart ( void);
56+
void restart ();
8657

8758
//! Close event
8859
void closeEvent(QCloseEvent *e);
8960

90-
//! Redraw lines to match current state of canvas
91-
void mapCanvasChanged();
92-
9361
//! Show the help for the dialog
9462
void on_btnHelp_clicked();
9563

@@ -104,33 +72,16 @@ public slots:
10472
//! shows/hides table, shows correct units
10573
void updateUi();
10674

107-
//! updates the projections we're using
108-
void updateProjection();
109-
110-
QgsMapCanvas *mMapCanvas;
111-
112-
//! distance/area calculator
113-
//QgsDistanceArea* mCalc;
114-
115-
QList<QgsPoint> mPoints;
116-
11775
double mTotal;
11876

119-
//! Rubberband widget tracking the lines being drawn
120-
QgsRubberBand *mRubberBand;
121-
12277
//! Help context id
12378
static const int context_id = 687883780;
12479

12580
//! indicates whether we're measuring distances or areas
12681
bool mMeasureArea;
12782

128-
//! indicates whether we've just done a right mouse click
129-
bool mRightMouseClicked;
130-
131-
//! indicates whether we've recently warned the user about having the wrong
132-
// project projection
133-
bool mWrongProjectProjection;
83+
//! pointer to measure tool which owns this dialog
84+
QgsMeasureTool* mTool;
13485
};
13586

13687
#endif

‎src/app/qgsmeasuretool.cpp

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/***************************************************************************
2+
qgsmeasuretool.cpp - map tool for measuring distances and areas
3+
---------------------
4+
begin : April 2007
5+
copyright : (C) 2007 by Martin Dobias
6+
email : wonder.sk at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
/* $Id$ */
16+
17+
#include "qgsdistancearea.h"
18+
#include "qgslogger.h"
19+
#include "qgsmapcanvas.h"
20+
#include "qgsmaprender.h"
21+
#include "qgsmaptopixel.h"
22+
#include "qgsrubberband.h"
23+
24+
#include "qgsmeasuredialog.h"
25+
#include "qgsmeasuretool.h"
26+
27+
#include <QMessageBox>
28+
#include <QSettings>
29+
30+
QgsMeasureTool::QgsMeasureTool(QgsMapCanvas* canvas, bool measureArea)
31+
: QgsMapTool(canvas)
32+
{
33+
mMeasureArea = measureArea;
34+
35+
mRubberBand = new QgsRubberBand(canvas, mMeasureArea);
36+
37+
mCanvas->setCursor(Qt::CrossCursor);
38+
39+
mRightMouseClicked = false;
40+
41+
mDialog = new QgsMeasureDialog(this);
42+
}
43+
44+
QgsMeasureTool::~QgsMeasureTool()
45+
{
46+
delete mRubberBand;
47+
}
48+
49+
50+
const QList<QgsPoint>& QgsMeasureTool::points()
51+
{
52+
return mPoints;
53+
}
54+
55+
56+
void QgsMeasureTool::activate()
57+
{
58+
mDialog->restorePosition();
59+
QgsMapTool::activate();
60+
mRightMouseClicked = false;
61+
62+
// ensure that we have correct settings
63+
updateProjection();
64+
65+
// If we suspect that they have data that is projected, yet the
66+
// map SRS is set to a geographic one, warn them.
67+
if (mCanvas->mapRender()->distArea()->geographic() &&
68+
(mCanvas->extent().height() > 360 ||
69+
mCanvas->extent().width() > 720))
70+
{
71+
QMessageBox::warning(NULL, tr("Incorrect measure results"),
72+
tr("<p>This map is defined with a geographic coordinate system "
73+
"(latitude/longitude) "
74+
"but the map extents suggest that it is actually a projected "
75+
"coordinate system (e.g., Mercator). "
76+
"If so, the results from line or area measurements will be "
77+
"incorrect.</p>"
78+
"<p>To fix this, explicitly set an appropriate map coordinate "
79+
"system using the <tt>Settings:Project Properties</tt> menu."));
80+
mWrongProjectProjection = true;
81+
}
82+
}
83+
84+
void QgsMeasureTool::deactivate()
85+
{
86+
mDialog->close();
87+
QgsMapTool::deactivate();
88+
}
89+
90+
91+
void QgsMeasureTool::restart()
92+
{
93+
updateProjection();
94+
mPoints.clear();
95+
96+
mRubberBand->reset(mMeasureArea);
97+
98+
// re-read color settings
99+
QSettings settings;
100+
int myRed = settings.value("/qgis/default_measure_color_red", 180).toInt();
101+
int myGreen = settings.value("/qgis/default_measure_color_green", 180).toInt();
102+
int myBlue = settings.value("/qgis/default_measure_color_blue", 180).toInt();
103+
mRubberBand->setColor(QColor(myRed, myGreen, myBlue));
104+
105+
mRightMouseClicked = false;
106+
mWrongProjectProjection = false;
107+
108+
}
109+
110+
111+
112+
113+
114+
void QgsMeasureTool::updateProjection()
115+
{
116+
// set ellipsoid
117+
QSettings settings;
118+
// QString ellipsoid = settings.readEntry("/qgis/measure/ellipsoid", "WGS84");
119+
// mCalc->setEllipsoid(ellipsoid);
120+
121+
// set source SRS and projections enabled flag
122+
// QgsMapRender* mapRender = mCanvas->mapRender();
123+
// mCalc->setProjectionsEnabled(mapRender->projectionsEnabled());
124+
// int srsid = mapRender->destinationSrs().srsid();
125+
// mCalc->setSourceSRS(srsid);
126+
127+
int myRed = settings.value("/qgis/default_measure_color_red", 180).toInt();
128+
int myGreen = settings.value("/qgis/default_measure_color_green", 180).toInt();
129+
int myBlue = settings.value("/qgis/default_measure_color_blue", 180).toInt();
130+
mRubberBand->setColor(QColor(myRed, myGreen, myBlue));
131+
132+
}
133+
134+
//////////////////////////
135+
136+
void QgsMeasureTool::canvasPressEvent(QMouseEvent * e)
137+
{
138+
if (e->button() == Qt::LeftButton)
139+
{
140+
if (mRightMouseClicked)
141+
mDialog->restart();
142+
143+
QgsPoint idPoint = mCanvas->getCoordinateTransform()->toMapCoordinates(e->x(), e->y());
144+
mDialog->mousePress(idPoint);
145+
}
146+
}
147+
148+
149+
void QgsMeasureTool::canvasMoveEvent(QMouseEvent * e)
150+
{
151+
if (!mRightMouseClicked)
152+
{
153+
QgsPoint point = mCanvas->getCoordinateTransform()->toMapCoordinates(e->pos().x(), e->pos().y());
154+
mRubberBand->movePoint(point);
155+
mDialog->mouseMove(point);
156+
}
157+
}
158+
159+
160+
void QgsMeasureTool::canvasReleaseEvent(QMouseEvent * e)
161+
{
162+
QgsPoint point = mCanvas->getCoordinateTransform()->toMapCoordinates(e->x(), e->y());
163+
164+
if(e->button() == Qt::RightButton && (e->state() & Qt::LeftButton) == 0) // restart
165+
{
166+
if (mRightMouseClicked)
167+
mDialog->restart();
168+
else
169+
mRightMouseClicked = true;
170+
}
171+
else if (e->button() == Qt::LeftButton)
172+
{
173+
addPoint(point);
174+
mDialog->show();
175+
}
176+
177+
}
178+
179+
180+
void QgsMeasureTool::addPoint(QgsPoint &point)
181+
{
182+
QgsDebugMsg( QString::number(point.x()) + ", " + QString::number(point.y()));
183+
184+
if (mWrongProjectProjection)
185+
{
186+
updateProjection();
187+
mWrongProjectProjection = false;
188+
}
189+
190+
// don't add points with the same coordinates
191+
if (mPoints.size() > 0 && point == mPoints[0])
192+
return;
193+
194+
QgsPoint pnt(point);
195+
mPoints.append(pnt);
196+
197+
198+
mRubberBand->addPoint(point);
199+
mDialog->addPoint(point);
200+
}

‎src/app/qgsmeasuretool.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/***************************************************************************
2+
qgsmeasuretool.h - map tool for measuring distances and areas
3+
---------------------
4+
begin : April 2007
5+
copyright : (C) 2007 by Martin Dobias
6+
email : wonder.sk at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
/* $Id$ */
16+
17+
18+
#ifndef QGSMEASURETOOL_H
19+
#define QGSMEASURETOOL_H
20+
21+
#include "qgsmaptool.h"
22+
23+
class QgsDistanceArea;
24+
class QgsMapCanvas;
25+
class QgsMeasureDialog;
26+
class QgsRubberBand;
27+
28+
29+
30+
class QgsMeasureTool : public QgsMapTool
31+
{
32+
33+
public:
34+
35+
QgsMeasureTool(QgsMapCanvas* canvas, bool measureArea);
36+
37+
~QgsMeasureTool();
38+
39+
//! returns whether measuring distance or area
40+
bool measureArea() { return mMeasureArea; }
41+
42+
//! Reset and start new
43+
void restart ();
44+
45+
//! Add new point
46+
void addPoint(QgsPoint &point);
47+
48+
//! Returns reference to array of the points
49+
const QList<QgsPoint>& points();
50+
51+
// Inherited from QgsMapTool
52+
53+
//! Mouse move event for overriding
54+
virtual void canvasMoveEvent(QMouseEvent * e);
55+
56+
//! Mouse press event for overriding
57+
virtual void canvasPressEvent(QMouseEvent * e);
58+
59+
//! Mouse release event for overriding
60+
virtual void canvasReleaseEvent(QMouseEvent * e);
61+
62+
//! called when set as currently active map tool
63+
virtual void activate();
64+
65+
//! called when map tool is being deactivated
66+
virtual void deactivate();
67+
68+
protected:
69+
70+
//! updates the projections we're using
71+
void updateProjection();
72+
73+
QList<QgsPoint> mPoints;
74+
75+
QgsMeasureDialog* mDialog;
76+
77+
//! Rubberband widget tracking the lines being drawn
78+
QgsRubberBand *mRubberBand;
79+
80+
//! indicates whether we're measuring distances or areas
81+
bool mMeasureArea;
82+
83+
//! indicates whether we've just done a right mouse click
84+
bool mRightMouseClicked;
85+
86+
//! indicates whether we've recently warned the user about having the wrong
87+
// project projection
88+
bool mWrongProjectProjection;
89+
90+
};
91+
92+
#endif

‎src/gui/qgsmapcanvas.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class QgsMapLayer;
4848
class QgsMapLayerInterface;
4949
class QgsLegend;
5050
class QgsLegendView;
51-
class QgsMeasure;
5251
class QgsRubberBand;
5352

5453
class QgsMapRender;

‎src/gui/qgsmaptool.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <QAction>
2323

2424
QgsMapTool::QgsMapTool(QgsMapCanvas* canvas)
25-
: mCanvas(canvas), mCursor(Qt::CrossCursor), mAction(NULL)
25+
: QObject(canvas), mCanvas(canvas), mCursor(Qt::CrossCursor), mAction(NULL)
2626
{
2727
}
2828

@@ -80,3 +80,39 @@ void QgsMapTool::deactivate()
8080
if (mAction)
8181
mAction->setChecked(false);
8282
}
83+
84+
void QgsMapTool::setAction(QAction* action)
85+
{
86+
mAction = action;
87+
}
88+
89+
QAction* QgsMapTool::action()
90+
{
91+
return mAction;
92+
}
93+
94+
void QgsMapTool::canvasMoveEvent(QMouseEvent *)
95+
{
96+
}
97+
98+
void QgsMapTool::canvasPressEvent(QMouseEvent *)
99+
{
100+
}
101+
102+
void QgsMapTool::canvasReleaseEvent(QMouseEvent *)
103+
{
104+
}
105+
106+
void QgsMapTool::renderComplete()
107+
{
108+
}
109+
110+
bool QgsMapTool::isZoomTool()
111+
{
112+
return false;
113+
}
114+
115+
QgsMapCanvas* QgsMapTool::canvas()
116+
{
117+
return mCanvas;
118+
}

‎src/gui/qgsmaptool.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <QCursor>
2121
#include <QString>
22+
#include <QObject>
2223

2324
class QgsMapLayer;
2425
class QgsMapCanvas;
@@ -29,45 +30,48 @@ class QPoint;
2930
class QAction;
3031

3132

32-
class GUI_EXPORT QgsMapTool
33+
class GUI_EXPORT QgsMapTool : public QObject
3334
{
3435
public:
3536

3637
//! virtual destructor
3738
virtual ~QgsMapTool();
3839

39-
//! Mouse move event for overriding
40-
virtual void canvasMoveEvent(QMouseEvent * e) { }
40+
//! Mouse move event for overriding. Default implementation does nothing.
41+
virtual void canvasMoveEvent(QMouseEvent * e);
4142

42-
//! Mouse press event for overriding
43-
virtual void canvasPressEvent(QMouseEvent * e) { }
43+
//! Mouse press event for overriding. Default implementation does nothing.
44+
virtual void canvasPressEvent(QMouseEvent * e);
4445

45-
//! Mouse release event for overriding
46-
virtual void canvasReleaseEvent(QMouseEvent * e) { }
46+
//! Mouse release event for overriding. Default implementation does nothing.
47+
virtual void canvasReleaseEvent(QMouseEvent * e);
4748

48-
//! Called when rendering has finished
49-
virtual void renderComplete() {}
49+
//! Called when rendering has finished. Default implementation does nothing.
50+
virtual void renderComplete();
5051

5152
/** Use this to associate a button, toolbutton, menu entry etc
5253
* that inherits qaction to this maptool. Then when the setMapTool
5354
* method of mapcanvas is called the action state will be set to on.
5455
* Usually this will cause e.g. a toolbutton to appear pressed in and
5556
* the previously used toolbutton to pop out. */
56-
void setAction(QAction* action) { mAction = action; }
57+
void setAction(QAction* action);
5758

58-
QAction* action() { return mAction; }
59+
QAction* action();
5960

6061
/** Check whether this MapTool performs a zoom or pan operation.
6162
* If it does, we will be able to perform the zoom and then
6263
* resume operations with the original / previously used tool.*/
63-
virtual bool isZoomTool() { return false;}
64+
virtual bool isZoomTool();
6465

6566
//! called when set as currently active map tool
6667
virtual void activate();
6768

6869
//! called when map tool is being deactivated
6970
virtual void deactivate();
7071

72+
//! returns pointer to the tool's map canvas
73+
QgsMapCanvas* canvas();
74+
7175
protected:
7276

7377
//! constructor takes map canvas as a parameter

0 commit comments

Comments
 (0)
Please sign in to comment.