diff --git a/src/blackgui/statusmessagelistmodel.cpp b/src/blackgui/statusmessagelistmodel.cpp new file mode 100644 index 000000000..bf4ddbfc1 --- /dev/null +++ b/src/blackgui/statusmessagelistmodel.cpp @@ -0,0 +1,69 @@ +#include "statusmessagelistmodel.h" +#include "blackmisc/blackmiscfreefunctions.h" +#include +#include +#include + +using namespace BlackMisc; + +namespace BlackGui +{ + /* + * Constructor + */ + CStatusMessageListModel::CStatusMessageListModel(QObject *parent) : + CListModelBase("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); + } +} diff --git a/src/blackgui/statusmessagelistmodel.h b/src/blackgui/statusmessagelistmodel.h new file mode 100644 index 000000000..45d2ee007 --- /dev/null +++ b/src/blackgui/statusmessagelistmodel.h @@ -0,0 +1,30 @@ +#ifndef BLACKGUI_STATUSMESSAGELISTMODEL_H +#define BLACKGUI_STATUSMESSAGELISTMODEL_H + +#include +#include +#include "blackmisc/statusmessagelist.h" +#include "blackgui/listmodelbase.h" + +namespace BlackGui +{ + /*! + * \brief Server list model + */ + class CStatusMessageListModel : public CListModelBase + { + + 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 diff --git a/src/blackmisc/statusmessage.cpp b/src/blackmisc/statusmessage.cpp index 9501d1ba7..e3031fdc8 100644 --- a/src/blackmisc/statusmessage.cpp +++ b/src/blackmisc/statusmessage.cpp @@ -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(type); this->m_severity = static_cast(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(this->m_severity)); + case IndexSeverityAsString: + return QVariant(this->getSeverityAsString()); + case IndexTimestamp: + return QVariant(this->m_timestamp); + case IndexType: + return QVariant(static_cast(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(); + break; + case IndexTimestamp: + this->m_timestamp = variant.value(); + break; + case IndexSeverity: + this->m_severity = static_cast(variant.value()); + break; + case IndexType: + this->m_type = static_cast(variant.value()); + 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(); + if (dt.isNull() || !dt.isValid()) return ""; + return dt.toString("HH:mm::ss.zzz"); + break; + } + default: + break; + } + return BlackMisc::qVariantToString(qv, i18n); + } } diff --git a/src/blackmisc/statusmessage.h b/src/blackmisc/statusmessage.h index 27e5bc291..3fc46e76a 100644 --- a/src/blackmisc/statusmessage.h +++ b/src/blackmisc/statusmessage.h @@ -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 diff --git a/src/blackmisc/statusmessagelist.cpp b/src/blackmisc/statusmessagelist.cpp index ae5bafcff..bae98dd86 100644 --- a/src/blackmisc/statusmessagelist.cpp +++ b/src/blackmisc/statusmessagelist.cpp @@ -8,6 +8,13 @@ namespace BlackMisc { + /* + * Construct from base class object + */ + CStatusMessageList::CStatusMessageList(const CSequence &other) : + CSequence(other) + { } + /* * Messages by type */ @@ -45,6 +52,10 @@ namespace BlackMisc */ void CStatusMessageList::registerMetadata() { + qRegisterMetaType>(); + qDBusRegisterMetaType>(); + qRegisterMetaType>(); + qDBusRegisterMetaType>(); qRegisterMetaType(); qDBusRegisterMetaType(); }