refs #681, value object for FSD setup

This commit is contained in:
Klaus Basan
2016-06-25 19:12:38 +02:00
parent c85b5d9341
commit fcba4f2e75
7 changed files with 351 additions and 77 deletions

View File

@@ -0,0 +1,112 @@
/* Copyright (C) 2016
* 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 "blackmisc/network/fsdsetup.h"
#include "blackmisc/logcategory.h"
#include "blackmisc/logcategorylist.h"
#include "blackmisc/propertyindex.h"
#include "blackmisc/statusmessage.h"
#include "blackmisc/stringutils.h"
#include "blackmisc/variant.h"
#include <Qt>
#include <QtGlobal>
using namespace BlackMisc;
namespace BlackMisc
{
namespace Network
{
CFsdSetup::CFsdSetup(const QString &codec, SendReceiveDetails sendReceive)
: m_textCodec(codec), m_sendReceive(sendReceive) {}
CFsdSetup::SendReceiveDetails CFsdSetup::getSendReceiveDetails() const
{
return static_cast<SendReceiveDetails>(m_sendReceive);
}
QString CFsdSetup::convertToQString(bool i18n) const
{
Q_UNUSED(i18n);
QString s("Codec: ");
s.append(" ").append(this->m_textCodec);
s.append(" details:").append(sendReceiveDetailsToString(this->getSendReceiveDetails()));
return s;
}
QString CFsdSetup::sendReceiveDetailsToString(SendReceiveDetails details)
{
QString ds("Send parts; %1 interim: %2 Receive parts: %3 interim: %4");
return ds.arg(boolToYesNo(details.testFlag(SendAircraftParts))).
arg(boolToYesNo(details.testFlag(SendIterimPositions))).
arg(boolToYesNo(details.testFlag(ReceiveAircraftParts))).
arg(boolToYesNo(details.testFlag(ReceiveInterimPositions)));
}
void CFsdSetup::setSendReceiveDetails(bool partsSend, bool partsReceive, bool interimSend, bool interimReceive)
{
SendReceiveDetails s = Nothing;
if (partsSend) { s |= SendAircraftParts; }
if (partsReceive) { s |= ReceiveAircraftParts; }
if (interimSend) { s |= SendIterimPositions; }
if (interimReceive) { s |= ReceiveInterimPositions; }
this->setSendReceiveDetails(s);
}
const CFsdSetup &CFsdSetup::vatsimStandard()
{
static const CFsdSetup s;
return s;
}
CStatusMessageList CFsdSetup::validate() const
{
static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::validation()}));
CStatusMessageList msgs;
if (this->getTextCodec().isEmpty()) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityError, "No codec")); }
if (!textCodecNames(true, true).contains(this->getTextCodec())) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityError, "Unrecognized codec name")); }
msgs.addCategories(cats);
return msgs;
}
CVariant CFsdSetup::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
{
if (index.isMyself()) { return CVariant::from(*this); }
ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexTextCodec:
return CVariant::fromValue(this->m_textCodec);
case IndexSendReceiveDetails:
return CVariant::fromValue(this->m_sendReceive);
default:
return CValueObject::propertyByIndex(index);
}
}
void CFsdSetup::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant)
{
if (index.isMyself()) { (*this) = variant.to<CFsdSetup>(); return; }
ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexTextCodec:
this->setTextCodec(variant.value<QString>());
break;
case IndexSendReceiveDetails:
this->setSendReceiveDetails(variant.value<SendReceiveDetails>());
break;
default:
CValueObject::setPropertyByIndex(index, variant);
break;
}
}
} // namespace
} // namespace

View File

@@ -0,0 +1,111 @@
/* Copyright (C) 2016
* 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_NETWORK_FSDSETIP_H
#define BLACKMISC_NETWORK_FSDSETIP_H
#include "blackmisc/blackmiscexport.h"
#include "blackmisc/metaclass.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/propertyindex.h"
#include "blackmisc/valueobject.h"
#include "blackmisc/variant.h"
#include <QMetaType>
#include <QString>
namespace BlackMisc
{
namespace Network
{
//! Value object for a FSD setup
class BLACKMISC_EXPORT CFsdSetup : public CValueObject<CFsdSetup>
{
public:
//! Properties by index
enum ColumnIndex
{
IndexTextCodec = BlackMisc::CPropertyIndex::GlobalIndexCFsdSetup,
IndexSendReceiveDetails
};
//! Send/receive details
enum SendReceiveDetailsFlag
{
Nothing = 0, //!< nothing
SendAircraftParts = 1 << 0, //!< aircraft parts out
SendIterimPositions = 1 << 1, //!< interim positions in
ReceiveAircraftParts = 1 << 2, //!< fast position updates out
ReceiveInterimPositions = 1 << 3, //!< fast position updates in
AllSending = SendAircraftParts | SendIterimPositions, //!< all out
AllReceive = ReceiveAircraftParts | ReceiveInterimPositions, //!< all in
All = AllReceive | AllSending //!< all
};
Q_DECLARE_FLAGS(SendReceiveDetails, SendReceiveDetailsFlag)
//! Default constructor.
CFsdSetup() {}
//! Constructor.
CFsdSetup(const QString &codec, SendReceiveDetails sendReceive = All);
//! Get codec
const QString &getTextCodec() const { return m_textCodec; }
//! Set codec
void setTextCodec(const QString &codec) { m_textCodec = codec.trimmed().toLower(); }
//! Get send / receive details
SendReceiveDetails getSendReceiveDetails() const;
//! Set send / receive details
void setSendReceiveDetails(SendReceiveDetails sendReceive) { m_sendReceive = sendReceive; }
//! Set send / receive details
void setSendReceiveDetails(bool partsSend, bool partsReceive, bool interimSend, bool interimReceive);
//! Validate, provide details about issues
BlackMisc::CStatusMessageList validate() const;
//! \copydoc BlackMisc::Mixin::Index::propertyByIndex
CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const;
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant);
//! \copydoc BlackMisc::Mixin::String::toQString()
QString convertToQString(bool i18n = false) const;
//! Details as string
static QString sendReceiveDetailsToString(SendReceiveDetails details);
//! Standard FSD setup for official VATSIM servers
static const CFsdSetup &vatsimStandard();
private:
QString m_textCodec = "latin1";
int m_sendReceive = static_cast<int>(All);
BLACK_METACLASS(
CFsdSetup,
BLACK_METAMEMBER(textCodec),
BLACK_METAMEMBER(sendReceive)
);
};
} // namespace
} // namespace
Q_DECLARE_METATYPE(BlackMisc::Network::CFsdSetup)
Q_DECLARE_METATYPE(BlackMisc::Network::CFsdSetup::SendReceiveDetails)
Q_DECLARE_METATYPE(BlackMisc::Network::CFsdSetup::SendReceiveDetailsFlag)
Q_DECLARE_OPERATORS_FOR_FLAGS(BlackMisc::Network::CFsdSetup::SendReceiveDetails)
#endif // guard

View File

@@ -21,6 +21,7 @@
#include "blackmisc/network/client.h"
#include "blackmisc/network/clientlist.h"
#include "blackmisc/network/entityflags.h"
#include "blackmisc/network/fsdsetup.h"
#include "blackmisc/network/role.h"
#include "blackmisc/network/rolelist.h"
#include "blackmisc/network/server.h"

View File

@@ -20,6 +20,7 @@ namespace BlackMisc
CClient::registerMetadata();
CClientList::registerMetadata();
CEntityFlags::registerMetadata();
CFsdSetup::registerMetadata();
CRole::registerMetadata();
CRoleList::registerMetadata();
CServer::registerMetadata();

View File

@@ -87,14 +87,15 @@ namespace BlackMisc
GlobalIndexCAuthenticatedUser = 6200,
GlobalIndexCRole = 6300,
GlobalIndexCServer = 6400,
GlobalIndexCUrl = 6500,
GlobalIndexCAircraftModel = 6600,
GlobalIndexCSimulatedAircraft = 6700,
GlobalIndexCTextMessage = 6800,
GlobalIndexCSimulatorSetup = 6900,
GlobalIndexCSimulatorSettings = 7000,
GlobalIndexCAircraftCfgEntries = 7100,
GlobalIndexCDistributor = 7200,
GlobalIndexCFsdSetup = 6500,
GlobalIndexCUrl = 6600,
GlobalIndexCAircraftModel = 6700,
GlobalIndexCSimulatedAircraft = 6800,
GlobalIndexCTextMessage = 6900,
GlobalIndexCSimulatorSetup = 7000,
GlobalIndexCSimulatorSettings = 7100,
GlobalIndexCAircraftCfgEntries = 7200,
GlobalIndexCDistributor = 7300,
GlobalIndexCVPilotModelRule = 8000,
GlobalIndexCVoiceRoom = 9000,
GlobalIndexCSettingKeyboardHotkey = 10000,

View File

@@ -7,87 +7,132 @@
* contained in the LICENSE file.
*/
//! \cond PRIVATE
#include "stringutils.h"
#include <QChar>
#include <QTextCodec>
QString BlackMisc::boolToOnOff(bool v, bool i18n)
namespace BlackMisc
{
Q_UNUSED(i18n);
return v ? "on" : "off";
}
QString BlackMisc::boolToYesNo(bool v, bool i18n)
{
Q_UNUSED(i18n);
return v ? "yes" : "no";
}
QString BlackMisc::boolToTrueFalse(bool v, bool i18n)
{
Q_UNUSED(i18n);
return v ? "true" : "false";
}
bool BlackMisc::stringToBool(const QString &string)
{
QString s(string.trimmed().toLower());
if (s.isEmpty()) { return false; }
QChar c = s.at(0);
// explicit values
if (c == '1' || c == 't' || c == 'y' || c == 'x') { return true; }
if (c == '0' || c == 'f' || c == 'n' || c == '_') { return false; }
return false;
}
QString BlackMisc::intToHex(int value, int digits)
{
QString hex(QString::number(value, 16).toUpper());
int l = hex.length();
if (l >= digits) { return hex.right(digits); }
int d = digits - l;
return QString(d, '0') + hex;
}
QString BlackMisc::bytesToHexString(const QByteArray &bytes)
{
QString h;
for (int i = 0; i < bytes.size(); i++)
QString boolToOnOff(bool v, bool i18n)
{
int b = static_cast<int>(bytes.at(i));
h.append(intToHex(b, 2));
Q_UNUSED(i18n);
return v ? "on" : "off";
}
return h;
}
QByteArray BlackMisc::byteArrayFromHexString(const QString &hexString)
{
QByteArray ba;
int pos = 0;
while (pos + 1 < hexString.length())
QString boolToYesNo(bool v, bool i18n)
{
bool ok;
QString h = hexString.mid(pos, 2);
int hex = h.toInt(&ok, 16);
Q_ASSERT_X(ok, Q_FUNC_INFO, "Invalid hex");
if (!ok) { return QByteArray(); }
ba.push_back(static_cast<char>(hex));
pos += 2;
Q_UNUSED(i18n);
return v ? "yes" : "no";
}
return ba;
}
QString BlackMisc::stripDesignatorFromCompleterString(const QString &candidate)
{
const QString s(candidate.trimmed().toUpper());
if (s.isEmpty()) { return QString(); }
if (s.contains(' '))
QString boolToTrueFalse(bool v, bool i18n)
{
return s.left(s.indexOf(' '));
Q_UNUSED(i18n);
return v ? "true" : "false";
}
else
bool stringToBool(const QString &string)
{
return s;
QString s(string.trimmed().toLower());
if (s.isEmpty()) { return false; }
QChar c = s.at(0);
// explicit values
if (c == '1' || c == 't' || c == 'y' || c == 'x') { return true; }
if (c == '0' || c == 'f' || c == 'n' || c == '_') { return false; }
return false;
}
QString intToHex(int value, int digits)
{
QString hex(QString::number(value, 16).toUpper());
int l = hex.length();
if (l >= digits) { return hex.right(digits); }
int d = digits - l;
return QString(d, '0') + hex;
}
QString bytesToHexString(const QByteArray &bytes)
{
QString h;
for (int i = 0; i < bytes.size(); i++)
{
int b = static_cast<int>(bytes.at(i));
h.append(intToHex(b, 2));
}
return h;
}
QByteArray byteArrayFromHexString(const QString &hexString)
{
QByteArray ba;
int pos = 0;
while (pos + 1 < hexString.length())
{
bool ok;
QString h = hexString.mid(pos, 2);
int hex = h.toInt(&ok, 16);
Q_ASSERT_X(ok, Q_FUNC_INFO, "Invalid hex");
if (!ok) { return QByteArray(); }
ba.push_back(static_cast<char>(hex));
pos += 2;
}
return ba;
}
QString stripDesignatorFromCompleterString(const QString &candidate)
{
const QString s(candidate.trimmed().toUpper());
if (s.isEmpty()) { return QString(); }
if (s.contains(' '))
{
return s.left(s.indexOf(' '));
}
else
{
return s;
}
}
QStringList simpleTextCodecNamesImpl()
{
QStringList codecs;
for (const QByteArray &ba : QTextCodec::availableCodecs())
{
const QString c(QString::fromLocal8Bit(ba));
codecs << c;
}
return codecs;
}
QStringList mibTextCodecNamesImpl()
{
QStringList codecs;
for (int mib : QTextCodec::availableMibs())
{
const QByteArray ba(QTextCodec::codecForMib(mib)->name());
const QString c(QString::fromLocal8Bit(ba));
codecs << c;
}
return codecs;
}
QStringList textCodecNames(bool simpleNames, bool mibNames)
{
static const QStringList simple(simpleTextCodecNamesImpl());
static const QStringList mib(mibTextCodecNamesImpl());
if (simpleNames && mibNames)
{
QStringList s(simple);
s.append(mib);
return s;
}
if (simpleNames) { return simple; }
if (mibNames) { return mib; }
return QStringList();
}
}
//! \endcond

View File

@@ -69,6 +69,9 @@ namespace BlackMisc
//! Strip a designator from a combined string
BLACKMISC_EXPORT QString stripDesignatorFromCompleterString(const QString &candidate);
//! Strip a designator from a combined string
BLACKMISC_EXPORT QStringList textCodecNames(bool simpleNames, bool mibNames);
namespace Mixin
{
/*!