Skip to content

Commit

Permalink
Seperate out gps logging code from gps digitizing related code
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 16, 2022
1 parent 0dfac0c commit 77a1ca3
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 110 deletions.
1 change: 1 addition & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -266,6 +266,7 @@ set(QGIS_APP_SRCS

gps/qgsappgpsconnection.cpp
gps/qgsappgpsdigitizing.cpp
gps/qgsappgpslogging.cpp
gps/qgsappgpssettingsmenu.cpp
gps/qgsgpsbearingitem.cpp
gps/qgsgpscanvasbridge.cpp
Expand Down
81 changes: 0 additions & 81 deletions src/app/gps/qgsappgpsdigitizing.cpp
Expand Up @@ -345,39 +345,6 @@ void QgsAppGpsDigitizing::createFeature()
QgisApp::instance()->activateWindow();
}

void QgsAppGpsDigitizing::setNmeaLogFile( const QString &filename )
{
if ( mLogFile )
{
stopLogging();
}

mNmeaLogFile = filename;

if ( mEnableNmeaLogging && !mNmeaLogFile.isEmpty() )
{
startLogging();
}
}

void QgsAppGpsDigitizing::setNmeaLoggingEnabled( bool enabled )
{
if ( enabled == static_cast< bool >( mLogFile ) )
return;

if ( mLogFile && !enabled )
{
stopLogging();
}

mEnableNmeaLogging = enabled;

if ( mEnableNmeaLogging && !mNmeaLogFile.isEmpty() )
{
startLogging();
}
}

