forked from smartgauges/qvbf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvbfmodel.cpp
162 lines (122 loc) · 2.64 KB
/
vbfmodel.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
#include <QIcon>
#include "vbfmodel.h"
VbfModel::VbfModel(QObject *parent) : QAbstractListModel(parent)
{
reset();
}
void VbfModel::reset()
{
beginResetModel();
vbf.reset();
endResetModel();
}
int VbfModel::columnCount(const QModelIndex &) const
{
return e_col_nums;
}
int VbfModel::rowCount(const QModelIndex &) const
{
return vbf.blocks.size() + 1/*header*/;
}
QVariant VbfModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case e_col_block:
return tr("Blocks");
case e_col_size:
return tr("Bytes");
}
}
return QVariant();
}
QVariant VbfModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= (vbf.blocks.size() + 1/*header*/))
return QVariant();
if (index.column() >= e_col_nums)
return QVariant();
if (role == Qt::DisplayRole) {
switch (index.column()) {
case e_col_block:
if (!index.row()) {
return QString("header");
}
else {
return QString("block %1").arg(index.row());
}
case e_col_size:
if (!index.row()) {
return vbf.header.data.size();
}
else {
const block_t & block = vbf.blocks[index.row() - 1/*header*/];
if (vbf.header.data_format_identifier_exist && vbf.header.data_format_identifier == 0x10)
return QString("%1(%2)").arg(block.len).arg(block.data.size());
else
return QString("%1").arg(block.len);
}
}
}
else if (role == Qt::DecorationRole) {
if ((index.column() == e_col_rm) && index.row()) {
QVariant v;
v.setValue(QIcon(":/images/del.png"));
return v;
}
}
return QVariant();
}
void VbfModel::set(const vbf_t & _vbf)
{
beginResetModel();
vbf = _vbf;
endResetModel();
}
const vbf_t & VbfModel::get()
{
return vbf;
}
bool VbfModel::add(const QString & fileName)
{
beginResetModel();
bool r = vbf_add(fileName, vbf);
endResetModel();
return r;
}
void VbfModel::rm(int idx)
{
if (idx > vbf.blocks.size() || idx < 1)
return;
beginResetModel();
vbf.blocks.remove(idx - 1/*header*/);
endResetModel();
}
int VbfModel::size()
{
return vbf.blocks.size();
}
const block_t & VbfModel::get_block(int idx)
{
if (idx > vbf.blocks.size())
return empty;
return vbf.blocks[idx];
}
void VbfModel::update_block(int idx, uint32_t addr, const QByteArray & data)
{
if (idx > vbf.blocks.size())
return;
block_t & block = vbf.blocks[idx];
block.addr = addr;
block.data = data;
block.len = block.data.size();
}
void VbfModel::update_header(struct header_t & header)
{
vbf.header = header;
vbf_update_header(vbf);
}