Skip to content

Commit f1faa61

Browse files
author
wonder
committed
Ownership of map tools is not given to map canvas anymore, this means that
caller has to save the pointer to the map tool and then delete it. It's possible to delete map tool while it's being used in map canvas - destructor of QgsMapTool calls QgsMapCanvas::unsetMapTool(this) to ensure that the tool won't be used anymore. Having this function in destructor of every map tool, it's not needed to call it explicitly. git-svn-id: http://svn.osgeo.org/qgis/trunk@5940 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent eceb4e8 commit f1faa61

14 files changed

+187
-131
lines changed

src/gui/qgisapp.cpp

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,21 @@ static void setTitleBarText_( QWidget & qgisApp )
327327

328328

329329
QgisApp::~QgisApp()
330-
{}
330+
{
331+
delete mMapTools.mZoomIn;
332+
delete mMapTools.mZoomOut;
333+
delete mMapTools.mPan;
334+
delete mMapTools.mIdentify;
335+
delete mMapTools.mMeasureDist;
336+
delete mMapTools.mMeasureArea;
337+
delete mMapTools.mCapturePoint;
338+
delete mMapTools.mCaptureLine;
339+
delete mMapTools.mCapturePolygon;
340+
delete mMapTools.mSelect;
341+
delete mMapTools.mVertexAdd;
342+
delete mMapTools.mVertexMove;
343+
delete mMapTools.mVertexDelete;
344+
}
331345

332346
// restore any application settings stored in QSettings
333347
void QgisApp::readSettings()
@@ -1060,6 +1074,34 @@ void QgisApp::createCanvas()
10601074
tabWidget->widget(0)->setLayout(myCanvasLayout);
10611075
// set the focus to the map canvas
10621076
mMapCanvas->setFocus();
1077+
1078+
// create tools
1079+
mMapTools.mZoomIn = new QgsMapToolZoom(mMapCanvas, FALSE /* zoomIn */);
1080+
mMapTools.mZoomIn->setAction(mActionZoomIn);
1081+
mMapTools.mZoomOut = new QgsMapToolZoom(mMapCanvas, TRUE /* zoomOut */);
1082+
mMapTools.mZoomOut->setAction(mActionZoomOut);
1083+
mMapTools.mPan = new QgsMapToolPan(mMapCanvas);
1084+
mMapTools.mPan->setAction(mActionPan);
1085+
mMapTools.mIdentify = new QgsMapToolIdentify(mMapCanvas);
1086+
mMapTools.mIdentify->setAction(mActionIdentify);
1087+
mMapTools.mMeasureDist = new QgsMeasure(FALSE /* area */, mMapCanvas);
1088+
mMapTools.mMeasureDist->setAction(mActionMeasure);
1089+
mMapTools.mMeasureArea = new QgsMeasure(TRUE /* area */, mMapCanvas);
1090+
mMapTools.mMeasureArea->setAction(mActionMeasureArea);
1091+
mMapTools.mCapturePoint = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CapturePoint);
1092+
mMapTools.mCapturePoint->setAction(mActionCapturePoint);
1093+
mMapTools.mCaptureLine = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CaptureLine);
1094+
mMapTools.mCaptureLine->setAction(mActionCaptureLine);
1095+
mMapTools.mCapturePolygon = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CapturePolygon);
1096+
mMapTools.mCapturePolygon->setAction(mActionCapturePolygon);
1097+
mMapTools.mSelect = new QgsMapToolSelect(mMapCanvas);
1098+
mMapTools.mSelect->setAction(mActionSelect);
1099+
mMapTools.mVertexAdd = new QgsMapToolVertexEdit(mMapCanvas, QgsMapToolVertexEdit::AddVertex);
1100+
mMapTools.mVertexAdd->setAction(mActionAddVertex);
1101+
mMapTools.mVertexMove = new QgsMapToolVertexEdit(mMapCanvas, QgsMapToolVertexEdit::MoveVertex);
1102+
mMapTools.mVertexMove->setAction(mActionMoveVertex);
1103+
mMapTools.mVertexDelete = new QgsMapToolVertexEdit(mMapCanvas, QgsMapToolVertexEdit::DeleteVertex);
1104+
mMapTools.mVertexDelete->setAction(mActionDeleteVertex);
10631105
}
10641106

