Skip to content
Permalink
Browse files

Switch to flags for map tool behaviour

  • Loading branch information
nyalldawson committed May 29, 2016
1 parent 71f8e3e commit 8628f211dd71382d4f0b131c8272213b616f33bb
@@ -30,6 +30,22 @@ class QgsMapTool : QObject

public:

//! Enumeration of flags that adjust the way the map tool operates
//! @note added in QGIS 2.16
enum Flag
{
Transient, /*!< Indicates that this map tool performs a transient (one-off) operation.
If it does, the tool can be operated once and then a previous map
tool automatically restored. */
EditTool, /*!< Map tool is an edit tool, which can only be used when layer is editable*/
};
typedef QFlags<QgsMapTool::Flag> Flags;

/** Returns the flags for the map tool.
* @note added in QGIS 2.16
*/
virtual Flags flags() const;

//! virtual destructor
virtual ~QgsMapTool();

@@ -85,13 +101,16 @@ class QgsMapTool : QObject

/** Check whether this MapTool performs a zoom or pan operation.
* If it does, we will be able to perform the zoom and then
* resume operations with the original / previously used tool.*/
virtual bool isTransient();
* resume operations with the original / previously used tool.
* @deprecated use flags() instead
*/
virtual bool isTransient() const /Deprecated/;

/** Check whether this MapTool performs an edit operation.
* If it does, we will deactivate it when editing is turned off
* If it does, we will deactivate it when editing is turned off.
* @deprecated use flags() instead
*/
virtual bool isEditTool();
virtual bool isEditTool() const /Deprecated/;

//! called when set as currently active map tool
virtual void activate();
@@ -25,11 +25,7 @@ class QgsMapToolEdit: QgsMapTool
QgsMapToolEdit( QgsMapCanvas* canvas );
virtual ~QgsMapToolEdit();

/**
* Is this an edit tool?
* @return Of course it is or you would not be inheriting from it.
*/
virtual bool isEditTool();
virtual Flags flags() const;

protected:

@@ -9,6 +9,8 @@ class QgsMapToolPan : QgsMapTool
//! constructor
QgsMapToolPan( QgsMapCanvas* canvas );

virtual Flags flags() const;

//! Mouse press event
virtual void canvasPressEvent( QgsMapMouseEvent* e );

@@ -18,6 +20,5 @@ class QgsMapToolPan : QgsMapTool
//! Overridden mouse release event
virtual void canvasReleaseEvent( QgsMapMouseEvent* e );

virtual bool isTransient();
};

@@ -11,6 +11,8 @@ class QgsMapToolZoom : QgsMapTool

~QgsMapToolZoom();

virtual Flags flags() const;

//! Overridden mouse move event
virtual void canvasMoveEvent( QgsMapMouseEvent* e );

@@ -20,9 +22,6 @@ class QgsMapToolZoom : QgsMapTool
//! Overridden mouse release event
virtual void canvasReleaseEvent( QgsMapMouseEvent* e );

//! indicates whether we're zooming in or out
virtual bool isTransient();

//! Flag to indicate a map canvas drag operation is taking place
virtual void deactivate();
};
@@ -9763,7 +9763,7 @@ void QgisApp::mapToolChanged( QgsMapTool *newTool, QgsMapTool *oldTool )

if ( newTool )
{
if ( !newTool->isEditTool() )
if ( !( newTool->flags() & QgsMapTool::EditTool ) )
{
mNonEditMapTool = newTool;
}
@@ -10205,7 +10205,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
mActionFeatureAction->setEnabled( layerHasActions );

if ( !isEditable && mMapCanvas && mMapCanvas->mapTool()
&& mMapCanvas->mapTool()->isEditTool() && !mSaveRollbackInProgress )
&& ( mMapCanvas->mapTool()->flags() & QgsMapTool::EditTool ) && !mSaveRollbackInProgress )
{
mMapCanvas->setMapTool( mNonEditMapTool );
}
@@ -34,8 +34,9 @@ class APP_EXPORT QgsMapToolPointSymbol: public QgsMapToolEdit
public:
QgsMapToolPointSymbol( QgsMapCanvas* canvas );

virtual Flags flags() const { return QgsMapTool::EditTool; }

void canvasPressEvent( QgsMapMouseEvent* e ) override;
bool isEditTool() override { return true; }

protected:
QgsVectorLayer* mActiveLayer;
@@ -1386,7 +1386,7 @@ void QgsMapCanvas::mouseReleaseEvent( QMouseEvent* e )
if ( mMapTool )
{
// right button was pressed in zoom tool? return to previous non zoom tool
if ( e->button() == Qt::RightButton && mMapTool->isTransient() )
if ( e->button() == Qt::RightButton && mMapTool->flags() & QgsMapTool::Transient )
{
QgsDebugMsg( "Right click in map tool zoom or pan, last tool is " +
QString( mLastNonZoomMapTool ? "not null." : "null." ) );
@@ -1395,7 +1395,8 @@ void QgsMapCanvas::mouseReleaseEvent( QMouseEvent* e )

// change to older non-zoom tool
if ( mLastNonZoomMapTool
&& ( !mLastNonZoomMapTool->isEditTool() || ( vlayer && vlayer->isEditable() ) ) )
&& ( !( mLastNonZoomMapTool->flags() & QgsMapTool::EditTool )
|| ( vlayer && vlayer->isEditable() ) ) )
{
QgsMapTool* t = mLastNonZoomMapTool;
mLastNonZoomMapTool = nullptr;
@@ -1601,7 +1602,8 @@ void QgsMapCanvas::setMapTool( QgsMapTool* tool )
mMapTool->deactivate();
}

if ( tool->isTransient() && mMapTool && !mMapTool->isTransient() )
if (( tool->flags() & QgsMapTool::Transient )
&& mMapTool && !( mMapTool->flags() & QgsMapTool::Transient ) )
{
// if zoom or pan tool will be active, save old tool
// to bring it back on right click
@@ -189,14 +189,14 @@ void QgsMapTool::renderComplete()
{
}

bool QgsMapTool::isTransient()
bool QgsMapTool::isTransient() const
{
return false;
return flags() & Transient;
}

bool QgsMapTool::isEditTool()
bool QgsMapTool::isEditTool() const
{
return false;
return flags() & EditTool;
}

QgsMapCanvas* QgsMapTool::canvas()
@@ -54,6 +54,22 @@ class GUI_EXPORT QgsMapTool : public QObject

public:

//! Enumeration of flags that adjust the way the map tool operates
//! @note added in QGIS 2.16
enum Flag
{
Transient = 1 << 1, /*!< Indicates that this map tool performs a transient (one-off) operation.
If it does, the tool can be operated once and then a previous map
tool automatically restored. */
EditTool = 1 << 2, /*!< Map tool is an edit tool, which can only be used when layer is editable*/
};
Q_DECLARE_FLAGS( Flags, Flag )

/** Returns the flags for the map tool.
* @note added in QGIS 2.16
*/
virtual Flags flags() const { return Flags(); }

//! virtual destructor
virtual ~QgsMapTool();

@@ -87,7 +103,6 @@ class GUI_EXPORT QgsMapTool : public QObject
//! @deprecated since 2.4 - not called anymore - map tools must not directly depend on rendering progress
Q_DECL_DEPRECATED virtual void renderComplete();


/** Use this to associate a QAction to this maptool. Then when the setMapTool
* method of mapcanvas is called the action state will be set to on.
* Usually this will cause e.g. a toolbutton to appear pressed in and
@@ -109,13 +124,16 @@ class GUI_EXPORT QgsMapTool : public QObject

/** Check whether this MapTool performs a zoom or pan operation.
* If it does, we will be able to perform the zoom and then
* resume operations with the original / previously used tool.*/
virtual bool isTransient();
* resume operations with the original / previously used tool.
* @deprecated use flags() instead
*/
Q_DECL_DEPRECATED virtual bool isTransient() const;

/** Check whether this MapTool performs an edit operation.
* If it does, we will deactivate it when editing is turned off
* If it does, we will deactivate it when editing is turned off.
* @deprecated use flags() instead
*/
virtual bool isEditTool();
Q_DECL_DEPRECATED virtual bool isEditTool() const;

//! called when set as currently active map tool
virtual void activate();
@@ -33,11 +33,7 @@ class GUI_EXPORT QgsMapToolEdit: public QgsMapTool
QgsMapToolEdit( QgsMapCanvas* canvas );
virtual ~QgsMapToolEdit();

/**
* Is this an edit tool?
* @return Of course it is or you would not be inheriting from it.
*/
virtual bool isEditTool() override { return true; }
virtual Flags flags() const { return QgsMapTool::EditTool; }

protected:

@@ -32,6 +32,8 @@ class GUI_EXPORT QgsMapToolPan : public QgsMapTool
//! constructor
QgsMapToolPan( QgsMapCanvas* canvas );

virtual Flags flags() const { return QgsMapTool::Transient; }

//! Mouse press event
virtual void canvasPressEvent( QgsMapMouseEvent* e ) override;

@@ -41,8 +43,6 @@ class GUI_EXPORT QgsMapToolPan : public QgsMapTool
//! Overridden mouse release event
virtual void canvasReleaseEvent( QgsMapMouseEvent* e ) override;

virtual bool isTransient() override { return true; }

private:

//! Flag to indicate a map canvas drag operation is taking place
@@ -35,6 +35,8 @@ class GUI_EXPORT QgsMapToolZoom : public QgsMapTool

~QgsMapToolZoom();

virtual Flags flags() const { return QgsMapTool::Transient; }

//! Overridden mouse move event
virtual void canvasMoveEvent( QgsMapMouseEvent* e ) override;

@@ -44,10 +46,6 @@ class GUI_EXPORT QgsMapToolZoom : public QgsMapTool
//! Overridden mouse release event
virtual void canvasReleaseEvent( QgsMapMouseEvent* e ) override;

//! indicates whether we're zooming in or out
virtual bool isTransient() override { return true; }

//! Flag to indicate a map canvas drag operation is taking place
virtual void deactivate() override;

protected:

0 comments on commit 8628f21

Please sign in to comment.
You can’t perform that action at this time.