Mon, 20 Jun 2022 22:21:53 +0300
fix typo
236
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
1 | #include <QColor> |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
2 | #include "messagelog.h" |
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 | { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
12 | this->beginInsertRows({}, this->messages.size(), this->messages.size()); |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
13 | this->messages.push_back(message); |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
14 | this->endInsertRows(); |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
15 | } |
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 | 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
|
18 | { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
19 | if (orientation != Qt::Horizontal or role != Qt::DisplayRole) { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
20 | return {}; |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
21 | } |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
22 | else { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
23 | switch (static_cast<Column>(section)) { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
24 | case TimeColumn: |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
25 | return tr("Time"); |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
26 | case MessageColumn: |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
27 | return tr("Message"); |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
28 | } |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
29 | return {}; |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
30 | } |
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 | int MessageLog::rowCount(const QModelIndex&) const |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
34 | { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
35 | return signed_cast(this->messages.size()); |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
36 | } |
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 | int MessageLog::columnCount(const QModelIndex&) const |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
39 | { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
40 | return NUM_COLUMNS; |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
41 | } |
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 | 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
|
44 | { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
45 | 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
|
46 | if (false |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
47 | or row >= this->messages.size() |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
48 | or index.column() < 0 |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
49 | or index.column() >= NUM_COLUMNS |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
50 | ) { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
51 | return {}; |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
52 | } |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
53 | else { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
54 | const Message& message = this->messages[row]; |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
55 | switch (role) { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
56 | case Qt::DisplayRole: |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
57 | switch (static_cast<Column>(index.column())) { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
58 | case TimeColumn: |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
59 | return message.time.toString(tr("hh:mm:ss")); |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
60 | case MessageColumn: |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
61 | return message.text; |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
62 | } |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
63 | break; |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
64 | case Qt::BackgroundRole: |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
65 | switch(message.type) { |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
66 | case Message::Info: |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
67 | return {}; |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
68 | case Message::Warning: |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
69 | return QColor{Qt::yellow}; |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
70 | case Message::Error: |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
71 | return QColor{Qt::red}; |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
72 | } |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
73 | break; |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
74 | } |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
75 | return {}; |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
76 | } |
1fa0e1de9f0a
Made message log a model
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
77 | } |