Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New map canvas item QgsGpsBearingItem, for showing a bearing line ori…
…ginating from the GPS location
- Loading branch information
1 parent
12e0b0e
commit 9356cd8
Showing
3 changed files
with
136 additions
and
0 deletions.
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,80 @@ | ||
/*************************************************************************** | ||
qgsgpsbearingitem.cpp | ||
--------------------- | ||
begin : December 2019 | ||
copyright : (C) 2019 Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* 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 <QPainter> | ||
#include <QObject> | ||
|
||
#include "qgsgpsbearingitem.h" | ||
#include "qgscoordinatetransform.h" | ||
#include "qgsmapcanvas.h" | ||
#include "qgsexception.h" | ||
#include "qgsproject.h" | ||
#include "qgsmessagelog.h" | ||
#include "qgssymbol.h" | ||
|
||
|
||
QgsGpsBearingItem::QgsGpsBearingItem( QgsMapCanvas *mapCanvas ) | ||
: QgsMapCanvasLineSymbolItem( mapCanvas ) | ||
{ | ||
mSymbol->setColor( QColor( 255, 0, 0 ) ); | ||
mWgs84CRS = QgsCoordinateReferenceSystem::fromOgcWmsCrs( QStringLiteral( "EPSG:4326" ) ); | ||
|
||
connect( mMapCanvas, &QgsMapCanvas::rotationChanged, this, &QgsGpsBearingItem::updateLine ); | ||
connect( mMapCanvas, &QgsMapCanvas::extentsChanged, this, &QgsGpsBearingItem::updateLine ); | ||
} | ||
|
||
void QgsGpsBearingItem::setGpsPosition( const QgsPointXY &point ) | ||
{ | ||
//transform to map crs | ||
if ( mMapCanvas ) | ||
{ | ||
QgsCoordinateTransform t( mWgs84CRS, mMapCanvas->mapSettings().destinationCrs(), QgsProject::instance() ); | ||
try | ||
{ | ||
mCenter = t.transform( point ); | ||
} | ||
catch ( QgsCsException &e ) //silently ignore transformation exceptions | ||
{ | ||
QgsMessageLog::logMessage( QObject::tr( "Error transforming the map center point: %1" ).arg( e.what() ), QStringLiteral( "GPS" ), Qgis::Warning ); | ||
return; | ||
} | ||
} | ||
else | ||
{ | ||
mCenter = point; | ||
} | ||
updateLine(); | ||
} | ||
|
||
void QgsGpsBearingItem::setGpsBearing( double bearing ) | ||
{ | ||
mBearing = bearing; | ||
updateLine(); | ||
} | ||
|
||
void QgsGpsBearingItem::updatePosition() | ||
{ | ||
setGpsPosition( mCenter ); | ||
} | ||
|
||
void QgsGpsBearingItem::updateLine() | ||
{ | ||
QLineF bearingLine; | ||
bearingLine.setP1( toCanvasCoordinates( mCenter ) ); | ||
bearingLine.setLength( 5 * std::max( mMapCanvas->width(), mMapCanvas->height() ) ); | ||
bearingLine.setAngle( 90 - mBearing - mMapCanvas->rotation() ); | ||
setLine( bearingLine ); | ||
} |
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,55 @@ | ||
/*************************************************************************** | ||
qgsgpsbearingitem.h | ||
------------------- | ||
begin : December 2019 | ||
copyright : (C) 2019 Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* 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 QGSGPSBEARINGITEM_H | ||
#define QGSGPSBEARINGITEM_H | ||
|
||
#include "qgspointmarkeritem.h" | ||
#include "qgscoordinatereferencesystem.h" | ||
#include "qgspointxy.h" | ||
|
||
class QPainter; | ||
class QgsLineSymbol; | ||
|
||
/** | ||
* \ingroup app | ||
* A canvas item for showing the bearing of the GPS device | ||
*/ | ||
class QgsGpsBearingItem : public QObject, QgsMapCanvasLineSymbolItem | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit QgsGpsBearingItem( QgsMapCanvas *mapCanvas ); | ||
|
||
void setGpsPosition( const QgsPointXY &point ); | ||
void setGpsBearing( double bearing ); | ||
|
||
void updatePosition() override; | ||
|
||
protected: | ||
|
||
//! coordinates of the point in the center | ||
QgsPointXY mCenter; | ||
|
||
private: | ||
void updateLine(); | ||
|
||
QgsCoordinateReferenceSystem mWgs84CRS; | ||
double mBearing = 0; | ||
|
||
}; | ||
|
||
#endif // QGSGPSBEARINGITEM_H |