-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbinfileview.cpp
338 lines (285 loc) · 11.7 KB
/
binfileview.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//*****************************************************************************
//
// binfileview.cpp
// Copyright(c) 2016 Juha T Nikkanen <[email protected]>
//
// --- Legal stuff ---
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//*****************************************************************************
#include "binfileview.h"
#include <QDebug>
#include <QObject>
#include <QPainter>
#include <QPaintEvent>
#include <QScrollBar>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QUrl>
#include <QMenu>
#include <QAction>
#include <ctype.h>
#include <climits>
BinFileView::BinFileView( QWidget* parent )
: QAbstractScrollArea( parent ),
_contextMenu( new QMenu( this ) ),
_contextAction( new QAction( tr( "&Open" ), this ) ),
_data( nullptr ),
_colorData( nullptr ),
_size( 0 ),
_upperMask( 0xffff0000LL ),
_lowerMask( 0x0000ffffLL ),
_addend( 0 ),
_addressChars( 8 ),
_lineCount( 0 ),
_linesOnViewPort( BinFileView::minimumOfLines ),
_byteGroups( BinFileView::minimumOfByteGroups ),
_bytesPerLine( _byteGroups * BinFileView::bytesPerGroup ),
_leftMargin( 0 ),
_rightMargin( 0 ),
_bottomMargin( 0 ),
_byteWidth( 0 ),
_groupWidth( 0 ),
_addressAreaWidth( 0 ),
_hexAreaWidth( 0 ),
_asciiAreaWidth( 0 ),
_groupGap( 0 ),
_vscrollBarWidth( 0 )
{
_contextMenu->addAction( _contextAction );
connect( _contextAction, &QAction::triggered, [=](){ emit fileOpenRequested( this ); } );
connect( this, SIGNAL( customContextMenuRequested( const QPoint& ) ), \
this, SLOT( showContextMenu( const QPoint& ) ) );
setSizeAdjustPolicy( QAbstractScrollArea::AdjustToContents );
setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
}
BinFileView::~BinFileView()
{
}
QSize BinFileView::viewportSizeHint() const
{
return( QSize( preFitWidth( BinFileView::minimumOfByteGroups ) + _vscrollBarWidth,
fontMetrics().height() * BinFileView::minimumOfLines + _bottomMargin ) );
}
void BinFileView::setFont( QFont font )
{
if( !font.fixedPitch() ) {
font.setFixedPitch( true );
}
QWidget::setFont( font );
adjust();
viewport()->update();
}
const uchar* BinFileView::data()
{
return _data;
}
void BinFileView::setData( const uchar* data, const qint64 size )
{
_data = data;
_size = size;
// Give others, possibly connected views a change to adjust
// parameters affecting view port width & data layout...
int oldAddressChars = _addressChars;
if( _size > UINT_MAX )
setAddressCharacters( 16 );
else
setAddressCharacters( 8 );
// ...and signal those views
if( oldAddressChars < _addressChars )
emit defaultVisualsChanged( this );
_lineCount = static_cast<int>( _size / _bytesPerLine );
if( _size % _bytesPerLine )
_lineCount += 1;
_linesOnViewPort = viewport()->height() / fontMetrics().height();
verticalScrollBar()->setRange( 0, _lineCount - _linesOnViewPort );
verticalScrollBar()->setPageStep( _linesOnViewPort );
viewport()->update();
}
void BinFileView::setColoringData( uchar* data )
{
_colorData = data;
viewport()->update();
}
void BinFileView::setAddressCharacters( const int chars )
{
_addressChars = chars;
if( _addressChars & 1 )
_addressChars++;
// Calculate upper and lower half masks
_lowerMask = 0xf;
for( int t( 1 ); t < _addressChars / 2; t++ ) {
_lowerMask |= static_cast<qint64>( 0xf << (4 * t) );
}
_upperMask = _lowerMask << (4 * _addressChars / 2);
adjust();
}
qint64 BinFileView::addressAddend()
{
return static_cast<qint64>( verticalScrollBar()->sliderPosition() ) * _bytesPerLine;
}
void BinFileView::showContextMenu( const QPoint& pos )
{
_contextMenu->exec( mapToGlobal( pos ) );
}
void BinFileView::synchronizeVisuals( BinFileView* view )
{
setAddressCharacters( view->addressCharacters() );
calculateNumOfByteGroups();
horizontalScrollBar()->setRange( 0, preFitWidth( _byteGroups ) - viewport()->width() );
horizontalScrollBar()->setPageStep( viewport()->width() );
viewport()->update();
}
void BinFileView::dragEnterEvent( QDragEnterEvent* event )
{
if( event->mimeData()->hasUrls() ) {
event->acceptProposedAction();
}
}
void BinFileView::dropEvent( QDropEvent* event )
{
QList<QUrl> urls = event->mimeData()->urls();
if( urls.count() > 0 ) {
emit fileDropped( urls.at( 0 ).toLocalFile(), this );
}
// In view instance, we can ignore the event data because we
// have passed it to controller via signal
event->setDropAction( Qt::IgnoreAction );
event->accept();
}
void BinFileView::resizeEvent( QResizeEvent* )
{
_linesOnViewPort = ( viewport()->height() - _bottomMargin ) / fontMetrics().height();
if( _vscrollBarWidth != verticalScrollBar()->width() ) {
_vscrollBarWidth = verticalScrollBar()->width();
updateGeometry();
}
calculateNumOfByteGroups();
horizontalScrollBar()->setRange( 0, preFitWidth( _byteGroups ) - viewport()->width() );
horizontalScrollBar()->setPageStep( viewport()->width() );
verticalScrollBar()->setRange( 0, _lineCount - _linesOnViewPort );
verticalScrollBar()->setPageStep( _linesOnViewPort );
verticalScrollBar()->setSliderPosition( static_cast<int>( _addend / _bytesPerLine ) );
emit fileViewContentChanged( this );
}
void BinFileView::paintEvent( QPaintEvent* event )
{
QPainter painter( viewport() );
int xOffset = horizontalScrollBar()->value();
painter.fillRect( QRect( -xOffset, event->rect().top(), _addressAreaWidth, height() ), viewport()->palette().color( QPalette::Button ) );
painter.setPen( viewport()->palette().color( QPalette::WindowText ) );
painter.drawLine( _addressAreaWidth + _hexAreaWidth - xOffset, event->rect().top(), _addressAreaWidth + _hexAreaWidth -xOffset, event->rect().height() );
if( _lineCount ) {
_addend = addressAddend();
int yIncr = fontMetrics().height();
int yPos = yIncr;
for( qint64 row( 0 ); row < qMin( _linesOnViewPort, _lineCount ); row++, yPos += yIncr ) {
qint64 addr = row * static_cast<qint64>( _bytesPerLine ) + _addend;
QString addrString = QString( "%1:%2" ).arg( ( addr & _upperMask ) >> (4 * _addressChars / 2), _addressChars / 2, 16, QChar( '0' ) ).toUpper() \
.arg( addr & _lowerMask, _addressChars / 2, 16, QChar( '0' ) ).toUpper();
painter.setPen( viewport()->palette().color( QPalette::ButtonText ) );
painter.drawText( _leftMargin - xOffset, yPos, addrString );
painter.setPen( viewport()->palette().color( QPalette::WindowText ) );
int xPos = _addressAreaWidth + _leftMargin - xOffset;
for( qint64 b( 0 ); b < qMin( static_cast<qint64>( _bytesPerLine ), _size - addr ); b++, xPos += _byteWidth ) {
uchar binByte = *( _data + b + addr );
QString byte = QString( "%1" ).arg( binByte, 2, 16, QChar( '0' ) ).toUpper();
if( b > 0 && b % BinFileView::bytesPerGroup == 0 )
xPos += _groupGap;
if( _colorData )
painter.setPen( (Qt::GlobalColor)*( _colorData + b + addr ) );
painter.drawText( xPos, yPos, byte );
}
xPos = _addressAreaWidth + _hexAreaWidth + _leftMargin - xOffset;
int charWidth = fontMetrics().averageCharWidth();
for( qint64 c( 0 ); c < qMin( (qint64)_bytesPerLine, _size - addr ); c++, xPos += charWidth ) {
uchar binByte = *( _data + c + addr );
if( _colorData )
painter.setPen( (Qt::GlobalColor)*( _colorData + c + addr ) );
painter.drawText( xPos, yPos, isprint( binByte ) ? QString( binByte ) : QString( '.' ) );
}
}
painter.setPen( viewport()->palette().color( QPalette::WindowText ) );
} else {
drawEmptyViewInstructions( painter );
}
}
void BinFileView::scrollContentsBy( int, int )
{
emit fileViewContentChanged( this );
viewport()->update();
}
void BinFileView::calculateNumOfByteGroups()
{
// Calculate new # of byte groups
while( viewport()->width() > preFitWidth( _byteGroups + 1 ) || viewport()->width() < preFitWidth( _byteGroups ) ) {
if( viewport()->width() > preFitWidth( _byteGroups + 1 ) )
adjust( ++_byteGroups );
else if( viewport()->width() < preFitWidth( _byteGroups ) && _byteGroups > BinFileView::minimumOfByteGroups )
adjust( --_byteGroups );
else {
adjust( BinFileView::minimumOfByteGroups );
_byteGroups = BinFileView::minimumOfByteGroups;
break;
}
}
}
void BinFileView::adjust( const int newbyteGroups )
{
if( newbyteGroups ) {
_byteGroups = newbyteGroups;
_bytesPerLine = _byteGroups * BinFileView::bytesPerGroup;
}
_lineCount = static_cast<int>( _size / _bytesPerLine );
if( _size % _bytesPerLine )
_lineCount += 1;
QString address = QString( "%1:%2" ).arg( 0, _addressChars / 2, 16, QChar( '0' ) ) \
.arg( 0, _addressChars / 2, 16, QChar( '0' ) );
QString byte = QString( "%1" ).arg( 0, 2, 16, QChar( '0' ) );
// Set side margins to half of a character width,
// bottom margin half of font height
_leftMargin = fontMetrics().averageCharWidth() / 2;
_rightMargin = _leftMargin;
_bottomMargin = fontMetrics().height() / 2;
_byteWidth = fontMetrics().width( byte ) + _leftMargin;
_addressAreaWidth = fontMetrics().width( address ) + _leftMargin + _rightMargin;
_groupGap = fontMetrics().averageCharWidth();
_groupWidth = BinFileView::bytesPerGroup * _byteWidth;
_hexAreaWidth = _byteGroups * _groupWidth + (_byteGroups - 1) * _groupGap + _rightMargin;
_asciiAreaWidth = _bytesPerLine * fontMetrics().averageCharWidth() + _leftMargin + _rightMargin;
}
int BinFileView::preFitWidth( const int byteGroups ) const
{
int newHexAreaWidth = byteGroups * _groupWidth + (byteGroups - 1) * _groupGap + _rightMargin;
int newAsciiAreaWidth = byteGroups * BinFileView::bytesPerGroup * fontMetrics().averageCharWidth() + _leftMargin + _rightMargin;
return( _addressAreaWidth + newHexAreaWidth + newAsciiAreaWidth );
}
void BinFileView::drawEmptyViewInstructions( QPainter& painter )
{
QString help1( tr( "< Drag & drop or >" ) );
QString help2( tr( "< use ctx menu >" ) );
QString help3( tr( "< to open a file >" ) ) ;
int xPos = _addressAreaWidth + _hexAreaWidth / 2 - fontMetrics().width( help1 ) / 2;
int yIncr = fontMetrics().height();
int yPos = ( viewport()->height() + yIncr / 2 - 3 * yIncr + yIncr ) / 2;
painter.drawText( xPos, yPos, help1 );
yPos += yIncr;
int xPos2 = _addressAreaWidth + _hexAreaWidth / 2 - fontMetrics().width( help2 ) / 2;
painter.drawText( xPos2, yPos, help2 );
yPos += yIncr;
painter.drawText( xPos, yPos, help3 );
}