Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[qgsquick] New map to screen class to tie QML items to the map canvas
- Loading branch information
Showing
4 changed files
with
239 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/*************************************************************************** | ||
qgsquickmaptoscreen.cpp | ||
---------------------------------------------------- | ||
Date : 22.08.2018 | ||
Copyright : (C) 2018 by Denis Rouzaud | ||
Email : denis (at) opengis.ch | ||
*************************************************************************** | ||
* * | ||
* 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 "qgsquickmaptoscreen.h" | ||
#include "qgspoint.h" | ||
|
||
QgsQuickMapToScreen::QgsQuickMapToScreen( QObject *parent ) | ||
: QObject( parent ) | ||
{ | ||
} | ||
|
||
void QgsQuickMapToScreen::setMapSettings( QgsQuickMapSettings *mapSettings ) | ||
{ | ||
if ( mMapSettings == mapSettings ) | ||
return; | ||
|
||
if ( mMapSettings ) | ||
{ | ||
disconnect( mMapSettings, &QgsQuickMapSettings::extentChanged, this, &QgsQuickMapToScreen::transformPoint ); | ||
disconnect( mMapSettings, &QgsQuickMapSettings::rotationChanged, this, &QgsQuickMapToScreen::transformPoint ); | ||
disconnect( mMapSettings, &QgsQuickMapSettings::outputSizeChanged, this, &QgsQuickMapToScreen::transformPoint ); | ||
} | ||
|
||
mMapSettings = mapSettings; | ||
|
||
connect( mMapSettings, &QgsQuickMapSettings::extentChanged, this, &QgsQuickMapToScreen::transformPoint ); | ||
connect( mMapSettings, &QgsQuickMapSettings::rotationChanged, this, &QgsQuickMapToScreen::transformPoint ); | ||
connect( mMapSettings, &QgsQuickMapSettings::outputSizeChanged, this, &QgsQuickMapToScreen::transformPoint ); | ||
|
||
transformPoint(); | ||
transformDistance(); | ||
|
||
emit mapSettingsChanged(); | ||
} | ||
|
||
QgsQuickMapSettings *QgsQuickMapToScreen::mapSettings() const | ||
{ | ||
return mMapSettings; | ||
} | ||
|
||
void QgsQuickMapToScreen::setMapPoint( const QgsPoint &point ) | ||
{ | ||
if ( mMapPoint == point ) | ||
return; | ||
|
||
mMapPoint = point; | ||
emit mapPointChanged(); | ||
transformPoint(); | ||
} | ||
|
||
QgsPoint QgsQuickMapToScreen::mapPoint() const | ||
{ | ||
return mMapPoint; | ||
} | ||
|
||
QPointF QgsQuickMapToScreen::screenPoint() const | ||
{ | ||
return mScreenPoint; | ||
} | ||
|
||
void QgsQuickMapToScreen::transformPoint() | ||
{ | ||
if ( !mMapSettings ) | ||
{ | ||
mScreenPoint = QPointF(); | ||
} | ||
else | ||
{ | ||
mScreenPoint = mMapSettings->coordinateToScreen( mMapPoint ); | ||
} | ||
emit screenPointChanged(); | ||
} | ||
|
||
void QgsQuickMapToScreen::setMapDistance( const double distance ) | ||
{ | ||
if ( mMapDistance == distance ) | ||
return; | ||
|
||
mMapDistance = distance; | ||
emit mapDistanceChanged(); | ||
transformDistance(); | ||
} | ||
|
||
double QgsQuickMapToScreen::mapDistance() const | ||
{ | ||
return mMapDistance; | ||
} | ||
|
||
double QgsQuickMapToScreen::screenDistance() const | ||
{ | ||
return mScreenDistance; | ||
} | ||
|
||
void QgsQuickMapToScreen::transformDistance() | ||
{ | ||
if ( !mMapSettings || qgsDoubleNear( mMapDistance, 0.0 ) || qgsDoubleNear( mMapSettings->mapUnitsPerPoint(), 0.0 ) ) | ||
{ | ||
mScreenDistance = 0.0; | ||
} | ||
else | ||
{ | ||
mScreenDistance = mMapDistance / mMapSettings->mapUnitsPerPoint(); | ||
} | ||
emit screenDistanceChanged(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/*************************************************************************** | ||
qgsquickmaptoscreen.h | ||
---------------------------------------------------- | ||
Date : 22.08.2018 | ||
Copyright : (C) 2018 by Denis Rouzaud | ||
Email : denis (at) opengis.ch | ||
*************************************************************************** | ||
* * | ||
* 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 QGSQUICKMAPTOSCREEN_H | ||
#define QGSQUICKMAPTOSCREEN_H | ||
|
||
#include "qgsquickmapsettings.h" | ||
|
||
#include <QObject> | ||
#include <QPointF> | ||
#include <qgspoint.h> | ||
|
||
/** | ||
* \ingroup quick | ||
* | ||
* @brief The QgsQuickMapToScreen class transform map points to screen coordinates as | ||
* well as distances from map to screen units. Screen points and/or distances will be | ||
* automatically updated on map extent changes. | ||
* | ||
* \since QGIS 3.32 | ||
*/ | ||
class QUICK_EXPORT QgsQuickMapToScreen : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
//! Map settings used to define the map canvas CRS and detect any extent change | ||
Q_PROPERTY( QgsQuickMapSettings *mapSettings READ mapSettings WRITE setMapSettings NOTIFY mapSettingsChanged ) | ||
|
||
//! Point in map coordinates | ||
Q_PROPERTY( QgsPoint mapPoint READ mapPoint WRITE setMapPoint NOTIFY mapPointChanged ) | ||
//! Point in screen coordinates (read-only) | ||
Q_PROPERTY( QPointF screenPoint READ screenPoint NOTIFY screenPointChanged ) | ||
|
||
//! Distance in map unit | ||
Q_PROPERTY( double mapDistance READ mapDistance WRITE setMapDistance NOTIFY mapDistanceChanged ) | ||
//! Distance in screen coordinates (read-only) | ||
Q_PROPERTY( double screenDistance READ screenDistance NOTIFY screenDistanceChanged ) | ||
|
||
public: | ||
|
||
explicit QgsQuickMapToScreen( QObject *parent = nullptr ); | ||
|
||
//! \copydoc mapSettings | ||
void setMapSettings( QgsQuickMapSettings *mapSettings ); | ||
//! \copydoc mapSettings | ||
QgsQuickMapSettings *mapSettings() const; | ||
|
||
//! \copydoc mapPoint | ||
void setMapPoint( const QgsPoint &point ); | ||
//! \copydoc mapPoint | ||
QgsPoint mapPoint() const; | ||
|
||
//! \copydoc mapDistance | ||
void setMapDistance( const double distance ); | ||
//! \copydoc mapDistance | ||
double mapDistance() const; | ||
|
||
//! \copydoc screenPoint | ||
QPointF screenPoint() const; | ||
|
||
//! \copydoc screenDistance | ||
double screenDistance() const; | ||
|
||
signals: | ||
|
||
//! \copydoc mapSettings | ||
void mapSettingsChanged(); | ||
//! \copydoc mapPoint | ||
void mapPointChanged(); | ||
//! \copydoc mapDistance | ||
void mapDistanceChanged(); | ||
//! \copydoc screenPoint | ||
void screenPointChanged(); | ||
//! \copydoc screenDistance | ||
void screenDistanceChanged(); | ||
|
||
private slots: | ||
void transformPoint(); | ||
void transformDistance(); | ||
|
||
private: | ||
QgsQuickMapSettings *mMapSettings = nullptr; | ||
QgsPoint mMapPoint = QgsPoint(); | ||
double mMapDistance = 0.0; | ||
QPointF mScreenPoint = QPointF(); | ||
double mScreenDistance = 0.0; | ||
}; | ||
|
||
#endif // QGSQUICKMAPTOSCREEN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters