refs #273, added settings for simulator

* added class
* adjusted settings context
* adjusted "global" methods as register metadata
* improved settings utility
* renamed "settingsallclasses" as settings can also be in other namespaces, settingsallclasses now in core
This commit is contained in:
Klaus Basan
2014-06-25 19:12:00 +02:00
parent abd2d6d86d
commit 42db1fe9ae
15 changed files with 360 additions and 34 deletions

View File

@@ -1,8 +1,12 @@
#include "blacksimfreefunctions.h"
#include "blacksim/simulatorinfo.h"
#include "blacksim/setsimulator.h"
#include "fsx/simconnectutilities.h"
#include "fscommon/aircraftcfgentrieslist.h"
#include "fscommon/aircraftmappinglist.h"
#include "simulatorinfo.h"
namespace BlackSim
{
@@ -10,6 +14,7 @@ namespace BlackSim
void registerMetadata()
{
BlackSim::CSimulatorInfo::registerMetadata();
BlackSim::Settings::CSettingsSimulator::registerMetadata();
BlackSim::FsCommon::CAircraftCfgEntries::registerMetadata();
BlackSim::FsCommon::CAircraftMapping::registerMetadata();
BlackSim::FsCommon::CAircraftCfgEntriesList::registerMetadata();

View File

@@ -0,0 +1,162 @@
#include "setsimulator.h"
using namespace BlackMisc;
using namespace BlackMisc::Settings;
namespace BlackSim
{
namespace Settings
{
/*
* Constructor
*/
CSettingsSimulator::CSettingsSimulator()
{
this->initDefaultValues();
}
/*
* Convert to string
*/
QString CSettingsSimulator::convertToQString(bool i18n) const
{
Q_UNUSED(i18n);
QString s("Sel.driver:");
s.append(" ").append(m_selectedDriver.toQString(i18n));
return s;
}
/*
* metaTypeId
*/
int CSettingsSimulator::getMetaTypeId() const
{
return qMetaTypeId<CSettingsSimulator>();
}
/*
* is a
*/
bool CSettingsSimulator::isA(int metaTypeId) const
{
if (metaTypeId == qMetaTypeId<CSettingsSimulator>()) { return true; }
return this->CValueObject::isA(metaTypeId);
}
/*
* Compare
*/
int CSettingsSimulator::compareImpl(const CValueObject &otherBase) const
{
const auto &other = static_cast<const CSettingsSimulator &>(otherBase);
return compare(TupleConverter<CSettingsSimulator>::toTuple(*this), TupleConverter<CSettingsSimulator>::toTuple(other));
}
/*
* Marshall
*/
void CSettingsSimulator::marshallToDbus(QDBusArgument &argument) const
{
argument << TupleConverter<CSettingsSimulator>::toTuple(*this);
}
/*
* Unmarshall
*/
void CSettingsSimulator::unmarshallFromDbus(const QDBusArgument &argument)
{
argument >> TupleConverter<CSettingsSimulator>::toTuple(*this);
}
/*
* Equal?
*/
bool CSettingsSimulator::operator ==(const CSettingsSimulator &other) const
{
if (this == &other) return true;
return compare(*this, other) == 0;
}
/*
* Unequal?
*/
bool CSettingsSimulator::operator !=(const CSettingsSimulator &other) const
{
return !((*this) == other);
}
/*
* Hash
*/
uint CSettingsSimulator::getValueHash() const
{
return qHash(TupleConverter<CSettingsSimulator>::toTuple(*this));
}
/*
* To JSON
*/
QJsonObject CSettingsSimulator::toJson() const
{
return BlackMisc::serializeJson(CSettingsSimulator::jsonMembers(), TupleConverter<CSettingsSimulator>::toTuple(*this));
}
/*
* From JSON
*/
void CSettingsSimulator::fromJson(const QJsonObject &json)
{
BlackMisc::deserializeJson(json, CSettingsSimulator::jsonMembers(), TupleConverter<CSettingsSimulator>::toTuple(*this));
}
/*
* Members
*/
const QStringList &CSettingsSimulator::jsonMembers()
{
return TupleConverter<CSettingsSimulator>::jsonMembers();
}
/*
* Default values
*/
void CSettingsSimulator::initDefaultValues()
{
this->m_selectedDriver = CSimulatorInfo::FSX();
}
/*
* Register metadata
*/
void CSettingsSimulator::registerMetadata()
{
qRegisterMetaType<CSettingsSimulator>();
qDBusRegisterMetaType<CSettingsSimulator>();
}
/*
* Value
*/
BlackMisc::CStatusMessageList CSettingsSimulator::value(const QString &path, const QString &command, const CVariant &value, bool &changedFlag)
{
// TODO: This needs to be refactored to a smarter way to delegate commands
changedFlag = false;
CStatusMessageList msgs;
if (path == CSettingsSimulator::ValueSelectedDriver())
{
if (command == CSettingUtilities::CmdAdd() || command == CSettingUtilities::CmdUpdate())
{
if (command == CSettingUtilities::CmdUpdate())
{
CSimulatorInfo v = value.value<CSimulatorInfo>();
msgs.push_back(CSettingUtilities::valueChangedMessage(v != this->m_selectedDriver, "selected driver"));
this->m_selectedDriver = v;
return msgs;
}
return msgs;
}
}
return CSettingUtilities::wrongPathMessages(path);
}
} // namespace
} // namespace

103
src/blacksim/setsimulator.h Normal file
View File

@@ -0,0 +1,103 @@
/* 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/. */
//! \file
#ifndef BLACKMISC_SETTINGS_SIMULATOR_H
#define BLACKMISC_SETTINGS_SIMULATOR_H
#include "blackmisc/valueobject.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/settingutilities.h"
#include "simulatorinfo.h"
namespace BlackSim
{
namespace Settings
{
//! Value object encapsulating information of simulator related settings.
class CSettingsSimulator : public BlackMisc::CValueObject
{
public:
//! Default constructor.
CSettingsSimulator();
//! Destructor.
virtual ~CSettingsSimulator() {}
//! Path
static const QString &ValueSelectedDriver()
{
static const QString value("selecteddriver");
return value;
}
//! \copydoc CValueObject::toQVariant()
virtual QVariant toQVariant() const override { return QVariant::fromValue(*this); }
//! Selected driver
BlackSim::CSimulatorInfo getSelectedDriver() const { return this->m_selectedDriver; }
//! Selected driver
bool setSelectedDriver(const BlackSim::CSimulatorInfo &driver) { this->m_selectedDriver = driver; }
//! Equal operator ==
bool operator ==(const CSettingsSimulator &other) const;
//! Unequal operator !=
bool operator !=(const CSettingsSimulator &other) const;
//! \copydoc BlackCore::IContextSettings
virtual BlackMisc::CStatusMessageList value(const QString &path, const QString &command, const BlackMisc::CVariant &value, bool &changedFlag);
//! \copydoc CValueObject::getValueHash
virtual uint getValueHash() const override;
//! \copydoc CValueObject::toJson
virtual QJsonObject toJson() const override;
//! \copydoc CValueObject::fromJson
virtual void fromJson(const QJsonObject &json) override;
//! Init with meaningful default values
void initDefaultValues();
//! \copydoc CValueObject::registerMetadata
static void registerMetadata();
//! \copydoc TupleConverter<>::jsonMembers()
static const QStringList &jsonMembers();
protected:
//! \copydoc CValueObject::convertToQString
virtual QString convertToQString(bool i18n = false) const override;
//! \copydoc CValueObject::getMetaTypeId
virtual int getMetaTypeId() const override;
//! \copydoc CValueObject::isA
virtual bool isA(int metaTypeId) const override;
//! \copydoc CValueObject::compareImpl
virtual int compareImpl(const CValueObject &other) const override;
//! \copydoc CValueObject::marshallToDbus
virtual void marshallToDbus(QDBusArgument &argument) const override;
//! \copydoc CValueObject::unmarshallFromDbus
virtual void unmarshallFromDbus(const QDBusArgument &argument) override;
private:
BLACK_ENABLE_TUPLE_CONVERSION(CSettingsSimulator)
BlackSim::CSimulatorInfo m_selectedDriver;
};
} // namespace
} // namespace
Q_DECLARE_METATYPE(BlackSim::Settings::CSettingsSimulator)
BLACK_DECLARE_TUPLE_CONVERSION(BlackSim::Settings::CSettingsSimulator, (o.m_selectedDriver))
#endif // guard