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/qgis@5940 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
wonder 2006-10-12 23:49:22 +00:00
parent 98497bd065
commit 02394055e3
14 changed files with 187 additions and 131 deletions

View File

@ -327,7 +327,21 @@ static void setTitleBarText_( QWidget & qgisApp )
QgisApp::~QgisApp()
{}
{
delete mMapTools.mZoomIn;
delete mMapTools.mZoomOut;
delete mMapTools.mPan;
delete mMapTools.mIdentify;
delete mMapTools.mMeasureDist;
delete mMapTools.mMeasureArea;
delete mMapTools.mCapturePoint;
delete mMapTools.mCaptureLine;
delete mMapTools.mCapturePolygon;
delete mMapTools.mSelect;
delete mMapTools.mVertexAdd;
delete mMapTools.mVertexMove;
delete mMapTools.mVertexDelete;
}
// restore any application settings stored in QSettings
void QgisApp::readSettings()
@ -1060,6 +1074,34 @@ void QgisApp::createCanvas()
tabWidget->widget(0)->setLayout(myCanvasLayout);
// set the focus to the map canvas
mMapCanvas->setFocus();
// create tools
mMapTools.mZoomIn = new QgsMapToolZoom(mMapCanvas, FALSE /* zoomIn */);
mMapTools.mZoomIn->setAction(mActionZoomIn);
mMapTools.mZoomOut = new QgsMapToolZoom(mMapCanvas, TRUE /* zoomOut */);
mMapTools.mZoomOut->setAction(mActionZoomOut);
mMapTools.mPan = new QgsMapToolPan(mMapCanvas);
mMapTools.mPan->setAction(mActionPan);
mMapTools.mIdentify = new QgsMapToolIdentify(mMapCanvas);
mMapTools.mIdentify->setAction(mActionIdentify);
mMapTools.mMeasureDist = new QgsMeasure(FALSE /* area */, mMapCanvas);
mMapTools.mMeasureDist->setAction(mActionMeasure);
mMapTools.mMeasureArea = new QgsMeasure(TRUE /* area */, mMapCanvas);
mMapTools.mMeasureArea->setAction(mActionMeasureArea);
mMapTools.mCapturePoint = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CapturePoint);
mMapTools.mCapturePoint->setAction(mActionCapturePoint);
mMapTools.mCaptureLine = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CaptureLine);
mMapTools.mCaptureLine->setAction(mActionCaptureLine);
mMapTools.mCapturePolygon = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CapturePolygon);
mMapTools.mCapturePolygon->setAction(mActionCapturePolygon);
mMapTools.mSelect = new QgsMapToolSelect(mMapCanvas);
mMapTools.mSelect->setAction(mActionSelect);
mMapTools.mVertexAdd = new QgsMapToolVertexEdit(mMapCanvas, QgsMapToolVertexEdit::AddVertex);
mMapTools.mVertexAdd->setAction(mActionAddVertex);
mMapTools.mVertexMove = new QgsMapToolVertexEdit(mMapCanvas, QgsMapToolVertexEdit::MoveVertex);
mMapTools.mVertexMove->setAction(mActionMoveVertex);
mMapTools.mVertexDelete = new QgsMapToolVertexEdit(mMapCanvas, QgsMapToolVertexEdit::DeleteVertex);
mMapTools.mVertexDelete->setAction(mActionDeleteVertex);
}
void QgisApp::createOverview()
@ -3192,13 +3234,14 @@ void QgisApp::exportMapServer()
// tr("No layers to export. You must add at least one layer to the map in order to export the view."));
//}
}
void QgisApp::zoomIn()
{
QgsDebugMsg ("Setting map tool to zoomIn");
QgsMapTool* tool = new QgsMapToolZoom(mMapCanvas, FALSE /* zoomIn */);
tool->setAction(mActionZoomIn);
mMapCanvas->setMapTool(tool);
mMapCanvas->setMapTool(mMapTools.mZoomIn);
// notify the project we've made a change
QgsProject::instance()->dirty(true);
@ -3207,9 +3250,7 @@ void QgisApp::zoomIn()
void QgisApp::zoomOut()
{
QgsMapTool* tool = new QgsMapToolZoom(mMapCanvas, TRUE /* zoomOut */);
tool->setAction(mActionZoomOut);
mMapCanvas->setMapTool(tool);
mMapCanvas->setMapTool(mMapTools.mZoomOut);
// notify the project we've made a change
QgsProject::instance()->dirty(true);
@ -3225,9 +3266,7 @@ void QgisApp::zoomToSelected()
void QgisApp::pan()
{
QgsMapTool* tool = new QgsMapToolPan(mMapCanvas);
tool->setAction(mActionPan);
mMapCanvas->setMapTool(tool);
mMapCanvas->setMapTool(mMapTools.mPan);
}
void QgisApp::zoomFull()
@ -3248,23 +3287,17 @@ void QgisApp::zoomPrevious()
void QgisApp::identify()
{
QgsMapTool* tool = new QgsMapToolIdentify(mMapCanvas);
tool->setAction(mActionIdentify);
mMapCanvas->setMapTool(tool);
mMapCanvas->setMapTool(mMapTools.mIdentify);
}
void QgisApp::measure()
{
QgsMapTool* tool = new QgsMeasure(FALSE /* area */, mMapCanvas);
tool->setAction(mActionMeasure);
mMapCanvas->setMapTool(tool);
mMapCanvas->setMapTool(mMapTools.mMeasureDist);
}
void QgisApp::measureArea()
{
QgsMapTool* tool = new QgsMeasure(TRUE /* area */, mMapCanvas);
tool->setAction(mActionMeasureArea);
mMapCanvas->setMapTool(tool);
mMapCanvas->setMapTool(mMapTools.mMeasureArea);
}
@ -3316,9 +3349,7 @@ void QgisApp::deleteSelected()
void QgisApp::capturePoint()
{
// set current map tool to select
QgsMapTool* t = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CapturePoint);
t->setAction(mActionCapturePoint);
mMapCanvas->setMapTool(t);
mMapCanvas->setMapTool(mMapTools.mCapturePoint);
// FIXME: is this still actual or something old that's not used anymore?
//connect(t, SIGNAL(xyClickCoordinates(QgsPoint &)), this, SLOT(showCapturePointCoordinate(QgsPoint &)));
@ -3326,62 +3357,35 @@ void QgisApp::capturePoint()
void QgisApp::captureLine()
{
QgsMapTool* t = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CaptureLine);
t->setAction(mActionCaptureLine);
mMapCanvas->setMapTool(t);
mMapCanvas->setMapTool(mMapTools.mCaptureLine);
}
void QgisApp::capturePolygon()
{
QgsMapTool* t = new QgsMapToolCapture(mMapCanvas, QgsMapToolCapture::CapturePolygon);
t->setAction(mActionCapturePolygon);
mMapCanvas->setMapTool(t);
mMapCanvas->setMapTool(mMapTools.mCapturePolygon);
}
void QgisApp::select()
{
QgsMapTool* t = new QgsMapToolSelect(mMapCanvas);
t->setAction(mActionSelect);
mMapCanvas->setMapTool(t);
mMapCanvas->setMapTool(mMapTools.mSelect);
}
void QgisApp::addVertex()
{
#ifdef QGISDEBUG
std::cout << "QgisApp::addVertex." << std::endl;
#endif
QgsMapTool* t = new QgsMapToolVertexEdit(mMapCanvas, QgsMapToolVertexEdit::AddVertex);
t->setAction(mActionAddVertex);
mMapCanvas->setMapTool(t);
mMapCanvas->setMapTool(mMapTools.mVertexAdd);
}
void QgisApp::moveVertex()
{
#ifdef QGISDEBUG
std::cout << "QgisApp::moveVertex." << std::endl;
#endif
QgsMapTool* t = new QgsMapToolVertexEdit(mMapCanvas, QgsMapToolVertexEdit::MoveVertex);
t->setAction(mActionMoveVertex);
mMapCanvas->setMapTool(t);
mMapCanvas->setMapTool(mMapTools.mVertexMove);
}
void QgisApp::deleteVertex()
{
#ifdef QGISDEBUG
std::cout << "QgisApp::deleteVertex." << std::endl;
#endif
QgsMapTool* t = new QgsMapToolVertexEdit(mMapCanvas, QgsMapToolVertexEdit::DeleteVertex);
t->setAction(mActionDeleteVertex);
mMapCanvas->setMapTool(t);
mMapCanvas->setMapTool(mMapTools.mVertexDelete);
}

View File

@ -37,6 +37,7 @@ class QSplashScreen;
class QgsPoint;
class QgsLegend;
class QgsMapLayer;
class QgsMapTool;
class QgsProviderRegistry;
class QgsHelpViewer;
class QgsMapCanvas;
@ -521,6 +522,24 @@ private:
QMenu *mSettingsMenu;
QMenu *mHelpMenu;
class Tools
{
public:
QgsMapTool* mZoomIn;
QgsMapTool* mZoomOut;
QgsMapTool* mPan;
QgsMapTool* mIdentify;
QgsMapTool* mMeasureDist;
QgsMapTool* mMeasureArea;
QgsMapTool* mCapturePoint;
QgsMapTool* mCaptureLine;
QgsMapTool* mCapturePolygon;
QgsMapTool* mSelect;
QgsMapTool* mVertexAdd;
QgsMapTool* mVertexMove;
QgsMapTool* mVertexDelete;
} mMapTools;
//!The name of the active theme
QString mThemeName;

View File

@ -124,8 +124,12 @@ QgsMapCanvas::QgsMapCanvas()
QgsMapCanvas::~QgsMapCanvas()
{
delete mMapTool;
delete mLastNonZoomMapTool;
if (mMapTool)
{
mMapTool->deactivate();
mMapTool = NULL;
}
mLastNonZoomMapTool = NULL;
// delete canvas items prior to deleteing the canvas
// because they might try to update canvas when it's
@ -274,7 +278,7 @@ void QgsMapCanvas::refresh()
void QgsMapCanvas::drawContents(QPainter * p, int cx, int cy, int cw, int ch)
{
#ifdef QGISDEBUG
std::cout << "QgsMapCanvas::drawContents" << std::endl;
// std::cout << "QgsMapCanvas::drawContents" << std::endl;
#endif
if (mDirty && mRenderFlag && !mFrozen)
@ -790,30 +794,22 @@ void QgsMapCanvas::zoomByScale(int x, int y, double scaleFactor)
/** Sets the map tool currently being used on the canvas */
void QgsMapCanvas::setMapTool(QgsMapTool* tool)
{
if (!tool)
return;
if (mMapTool)
mMapTool->deactivate();
if (tool && tool->isZoomTool() )
if (tool->isZoomTool() && mMapTool && !mMapTool->isZoomTool())
{
// if zoom or pan tool will be active, save old tool
// to bring it back on right click
// (but only if it wasn't also zoom or pan tool)
if (mMapTool && !mMapTool->isZoomTool())
{
delete mLastNonZoomMapTool;
mLastNonZoomMapTool = mMapTool;
}
mLastNonZoomMapTool = mMapTool;
}
else
{
// if there's already an old tool, delete it
delete mLastNonZoomMapTool;
{
mLastNonZoomMapTool = NULL;
// delete current map tool
delete mMapTool;
}
// set new map tool and activate it
@ -831,9 +827,8 @@ void QgsMapCanvas::unsetMapTool(QgsMapTool* tool)
mMapTool = NULL;
}
if ( mLastNonZoomMapTool && mLastNonZoomMapTool == tool)
if (mLastNonZoomMapTool && mLastNonZoomMapTool == tool)
{
mLastNonZoomMapTool->deactivate(); // ? necessary
mLastNonZoomMapTool = NULL;
}
}

View File

@ -115,10 +115,11 @@ class QgsMapCanvas : public Q3CanvasView
/** \brief Sets the map tool currently being used on the canvas */
void setMapTool(QgsMapTool* mapTool);
/** \brief Unset the current mapset tool or last non zoom tool if
* it the same as passed map tool pointer. The tool is not
* referenced/used any more, but the instance is not deleted
* by this method.
/** \brief Unset the current map tool or last non zoom tool
*
* This is called from destructor of map tools to make sure
* that this map tool won't be used any more.
* You don't have to call it manualy, QgsMapTool takes care of it.
*/
void unsetMapTool(QgsMapTool* mapTool);

View File

@ -76,8 +76,8 @@ void QgsMapCanvasItem::updatePosition()
setSize(r.width(), r.height());
#ifdef QGISDEBUG
std::cout << "QgsMapCanvasItem::updatePosition: " << " [" << (int) x()
<< "," << (int) y() << "]-[" << width() << "x" << height() << "]" << std::endl;
// std::cout << "QgsMapCanvasItem::updatePosition: " << " [" << (int) x()
// << "," << (int) y() << "]-[" << width() << "x" << height() << "]" << std::endl;
#endif
}

View File

@ -14,6 +14,7 @@
***************************************************************************/
/* $Id$ */
#include "qgslogger.h"
#include "qgsmaptool.h"
#include "qgsmaplayer.h"
#include "qgsmapcanvas.h"
@ -29,6 +30,7 @@ QgsMapTool::QgsMapTool(QgsMapCanvas* canvas)
QgsMapTool::~QgsMapTool()
{
mCanvas->unsetMapTool(this);
}
@ -78,6 +80,7 @@ void QgsMapTool::activate()
// set cursor (map tools usually set it in constructor)
mCanvas->setCursor(mCursor);
QgsDebugMsg("Cursor has been set");
}

View File

@ -421,4 +421,7 @@ void QgsMapToolIdentify::resultsDialogGone()
mResults = 0;
}
// ENDS
void QgsMapToolIdentify::deactivate()
{
mResults->done(0); // close the window
}

