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)
This commit is contained in:
Nyall Dawson 2017-07-04 12:55:32 +10:00
parent b2a01d84a3
commit 99f34300b9
9 changed files with 555 additions and 5 deletions

View File

@ -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

View File

@ -50,6 +50,29 @@ 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 );
@ -57,6 +80,12 @@ class QgsLayoutView: QGraphicsView
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 );
};

View File

@ -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 *
************************************************************************/

View File

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

View File

@ -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

View File

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

View File

@ -17,12 +17,14 @@
#ifndef QGSLAYOUTVIEW_H
#define QGSLAYOUTVIEW_H
#include <QGraphicsView>
#include "qgis.h"
#include "qgsprevieweffect.h" // for QgsPreviewEffect::PreviewMode
#include "qgis_gui.h"
#include <QPointer>
#include <QGraphicsView>
class QgsLayout;
class QgsLayoutViewTool;
/**
* \ingroup gui
@ -38,6 +40,7 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
Q_OBJECT
Q_PROPERTY( QgsLayout *currentLayout READ currentLayout WRITE setCurrentLayout NOTIFY layoutSet )
Q_PROPERTY( QgsLayoutViewTool *tool READ tool WRITE setTool NOTIFY toolSet )
public:
@ -67,6 +70,28 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
*/
void setCurrentLayout( QgsLayout *layout SIP_KEEPREFERENCE );
/**
* Returns the currently active tool for the view.
* \see setTool()
*/
QgsLayoutViewTool *tool();
/**
* Sets the \a tool currently being used in the view.
* \see unsetTool()
* \see tool()
*/
void setTool( QgsLayoutViewTool *tool );
/**
* Unsets the current view tool, if it matches the specified \a 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.
*/
void unsetTool( QgsLayoutViewTool *tool );
signals:
/**
@ -76,11 +101,24 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
*/
void layoutSet( QgsLayout *layout );
/**
* Emitted when the current \a tool is changed.
* \see setTool()
*/
void toolSet( QgsLayoutViewTool *tool );
protected:
void mousePressEvent( QMouseEvent *event ) override;
void mouseReleaseEvent( QMouseEvent *event ) override;
void mouseMoveEvent( QMouseEvent *event ) override;
void mouseDoubleClickEvent( QMouseEvent *event ) override;
void wheelEvent( QWheelEvent *event ) override;
void keyPressEvent( QKeyEvent *event ) override;
void keyReleaseEvent( QKeyEvent *event ) override;
private:
QPointer< QgsLayoutViewTool > mTool;
};

View File

@ -0,0 +1,100 @@
/***************************************************************************
qgslayoutviewtool.cpp
---------------------
Date : July 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgslayoutviewtool.h"
#include "qgslayoutview.h"
QgsLayoutViewTool::QgsLayoutViewTool( QgsLayoutView *view, const QString &name )
: QObject( view )
, mView( view )
, mCursor( Qt::ArrowCursor )
, mToolName( name )
{
}
QgsLayoutViewTool::~QgsLayoutViewTool()
{
mView->unsetTool( this );
}
void QgsLayoutViewTool::layoutMoveEvent( QMouseEvent * )
{
}
void QgsLayoutViewTool::layoutDoubleClickEvent( QMouseEvent * )
{
}
void QgsLayoutViewTool::layoutPressEvent( QMouseEvent * )
{
}
void QgsLayoutViewTool::layoutReleaseEvent( QMouseEvent * )
{
}
void QgsLayoutViewTool::wheelEvent( QWheelEvent * )
{
}
void QgsLayoutViewTool::keyPressEvent( QKeyEvent * )
{
}
void QgsLayoutViewTool::keyReleaseEvent( QKeyEvent * )
{
}
void QgsLayoutViewTool::setAction( QAction *action )
{
mAction = action;
}
QAction *QgsLayoutViewTool::action()
{
return mAction;
}
void QgsLayoutViewTool::setCursor( const QCursor &cursor )
{
mCursor = cursor;
}
void QgsLayoutViewTool::activate()
{
// make action and/or button active
if ( mAction )
mAction->setChecked( true );
mView->setCursor( mCursor );
emit activated();
}
void QgsLayoutViewTool::deactivate()
{
if ( mAction )
mAction->setChecked( false );
emit deactivated();
}

View File

@ -0,0 +1,163 @@
/***************************************************************************
qgslayoutviewtool.h
-------------------
Date : July 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSLAYOUTVIEWTOOL_H
#define QGSLAYOUTVIEWTOOL_H
#include "qgis.h"
#include "qgis_gui.h"
#include <QCursor>
#include <QAction>
#include <QPointer>
class QMouseEvent;
class QWheelEvent;
class QKeyEvent;
class QgsLayoutView;
/**
* \ingroup gui
* Abstract base class for all layout view tools.
* Layout view tools are user interactive tools for manipulating and adding items
* to QgsLayoutView widgets.
* \since QGIS 3.0
*/
class GUI_EXPORT QgsLayoutViewTool : public QObject
{
#ifdef SIP_RUN
SIP_CONVERT_TO_SUBCLASS_CODE
if ( dynamic_cast<QgsMapToolZoom *>( sipCpp ) != NULL )
sipType = sipType_QgsMapToolZoom;
else
sipType = NULL;
SIP_END
#endif
Q_OBJECT
public:
virtual ~QgsLayoutViewTool();
/**
* Mouse move event for overriding. Default implementation does nothing.
*/
virtual void layoutMoveEvent( QMouseEvent *event );
/**
* Mouse double click event for overriding. Default implementation does nothing.
*/
virtual void layoutDoubleClickEvent( QMouseEvent *event );
/**
* Mouse press event for overriding. Default implementation does nothing.
*/
virtual void layoutPressEvent( QMouseEvent *event );
/**
* Mouse release event for overriding. Default implementation does nothing.
*/
virtual void layoutReleaseEvent( QMouseEvent *event );
/**
* Mouse wheel event for overriding. Default implementation does nothing.
*/
virtual void wheelEvent( QWheelEvent *event );
/**
* Key press event for overriding. Default implementation does nothing.
*/
virtual void keyPressEvent( QKeyEvent *event );
/**
* Key release event for overriding. Default implementation does nothing.
*/
virtual void keyReleaseEvent( QKeyEvent *event );
/**
* Associates an \a 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.
* \see action()
*/
void setAction( QAction *action );
/**
* Returns the action associated with the tool or nullptr if no action is associated.
* \see setAction()
*/
QAction *action();
/**
* Sets a user defined \a cursor for use when the tool is active.
*/
void setCursor( const QCursor &cursor );
/**
* Called when tool is set as the currently active layout tool.
* Overridden implementations must take care to call the base class implementation.
*/
virtual void activate();
/**
* Called when tool is deactivated.
* Overridden implementations must take care to call the base class implementation.
*/
virtual void deactivate();
/**
* Returns a user-visible, translated name for the tool.
*/
QString toolName() const { return mToolName; }
signals:
/**
* Emitted when the tool is activated.
*/
void activated();
/**
* Emitted when the tool is deactivated.
*/
void deactivated();
protected:
/**
* Constructor for QgsLayoutViewTool, taking a layout \a view and
* tool \a name as parameters.
*/
QgsLayoutViewTool( QgsLayoutView *view SIP_TRANSFERTHIS, const QString &name );
private:
//! Pointer to layout view.
QgsLayoutView *mView = nullptr;
//! Cursor used by tool
QCursor mCursor;
//! Optional action associated with tool
QPointer< QAction > mAction;
//! Translated name of the map tool
QString mToolName;
};
#endif // QGSLAYOUTVIEWTOOL_H