10651107
void QgisApp::createOverview()
@@ -3192,13 +3234,14 @@ void QgisApp::exportMapServer()
31923234
// tr("No layers to export. You must add at least one layer to the map in order to export the view."));
31933235
//}
31943236
}
3237+
3238+
3239+
31953240
void QgisApp::zoomIn()
31963241
{
31973242
QgsDebugMsg ("Setting map tool to zoomIn");
31983243

3199-
QgsMapTool* tool = new QgsMapToolZoom(mMapCanvas, FALSE /* zoomIn */);
3200-
tool->setAction(mActionZoomIn);
3201-
mMapCanvas->setMapTool(tool);
3244+
mMapCanvas->setMapTool(mMapTools.mZoomIn);
32023245

32033246
// notify the project we've made a change
32043247
QgsProject::instance()->dirty(true);
@@ -3207,9 +3250,7 @@ void QgisApp::zoomIn()
32073250

32083251
void QgisApp::zoomOut()
32093252
{
3210-
QgsMapTool* tool = new QgsMapToolZoom(mMapCanvas, TRUE /* zoomOut */);
3211-
tool->setAction(mActionZoomOut);
3212-
mMapCanvas->setMapTool(tool);
3253+
mMapCanvas->setMapTool(mMapTools.mZoomOut);
32133254

32143255
// notify the project we've made a change
32153256
QgsProject::instance()->dirty(true);
@@ -3225,9 +3266,7 @@ void QgisApp::zoomToSelected()
32253266

32263267
void QgisApp::pan()
32273268
{
3228-
QgsMapTool* tool = new QgsMapToolPan(mMapCanvas);
3229-
tool->setAction(mActionPan);
3230-
mMapCanvas->setMapTool(tool);
3269+
mMapCanvas->setMapTool(mMapTools.mPan);
32313270
}
32323271

32333272
void QgisApp::zoomFull()
@@ -3248,23 +3287,17 @@ void QgisApp::zoomPrevious()
32483287

32493288
void QgisApp::identify()
32503289
{
3251-
QgsMapTool* tool = new QgsMapToolIdentify(mMapCanvas);
3252-
tool->setAction(mActionIdentify);
3253-
mMapCanvas->setMapTool(tool);
3290+
mMapCanvas->setMapTool(mMapTools.mIdentify);
32543291
}
32553292

32563293
void QgisApp::measure()
32573294
{
3258-
QgsMapTool* tool = new QgsMeasure(FALSE /* area */, mMapCanvas);
3259-
tool->setAction(mActionMeasure);
3260-
mMapCanvas->setMapTool(tool);
3295+
mMapCanvas->setMapTool(mMapTools.mMeasureDist);
32613296
}
32623297

32633298
void QgisApp::measureArea()
32643299
{
3265-
QgsMapTool* tool = new QgsMeasure(TRUE /* area */, mMapCanvas);
3266-
tool->setAction(mActionMeasureArea);
3267-
mMapCanvas->setMapTool(tool);
3300+
mMapCanvas->setMapTool(mMapTools.mMeasureArea);
32683301
}
32693302

32703303

@@ -3316,72 +3349,43 @@ void QgisApp::deleteSelected()
33163349
void QgisApp::capturePoint()
33173350
{
33183351
// set current map tool to select
3319-
QgsMapTool* t = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CapturePoint);
3320-
t->setAction(mActionCapturePoint);
3321-
mMapCanvas->setMapTool(t);
3352+
mMapCanvas->setMapTool(mMapTools.mCapturePoint);
33223353

33233354
// FIXME: is this still actual or something old that's not used anymore?
33243355
//connect(t, SIGNAL(xyClickCoordinates(QgsPoint &)), this, SLOT(showCapturePointCoordinate(QgsPoint &)));
33253356
}
33263357

33273358
void QgisApp::captureLine()
33283359
{
3329-
QgsMapTool* t = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CaptureLine);
3330-
t->setAction(mActionCaptureLine);
3331-
mMapCanvas->setMapTool(t);
3360+
mMapCanvas->setMapTool(mMapTools.mCaptureLine);
33323361
}
33333362

33343363
void QgisApp::capturePolygon()
33353364
{
3336-
QgsMapTool* t = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CapturePolygon);
3337-
t->setAction(mActionCapturePolygon);
3338-
mMapCanvas->setMapTool(t);
3365+
mMapCanvas->setMapTool(mMapTools.mCapturePolygon);
33393366
}
33403367

33413368
void QgisApp::select()
33423369
{
3343-
QgsMapTool* t = new QgsMapToolSelect(mMapCanvas);
3344-
t->setAction(mActionSelect);
3345-
mMapCanvas->setMapTool(t);
3370+
mMapCanvas->setMapTool(mMapTools.mSelect);
33463371
}
33473372

