Skip to content

Commit

Permalink
Move "New GPX layer" functionality from GPS tools plugin to app
Browse files Browse the repository at this point in the history
While the functionality itself hasn't changed at all, it's now
no longer part of the GPS tools plugin and is instead present
for all QGIS installs.
  • Loading branch information
nyalldawson committed Jul 30, 2021
1 parent d0f3c26 commit d954f61
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 55 deletions.
50 changes: 50 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -2692,6 +2692,7 @@ void QgisApp::createActions()
connect( mActionNewGeoPackageLayer, &QAction::triggered, this, &QgisApp::newGeoPackageLayer );
connect( mActionNewMemoryLayer, &QAction::triggered, this, &QgisApp::newMemoryLayer );
connect( mActionNewMeshLayer, &QAction::triggered, this, &QgisApp::newMeshLayer );
connect( mActionNewGpxLayer, &QAction::triggered, this, &QgisApp::newGpxLayer );
connect( mActionNewVirtualLayer, &QAction::triggered, this, &QgisApp::addVirtualLayer );
connect( mActionShowRasterCalculator, &QAction::triggered, this, &QgisApp::showRasterCalculator );
connect( mActionShowMeshCalculator, &QAction::triggered, this, &QgisApp::showMeshCalculator );
Expand Down Expand Up @@ -6507,6 +6508,55 @@ void QgisApp::newMeshLayer()
dialog.exec();
}

