Skip to content

Commit

Permalink
Switch to flags for map tool behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 30, 2016
1 parent 71f8e3e commit 8628f21
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 39 deletions.
27 changes: 23 additions & 4 deletions python/gui/qgsmaptool.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand Down
6 changes: 1 addition & 5 deletions python/gui/qgsmaptooledit.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
3 changes: 2 additions & 1 deletion python/gui/qgsmaptoolpan.sip
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class QgsMapToolPan : QgsMapTool
//! constructor
QgsMapToolPan( QgsMapCanvas* canvas );

virtual Flags flags() const;

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

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

virtual bool isTransient();
};

5 changes: 2 additions & 3 deletions python/gui/qgsmaptoolzoom.sip
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class QgsMapToolZoom : QgsMapTool

~QgsMapToolZoom();

virtual Flags flags() const;

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

Expand All @@ -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();
};
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9763,7 +9763,7 @@ void QgisApp::mapToolChanged( QgsMapTool *newTool, QgsMapTool *oldTool )

if ( newTool )
{
if ( !newTool->isEditTool() )
if ( !( newTool->flags() & QgsMapTool::EditTool ) )
{
mNonEditMapTool = newTool;
}
Expand Down Expand Up @@ -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 );
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsmaptoolpointsymbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/gui/qgsmapcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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." ) );
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/gui/qgsmaptool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
28 changes: 23 additions & 5 deletions src/gui/qgsmaptool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down
6 changes: 1 addition & 5 deletions src/gui/qgsmaptooledit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsmaptoolpan.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/gui/qgsmaptoolzoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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:
Expand Down

0 comments on commit 8628f21

Please sign in to comment.