mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-12 23:35:33 +08:00
Status message list model (so they can displayed as table view in GUI),
and the corresponding property methods in CStatusMessage
This commit is contained in:
committed by
Mathew Sutcliffe
parent
5731bd3dd1
commit
f476388f8b
69
src/blackgui/statusmessagelistmodel.cpp
Normal file
69
src/blackgui/statusmessagelistmodel.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "statusmessagelistmodel.h"
|
||||
#include "blackmisc/blackmiscfreefunctions.h"
|
||||
#include <QMetaProperty>
|
||||
#include <QBrush>
|
||||
#include <QIcon>
|
||||
|
||||
using namespace BlackMisc;
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
CStatusMessageListModel::CStatusMessageListModel(QObject *parent) :
|
||||
CListModelBase<BlackMisc::CStatusMessage, BlackMisc::CStatusMessageList>("ViewStatusMessageList", parent)
|
||||
{
|
||||
this->m_columns.addColumn(CStatusMessage::IndexTimestamp, "time");
|
||||
this->m_columns.addColumn(CStatusMessage::IndexSeverity, "", Qt::AlignCenter);
|
||||
this->m_columns.addColumn(CStatusMessage::IndexMessage, "message");
|
||||
this->m_columns.addColumn(CStatusMessage::IndexTypeAsString, "type");
|
||||
|
||||
this->m_sortedColumn = CStatusMessage::IndexTimestamp;
|
||||
this->m_sortOrder = Qt::DescendingOrder;
|
||||
|
||||
// force strings for translation in resource files
|
||||
(void)QT_TRANSLATE_NOOP("ViewStatusMessageList", "time");
|
||||
(void)QT_TRANSLATE_NOOP("ViewStatusMessageList", "severity");
|
||||
(void)QT_TRANSLATE_NOOP("ViewStatusMessageList", "type");
|
||||
(void)QT_TRANSLATE_NOOP("ViewStatusMessageList", "message");
|
||||
}
|
||||
|
||||
/*
|
||||
* Display icons
|
||||
*/
|
||||
QVariant CStatusMessageListModel::data(const QModelIndex &modelIndex, int role) const
|
||||
{
|
||||
// shortcut, fast check
|
||||
if (role != Qt::DisplayRole && role != Qt::DecorationRole) return CListModelBase::data(modelIndex, role);
|
||||
|
||||
static QPixmap w(QPixmap(":/blackgui/iconsQt/warning.png").scaledToWidth(16, Qt::SmoothTransformation));
|
||||
static QPixmap e(QPixmap(":/blackgui/iconsQt/critical.png").scaledToWidth(16, Qt::SmoothTransformation));
|
||||
static QPixmap i(QPixmap(":/blackgui/iconsQt/information.png").scaledToWidth(16, Qt::SmoothTransformation));
|
||||
|
||||
if (this->columnToPropertyIndex(modelIndex.column()) == CStatusMessage::IndexSeverity)
|
||||
{
|
||||
if (role == Qt::DecorationRole)
|
||||
{
|
||||
CStatusMessage msg = this->at(modelIndex);
|
||||
switch (msg.getSeverity())
|
||||
{
|
||||
case CStatusMessage::SeverityError: return e;
|
||||
case CStatusMessage::SeverityWarning: return w;
|
||||
default: return i;
|
||||
}
|
||||
}
|
||||
else if (role == Qt::DisplayRole)
|
||||
{
|
||||
// the text itself should be empty
|
||||
return QVariant("");
|
||||
}
|
||||
else if (role == Qt::ToolTipRole)
|
||||
{
|
||||
CStatusMessage msg = this->at(modelIndex);
|
||||
return QVariant(msg.getSeverityAsString());
|
||||
}
|
||||
}
|
||||
return CListModelBase::data(modelIndex, role);
|
||||
}
|
||||
}
|
||||
30
src/blackgui/statusmessagelistmodel.h
Normal file
30
src/blackgui/statusmessagelistmodel.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef BLACKGUI_STATUSMESSAGELISTMODEL_H
|
||||
#define BLACKGUI_STATUSMESSAGELISTMODEL_H
|
||||
|
||||
#include <QAbstractItemModel>
|
||||
#include <QDBusConnection>
|
||||
#include "blackmisc/statusmessagelist.h"
|
||||
#include "blackgui/listmodelbase.h"
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
/*!
|
||||
* \brief Server list model
|
||||
*/
|
||||
class CStatusMessageListModel : public CListModelBase<BlackMisc::CStatusMessage, BlackMisc::CStatusMessageList>
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
//! \brief Constructor
|
||||
explicit CStatusMessageListModel(QObject *parent = nullptr);
|
||||
|
||||
//! \brief Destructor
|
||||
virtual ~CStatusMessageListModel() {}
|
||||
|
||||
//! \copydoc CListModelBase::data
|
||||
QVariant data(const QModelIndex &modelIndex, int role = Qt::DisplayRole) const;
|
||||
|
||||
};
|
||||
}
|
||||
#endif // guard
|
||||
@@ -101,6 +101,89 @@ namespace BlackMisc
|
||||
return BlackMisc::calculateHash(hashs, "CStatusMessage");
|
||||
}
|
||||
|
||||
/*
|
||||
* Type
|
||||
*/
|
||||
const QString &CStatusMessage::getTypeAsString() const
|
||||
{
|
||||
switch (this->m_type)
|
||||
{
|
||||
case TypeAudio:
|
||||
{
|
||||
static QString t("audio");
|
||||
return t;
|
||||
}
|
||||
case TypeCore:
|
||||
{
|
||||
static QString t("core");
|
||||
return t;
|
||||
}
|
||||
case TypeGui:
|
||||
{
|
||||
static QString t("gui");
|
||||
return t;
|
||||
}
|
||||
case TypeSettings:
|
||||
{
|
||||
static QString t("settings");
|
||||
return t;
|
||||
}
|
||||
case TypeTrafficNetwork:
|
||||
{
|
||||
static QString t("traffic network");
|
||||
return t;
|
||||
}
|
||||
case TypeUnknown:
|
||||
{
|
||||
static QString t("unknown");
|
||||
return t;
|
||||
}
|
||||
case TypeUnspecific:
|
||||
{
|
||||
static QString t("unspecific");
|
||||
return t;
|
||||
}
|
||||
case TypeValidation:
|
||||
{
|
||||
static QString t("validation");
|
||||
return t;
|
||||
}
|
||||
default:
|
||||
static QString x("unknown type");
|
||||
qFatal("Unknown type");
|
||||
return x; // just for compiler warning
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Severity
|
||||
*/
|
||||
const QString &CStatusMessage::getSeverityAsString() const
|
||||
{
|
||||
switch (this->m_severity)
|
||||
{
|
||||
case SeverityInfo:
|
||||
{
|
||||
static QString i("info");
|
||||
return i;
|
||||
}
|
||||
case SeverityWarning:
|
||||
{
|
||||
static QString w("warning");
|
||||
return w;
|
||||
}
|
||||
case SeverityError:
|
||||
{
|
||||
static QString e("error");
|
||||
return e;
|
||||
}
|
||||
default:
|
||||
static QString x("unknown severity");
|
||||
qFatal("Unknown severity");
|
||||
return x; // just for compiler warning
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Equal
|
||||
*/
|
||||
@@ -145,4 +228,78 @@ namespace BlackMisc
|
||||
this->m_type = static_cast<StatusType>(type);
|
||||
this->m_severity = static_cast<StatusSeverity>(severity);
|
||||
}
|
||||
|
||||
/*
|
||||
* Property by index
|
||||
*/
|
||||
QVariant CStatusMessage::propertyByIndex(int index) const
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case IndexMessage:
|
||||
return QVariant(this->m_message);
|
||||
case IndexSeverity:
|
||||
return QVariant(static_cast<uint>(this->m_severity));
|
||||
case IndexSeverityAsString:
|
||||
return QVariant(this->getSeverityAsString());
|
||||
case IndexTimestamp:
|
||||
return QVariant(this->m_timestamp);
|
||||
case IndexType:
|
||||
return QVariant(static_cast<uint>(this->m_type));
|
||||
case IndexTypeAsString:
|
||||
return QVariant(this->getTypeAsString());
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Q_ASSERT_X(false, "CStatusMessage", "index unknown");
|
||||
QString m = QString("no property, index ").append(QString::number(index));
|
||||
return QVariant::fromValue(m);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set property as index
|
||||
*/
|
||||
void CStatusMessage::propertyByIndex(const QVariant &variant, int index)
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case IndexMessage:
|
||||
this->m_message = variant.value<QString>();
|
||||
break;
|
||||
case IndexTimestamp:
|
||||
this->m_timestamp = variant.value<QDateTime>();
|
||||
break;
|
||||
case IndexSeverity:
|
||||
this->m_severity = static_cast<StatusSeverity>(variant.value<uint>());
|
||||
break;
|
||||
case IndexType:
|
||||
this->m_type = static_cast<StatusType>(variant.value<uint>());
|
||||
break;
|
||||
default:
|
||||
Q_ASSERT_X(false, "CStatusMessage", "index unknown (setter)");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Property as string by index
|
||||
*/
|
||||
QString CStatusMessage::propertyByIndexAsString(int index, bool i18n) const
|
||||
{
|
||||
QVariant qv = this->propertyByIndex(index);
|
||||
switch (index)
|
||||
{
|
||||
case IndexTimestamp:
|
||||
{
|
||||
QDateTime dt = qv.value<QDateTime>();
|
||||
if (dt.isNull() || !dt.isValid()) return "";
|
||||
return dt.toString("HH:mm::ss.zzz");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return BlackMisc::qVariantToString(qv, i18n);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,19 @@ namespace BlackMisc
|
||||
SeverityError
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief Properties by index
|
||||
*/
|
||||
enum ColumnIndex
|
||||
{
|
||||
IndexType,
|
||||
IndexTypeAsString,
|
||||
IndexSeverity,
|
||||
IndexSeverityAsString,
|
||||
IndexMessage,
|
||||
IndexTimestamp
|
||||
};
|
||||
|
||||
private:
|
||||
StatusType m_type;
|
||||
StatusSeverity m_severity;
|
||||
@@ -98,6 +111,21 @@ namespace BlackMisc
|
||||
return QVariant::fromValue(*this);
|
||||
}
|
||||
|
||||
//! \brief Type as string
|
||||
const QString &getTypeAsString() const;
|
||||
|
||||
//! \brief Type as string
|
||||
const QString &getSeverityAsString() const;
|
||||
|
||||
//! \copydoc CValueObject::propertyByIndex(int)
|
||||
QVariant propertyByIndex(int index) const;
|
||||
|
||||
//! \copydoc CValueObject::propertyByIndex(const QVariant, int)
|
||||
void propertyByIndex(const QVariant &variant, int index);
|
||||
|
||||
//! \copydoc CValueObject::propertyByIndexAsString
|
||||
QString propertyByIndexAsString(int index, bool i18n) const;
|
||||
|
||||
/*!
|
||||
* \brief Equal operator ==
|
||||
* \param other
|
||||
|
||||
@@ -8,6 +8,13 @@
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
/*
|
||||
* Construct from base class object
|
||||
*/
|
||||
CStatusMessageList::CStatusMessageList(const CSequence<CStatusMessage> &other) :
|
||||
CSequence<CStatusMessage>(other)
|
||||
{ }
|
||||
|
||||
/*
|
||||
* Messages by type
|
||||
*/
|
||||
@@ -45,6 +52,10 @@ namespace BlackMisc
|
||||
*/
|
||||
void CStatusMessageList::registerMetadata()
|
||||
{
|
||||
qRegisterMetaType<BlackMisc::CSequence<CStatusMessage>>();
|
||||
qDBusRegisterMetaType<BlackMisc::CSequence<CStatusMessage>>();
|
||||
qRegisterMetaType<BlackMisc::CCollection<CStatusMessage>>();
|
||||
qDBusRegisterMetaType<BlackMisc::CCollection<CStatusMessage>>();
|
||||
qRegisterMetaType<CStatusMessageList>();
|
||||
qDBusRegisterMetaType<CStatusMessageList>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user