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