Skip to content

Commit

Permalink
Start on dedicated GPS toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 4, 2022
1 parent aaf7c89 commit a9a57a6
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 14 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_additions/qgis.py
Expand Up @@ -850,6 +850,13 @@
Qgis.GpsConnectionType.__doc__ = 'GPS connection types.\n\n.. versionadded:: 3.30\n\n' + '* ``Automatic``: ' + Qgis.GpsConnectionType.Automatic.__doc__ + '\n' + '* ``Internal``: ' + Qgis.GpsConnectionType.Internal.__doc__ + '\n' + '* ``Serial``: ' + Qgis.GpsConnectionType.Serial.__doc__ + '\n' + '* ``Gpsd``: ' + Qgis.GpsConnectionType.Gpsd.__doc__
# --
Qgis.GpsConnectionType.baseClass = Qgis
# monkey patching scoped based enum
Qgis.GpsConnectionStatus.Disconnected.__doc__ = "Device is disconnected"
Qgis.GpsConnectionStatus.Connecting.__doc__ = "Device is connecting"
Qgis.GpsConnectionStatus.Connected.__doc__ = "Device is successfully connected"
Qgis.GpsConnectionStatus.__doc__ = 'GPS connection status.\n\n.. versionadded:: 3.30\n\n' + '* ``Disconnected``: ' + Qgis.GpsConnectionStatus.Disconnected.__doc__ + '\n' + '* ``Connecting``: ' + Qgis.GpsConnectionStatus.Connecting.__doc__ + '\n' + '* ``Connected``: ' + Qgis.GpsConnectionStatus.Connected.__doc__
# --
Qgis.GpsConnectionStatus.baseClass = Qgis
QgsGpsInformation.FixStatus = Qgis.GpsFixStatus
# monkey patching scoped based enum
QgsGpsInformation.NoData = Qgis.GpsFixStatus.NoData
Expand Down
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -572,6 +572,13 @@ The development version
Gpsd,
};

enum class GpsConnectionStatus
{
Disconnected,
Connecting,
Connected,
};

enum class GpsFixStatus
{
NoData,
Expand Down
1 change: 1 addition & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -267,6 +267,7 @@ set(QGIS_APP_SRCS
gps/qgsgpsbearingitem.cpp
gps/qgsgpsinformationwidget.cpp
gps/qgsgpsmarker.cpp
gps/qgsgpstoolbar.cpp

project/qgsprojectelevationsettingswidget.cpp

Expand Down
6 changes: 5 additions & 1 deletion src/app/gps/qgsappgpsconnection.cpp
Expand Up @@ -121,6 +121,7 @@ void QgsAppGpsConnection::connectGps()
QgisApp::instance()->statusBarIface()->clearMessage();
QgisApp::instance()->messageBar()->pushCritical( QString(), tr( "No path to the GPS port is specified. Please set a path then try again." ) );
emit connectionError( tr( "No path to the GPS port is specified. Please set a path then try again." ) );
emit statusChanged( Qgis::GpsConnectionStatus::Disconnected );
return;
}
break;
Expand All @@ -132,7 +133,7 @@ void QgsAppGpsConnection::connectGps()
}

emit connecting();

emit statusChanged( Qgis::GpsConnectionStatus::Connecting );
emit fixStatusChanged( Qgis::GpsFixStatus::NoData );

showStatusBarMessage( tr( "Connecting to GPS device %1…" ).arg( port ) );
Expand All @@ -152,6 +153,7 @@ void QgsAppGpsConnection::disconnectGps()
mConnection = nullptr;

emit disconnected();
emit statusChanged( Qgis::GpsConnectionStatus::Disconnected );
emit fixStatusChanged( Qgis::GpsFixStatus::NoData );

showStatusBarMessage( tr( "Disconnected from GPS device." ) );
Expand All @@ -162,6 +164,7 @@ void QgsAppGpsConnection::onTimeOut()
{
mConnection = nullptr;
emit connectionTimedOut();
emit statusChanged( Qgis::GpsConnectionStatus::Disconnected );

QgisApp::instance()->statusBarIface()->clearMessage();
QgisApp::instance()->messageBar()->pushCritical( QString(), tr( "Failed to connect to GPS device." ) );
Expand All @@ -183,6 +186,7 @@ void QgsAppGpsConnection::onConnected( QgsGpsConnection *conn )
QgsApplication::gpsConnectionRegistry()->registerConnection( mConnection );

emit connected();
emit statusChanged( Qgis::GpsConnectionStatus::Connected );
showStatusBarMessage( tr( "Connected to GPS device." ) );
}

Expand Down
5 changes: 5 additions & 0 deletions src/app/gps/qgsappgpsconnection.h
Expand Up @@ -95,6 +95,11 @@ class APP_EXPORT QgsAppGpsConnection : public QObject
*/
void connected();

/**
* Emitted when the connection status changes.
*/
void statusChanged( Qgis::GpsConnectionStatus status );

