File indexing completed on 2024-11-24 03:56:26
0001 /* 0002 * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best> 0003 * 0004 * SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "log_model.hpp" 0008 0009 namespace { 0010 enum Columns 0011 { 0012 Time, 0013 Source, 0014 SourceDetail, 0015 Message, 0016 0017 Count 0018 }; 0019 } // namespace 0020 0021 app::log::LogModel::LogModel() 0022 { 0023 connect(&Logger::instance(), &Logger::logged, this, &LogModel::on_line); 0024 } 0025 0026 int app::log::LogModel::rowCount(const QModelIndex &) const 0027 { 0028 return lines.size(); 0029 } 0030 0031 int app::log::LogModel::columnCount(const QModelIndex &) const 0032 { 0033 return Count; 0034 } 0035 0036 QVariant app::log::LogModel::headerData(int section, Qt::Orientation orientation, int role) const 0037 { 0038 if ( orientation == Qt::Horizontal ) 0039 { 0040 if ( role == Qt::DisplayRole ) 0041 { 0042 switch ( section ) 0043 { 0044 case Time: 0045 return tr("Time"); 0046 case Source: 0047 return tr("Source"); 0048 case SourceDetail: 0049 return tr("Details"); 0050 case Message: 0051 return tr("Message"); 0052 } 0053 } 0054 } 0055 else 0056 { 0057 if ( role == Qt::DecorationRole ) 0058 { 0059 switch ( lines[section].severity ) 0060 { 0061 case Info: return QIcon::fromTheme("emblem-information"); 0062 case Warning: return QIcon::fromTheme("emblem-warning"); 0063 case Error: return QIcon::fromTheme("emblem-error"); 0064 } 0065 } 0066 else if ( role == Qt::ToolTipRole ) 0067 { 0068 return Logger::severity_name(lines[section].severity); 0069 } 0070 } 0071 0072 return {}; 0073 } 0074 0075 QVariant app::log::LogModel::data(const QModelIndex & index, int role) const 0076 { 0077 if ( !index.isValid() ) 0078 return {}; 0079 0080 const LogLine& line = lines[index.row()]; 0081 if ( role == Qt::DisplayRole ) 0082 { 0083 switch ( index.column() ) 0084 { 0085 case Time: 0086 return line.time.toString(Qt::ISODate); 0087 case Source: 0088 return line.source; 0089 case SourceDetail: 0090 return line.source_detail; 0091 case Message: 0092 return line.message; 0093 } 0094 } 0095 else if ( role == Qt::ToolTipRole ) 0096 { 0097 switch ( index.column() ) 0098 { 0099 case Time: 0100 return line.time.toString(); 0101 case SourceDetail: 0102 return line.source_detail; 0103 } 0104 } 0105 0106 return {}; 0107 } 0108 0109 void app::log::LogModel::on_line(const LogLine& line) 0110 { 0111 beginInsertRows(QModelIndex(), lines.size(), lines.size()); 0112 lines.push_back(line); 0113 endInsertRows(); 0114 } 0115 0116 void app::log::LogModel::populate(const std::vector<LogLine>& lines) 0117 { 0118 beginResetModel(); 0119 this->lines = lines; 0120 endResetModel(); 0121 }