Ref T345, fixed value class CInformationMessage

- added property functions
- used ITimestampBased
This commit is contained in:
Klaus Basan
2018-09-09 23:27:38 +02:00
parent e89b29fe1f
commit 62601bde9d
4 changed files with 114 additions and 23 deletions

View File

@@ -8,6 +8,7 @@
*/ */
#include "blackmisc/aviation/informationmessage.h" #include "blackmisc/aviation/informationmessage.h"
#include "blackmisc/comparefunctions.h"
namespace BlackMisc namespace BlackMisc
{ {
@@ -16,12 +17,24 @@ namespace BlackMisc
QString CInformationMessage::convertToQString(bool i18n) const QString CInformationMessage::convertToQString(bool i18n) const
{ {
Q_UNUSED(i18n); Q_UNUSED(i18n);
return this->m_message; return m_message;
}
void CInformationMessage::registerMetadata()
{
CValueObject<CInformationMessage>::registerMetadata();
qRegisterMetaType<CInformationMessage::InformationType>();
}
const CInformationMessage &CInformationMessage::unspecified()
{
static const CInformationMessage u(Unspecified);
return u;
} }
const QString &CInformationMessage::getTypeAsString() const const QString &CInformationMessage::getTypeAsString() const
{ {
switch (this->m_type) switch (m_type)
{ {
case ATIS: case ATIS:
{ {
@@ -45,5 +58,54 @@ namespace BlackMisc
} }
} }
} }
CVariant CInformationMessage::propertyByIndex(const CPropertyIndex &index) const
{
if (index.isMyself()) { return CVariant::from(*this); }
if (ITimestampBased::canHandleIndex(index)) { return ITimestampBased::propertyByIndex(index); }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexType: return CVariant::from(m_type);
case IndexMessage: return CVariant::from(m_message);
default: break;
}
return CValueObject::propertyByIndex(index);
}
void CInformationMessage::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant)
{
if (index.isMyself()) { (*this) = variant.to<CInformationMessage>(); return; }
if (ITimestampBased::canHandleIndex(index)) { ITimestampBased::setPropertyByIndex(index, variant); return; }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexType: m_type = static_cast<InformationType>(variant.toInt()); break;
case IndexMessage: m_message = variant.toQString(); break;
default: break;
}
CValueObject::setPropertyByIndex(index, variant);
}
int CInformationMessage::comparePropertyByIndex(const CPropertyIndex &index, const CInformationMessage &compareValue) const
{
if (index.isMyself())
{
const int c = Compare::compare(m_type, compareValue.m_type);
if (c != 0) return c;
return m_message.compare(compareValue.m_message, Qt::CaseInsensitive);
}
if (ITimestampBased::canHandleIndex(index)) { return ITimestampBased::comparePropertyByIndex(index, compareValue); }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexMessage: return m_message.compare(compareValue.m_message, Qt::CaseInsensitive);
case IndexType: return Compare::compare(this->getType(), compareValue.getType());
default:
return CValueObject::comparePropertyByIndex(index, *this);
}
Q_ASSERT_X(false, Q_FUNC_INFO, "Compare failed");
return 0;
}
} // namespace } // namespace
} // namespace } // namespace

View File

