Skip to content

Commit

Permalink
New map canvas item QgsGpsBearingItem, for showing a bearing line ori…
Browse files Browse the repository at this point in the history
…ginating from the GPS location
  • Loading branch information
nyalldawson committed Dec 10, 2019
1 parent 12e0b0e commit 9356cd8
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -232,6 +232,7 @@ SET(QGIS_APP_SRCS
locator/qgsinbuiltlocatorfilters.cpp
locator/qgslocatoroptionswidget.cpp

gps/qgsgpsbearingitem.cpp
gps/qgsgpsinformationwidget.cpp
gps/qgsgpsmarker.cpp

Expand Down
80 changes: 80 additions & 0 deletions src/app/gps/qgsgpsbearingitem.cpp
@@ -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 );
}
55 changes: 55 additions & 0 deletions src/app/gps/qgsgpsbearingitem.h
@@ -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

0 comments on commit 9356cd8

Please sign in to comment.