View File

@ -54,6 +54,8 @@ class QgsMapToolIdentify : public QObject, public QgsMapTool
//! Overridden mouse release event
virtual void canvasReleaseEvent(QMouseEvent * e);
//! called when map tool is being deactivated
virtual void deactivate();
private:

View File

@ -51,16 +51,26 @@ QgsMeasure::QgsMeasure(bool measureArea, QgsMapCanvas *mc, Qt::WFlags f)
updateUi();
connect( mMapCanvas, SIGNAL(renderComplete(QPainter*)), this, SLOT(mapCanvasChanged()) );
restorePosition();
mCalc = new QgsDistanceArea;
mRubberBand = new QgsRubberBand(mMapCanvas, mMeasureArea);
mRubberBand->show();
mCanvas->setCursor(Qt::CrossCursor);
}
void QgsMeasure::activate()
{
restorePosition();
QgsMapTool::activate();
}
void QgsMeasure::deactivate()
{
close();
QgsMapTool::deactivate();
}
void QgsMeasure::setMeasureArea(bool measureArea)
{

View File

@ -72,6 +72,11 @@ public:
//! Mouse release event for overriding
virtual void canvasReleaseEvent(QMouseEvent * e);
//! called when set as currently active map tool
virtual void activate();
//! called when map tool is being deactivated
virtual void deactivate();
public slots:
//! Close

View File

@ -115,6 +115,18 @@ QgsPointDialog::QgsPointDialog(QString layerPath, QgisIface* theQgisInterface,
mCanvas->setMinimumWidth(400);
mCanvas->freeze(true);
// set up map tools
mToolZoomIn = new QgsMapToolZoom(mCanvas, FALSE /* zoomOut */);
mToolZoomIn->setAction(mActionZoomIn);
mToolZoomOut = new QgsMapToolZoom(mCanvas, TRUE /* zoomOut */);
mToolZoomOut->setAction(mActionZoomOut);
mToolPan = new QgsMapToolPan(mCanvas);
mToolPan->setAction(mActionPan);
mToolAddPoint = new QgsGeorefTool(mCanvas, this, TRUE /* addPoint */);
mToolAddPoint->setAction(mActionAddPoint);
mToolDeletePoint = new QgsGeorefTool(mCanvas, this, FALSE /* addPoint */);
mToolDeletePoint->setAction(mActionDeletePoint);
// open raster layer
QgsRasterLayer* layer = new QgsRasterLayer(layerPath, "Raster");
mLayer = layer;
@ -173,6 +185,12 @@ QgsPointDialog::~QgsPointDialog()
{
QgsMapLayerRegistry::instance()->removeMapLayer(mLayer->getLayerID(), FALSE);
}
delete mToolZoomIn;
delete mToolZoomOut;
delete mToolPan;
delete mToolAddPoint;
delete mToolDeletePoint;
}
@ -378,17 +396,13 @@ bool QgsPointDialog::generateWorldFile()
void QgsPointDialog::zoomIn()
{
QgsMapToolZoom* tool = new QgsMapToolZoom(mCanvas, FALSE /* zoomOut */);
tool->setAction(mActionZoomIn);
mCanvas->setMapTool(tool);
mCanvas->setMapTool(mToolZoomIn);
}
void QgsPointDialog::zoomOut()
{
QgsMapToolZoom* tool = new QgsMapToolZoom(mCanvas, TRUE /* zoomOut */);
tool->setAction(mActionZoomOut);
mCanvas->setMapTool(tool);
mCanvas->setMapTool(mToolZoomOut);
}
@ -401,25 +415,19 @@ void QgsPointDialog::zoomToLayer()
void QgsPointDialog::pan()
{
QgsMapToolPan* tool = new QgsMapToolPan(mCanvas);
tool->setAction(mActionPan);
mCanvas->setMapTool(tool);
mCanvas->setMapTool(mToolPan);
}
void QgsPointDialog::addPoint()
{
QgsGeorefTool* tool = new QgsGeorefTool(mCanvas, this, TRUE /* addPoint */);
tool->setAction(mActionAddPoint);
mCanvas->setMapTool(tool);
mCanvas->setMapTool(mToolAddPoint);
}
void QgsPointDialog::deletePoint()
{
QgsGeorefTool* tool = new QgsGeorefTool(mCanvas, this, FALSE /* addPoint */);
tool->setAction(mActionDeletePoint);
mCanvas->setMapTool(tool);
mCanvas->setMapTool(mToolDeletePoint);
}

View File

@ -25,6 +25,7 @@
class QAction;
class QActionGroup;
class QgsGeorefDataPoint;
class QgsMapTool;
class QgisIface;
class QgsPointDialog : public QDialog, private Ui::QgsPointDialogBase
@ -73,6 +74,12 @@ private:
QgsMapCanvas* mCanvas;
QgsRasterLayer* mLayer;
QgsMapTool* mToolZoomIn;
QgsMapTool* mToolZoomOut;
QgsMapTool* mToolPan;
QgsMapTool* mToolAddPoint;
QgsMapTool* mToolDeletePoint;
// std::vector<QgsPoint> mPixelCoords, mMapCoords;
// std::vector<QString> mAcetateIDs;
std::vector<QgsGeorefDataPoint*> mPoints;

View File

@ -926,8 +926,8 @@ QgsGrassEdit::~QgsGrassEdit()
// destructor is called
if (mInited)
{
if ( mMapTool ) mCanvas->unsetMapTool ( mMapTool );
// TODO: delete tool? Probably
// delete tool if exists
delete mMapTool;
eraseDynamic();
mRubberBandLine->hide();
@ -1244,11 +1244,12 @@ void QgsGrassEdit::startTool(int tool)
if ( mSelectedLine > 0 )
displayElement ( mSelectedLine, mSymb[mLineSymb[mSelectedLine]], mSize );
// close old tool by setting NULL tool
// TODO: delete old tool? (check in set/unsetMapTool canvas methods)
if ( mMapTool ) mCanvas->unsetMapTool ( mMapTool );
mCanvas->setMapTool(NULL); // ? necessary
mMapTool = NULL;
// close old tool
if (mMapTool)
{
delete mMapTool;
mMapTool = NULL;
}
// All necessary data were written -> reset mEditPoints etc.
Vect_reset_line ( mEditPoints );
@ -1263,62 +1264,61 @@ void QgsGrassEdit::startTool(int tool)
// Start new tool
mTool = tool;
QgsMapTool* t = NULL;
switch (mTool)
{
case NEW_POINT:
t = new QgsGrassEditNewPoint(this, false);
t->setAction(mNewPointAction);
mMapTool = new QgsGrassEditNewPoint(this, false);
mMapTool->setAction(mNewPointAction);
break;
case NEW_CENTROID:
t = new QgsGrassEditNewPoint(this, true);
t->setAction(mNewCentroidAction);
mMapTool = new QgsGrassEditNewPoint(this, true);
mMapTool->setAction(mNewCentroidAction);
break;
case NEW_LINE:
t = new QgsGrassEditNewLine(this, false);
t->setAction(mNewLineAction);
mMapTool = new QgsGrassEditNewLine(this, false);
mMapTool->setAction(mNewLineAction);
break;
case NEW_BOUNDARY:
t = new QgsGrassEditNewLine(this, true);
t->setAction(mNewBoundaryAction);
mMapTool = new QgsGrassEditNewLine(this, true);
mMapTool->setAction(mNewBoundaryAction);
break;
case MOVE_VERTEX:
t = new QgsGrassEditMoveVertex(this);
t->setAction(mMoveVertexAction);
mMapTool = new QgsGrassEditMoveVertex(this);
mMapTool->setAction(mMoveVertexAction);
break;
case ADD_VERTEX:
t = new QgsGrassEditAddVertex(this);
t->setAction(mAddVertexAction);
mMapTool = new QgsGrassEditAddVertex(this);
mMapTool->setAction(mAddVertexAction);
break;
case DELETE_VERTEX:
t = new QgsGrassEditDeleteVertex(this);
t->setAction(mDeleteVertexAction);
mMapTool = new QgsGrassEditDeleteVertex(this);
mMapTool->setAction(mDeleteVertexAction);
break;
case MOVE_LINE:
t = new QgsGrassEditMoveLine(this);
t->setAction(mMoveLineAction);
mMapTool = new QgsGrassEditMoveLine(this);
mMapTool->setAction(mMoveLineAction);
break;
case DELETE_LINE:
t = new QgsGrassEditDeleteLine(this);
t->setAction(mDeleteLineAction);
mMapTool = new QgsGrassEditDeleteLine(this);
mMapTool->setAction(mDeleteLineAction);
break;
case SPLIT_LINE:
t = new QgsGrassEditSplitLine(this);
t->setAction(mSplitLineAction);
mMapTool = new QgsGrassEditSplitLine(this);
mMapTool->setAction(mSplitLineAction);
break;
case EDIT_ATTRIBUTES:
t = new QgsGrassEditAttributes(this);
t->setAction(mEditAttributesAction);
mMapTool = new QgsGrassEditAttributes(this);
mMapTool->setAction(mEditAttributesAction);
break;
case EDIT_CATS:
@ -1332,9 +1332,7 @@ void QgsGrassEdit::startTool(int tool)
}
// assign newly created tool to map canvas
// canvas will take care of destroying it
mCanvas->setMapTool(t);
mMapTool = t;
mCanvas->setMapTool(mMapTool);
}

View File

@ -290,6 +290,7 @@ void QgsGrassRegion::setGuiValues( bool north, bool south, bool east, bool west,
QgsGrassRegion::~QgsGrassRegion ()
{
delete mRegionEdit;
}
void QgsGrassRegion::northChanged(const QString &str)