mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-23 15:25:35 +08:00
refs #267, model mappings backend class (used in context)
* interface * implementation for vPilot mapping files
This commit is contained in:
28
src/blacksim/simulatormodelmappings.cpp
Normal file
28
src/blacksim/simulatormodelmappings.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "simulatormodelmappings.h"
|
||||
|
||||
namespace BlackSim
|
||||
{
|
||||
|
||||
ISimulatorModelMappings::ISimulatorModelMappings(QObject *parent) : QObject(parent) {}
|
||||
|
||||
int ISimulatorModelMappings::size() const
|
||||
{
|
||||
return this->m_mappings.size();
|
||||
}
|
||||
|
||||
bool ISimulatorModelMappings::isEmpty() const
|
||||
{
|
||||
return this->m_mappings.isEmpty();
|
||||
}
|
||||
|
||||
const BlackMisc::Network::CAircraftMappingList &ISimulatorModelMappings::getMappingList() const
|
||||
{
|
||||
return this->m_mappings;
|
||||
}
|
||||
|
||||
BlackMisc::Network::CAircraftMappingList ISimulatorModelMappings::findByIcao(const BlackMisc::Aviation::CAircraftIcao &icao, bool emptyMeansWildCard) const
|
||||
{
|
||||
return this->m_mappings.findByIcaoCode(icao, emptyMeansWildCard);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
48
src/blacksim/simulatormodelmappings.h
Normal file
48
src/blacksim/simulatormodelmappings.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/* Copyright (C) 2013 VATSIM Community / authors
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef BLACKSIM_MODELMAPPINGS_H
|
||||
#define BLACKSIM_MODELMAPPINGS_H
|
||||
|
||||
#include "blackmisc/nwaircraftmappinglist.h"
|
||||
#include <QObject>
|
||||
|
||||
namespace BlackSim
|
||||
{
|
||||
/*!
|
||||
* \brief Model mappings
|
||||
*/
|
||||
class ISimulatorModelMappings : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
ISimulatorModelMappings(QObject *parent = nullptr);
|
||||
|
||||
//! Destructor
|
||||
virtual ~ISimulatorModelMappings() {}
|
||||
|
||||
//! Load data
|
||||
virtual bool load() = 0;
|
||||
|
||||
//! Empty
|
||||
bool isEmpty() const;
|
||||
|
||||
//! Size
|
||||
int size() const;
|
||||
|
||||
//! Get list
|
||||
const BlackMisc::Network::CAircraftMappingList &getMappingList() const;
|
||||
|
||||
//! Find by ICAO code
|
||||
BlackMisc::Network::CAircraftMappingList findByIcao(const BlackMisc::Aviation::CAircraftIcao &icao, bool emptyMeansWildCard = true) const;
|
||||
|
||||
protected:
|
||||
BlackMisc::Network::CAircraftMappingList m_mappings; //!< Mappings
|
||||
};
|
||||
}
|
||||
|
||||
#endif // guard
|
||||
95
src/blacksim/vpilotmodelmappings.cpp
Normal file
95
src/blacksim/vpilotmodelmappings.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
#include "vpilotmodelmappings.h"
|
||||
#include "blackmisc/nwaircraftmapping.h"
|
||||
|
||||
#include <QtXml/QDomElement>
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QStandardPaths>
|
||||
|
||||
using namespace BlackMisc::Network;
|
||||
|
||||
namespace BlackSim
|
||||
{
|
||||
|
||||
void CVPilotModelMappings::addFilename(const QString &fileName)
|
||||
{
|
||||
if (this->m_fileList.contains(fileName)) return;
|
||||
this->m_fileList.append(fileName);
|
||||
}
|
||||
|
||||
void CVPilotModelMappings::addDirectory(const QString &directory)
|
||||
{
|
||||
QDir dir(directory);
|
||||
if (!dir.exists()) return;
|
||||
QStringList nameFilters({"*.vmr"});
|
||||
QFileInfoList entries = dir.entryInfoList(nameFilters, QDir::Files | QDir::Readable);
|
||||
foreach(QFileInfo file, entries)
|
||||
{
|
||||
this->addFilename(file.absoluteFilePath());
|
||||
}
|
||||
}
|
||||
|
||||
const QString &CVPilotModelMappings::standardMappingsDirectory()
|
||||
{
|
||||
static QString directory;
|
||||
if (directory.isEmpty())
|
||||
{
|
||||
directory = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first();
|
||||
if (!directory.endsWith('/')) directory.append('/');
|
||||
directory.append("vPilot Files/Model Matching Rule Sets");
|
||||
}
|
||||
return directory;
|
||||
}
|
||||
|
||||
bool CVPilotModelMappings::load()
|
||||
{
|
||||
bool success = true;
|
||||
foreach(QString fn, this->m_fileList)
|
||||
{
|
||||
bool s = this->loadFile(fn);
|
||||
success = s && success;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool CVPilotModelMappings::loadFile(const QString &fileName)
|
||||
{
|
||||
QFile f(fileName);
|
||||
if (!f.exists()) return false;
|
||||
if (!f.open(QFile::ReadOnly | QFile::Text)) return false;
|
||||
QByteArray fc = f.readAll();
|
||||
if (fc.isEmpty()) return false;
|
||||
QDomDocument doc;
|
||||
if (!doc.setContent(fc)) return false;
|
||||
QDomNodeList rules = doc.elementsByTagName("ModelMatchRule");
|
||||
if (rules.isEmpty()) return false;
|
||||
int size = rules.size();
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
QDomNamedNodeMap attributes = rules.at(i).attributes();
|
||||
const QString typeCode = attributes.namedItem("TypeCode").nodeValue();
|
||||
const QString modelName = attributes.namedItem("ModelName").nodeValue();
|
||||
const QString callsignPrefix = attributes.namedItem("CallsignPrefix").nodeValue();
|
||||
if (modelName.isEmpty()) continue;
|
||||
|
||||
if (modelName.contains("//"))
|
||||
{
|
||||
// multiple models
|
||||
QStringList models = modelName.split("//");
|
||||
foreach(QString model, models)
|
||||
{
|
||||
if (model.isEmpty()) continue;
|
||||
CAircraftMapping mapping(typeCode, callsignPrefix, model);
|
||||
this->m_mappings.push_back(mapping);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// single model
|
||||
CAircraftMapping mapping(typeCode, callsignPrefix, modelName);
|
||||
this->m_mappings.push_back(mapping);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
49
src/blacksim/vpilotmodelmappings.h
Normal file
49
src/blacksim/vpilotmodelmappings.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/* Copyright (C) 2013 VATSIM Community / authors
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef BLACKSIM_VPILOTMODELMAPPINGS_H
|
||||
#define BLACKSIM_VPILOTMODELMAPPINGS_H
|
||||
|
||||
#include "simulatormodelmappings.h"
|
||||
#include <QStringList>
|
||||
|
||||
namespace BlackSim
|
||||
{
|
||||
/*!
|
||||
* \brief Model mappings
|
||||
*/
|
||||
class CVPilotModelMappings : public ISimulatorModelMappings
|
||||
{
|
||||
|
||||
protected:
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
CVPilotModelMappings(QObject *parent = nullptr) : ISimulatorModelMappings(parent) {}
|
||||
|
||||
//! Destructor
|
||||
virtual ~CVPilotModelMappings() {}
|
||||
|
||||
//! File names
|
||||
void addFilename(const QString &fileName);
|
||||
|
||||
//! Directory with .vmr files
|
||||
void addDirectory(const QString &directory);
|
||||
|
||||
//! The standard directory for vPilot mappings
|
||||
static const QString &standardMappingsDirectory();
|
||||
|
||||
public slots:
|
||||
//! Load data
|
||||
virtual bool load() override;
|
||||
|
||||
private:
|
||||
QStringList m_fileList; //!< list of file names
|
||||
|
||||
//! Single file read and parsing
|
||||
bool loadFile(const QString &fileName);
|
||||
};
|
||||
} // namespace
|
||||
#endif // guard
|
||||
Reference in New Issue
Block a user