@@ -12,9 +12,10 @@
#ifndef BLACKMISC_AVIATION_INFORMATIONMESSAGE_H #ifndef BLACKMISC_AVIATION_INFORMATIONMESSAGE_H
#define BLACKMISC_AVIATION_INFORMATIONMESSAGE_H #define BLACKMISC_AVIATION_INFORMATIONMESSAGE_H
#include "blackmisc/blackmiscexport.h" #include "blackmisc/timestampbased.h"
#include "blackmisc/metaclass.h" #include "blackmisc/metaclass.h"
#include "blackmisc/valueobject.h" #include "blackmisc/valueobject.h"
#include "blackmisc/blackmiscexport.h"
#include <QDateTime> #include <QDateTime>
#include <QMetaType> #include <QMetaType>
@@ -26,7 +27,9 @@ namespace BlackMisc
namespace Aviation namespace Aviation
{ {
//! Value object encapsulating information message (ATIS, METAR, TAF) //! Value object encapsulating information message (ATIS, METAR, TAF)
class BLACKMISC_EXPORT CInformationMessage : public CValueObject<CInformationMessage> class BLACKMISC_EXPORT CInformationMessage :
public CValueObject<CInformationMessage>,
public ITimestampBased
{ {
public: public:
//! Type //! Type
@@ -38,8 +41,15 @@ namespace BlackMisc
TAF TAF
}; };
//! Properties by index
enum ColumnIndex
{
IndexType = CPropertyIndex::GlobalIndexCInformationMessage,
IndexMessage,
};
//! Default constructor. //! Default constructor.
CInformationMessage() : m_type(CInformationMessage::Unspecified), m_receivedTimestamp(QDateTime::currentDateTimeUtc()) CInformationMessage() : m_type(CInformationMessage::Unspecified)
{} {}
//! Information message of type //! Information message of type
@@ -59,56 +69,68 @@ namespace BlackMisc
//! Set message //! Set message
void setMessage(const QString &message) void setMessage(const QString &message)
{ {
this->m_receivedTimestamp = QDateTime::currentDateTimeUtc(); this->setCurrentUtcTime();
this->m_message = message; m_message = message;
} }
//! Append message part //! Append message part
void appendMessage(const QString &messagePart) void appendMessage(const QString &messagePart)
{ {
this->m_receivedTimestamp = QDateTime::currentDateTimeUtc(); this->setCurrentUtcTime();
this->m_message.append(messagePart); m_message.append(messagePart);
} }
//! Type as string //! Type as string
const QString &getTypeAsString() const; const QString &getTypeAsString() const;
//! Type //! Type
InformationType getType() const { return this->m_type; } InformationType getType() const { return m_type; }
//! Set type //! Set type
void setType(InformationType type) { this->m_type = type; } void setType(InformationType type) { m_type = type; }
//! Timestamp
const QDateTime &getReceivedTimestamp() const { return this->m_receivedTimestamp; }
//! Received before n ms //! Received before n ms
qint64 timeDiffReceivedMs() const qint64 timeDiffReceivedMs() const
{ {
return this->m_receivedTimestamp.msecsTo(QDateTime::currentDateTimeUtc()); return this->getTimeDifferenceToNowMs();
} }
//! Is empty? //! Is empty?
bool isEmpty() const { return this->m_message.isEmpty(); } bool isEmpty() const { return m_message.isEmpty(); }
//! \copydoc BlackMisc::Mixin::Index::propertyByIndex
CVariant propertyByIndex(const CPropertyIndex &index) const;
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant);
//! \copydoc BlackMisc::Mixin::Index::comparePropertyByIndex
int comparePropertyByIndex(const CPropertyIndex &index, const CInformationMessage &compareValue) const;
//! \copydoc BlackMisc::Mixin::String::toQString //! \copydoc BlackMisc::Mixin::String::toQString
QString convertToQString(bool i18n = false) const; QString convertToQString(bool i18n = false) const;
//! \copydoc BlackMisc::CValueObject::registerMetadata
static void registerMetadata();
//! Unspecified object
static const CInformationMessage &unspecified();
private: private:
InformationType m_type; InformationType m_type;
QString m_message; QString m_message;
QDateTime m_receivedTimestamp;
BLACK_METACLASS( BLACK_METACLASS(
CInformationMessage, CInformationMessage,
BLACK_METAMEMBER(type), BLACK_METAMEMBER(type),
BLACK_METAMEMBER(message), BLACK_METAMEMBER(message),
BLACK_METAMEMBER(receivedTimestamp) BLACK_METAMEMBER(timestampMSecsSinceEpoch)
); );
}; };
} // namespace } // namespace
} // namespace } // namespace
Q_DECLARE_METATYPE(BlackMisc::Aviation::CInformationMessage) Q_DECLARE_METATYPE(BlackMisc::Aviation::CInformationMessage)
Q_DECLARE_METATYPE(BlackMisc::Aviation::CInformationMessage::InformationType)
#endif // guard #endif // guard

View File

@@ -93,11 +93,12 @@ namespace BlackMisc
GlobalIndexCAircraftSituation = 2100, GlobalIndexCAircraftSituation = 2100,
GlobalIndexCAircraftSituationChange = 2200, GlobalIndexCAircraftSituationChange = 2200,
GlobalIndexCAtcStation = 2300, GlobalIndexCAtcStation = 2300,
GlobalIndexCAirport = 2400, GlobalIndexCInformationMessage = 2400,
GlobalIndexCAircraftParts = 2500, GlobalIndexCAirport = 2500,
GlobalIndexCAircraftLights = 2600, GlobalIndexCAircraftParts = 2600,
GlobalIndexCLivery = 2700, GlobalIndexCAircraftLights = 2700,
GlobalIndexCFlightPlan = 2800, GlobalIndexCLivery = 2800,
GlobalIndexCFlightPlan = 2900,
GlobalIndexCComSystem = 3000, GlobalIndexCComSystem = 3000,
GlobalIndexCModulator = 3100, GlobalIndexCModulator = 3100,
GlobalIndexCTransponder = 3200, GlobalIndexCTransponder = 3200,
@@ -258,6 +259,9 @@ namespace BlackMisc
//! \copydoc BlackMisc::CValueObject::convertFromJson //! \copydoc BlackMisc::CValueObject::convertFromJson
void convertFromJson(const QJsonObject &json); void convertFromJson(const QJsonObject &json);
//! an empty property index
static const CPropertyIndex &empty() { static const CPropertyIndex pi; return pi; }
protected: protected:
//! Parse indexes from string //! Parse indexes from string
void parseFromString(const QString &indexes); void parseFromString(const QString &indexes);

View File

@@ -49,6 +49,9 @@ namespace BlackMisc
//! Time difference in ms //! Time difference in ms
qint64 getTimeDifferenceMs(qint64 compareTime) const { return compareTime - this->getMSecsSinceEpoch(); } qint64 getTimeDifferenceMs(qint64 compareTime) const { return compareTime - this->getMSecsSinceEpoch(); }
//! Time difference to now
qint64 getTimeDifferenceToNowMs() const { return this->getTimeDifferenceMs(QDateTime::currentMSecsSinceEpoch()); }
//! Time difference in ms //! Time difference in ms
qint64 getTimeDifferenceMs(const ITimestampBased &compare) const { return compare.getMSecsSinceEpoch() - this->getMSecsSinceEpoch(); } qint64 getTimeDifferenceMs(const ITimestampBased &compare) const { return compare.getMSecsSinceEpoch() - this->getMSecsSinceEpoch(); }