-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogentrymodel.cpp
More file actions
70 lines (59 loc) · 1.56 KB
/
logentrymodel.cpp
File metadata and controls
70 lines (59 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "logentrymodel.h"
LogEntryModel::LogEntryModel(QObject *parent)
: QAbstractListModel(parent)
{
}
int LogEntryModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_entries.size();
}
QVariant LogEntryModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= m_entries.size())
return QVariant();
const LogEntry &entry = m_entries.at(index.row());
switch (role) {
case StartTimestampRole:
return entry.startTimestamp;
case EndTimestampRole:
return entry.endTimestamp;
case OnlineRole:
return entry.online;
default:
return QVariant();
}
}
QHash<int, QByteArray> LogEntryModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[StartTimestampRole] = "startTimestamp";
roles[EndTimestampRole] = "endTimestamp";
roles[OnlineRole] = "online";
return roles;
}
void LogEntryModel::addEntry(const QDateTime &startTimestamp, const QDateTime &endTimestamp, bool online)
{
beginInsertRows(QModelIndex(), m_entries.size(), m_entries.size());
LogEntry entry;
entry.startTimestamp = startTimestamp;
entry.endTimestamp = endTimestamp;
entry.online = online;
m_entries.append(entry);
endInsertRows();
}
void LogEntryModel::setLastEndTimestamp(const QDateTime &endTimestamp)
{
if (m_entries.isEmpty())
return;
int lastIndex = m_entries.size() - 1;
m_entries[lastIndex].endTimestamp = endTimestamp;
QModelIndex index = createIndex(lastIndex, 0);
emit dataChanged(index, index, {EndTimestampRole});
}
void LogEntryModel::clear()
{
beginResetModel();
m_entries.clear();
endResetModel();
}