/**
* Emitted when the GPS device has been disconnected.
*/
Expand Down
37 changes: 25 additions & 12 deletions src/app/gps/qgsgpsinformationwidget.cpp
Expand Up @@ -81,7 +81,6 @@ QgsGpsInformationWidget::QgsGpsInformationWidget( QgsAppGpsConnection *connectio
setupUi( this );
connect( mConnectButton, &QPushButton::toggled, this, &QgsGpsInformationWidget::mConnectButton_toggled );
connect( mRecenterButton, &QPushButton::clicked, this, &QgsGpsInformationWidget::recenter );
connect( mConnectButton, &QAbstractButton::toggled, mRecenterButton, &QWidget::setEnabled );
connect( mBtnPosition, &QToolButton::clicked, this, &QgsGpsInformationWidget::mBtnPosition_clicked );
connect( mBtnSignal, &QToolButton::clicked, this, &QgsGpsInformationWidget::mBtnSignal_clicked );
connect( mBtnSatellites, &QToolButton::clicked, this, &QgsGpsInformationWidget::mBtnSatellites_clicked );
Expand Down Expand Up @@ -355,11 +354,35 @@ QgsGpsInformationWidget::QgsGpsInformationWidget( QgsAppGpsConnection *connectio
mMapCanvas->installInteractionBlocker( this );

connect( mConnection, &QgsAppGpsConnection::connecting, this, &QgsGpsInformationWidget::gpsConnecting );
connect( mConnection, &QgsAppGpsConnection::connectionError, this, &QgsGpsInformationWidget::gpsConnectionError );
connect( mConnection, &QgsAppGpsConnection::connectionTimedOut, this, &QgsGpsInformationWidget::timedout );
connect( mConnection, &QgsAppGpsConnection::connected, this, &QgsGpsInformationWidget::gpsConnected );
connect( mConnection, &QgsAppGpsConnection::disconnected, this, &QgsGpsInformationWidget::gpsDisconnected );
connect( mConnection, &QgsAppGpsConnection::stateChanged, this, &QgsGpsInformationWidget::displayGPSInformation );

connect( mConnection, &QgsAppGpsConnection::statusChanged, this, [ = ]( Qgis::GpsConnectionStatus status )
{
switch ( status )
{
case Qgis::GpsConnectionStatus::Disconnected:
whileBlocking( mConnectButton )->setChecked( false );
mConnectButton->setText( tr( "Connect" ) );
mConnectButton->setEnabled( true );
mRecenterButton->setEnabled( false );
break;
case Qgis::GpsConnectionStatus::Connecting:
whileBlocking( mConnectButton )->setChecked( true );
mConnectButton->setText( tr( "Connecting" ) );
mConnectButton->setEnabled( false );
mRecenterButton->setEnabled( false );
break;
case Qgis::GpsConnectionStatus::Connected:
whileBlocking( mConnectButton )->setChecked( true );
mConnectButton->setText( tr( "Disconnect" ) );
mConnectButton->setEnabled( true );
mRecenterButton->setEnabled( true );
break;
}
} );
}

QgsGpsInformationWidget::~QgsGpsInformationWidget()
Expand Down Expand Up @@ -584,22 +607,14 @@ void QgsGpsInformationWidget::gpsConnecting()
mGPSPlainTextEdit->appendPlainText( tr( "Connecting…" ) );
}

void QgsGpsInformationWidget::gpsConnectionError( const QString & )
{
//toggle the button back off
mConnectButton->setChecked( false );
}

void QgsGpsInformationWidget::timedout()
{
mConnectButton->setChecked( false );
mGPSPlainTextEdit->appendPlainText( tr( "Timed out!" ) );
}

void QgsGpsInformationWidget::gpsConnected()
{
mGPSPlainTextEdit->appendPlainText( tr( "Connected!" ) );
mConnectButton->setText( tr( "Dis&connect" ) );

if ( mLogFileGroupBox->isChecked() && !mLogFilename->filePath().isEmpty() )
{
Expand Down Expand Up @@ -649,8 +664,6 @@ void QgsGpsInformationWidget::gpsDisconnected()
mMapBearingItem = nullptr;
}
mGPSPlainTextEdit->appendPlainText( tr( "Disconnected…" ) );
mConnectButton->setChecked( false );
mConnectButton->setText( tr( "&Connect" ) );

setStatusIndicator( Qgis::GpsFixStatus::NoData );
}
Expand Down
1 change: 0 additions & 1 deletion src/app/gps/qgsgpsinformationwidget.h
Expand Up @@ -105,7 +105,6 @@ class APP_EXPORT QgsGpsInformationWidget: public QgsPanelWidget, public QgsMapCa
void updateTimestampDestinationFields( QgsMapLayer *mapLayer );

void gpsConnecting();
void gpsConnectionError( const QString &error );
void gpsDisconnected();
void gpsConnected();

