1010BufferTableModel::BufferTableModel (QObject *parent):
1111 QAbstractTableModel(parent)
1212{
13- buf = nullptr ;
14- bufSize = 0 ;
13+ state. bufFilePos = 0 ;
14+ state. fileSize = 0 ;
1515}
1616
1717int BufferTableModel::rowCount (const QModelIndex & /* parent*/ ) const
1818{
19- qint64 tableBufferSize = bufSize ;
19+ qint64 tableBufferSize = state. fileSize ;
2020
2121 // Limit buffer size to avoid memory overlows for large buffers
2222 if (tableBufferSize > TABLE_BUFFER_LIMIT)
@@ -34,32 +34,42 @@ QVariant BufferTableModel::data(const QModelIndex &index, int role) const
3434{
3535 QString hexString;
3636 QChar decodedChar;
37- uint32_t start, end;
37+ qint64 start, end;
3838
3939 if (role == Qt::DisplayRole)
4040 {
4141 switch (index.column ())
4242 {
4343 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 ,
4545 QChar (' 0' ));
4646 case HEADER_HEX_COL:
4747 start = static_cast <uint32_t >(index.row ()) * ROW_DATA_SIZE;
4848 end = start + ROW_DATA_SIZE;
4949
50- for ( uint32_t i = start; i < end && i < bufSize; i++ )
50+ if (( sPtr -> bufFilePos > start) || (( sPtr -> bufFilePos + BUF_SIZE) < end) )
5151 {
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 ,
5363 QChar (' 0' )));
5464 }
5565 return hexString;
5666 case HEADER_ASCII_COL:
5767 start = static_cast <uint32_t >(index.row ()) * ROW_DATA_SIZE;
5868 end = start + ROW_DATA_SIZE;
5969
60- for (uint32_t i = start; i < end && i < bufSize ; i++)
70+ for (qint64 i = start; i < end && i < state. fileSize ; i++)
6171 {
62- decodedChar = QChar (buf[i]);
72+ decodedChar = QChar (state. buf [i - state. bufFilePos ]);
6373 if (!decodedChar.isPrint ())
6474 decodedChar = QChar (' .' );
6575 hexString.append (decodedChar);
@@ -93,16 +103,25 @@ QVariant BufferTableModel::headerData(int section, Qt::Orientation orientation,
93103 return QVariant ();
94104}
95105
96- void BufferTableModel::setBuffer ( uint8_t *buffer, uint32_t size )
106+ void BufferTableModel::setFile (QString filePath )
97107{
98108 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+ }
101126 endResetModel ();
102127}
103-
104- void BufferTableModel::getBuffer (uint8_t *&buffer, uint32_t &size)
105- {
106- buffer = buf;
107- size = bufSize;
108- }
0 commit comments