-
Notifications
You must be signed in to change notification settings - Fork 1
/
beanpacketssendermodel.cpp
129 lines (117 loc) · 3.4 KB
/
beanpacketssendermodel.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
#include "beanpacketssendermodel.h"
#include <QApplication>
#include <QVariant>
#define DSTID 0
#define MSGID 1
#define DLC 2
#define DATA 3
#define CRC 4
#define PRIO 5
#define DEBUG 6
#define REPEAT 7
#define COUNTER 8
BeanPacketsSenderModel::BeanPacketsSenderModel(QObject *parent)
: QAbstractTableModel(parent)
{
packets = new QList<BeanPacket*>;
}
int BeanPacketsSenderModel::appendPacket(BeanPacket *packet)
{
packet->ro = false;
packets->append(packet);
layoutChanged();
return packets->size();
}
QVariant BeanPacketsSenderModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole || orientation != Qt::Horizontal )
return QVariant();
switch (section) {
case DSTID:
return QString("Dst");
case MSGID:
return QString("Msg");
case DLC:
return QString("Len");
case DATA:
return QString("Data");
case CRC:
return QString("CRC");
case PRIO:
return QString("Pri");
case REPEAT:
return QString("Delay");
case COUNTER:
return QString("Counter");
default:
return QVariant();
}
}
int BeanPacketsSenderModel::rowCount(const QModelIndex &parent) const
{
return packets->size();
}
int BeanPacketsSenderModel::columnCount(const QModelIndex &parent) const
{
return COUNTER +1;
}
QVariant BeanPacketsSenderModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole) {
if (index.row() <= packets->size()) {
const BeanPacket *packet = packets->at(index.row());
switch (index.column()) {
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 REPEAT:
return QVariant(packet->getRepeatStr());
case COUNTER:
return QVariant(packet->getCounterStr());
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();
}
BeanPacket* BeanPacketsSenderModel::packetAt(int row) {
if (row >= 0 && row < packets->size()) {
return packets->at(row);
}
return nullptr;
}
void BeanPacketsSenderModel::removePacketAt(int row) {
if (row > 0 && row <= packets->size()) {
auto tmpPacket = packetAt(row);
packets->removeAt(row);
delete(tmpPacket);
layoutChanged();
}
}