Expand Down
62 changes: 62 additions & 0 deletions src/app/gps/qgsgpstoolbar.cpp
@@ -0,0 +1,62 @@
/***************************************************************************
qgsgpstoolbar.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 "qgsgpstoolbar.h"
#include "qgsappgpsconnection.h"
#include "qgis.h"

QgsGpsToolBar::QgsGpsToolBar( QgsAppGpsConnection *connection, QWidget *parent )
: QToolBar( parent )
, mConnection( connection )
{
setObjectName( QStringLiteral( "mGpsToolBar" ) );
setWindowTitle( tr( "GPS Toolbar" ) );
setToolTip( tr( "GPS Toolbar" ) );

mConnectAction = new QAction( tr( "Connect" ), this );
mConnectAction->setCheckable( true );
addAction( mConnectAction );

connect( mConnection, &QgsAppGpsConnection::statusChanged, this, [ = ]( Qgis::GpsConnectionStatus status )
{
switch ( status )
{
case Qgis::GpsConnectionStatus::Disconnected:
whileBlocking( mConnectAction )->setChecked( false );
mConnectAction->setText( tr( "Connect" ) );
mConnectAction->setEnabled( true );
break;
case Qgis::GpsConnectionStatus::Connecting:
whileBlocking( mConnectAction )->setChecked( true );
mConnectAction->setText( tr( "Connecting" ) );
mConnectAction->setEnabled( false );
break;
case Qgis::GpsConnectionStatus::Connected:
whileBlocking( mConnectAction )->setChecked( true );
mConnectAction->setText( tr( "Disconnect" ) );
mConnectAction->setEnabled( true );
break;
}
} );

connect( mConnectAction, &QAction::toggled, this, [ = ]( bool connect )
{
if ( connect )
mConnection->connectGps();
else
mConnection->disconnectGps();
} );

}
38 changes: 38 additions & 0 deletions src/app/gps/qgsgpstoolbar.h
@@ -0,0 +1,38 @@
/***************************************************************************
qgsgpstoolbar.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 QGSGPSTOOLBAR_H
#define QGSGPSTOOLBAR_H

#include <QToolBar>

class QgsAppGpsConnection;

class QgsGpsToolBar : public QToolBar
{
Q_OBJECT

public:

QgsGpsToolBar( QgsAppGpsConnection *connection, QWidget *parent = nullptr );

private:

QgsAppGpsConnection *mConnection = nullptr;
QAction *mConnectAction = nullptr;

};

#endif // QGSGPSTOOLBAR_H
8 changes: 8 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -250,6 +250,7 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
#include "qgsprojectionselectiondialog.h"
#include "qgsgpsinformationwidget.h"
#include "qgsappgpsconnection.h"
#include "qgsgpstoolbar.h"
#include "qgsguivectorlayertools.h"
#include "qgsdiagramproperties.h"
#include "qgslayerdefinition.h"
Expand Down Expand Up @@ -1394,6 +1395,9 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipBadLayers
// create the GPS tool on starting QGIS - this is like the browser
mGpsConnection = new QgsAppGpsConnection( this );

mGpsToolBar = new QgsGpsToolBar( mGpsConnection, this );
addToolBar( mGpsToolBar );

mpGpsWidget = new QgsGpsInformationWidget( mGpsConnection, mMapCanvas );
QgsPanelWidgetStack *gpsStack = new QgsPanelWidgetStack();
gpsStack->setMainPanel( mpGpsWidget );
Expand Down Expand Up @@ -1969,6 +1973,10 @@ QgisApp::~QgisApp()

delete mpGpsWidget;
mpGpsWidget = nullptr;

delete mGpsToolBar;
mGpsToolBar = nullptr;

delete mGpsConnection;
mGpsConnection = nullptr;

Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -154,6 +154,7 @@ class QgsMapToolCapture;
class QgsElevationProfileWidget;
class QgsScreenHelper;
class QgsAppGpsConnection;
class QgsGpsToolBar;

#include <QMainWindow>
#include <QToolBar>
Expand Down Expand Up @@ -2558,6 +2559,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! Persistent GPS toolbox
QgsAppGpsConnection *mGpsConnection = nullptr;
QgsGpsInformationWidget *mpGpsWidget = nullptr;
QgsGpsToolBar *mGpsToolBar = nullptr;

QgsMessageBarItem *mLastMapToolMessage = nullptr;

Expand Down
13 changes: 13 additions & 0 deletions src/core/qgis.h
Expand Up @@ -932,6 +932,19 @@ class CORE_EXPORT Qgis
};
Q_ENUM( GpsConnectionType )

/**
* GPS connection status.
*
* \since QGIS 3.30
*/
enum class GpsConnectionStatus : int
{
Disconnected, //!< Device is disconnected
Connecting, //!< Device is connecting
Connected, //!< Device is successfully connected
};
Q_ENUM( GpsConnectionStatus )

/**
* GPS fix status.
*
Expand Down

0 comments on commit a9a57a6

Please sign in to comment.