10
10
BufferTableModel::BufferTableModel (QObject *parent):
11
11
QAbstractTableModel(parent)
12
12
{
13
- buf = nullptr ;
14
- bufSize = 0 ;
13
+ state. bufFilePos = 0 ;
14
+ state. fileSize = 0 ;
15
15
}
16
16
17
17
int BufferTableModel::rowCount (const QModelIndex & /* parent*/ ) const
18
18
{
19
- qint64 tableBufferSize = bufSize ;
19
+ qint64 tableBufferSize = state. fileSize ;
20
20
21
21
// Limit buffer size to avoid memory overlows for large buffers
22
22
if (tableBufferSize > TABLE_BUFFER_LIMIT)
@@ -34,32 +34,42 @@ QVariant BufferTableModel::data(const QModelIndex &index, int role) const
34
34
{
35
35
QString hexString;
36
36
QChar decodedChar;
37
- uint32_t start, end;
37
+ qint64 start, end;
38
38
39
39
if (role == Qt::DisplayRole)
40
40
{
41
41
switch (index.column ())
42
42
{
43
43
case HEADER_ADDRESS_COL:
44
- return QString (" %1" ).arg (index.row () * ROW_DATA_SIZE, 8 , 16 ,
44
+ return QString (" %1" ).arg (index.row () * ROW_DATA_SIZE, 10 , 16 ,
45
45
QChar (' 0' ));
46
46
case HEADER_HEX_COL:
47
47
start = static_cast <uint32_t >(index.row ()) * ROW_DATA_SIZE;
48
48
end = start + ROW_DATA_SIZE;
49
49
50
- for ( uint32_t i = start; i < end && i < bufSize; i++ )
50
+ if (( sPtr -> bufFilePos > start) || (( sPtr -> bufFilePos + BUF_SIZE) < end) )
51
51
{
52
- hexString.append (QString (" %1 " ).arg (buf[i], 2 , 16 ,
52
+ sPtr ->bufFilePos = start - (BUF_SIZE / 2 );
53
+ if (sPtr ->bufFilePos < 0 )
54
+ sPtr ->bufFilePos = 0 ;
55
+
56
+ sPtr ->file .seek (sPtr ->bufFilePos );
57
+ sPtr ->file .read ((char *)sPtr ->buf , BUF_SIZE);
58
+ }
59
+
60
+ for (qint64 i = start; i < end && i < state.fileSize ; i++)
61
+ {
62
+ hexString.append (QString (" %1 " ).arg (sPtr ->buf [i - sPtr ->bufFilePos ], 2 , 16 ,
53
63
QChar (' 0' )));
54
64
}
55
65
return hexString;
56
66
case HEADER_ASCII_COL:
57
67
start = static_cast <uint32_t >(index.row ()) * ROW_DATA_SIZE;
58
68
end = start + ROW_DATA_SIZE;
59
69
60
- for (uint32_t i = start; i < end && i < bufSize ; i++)
70
+ for (qint64 i = start; i < end && i < state. fileSize ; i++)
61
71
{
62
- decodedChar = QChar (buf[i]);
72
+ decodedChar = QChar (state. buf [i - state. bufFilePos ]);
63
73
if (!decodedChar.isPrint ())
64
74
decodedChar = QChar (' .' );
65
75
hexString.append (decodedChar);
@@ -93,16 +103,25 @@ QVariant BufferTableModel::headerData(int section, Qt::Orientation orientation,
93
103
return QVariant ();
94
104
}
95
105
96
- void BufferTableModel::setBuffer ( uint8_t *buffer, uint32_t size )
106
+ void BufferTableModel::setFile (QString filePath )
97
107
{
98
108
beginResetModel ();
99
- buf = buffer;
100
- bufSize = size;
109
+ state.file .close ();
110
+ if (!filePath.isEmpty ())
111
+ {
112
+ state.bufFilePos = 0 ;
113
+ state.fileSize = 0 ;
114
+ }
115
+ state.file .setFileName (filePath);
116
+ if (state.file .open (QIODevice::ReadOnly))
117
+ {
118
+ state.fileSize = state.file .size ();
119
+ state.bufFilePos = INT64_MAX;
120
+ }
121
+ else
122
+ {
123
+ state.bufFilePos = 0 ;
124
+ state.fileSize = 0 ;
125
+ }
101
126
endResetModel ();
102
127
}
103
-
104
- void BufferTableModel::getBuffer (uint8_t *&buffer, uint32_t &size)
105
- {
106
- buffer = buf;
107
- size = bufSize;
108
- }
0 commit comments