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();