Commit b7db61f7 authored by Volker Krause's avatar Volker Krause
Browse files

Allow to export favorite locations to a GPX file

This is compatible with Nextcloud Maps and can be imported there.
parent 8b35b406
......@@ -7,6 +7,7 @@
import QtQuick 2.5
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.1 as QQC2
import Qt.labs.platform 1.1 as Platform
import QtLocation 5.11 as QtLocation
import QtPositioning 5.11
import org.kde.kirigami 2.12 as Kirigami
......@@ -34,6 +35,15 @@ Kirigami.Page {
}
Platform.FileDialog {
id: favoriteGpxExportDialog
fileMode: Platform.FileDialog.SaveFile
title: i18n("Export Favorite Locations")
folder: Platform.StandardPaths.writableLocation(StandardPaths.DocumentsLocation)
nameFilters: [i18n("GPX Files (*.gpx)")]
onAccepted: FavoriteLocationModel.exportToGpx(file)
}
actions.main: Kirigami.Action {
icon.name: "crosshairs"
text: i18n("Pick Location")
......@@ -66,6 +76,11 @@ Kirigami.Page {
FavoriteLocationModel.removeLocation(combo.currentIndex);
combo.currentIndex = Math.min(prevIndex, combo.count - 1);
}
},
Kirigami.Action {
text: i18n("Export to GPX")
icon.name: "export-symbolic"
onTriggered: favoriteGpxExportDialog.open()
}
]
......
......@@ -5,7 +5,9 @@
*/
#include "favoritelocationmodel.h"
#include "gpxexport.h"
#include "json.h"
#include "logging.h"
#include <KLocalizedString>
......@@ -253,3 +255,20 @@ void FavoriteLocationModel::saveLocations() const
f.write(QJsonDocument(FavoriteLocation::toJson(m_locations)).toJson());
}
void FavoriteLocationModel::exportToGpx(const QString &filePath) const
{
if (filePath.isEmpty()) {
return;
}
QFile f(QUrl(filePath).isLocalFile() ? QUrl(filePath).toLocalFile() : filePath);
if (!f.open(QFile::WriteOnly)) {
qCWarning(Log) << f.errorString() << f.fileName();
return;
}
GpxExport exporter(&f);
for (const auto &fav : m_locations) {
exporter.writeFavoriteLocation(fav);
}
}
......@@ -77,6 +77,9 @@ public:
*/
void setFavoriteLocations(std::vector<FavoriteLocation> &&locs);
/** Export to GPX. */
Q_INVOKABLE void exportToGpx(const QString &filePath) const;
int rowCount(const QModelIndex &parent = {}) const override;
QVariant data(const QModelIndex &index, int role) const override;
bool setData(const QModelIndex & index, const QVariant & value, int role) override;
......
......@@ -5,6 +5,7 @@
*/
#include "gpxexport.h"
#include "favoritelocationmodel.h"
#include "transfer.h"
#include <KItinerary/Event>
......@@ -106,3 +107,10 @@ void GpxExport::writeJourneySection(const KPublicTransport::JourneySection &sect
}
}
}
void GpxExport::writeFavoriteLocation(const FavoriteLocation &fav)
{
m_writer.writeStartWaypoint(fav.latitude(), fav.longitude());
m_writer.writeName(fav.name());
m_writer.writeEndWaypoint();
}
......@@ -11,6 +11,7 @@
#include <gpx/gpxwriter.h>
class FavoriteLocation;
class Transfer;
/** Trip group to GPX export. */
......@@ -22,6 +23,7 @@ public:
void writeReservation(const QVariant &res, const KPublicTransport::JourneySection &journey = {});
void writeTransfer(const Transfer &transfer);
void writeFavoriteLocation(const FavoriteLocation &fav);
inline void writeStartRoute() { m_writer.writeStartRoute(); }
inline void writeEndRoute() { m_writer.writeEndRoute(); }
......
......@@ -45,6 +45,18 @@ void Gpx::Writer::writeEndRoute()
m_writer.writeEndElement();
}
void Gpx::Writer::writeStartWaypoint(float latitude, float longitude)
{
m_writer.writeStartElement(QStringLiteral("wpt"));
m_writer.writeAttribute(QStringLiteral("lat"), QString::number(latitude));
m_writer.writeAttribute(QStringLiteral("lon"), QString::number(longitude));
}
void Gpx::Writer::writeEndWaypoint()
{
m_writer.writeEndElement();
}
void Gpx::Writer::writeStartRoutePoint(float latitude, float longitude)
{
m_writer.writeStartElement(QStringLiteral("rtept"));
......
......@@ -29,6 +29,9 @@ public:
void writeStartRoute();
void writeEndRoute();
void writeStartWaypoint(float latitude, float longitude);
void writeEndWaypoint();
void writeStartRoutePoint(float latitude, float longitude);
void writeEndRoutePoint();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment