/* * @RDE_HEADER@ * * $Id$ * $HeadURL$ * $Author$ * Created by Malte Schröder, SLSE, 2016-07-14 */ #include "logviewtablemodel.h" #include #include LogViewTableModel::LogViewTableModel(QObject *parent) : QAbstractTableModel(parent) , m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("LogViewTableModel")) { setStdColors(); } void LogViewTableModel::setData(const QVector &logData) { beginResetModel(); m_logfile.clear(); endResetModel(); if (logData.isEmpty()) return; beginInsertRows(QModelIndex(), 0, logData.size() - 1); m_logfile = logData; endInsertRows(); } void LogViewTableModel::addData(const LogViewTableModel::logInformation &logLine) { beginInsertRows(QModelIndex(), m_logfile.size(), m_logfile.size()); m_logfile.append(logLine); endInsertRows(); } bool LogViewTableModel::reset() { beginResetModel(); m_logfile.clear(); m_cellColors.level.clear(); setStdColors(); endResetModel(); return true; } void LogViewTableModel::startDateTime() { if (m_logfile.isEmpty()) return; const QDateTime dtime = m_logfile.first().date; emit startTimeSetted(dtime); } void LogViewTableModel::endDateTime() { if (m_logfile.isEmpty()) return; const QDateTime dtime = m_logfile.last().date; emit endTimeSetted(dtime); } int LogViewTableModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return m_logfile.size(); } int LogViewTableModel::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent); return colLast + 1; } QVariant LogViewTableModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.column() == colLevel) { switch (role) { case Qt::BackgroundRole: // BACKGROUND COLOR { const QString logLevel = m_logfile.at(index.row()).level; QColor col = m_cellColors.level.value(logLevel); if (col.isValid()) return col; return QVariant(); } case Qt::TextColorRole: // TEXT COLOR { const QString logLevel = m_logfile.at(index.row()).level; QColor col = m_fontColors.level.value(logLevel); if (col.isValid()) return col; return QVariant(); } } } if (role != Qt::DisplayRole && role != Qt::UserRole) return QVariant(); int row = index.row(); Columns col = static_cast(index.column()); switch (col) { case colDate: if (role == Qt::UserRole) return m_logfile.at(row).date; return m_logfile.at(row).date.toString(QLatin1String("yyyy-MM-dd hh:mm:ss,zzz")); case colLevel: return m_logfile.at(row).level; case colLogger: return m_logfile.at(row).logger; case colData: return m_logfile.at(row).data; case colLocation: return m_logfile.at(row).location; } return QVariant(); } bool LogViewTableModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (role != Qt::EditRole) return false; int row = index.row(); Columns col = static_cast(index.column()); switch (col) { case colDate: m_logfile[row].date = value.toDateTime(); return true; case colLevel: m_logfile[row].level = value.toString(); return true; case colLogger: m_logfile[row].logger = value.toString(); return true; case colData: m_logfile[row].data = value.toString(); return true; case colLocation: m_logfile[row].location = value.toString(); return true; } return false; } QVariant LogViewTableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { Columns col = static_cast(section); switch (col) { case colDate: return tr("Date"); case colLevel: return tr("Level"); case colLogger: return tr("Logger"); case colData: return tr("Data"); case colLocation: return tr("Location"); } } return QAbstractTableModel::headerData(section, orientation, role); } int LogViewTableModel::length() const { return m_logfile.length(); } QString LogViewTableModel::itemAt(int row, int index) const { switch (index) { case colDate: return m_logfile.at(row).date.toString(QLatin1String("yyyy-MM-dd hh-mm-ss,zzz")); case colLevel: return m_logfile.at(row).level; case colLogger: return m_logfile.at(row).logger; case colData: return m_logfile.at(row).data; case colLocation: return m_logfile.at(row).location; default: return QString(); } } void LogViewTableModel::adjustCellColor(const QColor &color, const QString &str) { for (auto iter = m_cellColors.level.constBegin(); iter != m_cellColors.level.constEnd(); ++iter) { if (iter.key() == str) m_cellColors.level.insert(str, color); } } void LogViewTableModel::adjustFontColor(const QColor &color, const QString &str) { for (auto iter = m_fontColors.level.constBegin(); iter != m_fontColors.level.constEnd(); ++iter) { if (iter.key() == str) m_fontColors.level.insert(str, color); } } void LogViewTableModel::setStdColors() { m_cellColors.level[QLatin1String("INFO")] = Qt::blue; m_cellColors.level[QLatin1String("WARN")] = Qt::yellow; m_cellColors.level[QLatin1String("ERROR")] = Qt::darkRed; m_cellColors.level[QLatin1String("FATAL")] = Qt::red; m_cellColors.level[QLatin1String("DEBUG")] = Qt::green; m_cellColors.level[QLatin1String("TRACE")] = Qt::lightGray; m_fontColors.level[QLatin1String("INFO")] = Qt::white; m_fontColors.level[QLatin1String("WARN")] = Qt::black; m_fontColors.level[QLatin1String("ERROR")] = Qt::white; m_fontColors.level[QLatin1String("FATAL")] = Qt::black; m_fontColors.level[QLatin1String("DEBUG")] = Qt::black; m_fontColors.level[QLatin1String("TRACE")] = Qt::black; }