tageditor/gui/notificationmodel.cpp

151 lines
3.8 KiB
C++
Raw Permalink Normal View History

2015-09-06 20:20:00 +02:00
#include "./notificationmodel.h"
2015-04-22 19:33:53 +02:00
2018-03-06 02:04:35 +01:00
#include "../misc/utility.h"
2015-04-22 19:33:53 +02:00
#include <c++utilities/chrono/datetime.h>
#include <QApplication>
#include <QIcon>
2018-03-07 01:18:01 +01:00
#include <QStyle>
2015-04-22 19:33:53 +02:00
using namespace std;
2019-06-10 22:49:46 +02:00
using namespace CppUtilities;
2018-03-06 23:10:13 +01:00
using namespace TagParser;
2018-03-06 02:04:35 +01:00
using namespace Utility;
2015-04-22 19:33:53 +02:00
namespace QtGui {
2018-03-07 01:18:01 +01:00
DiagModel::DiagModel(QObject *parent)
: QAbstractListModel(parent)
{
}
2015-04-22 19:33:53 +02:00
2018-03-06 02:04:35 +01:00
QVariant DiagModel::headerData(int section, Qt::Orientation orientation, int role) const
2015-04-22 19:33:53 +02:00
{
2018-03-07 01:18:01 +01:00
switch (orientation) {
2015-04-22 19:33:53 +02:00
case Qt::Horizontal:
2018-03-07 01:18:01 +01:00
switch (role) {
2015-04-22 19:33:53 +02:00
case Qt::DisplayRole:
2018-03-07 01:18:01 +01:00
switch (section) {
2015-04-22 19:33:53 +02:00
case 0:
return tr("Context");
case 1:
return tr("Message");
case 2:
return tr("Time");
2018-03-07 01:18:01 +01:00
default:;
2015-04-22 19:33:53 +02:00
}
break;
2018-03-07 01:18:01 +01:00
}
2015-04-22 19:33:53 +02:00
break;
2018-03-07 01:18:01 +01:00
default:;
2015-04-22 19:33:53 +02:00
}
return QVariant();
}
2018-03-06 02:04:35 +01:00
int DiagModel::columnCount(const QModelIndex &parent) const
2015-04-22 19:33:53 +02:00
{
2018-03-07 01:18:01 +01:00
if (!parent.isValid()) {
2015-04-22 19:33:53 +02:00
return 3;
}
return 0;
}
2018-03-06 02:04:35 +01:00
int DiagModel::rowCount(const QModelIndex &parent) const
2015-04-22 19:33:53 +02:00
{
2018-03-07 01:18:01 +01:00
if (!parent.isValid()) {
2018-03-06 02:04:35 +01:00
return sizeToInt(m_diag.size());
2015-04-22 19:33:53 +02:00
}
return 0;
}
2018-03-06 02:04:35 +01:00
Qt::ItemFlags DiagModel::flags(const QModelIndex &index) const
2015-04-22 19:33:53 +02:00
{
return QAbstractListModel::flags(index);
}
2018-03-06 02:04:35 +01:00
QVariant DiagModel::data(const QModelIndex &index, int role) const
2015-04-22 19:33:53 +02:00
{
2018-03-07 01:18:01 +01:00
if (index.isValid() && index.row() >= 0 && static_cast<std::size_t>(index.row()) < m_diag.size()) {
switch (role) {
2015-04-22 19:33:53 +02:00
case Qt::DisplayRole:
2018-03-07 01:18:01 +01:00
switch (index.column()) {
2015-04-22 19:33:53 +02:00
case 0: {
2018-03-06 02:04:35 +01:00
const string &context = m_diag[static_cast<std::size_t>(index.row())].context();
2018-03-07 01:18:01 +01:00
if (context.empty()) {
2015-04-22 19:33:53 +02:00
return tr("unspecified");
} else {
return QString::fromUtf8(context.c_str());
2015-04-22 19:33:53 +02:00
}
}
case 1:
2018-03-06 02:04:35 +01:00
return QString::fromUtf8(m_diag[static_cast<std::size_t>(index.row())].message().c_str());
2015-04-22 19:33:53 +02:00
case 2:
2018-03-07 01:18:01 +01:00
return QString::fromUtf8(
m_diag[static_cast<std::size_t>(index.row())].creationTime().toString(DateTimeOutputFormat::DateAndTime, true).c_str());
default:;
2015-04-22 19:33:53 +02:00
}
break;
case Qt::DecorationRole:
2018-03-07 01:18:01 +01:00
switch (index.column()) {
2015-04-22 19:33:53 +02:00
case 0:
2018-03-07 01:18:01 +01:00
switch (m_diag[static_cast<std::size_t>(index.row())].level()) {
2018-03-06 02:04:35 +01:00
case DiagLevel::None:
case DiagLevel::Debug:
return debugIcon();
case DiagLevel::Information:
2015-04-22 19:33:53 +02:00
return informationIcon();
2018-03-06 02:04:35 +01:00
case DiagLevel::Warning:
2015-04-22 19:33:53 +02:00
return warningIcon();
2018-03-06 02:04:35 +01:00
case DiagLevel::Critical:
case DiagLevel::Fatal:
2015-04-22 19:33:53 +02:00
return errorIcon();
}
2018-03-06 02:04:35 +01:00
break;
2018-03-07 01:18:01 +01:00
default:;
2015-04-22 19:33:53 +02:00
}
break;
2018-03-07 01:18:01 +01:00
default:;
2015-04-22 19:33:53 +02:00
}
}
return QVariant();
}
2018-03-06 02:04:35 +01:00
const Diagnostics &DiagModel::diagnostics() const
2015-04-22 19:33:53 +02:00
{
2018-03-06 02:04:35 +01:00
return m_diag;
2015-04-22 19:33:53 +02:00
}
2018-03-06 23:10:13 +01:00
void DiagModel::setDiagnostics(const TagParser::Diagnostics &notifications)
2015-04-22 19:33:53 +02:00
{
beginResetModel();
2018-03-06 02:04:35 +01:00
m_diag = notifications;
2015-04-22 19:33:53 +02:00
endResetModel();
}
2018-03-06 02:04:35 +01:00
const QIcon &DiagModel::informationIcon()
2015-04-22 19:33:53 +02:00
{
static const QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxInformation);
return icon;
}
2018-03-06 02:04:35 +01:00
const QIcon &DiagModel::warningIcon()
2015-04-22 19:33:53 +02:00
{
static const QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning);
return icon;
}
2018-03-06 02:04:35 +01:00
const QIcon &DiagModel::errorIcon()
2015-04-22 19:33:53 +02:00
{
static const QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical);
return icon;
}
2018-03-06 02:04:35 +01:00
const QIcon &DiagModel::debugIcon()
2015-04-22 19:33:53 +02:00
{
static const QIcon icon = QIcon(QStringLiteral("/images/bug"));
return icon;
}
2018-03-07 01:18:01 +01:00
} // namespace QtGui