Skip to content

Commit

Permalink
Ownership of map tools is not given to map canvas anymore, this means…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
wonder committed Oct 12, 2006
1 parent 98497bd commit 0239405
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 131 deletions.
114 changes: 59 additions & 55 deletions src/gui/qgisapp.cpp
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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()
Expand All @@ -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);
}


Expand Down Expand Up @@ -3316,72 +3349,43 @@ 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 &)));
}

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


Expand Down
19 changes: 19 additions & 0 deletions src/gui/qgisapp.h
Expand Up @@ -37,6 +37,7 @@ class QSplashScreen;
class QgsPoint;
class QgsLegend;
class QgsMapLayer;
class QgsMapTool;
class QgsProviderRegistry;
class QgsHelpViewer;
class QgsMapCanvas;
Expand Down Expand Up @@ -521,6 +522,24 @@ public slots:
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;

Expand Down
33 changes: 14 additions & 19 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -831,9 +827,8 @@ void QgsMapCanvas::unsetMapTool(QgsMapTool* tool)
mMapTool = NULL;
}

if ( mLastNonZoomMapTool && mLastNonZoomMapTool == tool)
if (mLastNonZoomMapTool && mLastNonZoomMapTool == tool)
{
mLastNonZoomMapTool->deactivate(); // ? necessary
mLastNonZoomMapTool = NULL;
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsmapcanvasitem.cpp
Expand Up @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgsmaptool.cpp
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/
/* $Id$ */

#include "qgslogger.h"
#include "qgsmaptool.h"
#include "qgsmaplayer.h"
#include "qgsmapcanvas.h"
Expand All @@ -29,6 +30,7 @@ QgsMapTool::QgsMapTool(QgsMapCanvas* canvas)

QgsMapTool::~QgsMapTool()
{
mCanvas->unsetMapTool(this);
}


Expand Down Expand Up @@ -78,6 +80,7 @@ void QgsMapTool::activate()

// set cursor (map tools usually set it in constructor)
mCanvas->setCursor(mCursor);
QgsDebugMsg("Cursor has been set");
}


Expand Down
5 changes: 4 additions & 1 deletion src/gui/qgsmaptoolidentify.cpp
Expand Up @@ -421,4 +421,7 @@ void QgsMapToolIdentify::resultsDialogGone()
mResults = 0;
}

// ENDS
void QgsMapToolIdentify::deactivate()
{
mResults->done(0); // close the window
}
2 changes: 2 additions & 0 deletions src/gui/qgsmaptoolidentify.h
Expand Up @@ -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:

Expand Down

0 comments on commit 0239405

Please sign in to comment.