Skip to content

Commit

Permalink
Move click-and-drag detection to QgsLayoutViewTool
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 11, 2017
1 parent 5cac2f7 commit 867bdb6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
9 changes: 9 additions & 0 deletions python/gui/layout/qgslayoutviewtool.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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

};

/************************************************************************
Expand Down
11 changes: 11 additions & 0 deletions src/gui/layout/qgslayoutviewtool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/gui/layout/qgslayoutviewtool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 1 addition & 8 deletions src/gui/layout/qgslayoutviewtooladditem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down
2 changes: 0 additions & 2 deletions src/gui/layout/qgslayoutviewtoolpan.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include "qgis.h"
#include "qgis_gui.h"
#include "qgslayoutviewtool.h"
#include "qgslayoutviewrubberband.h"
#include <memory>

/**
* \ingroup gui
Expand Down
8 changes: 8 additions & 0 deletions tests/src/gui/testqgslayoutview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 867bdb6

Please sign in to comment.