Skip to content
Permalink
Browse files

Add shell classes for layout view tools

Copy the same model as QgsMapCanvas uses, with separate
classes for individual interaction tools instead of keeping
all the logic in the QGraphicsView subclass (as is done
with composer)
  • Loading branch information
nyalldawson committed Jul 11, 2017
1 parent b2a01d8 commit 99f34300b9fd5be683703f7b3b37add919fda9bb
@@ -276,6 +276,7 @@
%Include layertree/qgslayertreeviewdefaultactions.sip
%Include layout/qgslayoutdesignerinterface.sip
%Include layout/qgslayoutview.sip
%Include layout/qgslayoutviewtool.sip
%Include locator/qgslocator.sip
%Include locator/qgslocatorfilter.sip
%Include locator/qgslocatorwidget.sip
@@ -50,13 +50,42 @@ class QgsLayoutView: QGraphicsView
.. seealso:: layoutSet()
%End

QgsLayoutViewTool *tool();
%Docstring
Returns the currently active tool for the view.
.. seealso:: setTool()
:rtype: QgsLayoutViewTool
%End

void setTool( QgsLayoutViewTool *tool );
%Docstring
Sets the ``tool`` currently being used in the view.
.. seealso:: unsetTool()
.. seealso:: tool()
%End

void unsetTool( QgsLayoutViewTool *tool );
%Docstring
Unsets the current view tool, if it matches the specified ``tool``.

This is called from destructor of view tools to make sure
that the tool won't be used any more.
You don't have to call it manually, QgsLayoutViewTool takes care of it.
%End

signals:

void layoutSet( QgsLayout *layout );
%Docstring
Emitted when a ``layout`` is set for the view.
.. seealso:: currentLayout()
.. seealso:: setCurrentLayout()
%End

void toolSet( QgsLayoutViewTool *tool );
%Docstring
Emitted when the current ``tool`` is changed.
.. seealso:: setTool()
%End

protected:
@@ -68,6 +97,12 @@ class QgsLayoutView: QGraphicsView

virtual void mouseDoubleClickEvent( QMouseEvent *event );

virtual void wheelEvent( QWheelEvent *event );

virtual void keyPressEvent( QKeyEvent *event );

virtual void keyReleaseEvent( QKeyEvent *event );


};