void QgisApp::newGpxLayer()
{
QgsSettings settings;
const QString dir = settings.value( QStringLiteral( "Plugin-GPS/gpxdirectory" ), QDir::homePath() ).toString();
QString fileName =
QFileDialog::getSaveFileName( this,
tr( "New GPX File" ),
dir,
tr( "GPS eXchange file" ) + " (*.gpx)" );
if ( !fileName.isEmpty() )
{
fileName = QgsFileUtils::ensureFileNameHasExtension( fileName, { QStringLiteral( "gpx" )} );
const QFileInfo fileInfo( fileName );
settings.setValue( QStringLiteral( "Plugin-GPS/gpxdirectory" ), fileInfo.absolutePath() );

QFile outputFile( fileName );
if ( !outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
{
QMessageBox::warning( nullptr, tr( "New GPX File" ),
tr( "Unable to create a GPX file with the given name. "
"Try again with another name or in another "
"directory." ) );
return;
}

QTextStream outStream( &outputFile );
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
outStream.setCodec( "UTF-8" );
#endif

#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
outStream << "<gpx></gpx>" << endl;
#else
outStream << "<gpx></gpx>" << Qt::endl;
#endif
outputFile.close();

if ( QgsVectorLayer *trackLayer = addVectorLayer( fileName + "?type=track",
fileInfo.baseName() + ", tracks", QStringLiteral( "gpx" ) ) )
trackLayer->startEditing();
if ( QgsVectorLayer *routeLayer = addVectorLayer( fileName + "?type=route",
fileInfo.baseName() + ", routes", QStringLiteral( "gpx" ) ) )
routeLayer->startEditing();
if ( QgsVectorLayer *waypointLayer = addVectorLayer( fileName + "?type=waypoint",
fileInfo.baseName() + ", waypoints", QStringLiteral( "gpx" ) ) )
waypointLayer->startEditing();
}
}

void QgisApp::showRasterCalculator()
{
QgsRasterCalcDialog d( qobject_cast<QgsRasterLayer *>( activeLayer() ), this );
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -1577,6 +1577,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! Create a new empty mesh layer
void newMeshLayer();

//! Create a new empty GPX layer
void newGpxLayer();

//! Create a new print layout
void newPrintLayout();

Expand Down
51 changes: 0 additions & 51 deletions src/plugins/gps_importer/qgsgpsplugin.cpp
Expand Up @@ -79,22 +79,15 @@ QgsGpsPlugin::~QgsGpsPlugin()
void QgsGpsPlugin::initGui()
{
delete mQActionPointer;
delete mCreateGPXAction;

// add an action to the toolbar
mQActionPointer = new QAction( QIcon(), tr( "&GPS Tools" ), this );
mQActionPointer->setObjectName( QStringLiteral( "mQActionPointer" ) );
mCreateGPXAction = new QAction( QIcon(), tr( "&Create new GPX layer" ), this );
mCreateGPXAction->setObjectName( QStringLiteral( "mCreateGPXAction" ) );
setCurrentTheme( QString() );

mQActionPointer->setWhatsThis( tr( "Creates a new GPX layer and displays it on the map canvas" ) );
mCreateGPXAction->setWhatsThis( tr( "Creates a new GPX layer and displays it on the map canvas" ) );
connect( mQActionPointer, &QAction::triggered, this, &QgsGpsPlugin::run );
connect( mCreateGPXAction, &QAction::triggered, this, &QgsGpsPlugin::createGPX );

mQGisInterface->layerToolBar()->insertAction( nullptr, mCreateGPXAction );
mQGisInterface->newLayerMenu()->addAction( mCreateGPXAction );
mQGisInterface->addPluginToVectorMenu( QString(), mQActionPointer );
mQGisInterface->addVectorToolBarIcon( mQActionPointer );

Expand Down Expand Up @@ -148,44 +141,6 @@ void QgsGpsPlugin::run()
myPluginGui->show();
}

void QgsGpsPlugin::createGPX()
{
QgsSettings settings;
QString dir = settings.value( QStringLiteral( "Plugin-GPS/gpxdirectory" ), QDir::homePath() ).toString();
QString fileName =
QFileDialog::getSaveFileName( mQGisInterface->mainWindow(),
tr( "Save New GPX File As" ),
dir,
tr( "GPS eXchange file" ) + " (*.gpx)" );
if ( !fileName.isEmpty() )
{
if ( !fileName.endsWith( QLatin1String( ".gpx" ), Qt::CaseInsensitive ) )
{
fileName += QLatin1String( ".gpx" );
}
QFileInfo fileInfo( fileName );
std::ofstream ofs( fileName.toUtf8() );
if ( !ofs )
{
QMessageBox::warning( nullptr, tr( "Save New GPX File" ),
tr( "Unable to create a GPX file with the given name. "
"Try again with another name or in another "
"directory." ) );
return;
}
settings.setValue( QStringLiteral( "Plugin-GPS/gpxdirectory" ), fileInfo.absolutePath() );

ofs << "<gpx></gpx>" << std::endl;

drawVectorLayer( fileName + "?type=track",
fileInfo.baseName() + ", tracks", QStringLiteral( "gpx" ) );
drawVectorLayer( fileName + "?type=route",
fileInfo.baseName() + ", routes", QStringLiteral( "gpx" ) );
drawVectorLayer( fileName + "?type=waypoint",
fileInfo.baseName() + ", waypoints", QStringLiteral( "gpx" ) );
}
}

void QgsGpsPlugin::drawVectorLayer( const QString &pathNameQString,
const QString &baseNameQString,
const QString &providerQString )
Expand All @@ -198,8 +153,6 @@ void QgsGpsPlugin::drawVectorLayer( const QString &pathNameQString,
void QgsGpsPlugin::unload()
{
// remove the GUI
mQGisInterface->layerToolBar()->removeAction( mCreateGPXAction );
mQGisInterface->newLayerMenu()->removeAction( mCreateGPXAction );
mQGisInterface->vectorMenu()->removeAction( mQActionPointer );
mQGisInterface->removeVectorToolBarIcon( mQActionPointer );
delete mQActionPointer;
Expand Down Expand Up @@ -671,22 +624,18 @@ void QgsGpsPlugin::setCurrentTheme( const QString &themeName )
if ( QFile::exists( myCurThemePath ) )
{
mQActionPointer->setIcon( QIcon( myCurThemePath + "import_gpx.svg" ) );
mCreateGPXAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionNewGpx.svg" ) ) );
}
else if ( QFile::exists( myDefThemePath ) )
{
mQActionPointer->setIcon( QIcon( myDefThemePath + "import_gpx.svg" ) );
mCreateGPXAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionNewGpx.svg" ) ) );
}
else if ( QFile::exists( myQrcPath ) )
{
mQActionPointer->setIcon( QIcon( myQrcPath + "import_gpx.svg" ) );
mCreateGPXAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionNewGpx.svg" ) ) );
}
else
{
mQActionPointer->setIcon( QIcon() );
mCreateGPXAction->setIcon( QIcon() );
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/plugins/gps_importer/qgsgpsplugin.h
Expand Up @@ -51,8 +51,6 @@ class QgsGpsPlugin: public QObject, public QgisPlugin
void initGui() override;
//! Show the dialog box
void run();
//! Create a new GPX layer
void createGPX();
//! Add a vector layer given vectorLayerPath, baseName, providerKey
void drawVectorLayer( const QString &, const QString &, const QString & );
//! unload the plugin
Expand Down Expand Up @@ -93,8 +91,6 @@ class QgsGpsPlugin: public QObject, public QgisPlugin
QgisInterface *mQGisInterface = nullptr;
//! Pointer to the QAction object used in the menu and toolbar
QAction *mQActionPointer = nullptr;
//! Pointer to the QAction used for creating a new GPX layer
QAction *mCreateGPXAction = nullptr;
//! The path to the GPSBabel program
QString mBabelPath;
//! Importers for external GPS data file formats
Expand Down
13 changes: 13 additions & 0 deletions src/ui/qgisapp.ui
Expand Up @@ -176,6 +176,7 @@
<addaction name="mActionNewSpatiaLiteLayer"/>
<addaction name="mActionNewMemoryLayer"/>
<addaction name="mActionNewMeshLayer"/>
<addaction name="mActionNewGpxLayer"/>
<addaction name="separator"/>
<addaction name="mActionNewVirtualLayer"/>
</widget>
Expand Down Expand Up @@ -3560,6 +3561,18 @@ Shows placeholders for labels which could not be placed, e.g. due to overlaps wi
<string>New Mesh Layer</string>
</property>
</action>
<action name="mActionNewGpxLayer">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionNewGpx.svg</normaloff>:/images/themes/default/mActionNewGpx.svg</iconset>
</property>
<property name="text">
<string>New GPX Layer…</string>
</property>
<property name="toolTip">
<string>New GPX Layer</string>
</property>
</action>
</widget>
<resources/>
<connections/>
Expand Down

0 comments on commit d954f61

Please sign in to comment.