Skip to content
Permalink
Browse files

Move click-and-drag detection to QgsLayoutViewTool

  • Loading branch information
nyalldawson committed Jul 11, 2017
1 parent 5cac2f7 commit 867bdb6117e0725361d6395ebbd4b4b70809acc4
@@ -153,6 +153,15 @@ class QgsLayoutViewTool : QObject
tool ``name`` as parameters.
%End

bool isClickAndDrag( QPoint startViewPoint, QPoint endViewPoint ) const;
%Docstring
Returns true if a mouse press/release operation which started at
``startViewPoint`` and ended at ``endViewPoint`` should be considered
a "click and drag". If false is returned, the operation should be
instead treated as just a click on ``startViewPoint``.
:rtype: bool
%End

};

/************************************************************************
@@ -25,6 +25,17 @@ QgsLayoutViewTool::QgsLayoutViewTool( QgsLayoutView *view, const QString &name )

}

bool QgsLayoutViewTool::isClickAndDrag( QPoint startViewPoint, QPoint endViewPoint ) const
{
int diffX = endViewPoint.x() - startViewPoint.x();
int diffY = endViewPoint.y() - startViewPoint.y();
if ( qAbs( diffX ) >= 2 || qAbs( diffY ) >= 2 )
{
return true;
}
return false;
}

QgsLayoutView *QgsLayoutViewTool::view() const
{
return mView;
@@ -173,6 +173,14 @@ class GUI_EXPORT QgsLayoutViewTool : public QObject
*/
QgsLayoutViewTool( QgsLayoutView *view SIP_TRANSFERTHIS, const QString &name );

/**
* Returns true if a mouse press/release operation which started at
* \a startViewPoint and ended at \a endViewPoint should be considered
* a "click and drag". If false is returned, the operation should be
* instead treated as just a click on \a startViewPoint.
*/
bool isClickAndDrag( QPoint startViewPoint, QPoint endViewPoint ) const;

private:

//! Pointer to layout view.
@@ -74,14 +74,7 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
QRectF rect = mRubberBand->finish( event->layoutPoint(), event->modifiers() );

// click? or click-and-drag?
QPoint mousePressStopPoint = event->pos();
int diffX = mousePressStopPoint.x() - mMousePressStartPos.x();
int diffY = mousePressStopPoint.y() - mMousePressStartPos.y();
bool clickOnly = false;
if ( qAbs( diffX ) < 2 && qAbs( diffY ) < 2 )
{
clickOnly = true;
}
bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
Q_UNUSED( clickOnly );

QgsLayoutItem *item = QgsApplication::layoutItemRegistry()->createItem( mItemType, layout() );
@@ -19,8 +19,6 @@
#include "qgis.h"
#include "qgis_gui.h"
#include "qgslayoutviewtool.h"
#include "qgslayoutviewrubberband.h"
#include <memory>

/**
* \ingroup gui
@@ -79,6 +79,14 @@ void TestQgsLayoutView::tool()
QgsLayoutViewTool *tool = new QgsLayoutViewTool( view, QStringLiteral( "name" ) );
QgsLayoutViewTool *tool2 = new QgsLayoutViewTool( view, QStringLiteral( "name2" ) );

QVERIFY( tool->isClickAndDrag( QPoint( 0, 10 ), QPoint( 5, 10 ) ) );
QVERIFY( tool->isClickAndDrag( QPoint( 0, 10 ), QPoint( 5, 15 ) ) );
QVERIFY( tool->isClickAndDrag( QPoint( 5, 10 ), QPoint( 5, 15 ) ) );
QVERIFY( !tool->isClickAndDrag( QPoint( 0, 10 ), QPoint( 1, 11 ) ) );
QVERIFY( !tool->isClickAndDrag( QPoint( 1, 10 ), QPoint( 1, 11 ) ) );
QVERIFY( !tool->isClickAndDrag( QPoint( 0, 10 ), QPoint( 1, 10 ) ) );
QVERIFY( !tool->isClickAndDrag( QPoint( 0, 10 ), QPoint( 0, 10 ) ) );

QSignalSpy spySetTool( view, &QgsLayoutView::toolSet );
QSignalSpy spyToolActivated( tool, &QgsLayoutViewTool::activated );
QSignalSpy spyToolActivated2( tool2, &QgsLayoutViewTool::activated );

0 comments on commit 867bdb6

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