Skip to content

Commit

Permalink
[QgsQuick] Add moveTo animation to qgsquick map canvas and introduce …
Browse files Browse the repository at this point in the history
…qgsquick utils (#50810)
  • Loading branch information
tomasMizera committed Nov 14, 2022
1 parent 21edf1a commit 13498bd
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/quickgui/CMakeLists.txt
Expand Up @@ -5,13 +5,15 @@ set(QGIS_QUICK_GUI_MOC_HDRS
qgsquickmapcanvasmap.h
qgsquickmapsettings.h
qgsquickmaptransform.h
qgsquickutils.h
)

set(QGIS_QUICK_GUI_SRC
qgsquickcoordinatetransformer.cpp
qgsquickmapcanvasmap.cpp
qgsquickmapsettings.cpp
qgsquickmaptransform.cpp
qgsquickutils.cpp
)

include_directories(
Expand Down
66 changes: 62 additions & 4 deletions src/quickgui/plugin/qgsquickmapcanvas.qml
Expand Up @@ -55,9 +55,6 @@ Item {
//! This signal is emitted independently of double tap / click
signal clicked(var point)

//! This signal is only emitted if there is no double tap/click coming after a short delay
// signal confirmedClicked(var point) // TODO: ideally get rid of this one

//! Signal emitted when user holds pointer on map
signal longPressed(var point)

Expand Down Expand Up @@ -103,6 +100,32 @@ Item {
mapCanvasWrapper.refresh()
}

/**
* Animates movement of map canvas from the current center to newPos.
*
* newPos needs to be in device pixels.
*/
function moveTo( newPos )
{
freeze('moveTo')

let newPosMapCRS = mapCanvasWrapper.mapSettings.screenToCoordinate( newPos )
let oldPosMapCRS = mapCanvasWrapper.mapSettings.center

// Disable animation until initial position is set
jumpAnimator.enabled = false

jumpAnimator.px = oldPosMapCRS.x
jumpAnimator.py = oldPosMapCRS.y

jumpAnimator.enabled = true

jumpAnimator.px = newPosMapCRS.x
jumpAnimator.py = newPosMapCRS.y

unfreeze('moveTo')
}

QgsQuick.MapCanvasMap {
id: mapCanvasWrapper

Expand All @@ -114,6 +137,41 @@ Item {
freeze: false
}

Item {
id: jumpAnimator

property double px: 0
property double py: 0

Behavior on py {
SmoothedAnimation {
duration: 200
reversingMode: SmoothedAnimation.Immediate
}
enabled: jumpAnimator.enabled
}

Behavior on px {
SmoothedAnimation {
duration: 200
reversingMode: SmoothedAnimation.Immediate
}
enabled: jumpAnimator.enabled
}

onPxChanged: {
if ( enabled ) {
mapCanvasWrapper.mapSettings.center = QgsQuick.Utils.toQgsPoint( Qt.point( px, py ) )
}
}

onPyChanged: {
if ( enabled ) {
mapCanvasWrapper.mapSettings.center = QgsQuick.Utils.toQgsPoint( Qt.point( px, py ) )
}
}
}

// Mouse, stylus and other pointer devices handler
TapHandler {
id: stylusClick
Expand Down Expand Up @@ -330,7 +388,7 @@ Item {
target: null
grabPermissions: PointerHandler.CanTakeOverFromHandlersOfDifferentType | PointerHandler.ApprovesTakeOverByItems

onWheel: {
onWheel: function( event ) {
if (event.angleDelta.y > 0)
{
zoomIn(point.position)
Expand Down
11 changes: 11 additions & 0 deletions src/quickgui/plugin/qgsquickplugin.cpp
Expand Up @@ -35,6 +35,14 @@
#include "qgsquickmapsettings.h"
#include "qgsquickmaptransform.h"
#include "qgsquickplugin.h"
#include "qgsquickutils.h"

static QObject *buildUtilsSingleton( QQmlEngine *engine, QJSEngine *scriptEngine )
{
Q_UNUSED( engine )
Q_UNUSED( scriptEngine )
return new QgsQuickUtils(); // the object will be owned by QML engine and destroyed by the engine on exit
}

void QgsQuickPlugin::registerTypes( const char *uri )
{
Expand All @@ -58,4 +66,7 @@ void QgsQuickPlugin::registerTypes( const char *uri )
qmlRegisterType< QgsQuickMapSettings >( uri, 0, 1, "MapSettings" );
qmlRegisterType< QgsQuickMapTransform >( uri, 0, 1, "MapTransform" );
qmlRegisterType< QgsVectorLayer >( uri, 0, 1, "VectorLayer" );

qmlRegisterSingletonType< QgsQuickUtils >( uri, 0, 1, "Utils", buildUtilsSingleton );

}
26 changes: 26 additions & 0 deletions src/quickgui/qgsquickutils.cpp
@@ -0,0 +1,26 @@
/***************************************************************************
qgsquickutils.cpp
--------------------------------------
Date : 7.11.2022
Copyright : (C) 2022 by Tomas Mizera
Email : tomas.mizera (at) lutraconsulting.co.uk
***************************************************************************
* *
* 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 "qgsquickutils.h"

QgsQuickUtils::QgsQuickUtils( QObject *parent )
: QObject( parent )
{
}

QgsPoint QgsQuickUtils::toQgsPoint( const QPointF &point )
{
return QgsPoint( point );
}
53 changes: 53 additions & 0 deletions src/quickgui/qgsquickutils.h
@@ -0,0 +1,53 @@
/***************************************************************************
qgsquickutils.cpp
--------------------------------------
Date : 7.11.2022
Copyright : (C) 2022 by Tomas Mizera
Email : tomas.mizera (at) lutraconsulting.co.uk
***************************************************************************
* *
* 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 QGSQUICKUTILS_H
#define QGSQUICKUTILS_H

#include <QObject>
#include <qglobal.h>

#include "qgis_quick.h"

#include "qgspoint.h"

/**
* \ingroup quick
*
* \brief The QgsQuickUtils class serves as a utility class for common operations
* needed either from QML or cpp.
*
* \note use in qml as a singleton, registered as "Utils", e.g. QgsQuick.Utils.toQgsPoint
*
* \since QGIS 3.30
*/
class QUICK_EXPORT QgsQuickUtils : public QObject
{
Q_OBJECT

public:

//! Creates new QgsQuickUtils - this class is meant to serve as a QML singleton
explicit QgsQuickUtils( QObject *parent = nullptr );
~QgsQuickUtils() = default;

/**
* Helper function to convert QPointF to QgsPoint without any transformations.
* Useful for converting these values in QML.
*/
Q_INVOKABLE static QgsPoint toQgsPoint( const QPointF &point );
};

#endif // QGSQUICKUTILS_H

0 comments on commit 13498bd

Please sign in to comment.