33483373

33493374
void QgisApp::addVertex()
33503375
{
3351-
3352-
#ifdef QGISDEBUG
3353-
std::cout << "QgisApp::addVertex." << std::endl;
3354-
#endif
3355-
3356-
QgsMapTool* t = new QgsMapToolVertexEdit(mMapCanvas, QgsMapToolVertexEdit::AddVertex);
3357-
t->setAction(mActionAddVertex);
3358-
mMapCanvas->setMapTool(t);
3376+
mMapCanvas->setMapTool(mMapTools.mVertexAdd);
33593377

33603378
}
33613379

33623380
void QgisApp::moveVertex()
33633381
{
3364-
3365-
#ifdef QGISDEBUG
3366-
std::cout << "QgisApp::moveVertex." << std::endl;
3367-
#endif
3368-
3369-
QgsMapTool* t = new QgsMapToolVertexEdit(mMapCanvas, QgsMapToolVertexEdit::MoveVertex);
3370-
t->setAction(mActionMoveVertex);
3371-
mMapCanvas->setMapTool(t);
3382+
mMapCanvas->setMapTool(mMapTools.mVertexMove);
33723383
}
33733384

33743385

33753386
void QgisApp::deleteVertex()
33763387
{
3377-
3378-
#ifdef QGISDEBUG
3379-
std::cout << "QgisApp::deleteVertex." << std::endl;
3380-
#endif
3381-
3382-
QgsMapTool* t = new QgsMapToolVertexEdit(mMapCanvas, QgsMapToolVertexEdit::DeleteVertex);
3383-
t->setAction(mActionDeleteVertex);
3384-
mMapCanvas->setMapTool(t);
3388+
mMapCanvas->setMapTool(mMapTools.mVertexDelete);
33853389
}
33863390

33873391

src/gui/qgisapp.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class QSplashScreen;
3737
class QgsPoint;
3838
class QgsLegend;
3939
class QgsMapLayer;
40+
class QgsMapTool;
4041
class QgsProviderRegistry;
4142
class QgsHelpViewer;
4243
class QgsMapCanvas;
@@ -521,6 +522,24 @@ public slots:
521522
QMenu *mSettingsMenu;
522523
QMenu *mHelpMenu;
523524

525+
class Tools
526+
{
527+
public:
528+
QgsMapTool* mZoomIn;
529+
QgsMapTool* mZoomOut;
530+
QgsMapTool* mPan;
531+
QgsMapTool* mIdentify;
532+
QgsMapTool* mMeasureDist;
533+
QgsMapTool* mMeasureArea;
534+
QgsMapTool* mCapturePoint;
535+
QgsMapTool* mCaptureLine;
536+
QgsMapTool* mCapturePolygon;
537+
QgsMapTool* mSelect;
538+
QgsMapTool* mVertexAdd;
539+
QgsMapTool* mVertexMove;
540+
QgsMapTool* mVertexDelete;
541+
} mMapTools;
542+
524543
//!The name of the active theme
525544
QString mThemeName;
526545

src/gui/qgsmapcanvas.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,12 @@ QgsMapCanvas::QgsMapCanvas()
124124

