Skip to content

Commit 0b55873

Browse files
committedApr 14, 2018
Added ProSim flight plan export. albar965#204
1 parent 9a443a9 commit 0b55873

File tree

4 files changed

+130
-7
lines changed

4 files changed

+130
-7
lines changed
 

‎src/common/constants.h

+2
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ const QLatin1Literal FILE_PATTERN_CORTEIN("(corte.in)");
215215
const QLatin1Literal FILE_PATTERN_FLP("(*.flp)");
216216
const QLatin1Literal FILE_PATTERN_FMS("(*.fms)");
217217
const QLatin1Literal FILE_PATTERN_UFMC("(*.ufmc)");
218+
const QLatin1Literal FILE_PATTERN_COMPANYROUTES_XML("(companyroutes.xml)");
218219

219220
const QLatin1Literal FILE_PATTERN_GPX("(*.gpx)");
220221
const QLatin1Literal FILE_PATTERN_KML("(*.kml *.kmz)");
@@ -237,6 +238,7 @@ const QLatin1Literal FILE_PATTERN_CORTEIN("(corte.in Corte.in CORTE.IN)");
237238
const QLatin1Literal FILE_PATTERN_FLP("(*.flp *.Flp *.FLP)");
238239
const QLatin1Literal FILE_PATTERN_FMS("(*.fms *.Fms *.FMS)");
239240
const QLatin1Literal FILE_PATTERN_UFMC("(*.ufmc *.Ufmc *.UFMC)");
241+
const QLatin1Literal FILE_PATTERN_COMPANYROUTES_XML("(companyroutes.xml Companyroutes.xml COMPANYROUTES.XML)");
240242

241243
const QLatin1Literal FILE_PATTERN_GPX("(*.gpx *.Gpx *.GPX)");
242244
const QLatin1Literal FILE_PATTERN_KML("(*.kml *.KML *.kmz *.KMZ)");

‎src/gui/mainwindow.ui

+5-5
Original file line numberDiff line numberDiff line change
@@ -7683,7 +7683,7 @@
76837683
</action>
76847684
<action name="actionRouteSaveAsIFly">
76857685
<property name="text">
7686-
<string>Export Flight Plan as FLTPLAN for iFly ...</string>
7686+
<string>Export Flight Plan as &amp;FLTPLAN for iFly ...</string>
76877687
</property>
76887688
<property name="toolTip">
76897689
<string>Save flight plan as FLTPLAN file usable by iFly aircraft</string>
@@ -7694,7 +7694,7 @@
76947694
</action>
76957695
<action name="actionRouteSaveAsXFmc">
76967696
<property name="text">
7697-
<string>Export Flight Plan for X-FMC ...</string>
7697+
<string>Export Flight Plan for &amp;X-FMC ...</string>
76987698
</property>
76997699
<property name="toolTip">
77007700
<string>Save flight plan as FPL file usable by X-FMC</string>
@@ -7705,7 +7705,7 @@
77057705
</action>
77067706
<action name="actionRouteSaveAsUfmc">
77077707
<property name="text">
7708-
<string>Export Flight Plan for UFMC ...</string>
7708+
<string>Export Flight Plan for &amp;UFMC ...</string>
77097709
</property>
77107710
<property name="toolTip">
77117711
<string>Save flight plan as UFMC file</string>
@@ -7716,7 +7716,7 @@
77167716
</action>
77177717
<action name="actionRouteSaveAsProSim">
77187718
<property name="text">
7719-
<string>Export Flight Plan to XML for ProSim ...</string>
7719+
<string>Export Flight Plan for &amp;ProSim ...</string>
77207720
</property>
77217721
<property name="toolTip">
77227722
<string>Append flight plan to the companyroutes.xml file for ProSim</string>
@@ -7727,7 +7727,7 @@
77277727
</action>
77287728
<action name="actionRouteSaveAsBbsAirbus">
77297729
<property name="text">
7730-
<string>Export Flight Plan as PLN for BBS Airbus ...</string>
7730+
<string>Export Flight Plan as PLN for &amp;BBS Airbus ...</string>
77317731
</property>
77327732
<property name="toolTip">
77337733
<string>Save flight plan as PLN file usable by the Blackbox Simulations Airbus</string>

‎src/route/routeexport.cpp

+122-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "common/constants.h"
2222
#include "common/aircrafttrack.h"
2323
#include "route/route.h"
24+
#include "io/fileroller.h"
2425
#include "options/optiondata.h"
2526
#include "gui/dialog.h"
2627
#include "ui_mainwindow.h"
@@ -37,6 +38,7 @@
3738
#include <QDir>
3839
#include <QMessageBox>
3940
#include <QStandardPaths>
41+
#include <QXmlStreamReader>
4042
#include <exception.h>
4143

4244
RouteExport::RouteExport(MainWindow *parent)
@@ -290,7 +292,7 @@ bool RouteExport::routeExportCorteIn()
290292
{
291293
if(exportFlighplanAsCorteIn(routeFile))
292294
{
293-
mainWindow->setStatusMessage(tr("Flight plan saved to corte.in."));
295+
mainWindow->setStatusMessage(tr("Flight plan added to corte.in."));
294296
return true;
295297
}
296298
}
@@ -366,8 +368,27 @@ bool RouteExport::routeExportUFmc()
366368

