-
Notifications
You must be signed in to change notification settings - Fork 1
/
beanpacketsloggermodel.cpp
178 lines (153 loc) · 4.57 KB
/
beanpacketsloggermodel.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "beanpacketsloggermodel.h"
#include "beanpacketsloggerheaderview.h"
#include <QApplication>
#include <QFile>
#include <QSize>
#include <QTextStream>
#include <qtableview.h>
#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
BeanPacketsLoggerModel::BeanPacketsLoggerModel(QObject *parent)
: QAbstractTableModel(parent)
{
packets = new QList<BeanPacket*>;
//QTableView *tableLog = parent->findChild<QTableView *>("tableLog");
//QTableView* tableLog = dynamic_cast<QTableView*>(parent);
}
BeanPacketsLoggerModel::~BeanPacketsLoggerModel() = default;
void BeanPacketsLoggerModel::appendPacket(BeanPacket *packet)
{
qDebug() << "Adding packet to beanlogger";
packets->append(packet);
layoutChanged();
}
QVariant BeanPacketsLoggerModel::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 BeanPacketsLoggerModel::rowCount(const QModelIndex &parent) const
{
return packets->size();
}
int BeanPacketsLoggerModel::columnCount(const QModelIndex &parent) const
{
return PRIO + 1;
}
QVariant BeanPacketsLoggerModel::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(index.row() - 1);
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 BeanPacketsLoggerModel::clearPackets()
{
// qDeleteAll(packets);
packets->clear();
layoutChanged();
}
void BeanPacketsLoggerModel::saveAsCSV(const QString& fileName)
{
if (fileName.isEmpty()) {
return;
}
QFile f(fileName);
if (f.open(QIODevice::WriteOnly)) {
QTextStream ts(&f);
QStringList strList;
ts << " ,,Time,Dst,Msg,DLC,Data,CRC,Prio,Debug\n";
for (auto packet : *packets) {
strList << "\" \""; // 0
strList << "\" \""; // 1
strList << "\"" << QString::number(packet->timeEpoch) << "\"";
strList << "\"" << packet->getDstIdStr() << "\"";
strList << "\"" << packet->getMsgIdStr() << "\"";
strList << "\"" << packet->getDlcStr() << "\"";
strList << "\"" << packet->getDataStr() << "\"";
strList << "\"" << packet->getCrcStr() << "\"";
strList << "\"" << packet->getPrioStr() << "\"";
strList << "\"" << packet->debug << "\"";
ts << strList.join(",") + "\n";
strList.clear();
}
f.close();
} else {
qWarning() << "Can't open file";
}
}