src/messagelog.cpp

Sat, 08 Apr 2023 12:55:11 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 08 Apr 2023 12:55:11 +0300
changeset 343
4a82990affd5
parent 264
76a025db4948
permissions
-rw-r--r--

Fix BFC formatting not working due to being evaluated after comment format

236
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
1 #include <QColor>
264
76a025db4948 Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 250
diff changeset
2 #include "src/messagelog.h"
236
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
3
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
4 MessageLog::MessageLog(QObject *parent) :
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
5 QAbstractTableModel{parent}
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
6 {
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
7
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
8 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
9
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
10 void MessageLog::addMessage(const Message& message)
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
11 {
250
2837b549e616 I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 236
diff changeset
12 const int row = static_cast<int>(this->messages.size());
2837b549e616 I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 236
diff changeset
13 this->beginInsertRows({}, row, row);
236
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
14 this->messages.push_back(message);
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
15 this->endInsertRows();
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
16 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
17
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
18 QVariant MessageLog::headerData(int section, Qt::Orientation orientation, int role) const
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
19 {
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
20 if (orientation != Qt::Horizontal or role != Qt::DisplayRole) {
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
21 return {};
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
22 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
23 else {
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
24 switch (static_cast<Column>(section)) {
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
25 case TimeColumn:
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
26 return tr("Time");
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
27 case MessageColumn:
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
28 return tr("Message");
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
29 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
30 return {};
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
31 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
32 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
33
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
34 int MessageLog::rowCount(const QModelIndex&) const
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
35 {
250
2837b549e616 I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 236
diff changeset
36 return narrow<int>(signed_cast(this->messages.size()));
236
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
37 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
38
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
39 int MessageLog::columnCount(const QModelIndex&) const
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
40 {
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
41 return NUM_COLUMNS;
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
42 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
43
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
44 QVariant MessageLog::data(const QModelIndex& index, int role) const
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
45 {
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
46 const std::size_t row = unsigned_cast(index.row());
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
47 if (false
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
48 or row >= this->messages.size()
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
49 or index.column() < 0
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
50 or index.column() >= NUM_COLUMNS
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
51 ) {
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
52 return {};
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
53 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
54 else {
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
55 const Message& message = this->messages[row];
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
56 switch (role) {
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
57 case Qt::DisplayRole:
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
58 switch (static_cast<Column>(index.column())) {
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
59 case TimeColumn:
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
60 return message.time.toString(tr("hh:mm:ss"));
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
61 case MessageColumn:
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
62 return message.text;
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
63 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
64 break;
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
65 case Qt::BackgroundRole:
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
66 switch(message.type) {
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
67 case Message::Info:
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
68 return {};
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
69 case Message::Warning:
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
70 return QColor{Qt::yellow};
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
71 case Message::Error:
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
72 return QColor{Qt::red};
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
73 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
74 break;
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
75 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
76 return {};
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
77 }
1fa0e1de9f0a Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
78 }

mercurial