mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-28 20:25:34 +08:00
refs #933, utility class to start MCX and settings trait
This commit is contained in:
committed by
Mathew Sutcliffe
parent
54219966ac
commit
8eb9a8b217
74
src/blackmisc/simulation/modelconverterx.cpp
Normal file
74
src/blackmisc/simulation/modelconverterx.cpp
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
/* Copyright (C) 2017
|
||||||
|
* swift project Community / Contributors
|
||||||
|
*
|
||||||
|
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||||
|
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||||
|
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||||
|
* contained in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "blackconfig/buildconfig.h"
|
||||||
|
#include "blackmisc/simulation/aircraftmodel.h"
|
||||||
|
#include "blackmisc/logmessage.h"
|
||||||
|
#include "modelconverterx.h"
|
||||||
|
#include <QFile>
|
||||||
|
#include <QDir>
|
||||||
|
|
||||||
|
using namespace BlackConfig;
|
||||||
|
using namespace BlackMisc;
|
||||||
|
|
||||||
|
namespace BlackMisc
|
||||||
|
{
|
||||||
|
namespace Simulation
|
||||||
|
{
|
||||||
|
QProcess *CModelConverterX::s_proccess = nullptr;
|
||||||
|
|
||||||
|
bool CModelConverterX::supportsModelConverterX()
|
||||||
|
{
|
||||||
|
if (!CBuildConfig::isRunningOnWindowsNtPlatform()) { return false; }
|
||||||
|
return !getBinary().isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
QProcess *CModelConverterX::startModelConverterX(const CAircraftModel &model, QObject *parent)
|
||||||
|
{
|
||||||
|
// checks
|
||||||
|
if (model.getFileName().isEmpty()) { return nullptr; }
|
||||||
|
const QString modelConverterX = getBinary();
|
||||||
|
if (modelConverterX.isEmpty()) { return nullptr; }
|
||||||
|
|
||||||
|
// delete other MCX
|
||||||
|
if (s_proccess)
|
||||||
|
{
|
||||||
|
QProcess *old = s_proccess;
|
||||||
|
s_proccess = nullptr;
|
||||||
|
if (old->state() == QProcess::Running)
|
||||||
|
{
|
||||||
|
// if still running, terminate and then delete
|
||||||
|
QObject::connect(old, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), old, &QObject::deleteLater);
|
||||||
|
old->terminate();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
old->deleteLater();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QProcess *process = new QProcess(parent);
|
||||||
|
const QString argument = QDir::toNativeSeparators(model.getFileName());
|
||||||
|
process->setProgram(modelConverterX);
|
||||||
|
process->setArguments({argument});
|
||||||
|
process->start();
|
||||||
|
s_proccess = process;
|
||||||
|
return process;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString CModelConverterX::getBinary()
|
||||||
|
{
|
||||||
|
static const BlackMisc::CSettingReadOnly<TModelConverterXBinary> setting(new QObject());
|
||||||
|
const QString mcx = setting.get();
|
||||||
|
if (mcx.isEmpty()) return "";
|
||||||
|
const QFile f(mcx);
|
||||||
|
return (f.exists()) ? mcx : "";
|
||||||
|
}
|
||||||
|
} // ns
|
||||||
|
} // ns
|
||||||
64
src/blackmisc/simulation/modelconverterx.h
Normal file
64
src/blackmisc/simulation/modelconverterx.h
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
/* Copyright (C) 2017
|
||||||
|
* swift Project Community / Contributors
|
||||||
|
*
|
||||||
|
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||||
|
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||||
|
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||||
|
* contained in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//! \file
|
||||||
|
|
||||||
|
#ifndef BLACKMISC_SIMULATION_MODELCONVERTERX_H
|
||||||
|
#define BLACKMISC_SIMULATION_MODELCONVERTERX_H
|
||||||
|
|
||||||
|
#include "blackmisc/blackmiscexport.h"
|
||||||
|
#include "blackmisc/settingscache.h"
|
||||||
|
#include <QProcess>
|
||||||
|
#include <QString>
|
||||||
|
#include <QFile>
|
||||||
|
|
||||||
|
namespace BlackMisc
|
||||||
|
{
|
||||||
|
namespace Simulation
|
||||||
|
{
|
||||||
|
class CAircraftModel;
|
||||||
|
|
||||||
|
//! Simple utility class to support ModelConverterX integration.
|
||||||
|
class BLACKMISC_EXPORT CModelConverterX
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
//! Default constructor
|
||||||
|
CModelConverterX() = delete;
|
||||||
|
|
||||||
|
//! Guess a default simulator based on installation
|
||||||
|
static bool supportsModelConverterX();
|
||||||
|
|
||||||
|
//! Start ModelConverterX for given model
|
||||||
|
static QProcess *startModelConverterX(const CAircraftModel &model, QObject *parent);
|
||||||
|
|
||||||
|
private:
|
||||||
|
//! Get the binary
|
||||||
|
static QString getBinary();
|
||||||
|
|
||||||
|
static QProcess *s_proccess; //!< 0..1 process running
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Binary of MCX
|
||||||
|
struct TModelConverterXBinary : public BlackMisc::TSettingTrait<QString>
|
||||||
|
{
|
||||||
|
//! \copydoc BlackMisc::TSettingTrait::key
|
||||||
|
static const char *key() { return "mapping/modelconverterxbin"; }
|
||||||
|
|
||||||
|
//! \copydoc BlackMisc::TSettingTrait::isValid
|
||||||
|
static bool isValid(const QString &value)
|
||||||
|
{
|
||||||
|
if (value.isEmpty()) { return true; }
|
||||||
|
const QFile f(value);
|
||||||
|
return f.exists();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // ns
|
||||||
|
} // ns
|
||||||
|
|
||||||
|
#endif // guard
|
||||||
Reference in New Issue
Block a user