@@ -0,0 +1,137 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutviewtool.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsLayoutViewTool : QObject
{
%Docstring
Abstract base class for all layout view tools.
Layout view tools are user interactive tools for manipulating and adding items
to QgsLayoutView widgets.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoutviewtool.h"
%End

%ConvertToSubClassCode
if ( dynamic_cast<QgsMapToolZoom *>( sipCpp ) != NULL )
sipType = sipType_QgsMapToolZoom;
else
sipType = NULL;
%End
public:

virtual ~QgsLayoutViewTool();

virtual void layoutMoveEvent( QMouseEvent *event );
%Docstring
Mouse move event for overriding. Default implementation does nothing.
%End

virtual void layoutDoubleClickEvent( QMouseEvent *event );
%Docstring
Mouse double click event for overriding. Default implementation does nothing.
%End

virtual void layoutPressEvent( QMouseEvent *event );
%Docstring
Mouse press event for overriding. Default implementation does nothing.
%End

virtual void layoutReleaseEvent( QMouseEvent *event );
%Docstring
Mouse release event for overriding. Default implementation does nothing.
%End

virtual void wheelEvent( QWheelEvent *event );
%Docstring
Mouse wheel event for overriding. Default implementation does nothing.
%End

virtual void keyPressEvent( QKeyEvent *event );
%Docstring
Key press event for overriding. Default implementation does nothing.
%End

virtual void keyReleaseEvent( QKeyEvent *event );
%Docstring
Key release event for overriding. Default implementation does nothing.
%End

void setAction( QAction *action );
%Docstring
Associates an ``action`` with this tool. When the setLayoutTool
method of QgsLayoutView is called the action's state will be set to on.
Usually this will cause a toolbutton to appear pressed in and
the previously used toolbutton to pop out.
.. seealso:: action()
%End

QAction *action();
%Docstring
Returns the action associated with the tool or None if no action is associated.
.. seealso:: setAction()
:rtype: QAction
%End

void setCursor( const QCursor &cursor );
%Docstring
Sets a user defined ``cursor`` for use when the tool is active.
%End

virtual void activate();
%Docstring
Called when tool is set as the currently active layout tool.
Overridden implementations must take care to call the base class implementation.
%End

virtual void deactivate();
%Docstring
Called when tool is deactivated.
Overridden implementations must take care to call the base class implementation.
%End

QString toolName() const;
%Docstring
Returns a user-visible, translated name for the tool.
:rtype: str
%End

signals:

void activated();
%Docstring
Emitted when the tool is activated.
%End

void deactivated();
%Docstring
Emitted when the tool is deactivated.
%End

protected:

QgsLayoutViewTool( QgsLayoutView *view /TransferThis/, const QString &name );
%Docstring
Constructor for QgsLayoutViewTool, taking a layout ``view`` and
tool ``name`` as parameters.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutviewtool.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -200,6 +200,10 @@ void QgsLayoutDesignerDialog::restoreWindowState()
void QgsLayoutDesignerDialog::activateNewItemCreationTool( int type )
{
QgsLogger::debug( QStringLiteral( "creating new %1 item " ).arg( QgsApplication::layoutItemRegistry()->itemMetadata( type )->visibleName() ) );
if ( mView )
{
//mView->setTool( QgsLayoutView::ToolAddItem );
}
}


@@ -159,6 +159,7 @@ SET(QGIS_GUI_SRCS
layertree/qgslayertreeviewdefaultactions.cpp

layout/qgslayoutview.cpp
layout/qgslayoutviewtool.cpp

locator/qgslocator.cpp
locator/qgslocatorfilter.cpp
@@ -623,6 +624,7 @@ SET(QGIS_GUI_MOC_HDRS

layout/qgslayoutdesignerinterface.h
layout/qgslayoutview.h
layout/qgslayoutviewtool.h

locator/qgslocator.h
locator/qgslocatorfilter.h
@@ -17,6 +17,7 @@

#include "qgslayoutview.h"
#include "qgslayout.h"
#include "qgslayoutviewtool.h"

QgsLayoutView::QgsLayoutView( QWidget *parent )
: QGraphicsView( parent )
@@ -39,22 +40,91 @@ void QgsLayoutView::setCurrentLayout( QgsLayout *layout )
emit layoutSet( layout );
}

QgsLayoutViewTool *QgsLayoutView::tool()
{
return mTool;
}

void QgsLayoutView::setTool( QgsLayoutViewTool *tool )
{
if ( !tool )
return;

if ( mTool )
{
mTool->deactivate();
}

// set new tool and activate it
mTool = tool;
mTool->activate();

emit toolSet( mTool );
}

void QgsLayoutView::unsetTool( QgsLayoutViewTool *tool )
{
if ( mTool && mTool == tool )
{
mTool->deactivate();
mTool = nullptr;
emit toolSet( nullptr );
setCursor( Qt::ArrowCursor );
}
}

void QgsLayoutView::mousePressEvent( QMouseEvent *event )
{
QGraphicsView::mousePressEvent( event );
if ( mTool )
mTool->layoutPressEvent( event );
else
QGraphicsView::mousePressEvent( event );
}

void QgsLayoutView::mouseReleaseEvent( QMouseEvent *event )
{
QGraphicsView::mouseReleaseEvent( event );
if ( mTool )
mTool->layoutReleaseEvent( event );
else
QGraphicsView::mouseReleaseEvent( event );
}

void QgsLayoutView::mouseMoveEvent( QMouseEvent *event )
{
QGraphicsView::mouseMoveEvent( event );
if ( mTool )
mTool->layoutMoveEvent( event );
else
QGraphicsView::mouseMoveEvent( event );
}

void QgsLayoutView::mouseDoubleClickEvent( QMouseEvent *event )
{
QGraphicsView::mouseDoubleClickEvent( event );
if ( mTool )
mTool->layoutDoubleClickEvent( event );
else
QGraphicsView::mouseDoubleClickEvent( event );
}

void QgsLayoutView::wheelEvent( QWheelEvent *event )
{
if ( mTool )
mTool->wheelEvent( event );
else
QGraphicsView::wheelEvent( event );
}

void QgsLayoutView::keyPressEvent( QKeyEvent *event )
{
if ( mTool )
mTool->keyPressEvent( event );
else
QGraphicsView::keyPressEvent( event );
}

void QgsLayoutView::keyReleaseEvent( QKeyEvent *event )
{
if ( mTool )
mTool->keyReleaseEvent( event );
else
QGraphicsView::keyReleaseEvent( event );
}

0 comments on commit 99f3430

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