Skip to content
Permalink
Browse files

Subclass QMouseEvent to QgsLayoutViewMouseEvent

For easy access to mouse event coordinates in layout coordinate
space (and future use with snapped item coordinates)
  • Loading branch information
nyalldawson committed Jul 11, 2017
1 parent a6ccd63 commit 4ebea75e6a0997af86e1f2764123995f52f467aa
@@ -19,6 +19,11 @@ class QgsLayout : QGraphicsScene
%End
public:

enum ZValues
{
ZMapTool,
};

QgsLayout();

};
@@ -42,6 +42,7 @@
%Include editorwidgets/core/qgseditorwidgetautoconf.sip
%Include layertree/qgslayertreeembeddedconfigwidget.sip
%Include layertree/qgslayertreeembeddedwidgetregistry.sip
%Include layout/qgslayoutviewmouseevent.sip
%Include locator/qgslocatorcontext.sip
%Include raster/qgsrasterrendererwidget.sip
%Include symbology-ng/qgssymbolwidgetcontext.sip
@@ -0,0 +1,58 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutviewmouseevent.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsLayoutViewMouseEvent : QMouseEvent
{
%Docstring
A QgsLayoutViewMouseEvent is the result of a user interaction with the mouse on a QgsLayoutView.

It is sent whenever the user moves, clicks, releases or double clicks the mouse.
In addition to the coordinates in pixel space it also knows the coordinates the layout space.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoutviewmouseevent.h"
%End

%ConvertToSubClassCode
if ( dynamic_cast<QgsLayoutViewMouseEvent *>( sipCpp ) )
sipType = sipType_QgsLayoutViewMouseEvent;
else
sipType = 0;
%End
public:

QgsLayoutViewMouseEvent( QgsLayoutView *view, QMouseEvent *event );
%Docstring
Constructor for QgsLayoutViewMouseEvent. Should only be required to be called from the QgsLayoutView.

\param view The view in which the event occurred.
\param event The original mouse event
%End

QPointF layoutPoint() const;
%Docstring
Returns the event point location in layout coordinates.
.. seealso:: pos()
:rtype: QPointF
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutviewmouseevent.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -36,22 +36,22 @@ class QgsLayoutViewTool : QObject

virtual ~QgsLayoutViewTool();

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

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

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

virtual void layoutReleaseEvent( QMouseEvent *event );
virtual void layoutReleaseEvent( QgsLayoutViewMouseEvent *event );
%Docstring
Mouse release event for overriding. Default implementation does nothing.
%End
@@ -110,6 +110,21 @@ class QgsLayoutViewTool : QObject
:rtype: str
%End

QgsLayoutView *view() const;
%Docstring
Returns the view associated with the tool.
.. seealso:: layout()
:rtype: QgsLayoutView
%End

QgsLayout *layout() const;
%Docstring
Returns the layout associated with the tool.
.. seealso:: view()
:rtype: QgsLayout
%End


signals:

void activated();
@@ -8,6 +8,7 @@




class QgsLayoutViewToolAddItem : QgsLayoutViewTool
{
%Docstring
@@ -35,6 +36,13 @@ class QgsLayoutViewToolAddItem : QgsLayoutViewTool
.. seealso:: itemType()
%End

virtual void layoutPressEvent( QgsLayoutViewMouseEvent *event );

virtual void layoutMoveEvent( QgsLayoutViewMouseEvent *event );

virtual void layoutReleaseEvent( QgsLayoutViewMouseEvent *event );


};

/************************************************************************
@@ -21,6 +21,7 @@
#include "qgisapp.h"
#include "qgslogger.h"
#include "qgslayoutview.h"
#include "qgslayoutviewtooladditem.h"

QgsAppLayoutDesignerInterface::QgsAppLayoutDesignerInterface( QgsLayoutDesignerDialog *dialog )
: QgsLayoutDesignerInterface( dialog )
@@ -88,6 +89,8 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
//..and listen out for new item types
connect( QgsApplication::layoutItemRegistry(), &QgsLayoutItemRegistry::typeAdded, this, &QgsLayoutDesignerDialog::itemTypeAdded );

mAddItemTool = new QgsLayoutViewToolAddItem( mView );

restoreWindowState();
}

@@ -104,7 +107,7 @@ QgsLayout *QgsLayoutDesignerDialog::currentLayout()
void QgsLayoutDesignerDialog::setCurrentLayout( QgsLayout *layout )
{
mLayout = layout;

mView->setCurrentLayout( layout );
}

void QgsLayoutDesignerDialog::setIconSizes( int size )
@@ -199,10 +202,10 @@ void QgsLayoutDesignerDialog::restoreWindowState()

void QgsLayoutDesignerDialog::activateNewItemCreationTool( int type )
{
QgsLogger::debug( QStringLiteral( "creating new %1 item " ).arg( QgsApplication::layoutItemRegistry()->itemMetadata( type )->visibleName() ) );
mAddItemTool->setItemType( type );
if ( mView )
{
//mView->setTool( QgsLayoutView::ToolAddItem );
mView->setTool( mAddItemTool );
}
}

@@ -22,7 +22,7 @@

class QgsLayoutDesignerDialog;
class QgsLayoutView;

class QgsLayoutViewToolAddItem;

class QgsAppLayoutDesignerInterface : public QgsLayoutDesignerInterface
{
@@ -112,6 +112,8 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner

QgsLayoutView *mView = nullptr;

QgsLayoutViewToolAddItem *mAddItemTool = nullptr;


//! Save window state
void saveWindowState();
@@ -31,6 +31,12 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene

public:

//! Preset item z-values, to ensure correct stacking
enum ZValues
{
ZMapTool = 10000, //!< Z-Value for temporary map tool items
};

QgsLayout();

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

layout/qgslayoutview.cpp
layout/qgslayoutviewmouseevent.cpp
layout/qgslayoutviewtool.cpp
layout/qgslayoutviewtooladditem.cpp

@@ -720,6 +721,8 @@ SET(QGIS_GUI_HDRS
layertree/qgslayertreeembeddedconfigwidget.h
layertree/qgslayertreeembeddedwidgetregistry.h

layout/qgslayoutviewmouseevent.h

locator/qgslocatorcontext.h

raster/qgsrasterrendererwidget.h
@@ -18,6 +18,8 @@
#include "qgslayoutview.h"
#include "qgslayout.h"
#include "qgslayoutviewtool.h"
#include "qgslayoutviewmouseevent.h"
#include <memory>

QgsLayoutView::QgsLayoutView( QWidget *parent )
: QGraphicsView( parent )
@@ -75,39 +77,53 @@ void QgsLayoutView::unsetTool( QgsLayoutViewTool *tool )
void QgsLayoutView::mousePressEvent( QMouseEvent *event )
{
if ( mTool )
mTool->layoutPressEvent( event );
{
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event ) );
mTool->layoutPressEvent( me.get() );
}
else
QGraphicsView::mousePressEvent( event );
}

void QgsLayoutView::mouseReleaseEvent( QMouseEvent *event )
{
if ( mTool )
mTool->layoutReleaseEvent( event );
{
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event ) );
mTool->layoutReleaseEvent( me.get() );
}
else
QGraphicsView::mouseReleaseEvent( event );
}

void QgsLayoutView::mouseMoveEvent( QMouseEvent *event )
{
if ( mTool )
mTool->layoutMoveEvent( event );
{
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event ) );
mTool->layoutMoveEvent( me.get() );
}
else
QGraphicsView::mouseMoveEvent( event );
}

void QgsLayoutView::mouseDoubleClickEvent( QMouseEvent *event )
{
if ( mTool )
mTool->layoutDoubleClickEvent( event );
{
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event ) );
mTool->layoutDoubleClickEvent( me.get() );
}
else
QGraphicsView::mouseDoubleClickEvent( event );
}

void QgsLayoutView::wheelEvent( QWheelEvent *event )
{
if ( mTool )
{
mTool->wheelEvent( event );
}
else
QGraphicsView::wheelEvent( event );
}
@@ -0,0 +1,31 @@
/***************************************************************************
qgslayoutviewmouseevent.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 "qgslayoutviewmouseevent.h"
#include "qgslayoutview.h"


QgsLayoutViewMouseEvent::QgsLayoutViewMouseEvent( QgsLayoutView *view, QMouseEvent *event )
: QMouseEvent( event->type(), event->pos(), event->button(), event->buttons(), event->modifiers() )
, mView( view )
{

}

QPointF QgsLayoutViewMouseEvent::layoutPoint() const
{
return mView->mapToScene( x(), y() );
}

0 comments on commit 4ebea75

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