void QgsAppGpsDigitizing::createVertexAtCurrentLocation()
{
addTrackVertex();
Expand Down Expand Up @@ -413,62 +380,14 @@ void QgsAppGpsDigitizing::updateTrackAppearance()

void QgsAppGpsDigitizing::gpsConnected()
{
if ( !mLogFile && mEnableNmeaLogging && !mNmeaLogFile.isEmpty() )
{
startLogging();
}
setConnection( mConnection->connection() );
}

void QgsAppGpsDigitizing::gpsDisconnected()
{
stopLogging();
setConnection( nullptr );
}

void QgsAppGpsDigitizing::logNmeaSentence( const QString &nmeaString )
{
if ( mEnableNmeaLogging && mLogFile && mLogFile->isOpen() )
{
mLogFileTextStream << nmeaString << "\r\n"; // specifically output CR + LF (NMEA requirement)
}
}

void QgsAppGpsDigitizing::startLogging()
{
if ( !mLogFile )
{
mLogFile = std::make_unique< QFile >( mNmeaLogFile );
}

if ( mLogFile->open( QIODevice::Append ) ) // open in binary and explicitly output CR + LF per NMEA
{
mLogFileTextStream.setDevice( mLogFile.get() );

// crude way to separate chunks - use when manually editing file - NMEA parsers should discard
mLogFileTextStream << "====" << "\r\n";

connect( mConnection, &QgsAppGpsConnection::nmeaSentenceReceived, this, &QgsAppGpsDigitizing::logNmeaSentence ); // added to handle raw data
}
else // error opening file
{
mLogFile.reset();

// need to indicate why - this just reports that an error occurred
QgisApp::instance()->messageBar()->pushCritical( QString(), tr( "Error opening log file." ) );
}
}

void QgsAppGpsDigitizing::stopLogging()
{
if ( mLogFile && mLogFile->isOpen() )
{
disconnect( mConnection, &QgsAppGpsConnection::nmeaSentenceReceived, this, &QgsAppGpsDigitizing::logNmeaSentence );
mLogFile->close();
mLogFile.reset();
}
}

void QgsAppGpsDigitizing::createRubberBand()
{
delete mRubberBand;
Expand Down
20 changes: 0 additions & 20 deletions src/app/gps/qgsappgpsdigitizing.h
Expand Up @@ -23,15 +23,12 @@
#include "qgssettingsentryimpl.h"
#include "qgsgpslogger.h"

#include <QTextStream>

class QgsAppGpsConnection;
class QgsMapCanvas;
class QgsRubberBand;
class QgsPoint;
class QgsGpsInformation;
class QgsVectorLayer;
class QFile;

class APP_EXPORT QgsAppGpsDigitizing: public QgsGpsLogger
{
Expand All @@ -41,17 +38,11 @@ class APP_EXPORT QgsAppGpsDigitizing: public QgsGpsLogger

static const inline QgsSettingsEntryString settingTrackLineSymbol = QgsSettingsEntryString( QStringLiteral( "track-line-symbol" ), QgsSettings::Prefix::GPS, QStringLiteral( "<symbol alpha=\"1\" name=\"gps-track-symbol\" force_rhr=\"0\" clip_to_extent=\"1\" type=\"line\"><layer enabled=\"1\" pass=\"0\" locked=\"0\" class=\"SimpleLine\"><Option type=\"Map\"><Option name=\"line_color\" type=\"QString\" value=\"219,30,42,255\"/><Option name=\"line_style\" type=\"QString\" value=\"solid\"/><Option name=\"line_width\" type=\"QString\" value=\"0.4\"/></Option></layer></symbol>" ), QStringLiteral( "Line symbol to use for GPS track line" ), Qgis::SettingsOptions(), 0 );

static const inline QgsSettingsEntryString settingLastLogFolder = QgsSettingsEntryString( QStringLiteral( "last-log-folder" ), QgsSettings::Prefix::GPS, QString(), QStringLiteral( "Last used folder for GPS log files" ) );
static const inline QgsSettingsEntryString settingLastGpkgLog = QgsSettingsEntryString( QStringLiteral( "last-gpkg-log" ), QgsSettings::Prefix::GPS, QString(), QStringLiteral( "Last used Geopackage/Spatialite file for logging GPS locations" ) );

QgsAppGpsDigitizing( QgsAppGpsConnection *connection, QgsMapCanvas *canvas, QObject *parent = nullptr );
~QgsAppGpsDigitizing() override;

public slots:
void createFeature();
void setNmeaLogFile( const QString &filename );
void setNmeaLoggingEnabled( bool enabled );

void createVertexAtCurrentLocation();

private slots:
Expand All @@ -63,11 +54,6 @@ class APP_EXPORT QgsAppGpsDigitizing: public QgsGpsLogger
void gpsConnected();
void gpsDisconnected();

void logNmeaSentence( const QString &nmeaString ); // added to handle 'raw' data

void startLogging();
void stopLogging();

private:
void createRubberBand();
QVariant timestamp( QgsVectorLayer *vlayer, int idx );
Expand All @@ -79,12 +65,6 @@ class APP_EXPORT QgsAppGpsDigitizing: public QgsGpsLogger

QgsCoordinateTransform mCanvasToWgs84Transform;

QString mNmeaLogFile;
bool mEnableNmeaLogging = false;

std::unique_ptr< QFile > mLogFile;
QTextStream mLogFileTextStream;

friend class TestQgsGpsIntegration;
};

Expand Down
146 changes: 146 additions & 0 deletions src/app/gps/qgsappgpslogging.cpp
@@ -0,0 +1,146 @@
/***************************************************************************
qgsappgpslogging.cpp
-------------------
begin : October 2022
copyright : (C) 2022 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 "qgsappgpslogging.h"
#include "qgsgui.h"
#include "qgisapp.h"
#include "qgsmessagebar.h"
#include "qgsgpsconnection.h"
#include "qgsappgpsconnection.h"

QgsAppGpsLogging::QgsAppGpsLogging( QgsAppGpsConnection *connection, QObject *parent )
: QgsGpsLogger( nullptr, parent )
, mConnection( connection )
{
connect( QgsProject::instance(), &QgsProject::transformContextChanged, this, [ = ]
{
setTransformContext( QgsProject::instance()->transformContext() );
} );
setTransformContext( QgsProject::instance()->transformContext() );

setEllipsoid( QgsProject::instance()->ellipsoid() );
connect( QgsProject::instance(), &QgsProject::ellipsoidChanged, this, [ = ]
{
setEllipsoid( QgsProject::instance()->ellipsoid() );
} );

connect( mConnection, &QgsAppGpsConnection::connected, this, &QgsAppGpsLogging::gpsConnected );
connect( mConnection, &QgsAppGpsConnection::disconnected, this, &QgsAppGpsLogging::gpsDisconnected );

connect( QgsGui::instance(), &QgsGui::optionsChanged, this, &QgsAppGpsLogging::updateGpsSettings );
updateGpsSettings();
}

QgsAppGpsLogging::~QgsAppGpsLogging()
{
}

void QgsAppGpsLogging::setNmeaLogFile( const QString &filename )
{
if ( mLogFile )
{
stopLogging();
}

mNmeaLogFile = filename;

if ( mEnableNmeaLogging && !mNmeaLogFile.isEmpty() )
{
startLogging();
}
}

void QgsAppGpsLogging::setNmeaLoggingEnabled( bool enabled )
{
if ( enabled == static_cast< bool >( mLogFile ) )
return;

if ( mLogFile && !enabled )
{
stopLogging();
}

mEnableNmeaLogging = enabled;

if ( mEnableNmeaLogging && !mNmeaLogFile.isEmpty() )
{
startLogging();
}
}

void QgsAppGpsLogging::setGpkgLogFile( const QString &filename )
{

}


void QgsAppGpsLogging::gpsConnected()
{
if ( !mLogFile && mEnableNmeaLogging && !mNmeaLogFile.isEmpty() )
{
startLogging();
}
setConnection( mConnection->connection() );
}

void QgsAppGpsLogging::gpsDisconnected()
{
stopLogging();
setConnection( nullptr );
}

void QgsAppGpsLogging::logNmeaSentence( const QString &nmeaString )
{
if ( mEnableNmeaLogging && mLogFile && mLogFile->isOpen() )
{
mLogFileTextStream << nmeaString << "\r\n"; // specifically output CR + LF (NMEA requirement)
}
}

void QgsAppGpsLogging::startLogging()
{
if ( !mLogFile )
{
mLogFile = std::make_unique< QFile >( mNmeaLogFile );
}

if ( mLogFile->open( QIODevice::Append ) ) // open in binary and explicitly output CR + LF per NMEA
{
mLogFileTextStream.setDevice( mLogFile.get() );

// crude way to separate chunks - use when manually editing file - NMEA parsers should discard
mLogFileTextStream << "====" << "\r\n";

connect( mConnection, &QgsAppGpsConnection::nmeaSentenceReceived, this, &QgsAppGpsLogging::logNmeaSentence ); // added to handle raw data
}
else // error opening file
{
mLogFile.reset();

// need to indicate why - this just reports that an error occurred
QgisApp::instance()->messageBar()->pushCritical( QString(), tr( "Error opening log file." ) );
}
}

void QgsAppGpsLogging::stopLogging()
{
if ( mLogFile && mLogFile->isOpen() )
{
disconnect( mConnection, &QgsAppGpsConnection::nmeaSentenceReceived, this, &QgsAppGpsLogging::logNmeaSentence );
mLogFile->close();
mLogFile.reset();
}
}

67 changes: 67 additions & 0 deletions src/app/gps/qgsappgpslogging.h
@@ -0,0 +1,67 @@
/***************************************************************************
qgsappgpslogging.h
-------------------
begin : October 2022
copyright : (C) 2022 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 QGSAPPGPSLOGGING_H
#define QGSAPPGPSLOGGING_H

#include <QObject>

#include "qgis_app.h"
#include "qgssettingsentryimpl.h"
#include "qgsgpslogger.h"

#include <QTextStream>

class QgsAppGpsConnection;
class QFile;

class APP_EXPORT QgsAppGpsLogging: public QgsGpsLogger
{
Q_OBJECT

public:

static const inline QgsSettingsEntryString settingLastLogFolder = QgsSettingsEntryString( QStringLiteral( "last-log-folder" ), QgsSettings::Prefix::GPS, QString(), QStringLiteral( "Last used folder for GPS log files" ) );
static const inline QgsSettingsEntryString settingLastGpkgLog = QgsSettingsEntryString( QStringLiteral( "last-gpkg-log" ), QgsSettings::Prefix::GPS, QString(), QStringLiteral( "Last used Geopackage/Spatialite file for logging GPS locations" ) );

QgsAppGpsLogging( QgsAppGpsConnection *connection, QObject *parent = nullptr );
~QgsAppGpsLogging() override;

public slots:
void setNmeaLogFile( const QString &filename );
void setNmeaLoggingEnabled( bool enabled );
void setGpkgLogFile( const QString &filename );

private slots:

void gpsConnected();
void gpsDisconnected();

void logNmeaSentence( const QString &nmeaString ); // added to handle 'raw' data

void startLogging();
void stopLogging();

private:
QgsAppGpsConnection *mConnection = nullptr;

QString mNmeaLogFile;
bool mEnableNmeaLogging = false;

std::unique_ptr< QFile > mLogFile;
QTextStream mLogFileTextStream;
};

#endif // QGSAPPGPSLOGGING_H

0 comments on commit 77a1ca3

Please sign in to comment.