-
Notifications
You must be signed in to change notification settings - Fork 1
/
beanpacketsplayermodel.cpp
163 lines (140 loc) · 3.97 KB
/
beanpacketsplayermodel.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "beanpacket.h"
#include "beanpacketsplayermodel.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
// columns order
#define CBOX 0
#define TIME 1
#define DSTID 2
#define MSGID 3
#define DLC 4
#define DATA 5
#define CRC 6
#define PRIO 7
#define DEBUG 8
BeanPacketsPlayerModel::~BeanPacketsPlayerModel() = default;
BeanPacketsPlayerModel::BeanPacketsPlayerModel(QObject *parent)
: QAbstractTableModel(parent)
{
packets = new QList<BeanPacket*>;
}
QVariant BeanPacketsPlayerModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole || orientation != Qt::Horizontal )
return QVariant();
switch (section) {
case TIME:
return QString("Time");
case DSTID:
return QString("Dst");
case MSGID:
return QString("Msg");
case DLC:
return QString("DLC");
case DATA:
return QString("Data");
case CRC:
return QString("CRC");
case PRIO:
return QString("PRI");
case DEBUG:
return QString("Debug");
default:
return QVariant();
}
}
int BeanPacketsPlayerModel::rowCount(const QModelIndex &parent) const
{
return packets->size();
}
int BeanPacketsPlayerModel::columnCount(const QModelIndex &parent) const
{
return DEBUG;
}
QVariant BeanPacketsPlayerModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::CheckStateRole && index.column() == 0){
return false;
}
if (role == Qt::DisplayRole) {
if (index.row() <= packets->size()) {
auto packet = packets->at(index.row());
switch (index.column()) {
case TIME: {
qint64 time = 0;
if (index.row() >= 1) {
const auto prevPacket = packets->at(0);
time = packet->timeEpoch - prevPacket->timeEpoch;
}
return QVariant(time);
}
case DSTID:
return QVariant(packet->getDstIdStr());
case MSGID:
return QVariant(packet->getMsgIdStr());
case PRIO:
return QVariant(packet->getPrioStr());
case DATA:
return QVariant(packet->getDataStr());
case DLC:
return QVariant(packet->getDlcStr());
case CRC:
return QVariant(packet->getCrcStr());
case DEBUG:
return QVariant(packet->debug);
default:
return QVariant();
}
}
} else if (role == Qt::TextAlignmentRole) {
switch (index.column()) {
case DATA:
return Qt::AlignLeft + Qt::AlignVCenter;
case PRIO:
case DLC:
case CRC:
return Qt::AlignCenter;
case MSGID:
case DSTID:
default:
return Qt::AlignRight + Qt::AlignVCenter;
}
}
return QVariant();
}
void BeanPacketsPlayerModel::loadFromCSV(const QString &fileName)
{
packets->clear();
QFile file(fileName);
if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
QTextStream in(&file);
int lineNum = 0;
while (!in.atEnd()) {
QString line = in.readLine();
qDebug() << line;
if (lineNum == 0) {
lineNum++;
continue;
}
auto localFromCSVLine = BeanPacket::fromCSVLine(line);
if (localFromCSVLine->bad) {
packets->append(localFromCSVLine);
}
lineNum++;
}
qDebug() << "Closing file. Packets size: " << packets->size();
file.close();
}
layoutChanged();
}
QList<BeanPacket*> BeanPacketsPlayerModel::getPackets() const
{
return *packets;
}
void BeanPacketsPlayerModel::setPackets(const QList<BeanPacket*> &value)
{
packets = new QList<BeanPacket*>(value);
}