125125
QgsMapCanvas::~QgsMapCanvas()
126126
{
127-
delete mMapTool;
128-
delete mLastNonZoomMapTool;
127+
if (mMapTool)
128+
{
129+
mMapTool->deactivate();
130+
mMapTool = NULL;
131+
}
132+
mLastNonZoomMapTool = NULL;
129133

130134
// delete canvas items prior to deleteing the canvas
131135
// because they might try to update canvas when it's
@@ -274,7 +278,7 @@ void QgsMapCanvas::refresh()
274278
void QgsMapCanvas::drawContents(QPainter * p, int cx, int cy, int cw, int ch)
275279
{
276280
#ifdef QGISDEBUG
277-
std::cout << "QgsMapCanvas::drawContents" << std::endl;
281+
// std::cout << "QgsMapCanvas::drawContents" << std::endl;
278282
#endif
279283

280284
if (mDirty && mRenderFlag && !mFrozen)
@@ -790,30 +794,22 @@ void QgsMapCanvas::zoomByScale(int x, int y, double scaleFactor)
790794
/** Sets the map tool currently being used on the canvas */
791795
void QgsMapCanvas::setMapTool(QgsMapTool* tool)
792796
{
797+
if (!tool)
798+
return;
799+
793800
if (mMapTool)
794801
mMapTool->deactivate();
795802

796-
797-
if (tool && tool->isZoomTool() )
803+
if (tool->isZoomTool() && mMapTool && !mMapTool->isZoomTool())
798804
{
799805
// if zoom or pan tool will be active, save old tool
800806
// to bring it back on right click
801807
// (but only if it wasn't also zoom or pan tool)
802-
if (mMapTool && !mMapTool->isZoomTool())
803-
{
804-
delete mLastNonZoomMapTool;
805-
mLastNonZoomMapTool = mMapTool;
806-
}
807-
808+
mLastNonZoomMapTool = mMapTool;
808809
}
809810
else
810-
{
811-
// if there's already an old tool, delete it
812-
delete mLastNonZoomMapTool;
811+
{
813812
mLastNonZoomMapTool = NULL;
814-
815-
// delete current map tool
816-
delete mMapTool;
817813
}
818814

819815
// set new map tool and activate it
@@ -831,9 +827,8 @@ void QgsMapCanvas::unsetMapTool(QgsMapTool* tool)
831827
mMapTool = NULL;
832828
}
833829

834-
if ( mLastNonZoomMapTool && mLastNonZoomMapTool == tool)
830+
if (mLastNonZoomMapTool && mLastNonZoomMapTool == tool)
835831
{
836-
mLastNonZoomMapTool->deactivate(); // ? necessary
837832
mLastNonZoomMapTool = NULL;
838833
}
839834
}

src/gui/qgsmapcanvas.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,11 @@ class QgsMapCanvas : public Q3CanvasView
115115
/** \brief Sets the map tool currently being used on the canvas */
116116
void setMapTool(QgsMapTool* mapTool);
117117

118-
/** \brief Unset the current mapset tool or last non zoom tool if
119-
* it the same as passed map tool pointer. The tool is not
120-
* referenced/used any more, but the instance is not deleted
121-
* by this method.
118+
/** \brief Unset the current map tool or last non zoom tool
119+
*
120+
* This is called from destructor of map tools to make sure
121+
* that this map tool won't be used any more.
122+
* You don't have to call it manualy, QgsMapTool takes care of it.
122123
*/
123124
void unsetMapTool(QgsMapTool* mapTool);
124125

src/gui/qgsmapcanvasitem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ void QgsMapCanvasItem::updatePosition()
7676
setSize(r.width(), r.height());
7777

7878
#ifdef QGISDEBUG
79-
std::cout << "QgsMapCanvasItem::updatePosition: " << " [" << (int) x()
80-
<< "," << (int) y() << "]-[" << width() << "x" << height() << "]" << std::endl;
79+
// std::cout << "QgsMapCanvasItem::updatePosition: " << " [" << (int) x()
80+
// << "," << (int) y() << "]-[" << width() << "x" << height() << "]" << std::endl;
8181
#endif
8282
}
8383

src/gui/qgsmaptool.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
***************************************************************************/
1515
/* $Id$ */
1616

17+
#include "qgslogger.h"
1718
#include "qgsmaptool.h"
1819
#include "qgsmaplayer.h"
1920
#include "qgsmapcanvas.h"
@@ -29,6 +30,7 @@ QgsMapTool::QgsMapTool(QgsMapCanvas* canvas)
2930

3031
QgsMapTool::~QgsMapTool()
3132
{
33+
mCanvas->unsetMapTool(this);
3234
}
3335

3436

@@ -78,6 +80,7 @@ void QgsMapTool::activate()
7880

7981
// set cursor (map tools usually set it in constructor)
8082
mCanvas->setCursor(mCursor);
83+
QgsDebugMsg("Cursor has been set");
8184
}
8285

8386

src/gui/qgsmaptoolidentify.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,7 @@ void QgsMapToolIdentify::resultsDialogGone()
421421
mResults = 0;
422422
}
423423

424-
// ENDS
424+
void QgsMapToolIdentify::deactivate()
425+
{
426+
mResults->done(0); // close the window
427+
}

src/gui/qgsmaptoolidentify.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class QgsMapToolIdentify : public QObject, public QgsMapTool
5454
//! Overridden mouse release event
5555
virtual void canvasReleaseEvent(QMouseEvent * e);
5656

57+
//! called when map tool is being deactivated
58+
virtual void deactivate();
5759

5860
private:
5961

0 commit comments

Comments
 (0)