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/client.h"
#include "blackmisc/network/clientlist.h" #include "blackmisc/network/clientlist.h"
#include "blackmisc/network/entityflags.h" #include "blackmisc/network/entityflags.h"
#include "blackmisc/network/fsdsetup.h"
#include "blackmisc/network/role.h" #include "blackmisc/network/role.h"
#include "blackmisc/network/rolelist.h" #include "blackmisc/network/rolelist.h"
#include "blackmisc/network/server.h" #include "blackmisc/network/server.h"

View File

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

View File

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

View File

@@ -7,30 +7,34 @@
* contained in the LICENSE file. * contained in the LICENSE file.
*/ */
//! \cond PRIVATE
#include "stringutils.h" #include "stringutils.h"
#include <QChar> #include <QChar>
#include <QTextCodec>
QString BlackMisc::boolToOnOff(bool v, bool i18n) namespace BlackMisc
{ {
QString boolToOnOff(bool v, bool i18n)
{
Q_UNUSED(i18n); Q_UNUSED(i18n);
return v ? "on" : "off"; return v ? "on" : "off";
} }
QString BlackMisc::boolToYesNo(bool v, bool i18n) QString boolToYesNo(bool v, bool i18n)
{ {
Q_UNUSED(i18n); Q_UNUSED(i18n);
return v ? "yes" : "no"; return v ? "yes" : "no";
} }
QString BlackMisc::boolToTrueFalse(bool v, bool i18n) QString boolToTrueFalse(bool v, bool i18n)
{ {
Q_UNUSED(i18n); Q_UNUSED(i18n);
return v ? "true" : "false"; return v ? "true" : "false";
} }
bool BlackMisc::stringToBool(const QString &string) bool stringToBool(const QString &string)
{ {
QString s(string.trimmed().toLower()); QString s(string.trimmed().toLower());
if (s.isEmpty()) { return false; } if (s.isEmpty()) { return false; }
QChar c = s.at(0); QChar c = s.at(0);
@@ -39,19 +43,19 @@ bool BlackMisc::stringToBool(const QString &string)
if (c == '1' || c == 't' || c == 'y' || c == 'x') { return true; } if (c == '1' || c == 't' || c == 'y' || c == 'x') { return true; }
if (c == '0' || c == 'f' || c == 'n' || c == '_') { return false; } if (c == '0' || c == 'f' || c == 'n' || c == '_') { return false; }
return false; return false;
} }
QString BlackMisc::intToHex(int value, int digits) QString intToHex(int value, int digits)
{ {
QString hex(QString::number(value, 16).toUpper()); QString hex(QString::number(value, 16).toUpper());
int l = hex.length(); int l = hex.length();
if (l >= digits) { return hex.right(digits); } if (l >= digits) { return hex.right(digits); }
int d = digits - l; int d = digits - l;
return QString(d, '0') + hex; return QString(d, '0') + hex;
} }
QString BlackMisc::bytesToHexString(const QByteArray &bytes) QString bytesToHexString(const QByteArray &bytes)
{ {
QString h; QString h;
for (int i = 0; i < bytes.size(); i++) for (int i = 0; i < bytes.size(); i++)
{ {
@@ -59,10 +63,10 @@ QString BlackMisc::bytesToHexString(const QByteArray &bytes)
h.append(intToHex(b, 2)); h.append(intToHex(b, 2));
} }
return h; return h;
} }
QByteArray BlackMisc::byteArrayFromHexString(const QString &hexString) QByteArray byteArrayFromHexString(const QString &hexString)
{ {
QByteArray ba; QByteArray ba;
int pos = 0; int pos = 0;
while (pos + 1 < hexString.length()) while (pos + 1 < hexString.length())
@@ -76,10 +80,10 @@ QByteArray BlackMisc::byteArrayFromHexString(const QString &hexString)
pos += 2; pos += 2;
} }
return ba; return ba;
} }
QString BlackMisc::stripDesignatorFromCompleterString(const QString &candidate) QString stripDesignatorFromCompleterString(const QString &candidate)
{ {
const QString s(candidate.trimmed().toUpper()); const QString s(candidate.trimmed().toUpper());
if (s.isEmpty()) { return QString(); } if (s.isEmpty()) { return QString(); }
if (s.contains(' ')) if (s.contains(' '))
@@ -90,4 +94,45 @@ QString BlackMisc::stripDesignatorFromCompleterString(const QString &candidate)
{ {
return s; 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 //! Strip a designator from a combined string
BLACKMISC_EXPORT QString stripDesignatorFromCompleterString(const QString &candidate); BLACKMISC_EXPORT QString stripDesignatorFromCompleterString(const QString &candidate);
//! Strip a designator from a combined string
BLACKMISC_EXPORT QStringList textCodecNames(bool simpleNames, bool mibNames);
namespace Mixin namespace Mixin
{ {
/*! /*!