Skip to content

Commit 867bdb6

Browse files
committed
Move click-and-drag detection to QgsLayoutViewTool
1 parent 5cac2f7 commit 867bdb6

File tree

6 files changed

+37
-10
lines changed

6 files changed

+37
-10
lines changed

python/gui/layout/qgslayoutviewtool.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ class QgsLayoutViewTool : QObject
153153
tool ``name`` as parameters.
154154
%End
155155

156+
bool isClickAndDrag( QPoint startViewPoint, QPoint endViewPoint ) const;
157+
%Docstring
158+
Returns true if a mouse press/release operation which started at
159+
``startViewPoint`` and ended at ``endViewPoint`` should be considered
160+
a "click and drag". If false is returned, the operation should be
161+
instead treated as just a click on ``startViewPoint``.
162+
:rtype: bool
163+
%End
164+
156165
};
157166

158167
/************************************************************************

src/gui/layout/qgslayoutviewtool.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ QgsLayoutViewTool::QgsLayoutViewTool( QgsLayoutView *view, const QString &name )
2525

2626
}
2727

28+
bool QgsLayoutViewTool::isClickAndDrag( QPoint startViewPoint, QPoint endViewPoint ) const
29+
{
30+
int diffX = endViewPoint.x() - startViewPoint.x();
31+
int diffY = endViewPoint.y() - startViewPoint.y();
32+
if ( qAbs( diffX ) >= 2 || qAbs( diffY ) >= 2 )
33+
{
34+
return true;
35+
}
36+
return false;
37+
}
38+
2839
QgsLayoutView *QgsLayoutViewTool::view() const
2940
{
3041
return mView;

src/gui/layout/qgslayoutviewtool.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ class GUI_EXPORT QgsLayoutViewTool : public QObject
173173
*/
174174
QgsLayoutViewTool( QgsLayoutView *view SIP_TRANSFERTHIS, const QString &name );
175175

176+
/**
177+
* Returns true if a mouse press/release operation which started at
178+
* \a startViewPoint and ended at \a endViewPoint should be considered
179+
* a "click and drag". If false is returned, the operation should be
180+
* instead treated as just a click on \a startViewPoint.
181+
*/
182+
bool isClickAndDrag( QPoint startViewPoint, QPoint endViewPoint ) const;
183+
176184
private:
177185

178186
//! Pointer to layout view.

src/gui/layout/qgslayoutviewtooladditem.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,7 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
7474
QRectF rect = mRubberBand->finish( event->layoutPoint(), event->modifiers() );
7575

7676
// click? or click-and-drag?
77-
QPoint mousePressStopPoint = event->pos();
78-
int diffX = mousePressStopPoint.x() - mMousePressStartPos.x();
79-
int diffY = mousePressStopPoint.y() - mMousePressStartPos.y();
80-
bool clickOnly = false;
81-
if ( qAbs( diffX ) < 2 && qAbs( diffY ) < 2 )
82-
{
83-
clickOnly = true;
84-
}
77+
bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
8578
Q_UNUSED( clickOnly );
8679

8780
QgsLayoutItem *item = QgsApplication::layoutItemRegistry()->createItem( mItemType, layout() );

src/gui/layout/qgslayoutviewtoolpan.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#include "qgis.h"
2020
#include "qgis_gui.h"
2121
#include "qgslayoutviewtool.h"
22-
#include "qgslayoutviewrubberband.h"
23-
#include <memory>
2422

2523
/**
2624
* \ingroup gui

tests/src/gui/testqgslayoutview.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ void TestQgsLayoutView::tool()
7979
QgsLayoutViewTool *tool = new QgsLayoutViewTool( view, QStringLiteral( "name" ) );
8080
QgsLayoutViewTool *tool2 = new QgsLayoutViewTool( view, QStringLiteral( "name2" ) );
8181

82+
QVERIFY( tool->isClickAndDrag( QPoint( 0, 10 ), QPoint( 5, 10 ) ) );
83+
QVERIFY( tool->isClickAndDrag( QPoint( 0, 10 ), QPoint( 5, 15 ) ) );
84+
QVERIFY( tool->isClickAndDrag( QPoint( 5, 10 ), QPoint( 5, 15 ) ) );
85+
QVERIFY( !tool->isClickAndDrag( QPoint( 0, 10 ), QPoint( 1, 11 ) ) );
86+
QVERIFY( !tool->isClickAndDrag( QPoint( 1, 10 ), QPoint( 1, 11 ) ) );
87+
QVERIFY( !tool->isClickAndDrag( QPoint( 0, 10 ), QPoint( 1, 10 ) ) );
88+
QVERIFY( !tool->isClickAndDrag( QPoint( 0, 10 ), QPoint( 0, 10 ) ) );
89+
8290
QSignalSpy spySetTool( view, &QgsLayoutView::toolSet );
8391
QSignalSpy spyToolActivated( tool, &QgsLayoutViewTool::activated );
8492
QSignalSpy spyToolActivated2( tool2, &QgsLayoutViewTool::activated );

0 commit comments

Comments
 (0)