367369
bool RouteExport::routeExportProSim()
368370
{
369-
qDebug() << Q_FUNC_INFO;
370371
// companyroutes.xml
372+
qDebug() << Q_FUNC_INFO;
373+
374+
if(routeValidate(false /* validate parking */, true /* validate departure and destination */))
375+
{
376+
QString routeFile = dialog->saveFileDialog(
377+
tr("Save Flight Plan to companyroutes.xml for ProSim"),
378+
tr("companyroutes.xml Files %1;;All Files (*)").arg(lnm::FILE_PATTERN_COMPANYROUTES_XML),
379+
".xml", "Route/CompanyRoutesXml",
380+
QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first(), "companyroutes.xml",
381+
true /* dont confirm overwrite */);
382+
383+
if(!routeFile.isEmpty())
384+
{
385+
if(exportFlighplanAsCompanyroutesXml(routeFile))
386+
{
387+
mainWindow->setStatusMessage(tr("Flight plan added to companyroutes.xml."));
388+
return true;
389+
}
390+
}
391+
}
371392
return false;
372393
}
373394

@@ -740,6 +761,105 @@ bool RouteExport::exportFlighplanAsCorteIn(const QString& filename)
740761
}
741762
}
742763

764+
bool RouteExport::exportFlighplanAsCompanyroutesXml(const QString& filename)
765+
{
766+
qDebug() << Q_FUNC_INFO << filename;
767+
768+
// <?xml version="1.0" encoding="UTF-8"?>
769+
// <companyroutes>
770+
// <route name="KDSMKOKC">KDSM DSM J25 TUL KOKC </route>
771+
// <route name="EDDHEDDS">EDDH IDEKO Y900 TIMEN UL126 WRB UN850 KRH T128 BADSO EDDS</route>
772+
// <route name="EDDSEDDH">EDDS KRH UZ210 NOSPA EDDL</route>
773+
// </companyroutes>
774+
775+
// Read the XML file and keep all routes ===========================================
776+
QVector<std::pair<QString, QString> > routes;
777+
QSet<QString> routeNames;
778+
779+
QFile file(filename);
780+
if(file.exists() && file.size() > 0)
781+
{
782+
if(file.open(QFile::ReadOnly | QIODevice::Text))
783+
{
784+
QXmlStreamReader reader(&file);
785+
786+
while(!reader.atEnd())
787+
{
788+
if(reader.error() != QXmlStreamReader::NoError)
789+
throw atools::Exception("Error reading \"" + filename + "\": " + reader.errorString());
790+
791+
QXmlStreamReader::TokenType token = reader.readNext();
792+
793+
if(token == QXmlStreamReader::StartElement && reader.name() == "route")
794+
{
795+
QString name = reader.attributes().value("name").toString();
796+
QString route = reader.readElementText();
797+
routes.append(std::make_pair(name, route));
798+
routeNames.insert(name);
799+
}
800+
}
801+
file.close();
802+
}
803+
else
804+
{
805+
atools::gui::ErrorHandler(mainWindow).handleIOError(file, tr("While reading from companyroutes.xml file:"));
806+
return false;
807+
}
808+
}
809+
810+
// Create maximum of two backup files
811+
QString backupFile = filename + "_lnm_backup";
812+
atools::io::FileRoller roller(1);
813+
roller.rollFile(backupFile);
814+
815+
// Copy file to backup before opening
816+
bool result = QFile(filename).copy(backupFile);
817+
qDebug() << Q_FUNC_INFO << "Copied" << filename << "to" << backupFile << result;
818+
819+
// Create route string
820+
QString route = RouteString::createStringForRoute(routeAdjustedToProcedureOptions(), 0.f, rs::START_AND_DEST);
821+
QString name = buildDefaultFilenameShort(QString(), QString());
822+
823+
// Find a unique name between all loaded
824+
QString newname = name;
825+
int i = 1;
826+
while(routeNames.contains(newname) && i < 99)
827+
newname = QString("%1%2").arg(name).arg(i++, 2, 10, QChar('0'));
828+
829+
// Add new route
830+
routes.append(std::make_pair(newname, route));
831+
832+
// Save and overwrite new file ====================================================
833+
if(file.open(QFile::WriteOnly | QIODevice::Text))
834+
{
835+
QXmlStreamWriter writer(&file);
836+
writer.setAutoFormatting(true);
837+
writer.setAutoFormattingIndent(2);
838+
writer.writeStartDocument();
839+
840+
writer.writeStartElement("companyroutes");
841+
842+
for(const std::pair<QString, QString>& entry : routes)
843+
{
844+
// <route name="KDSMKOKC">KDSM DSM J25 TUL KOKC </route>
845+
writer.writeStartElement("route");
846+
writer.writeAttribute("name", entry.first);
847+
writer.writeCharacters(entry.second);
848+
writer.writeEndElement(); // route
849+
}
850+
851+
writer.writeEndElement(); // companyroutes
852+
writer.writeEndDocument();
853+
file.close();
854+
return true;
855+
}
856+
else
857+
{
858+
atools::gui::ErrorHandler(mainWindow).handleIOError(file, tr("While saving to companyroutes.xml file:"));
859+
return false;
860+
}
861+
}
862+
743863
bool RouteExport::exportFlightplanAsGpx(const QString& filename)
744864
{
745865
qDebug() << Q_FUNC_INFO << filename;

‎src/route/routeexport.h

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class RouteExport :
8787
bool exportFlighplanAsRte(const QString& filename);
8888
bool exportFlighplanAsFpr(const QString& filename);
8989
bool exportFlighplanAsCorteIn(const QString& filename);
90+
bool exportFlighplanAsCompanyroutesXml(const QString& filename);
9091
bool exportFlighplanAsUFmc(const QString& filename);
9192

9293
bool exportFlightplanAsGpx(const QString& filename);

0 commit comments

Comments
 (0)
Please sign in to comment.