-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhextexteditor.cpp
More file actions
1452 lines (1154 loc) · 37.1 KB
/
hextexteditor.cpp
File metadata and controls
1452 lines (1154 loc) · 37.1 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "hextexteditor.h"
#include <QDebug>
#include <QPainter>
#include <QScrollArea>
#include <QScrollBar>
#include "qmenu.h"
#include "qapplication.h"
#include "clipboard.h"
#include <QClipboard>
#include <QLineEdit>
#include <QInputDialog>
#include <QMimeData>
#include "hexeditdialog.h"
#define OFS_SEPATOR 3
#define OFS_LINENUMBER 6
HexTextEditor::HexTextEditor( QWidget* parent )
:QAbstractScrollArea( parent )
, mMaxVisualLine( 0 )
, mMaxLine( 0 )
, mCurLine( 0 )
, mRollStep( 4 )
, mFontMetrics( nullptr )
, mColorHexText( Qt::darkBlue )
, mBKColorHexText( Qt::white )
, mColorLineNumer( Qt::blue )
, mBKColorLineNumber( Qt::white )
, mCharWidth( )
, mColorSelect( QColor( 0xcc , 0xcc, 0xcc) )
, mMouseLbuttonStatus(false )
, mSelectBeginIndex()
, mSelectEndIndex()
, mControlKeyStatus(false )
, mCursor(0)
{
setFont(QFont( "Consolas", 10) );
setMouseTracking( true );
mScrollBar = new QScrollBar( this );
mScrollBar->setRange( 0 , mMaxLine );
connect( mScrollBar , SIGNAL( sliderMoved( int ) ) , this , SLOT( scrollbarMove( int ) ) );
connect( mScrollBar , SIGNAL( valueChanged( int ) ) , this , SLOT( scrollbarMove( int ) ) );
}
int HexTextEditor::getNumerOfDigit( int num )
{
int power = 0;
while( num ) {
num /= LINE_ITEM_COUNT;
++power;
}
return power;
}
void HexTextEditor::hexToString( const char* pHex , int nLen , char* pBuff )
{
if( pHex == NULL || pBuff == nullptr )
return ;
for( int i = 0 ; i < nLen; ++i ) {
sprintf( pBuff , "%02X " , (unsigned char)*pHex );
pHex++;
pBuff += 3;
}
*( --pBuff ) = 0; // 倒数第二个是空格,将它去掉
}
void HexTextEditor::getHexString( const char* pHex , int nLen , char* pBuff )
{
memcpy( pBuff , pHex , nLen );
pBuff[ nLen ] = 0;
while( nLen-- ) {
if( *pBuff < 32 )
*pBuff = '.';
++pBuff;
}
}
//************************************
// 函数名: paintEvent
// 功 能: 窗口绘制事件
// 返回值: void
// 参 数: QPaintEvent * event
//************************************
void HexTextEditor::paintEvent( QPaintEvent *event )
{
event;
QPainter painter( this->viewport( ) );
// 将背景刷成白色
painter.fillRect( painter.viewport( ) , QBrush( mBKColorHexText ) );
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// 绘制选中区域
paintSelectRect( painter );
if( mHexData.isEmpty( ) )
return;
paintCursor(painter);
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// 绘制行号
paintLineNumber( painter );
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// 绘制十六进制字符串
paintHexText( painter );
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// 绘制字符串
paintString( painter );
}
// 滚动条滚动事件
void HexTextEditor::wheelEvent( QWheelEvent * e )
{
if( mHexData.isEmpty( ) )
return;
int numDegrees = e->delta( ) / 8;
int numSteps = numDegrees / 15;
if( numSteps > 0 ) {
if( (QApplication::keyboardModifiers() == Qt::ControlModifier) ){
QFont font = this->font();
font.setPixelSize(font.pixelSize() + 10);
setFont(font);
//viewport()->update();
return ;
}
if( mCurLine <= 0 )
return;
emit wheelUp( mRollStep );
// 向下移动滚动条
for( int i = 0; i < mRollStep && mCurLine>0; ++i ) {
//verticalScrollBar( )->triggerAction( QAbstractSlider::SliderSingleStepSub );
mCurLine--;
}
mScrollBar->setSliderPosition( mCurLine );
}
else {
if( (QApplication::keyboardModifiers() == Qt::ControlModifier) ){
QFont font = this->font();
font.setPixelSize(font.pixelSize() - 10);
setFont(font);
//viewport()->update();
return ;
}
if( mCurLine >= mMaxLine )
return ;
emit wheelDown( mRollStep );
// 向上移动滚动条
for( int i = 0; i < mRollStep&& mCurLine <= mMaxLine; ++i ) {
//verticalScrollBar( )->triggerAction( QAbstractSlider::SliderSingleStepAdd );
mCurLine++;
}
mScrollBar->setSliderPosition( mCurLine );
}
//this->viewport( )->update( );
}
// 窗口大小改变事件
void HexTextEditor::resizeEvent( QResizeEvent * event )
{
event;
mMaxVisualLine = viewport( )->height( ) / mLineHeight ;
mScrollBar->move( viewport()->width() - 10 , 0 );
mScrollBar->resize( 10 , viewport( )->height( ) );
}
// 鼠标移动事件
void HexTextEditor::mouseMoveEvent( QMouseEvent * e )
{
if(mHexData.isEmpty())
return;
QPoint pos = e->pos( );
QPainter painter( this->viewport( ) );
if( isOnLineNumberColumn( e->pos( ) ) ) {
setCursor(Qt::ForbiddenCursor);
emit mouseOnLineNumer( e->pos( ) );
}
else if( isOnHexTextColumn( e->pos( ) ) ) {
// 将坐标转行成行号, 并发出信号
//
int line = ( pos.y( ) - mLineHeight - 5 ) / mLineHeight + mCurLine;
int row = ( pos.x( ) - mLineNumberWidth + mCharWidth * 2 ) / mCharWidth / 3 ;
setCursor( Qt::IBeamCursor );
emit mouseOnHexText( line , row );
}
else if( isOnStringColumn( e->pos( ) ) ) {
setCursor( Qt::ArrowCursor );
QPoint pos = e->pos( );
int line = ( pos.y( ) - mLineHeight - 5 ) / mLineHeight + mCurLine;
int row = ( pos.x( ) - mLineNumberWidth- mHexTextWidth + mCharWidth * 2 ) / mCharWidth / 3 ;
emit mouseOnString( line , row );
}
if( mMouseLbuttonStatus ) {
int line = ( pos.y( ) - mLineHeight - 5 ) / mLineHeight + mCurLine;
int row = ( pos.x( ) - mLineNumberWidth + mCharWidth * 2 ) / mCharWidth / 3 ;
mSelectEndIndex = line*LINE_ITEM_COUNT + row ;
if( mSelectBeginIndex > mSelectEndIndex ) {
setSelection( mSelectEndIndex , mSelectBeginIndex );
}
else
setSelection( mSelectBeginIndex , mSelectEndIndex );
//viewport( )->repaint( );
}
}
// 鼠标按键按下事件
void HexTextEditor::mousePressEvent( QMouseEvent * e )
{
if( e->button( ) == Qt::LeftButton ) {
if( isOnHexTextColumn( e->pos( ) ) ) {
mSelect.clear( );
mMouseLbuttonStatus = true;
QPoint pos = e->pos( );
int line = ( pos.y( ) - mLineHeight - 5 ) / mLineHeight + mCurLine;
int row = ( pos.x( ) - mLineNumberWidth + mCharWidth * 2 ) / mCharWidth / 3 ;
mSelectBeginIndex = line*LINE_ITEM_COUNT + row ;
mCursor.setPos(mSelectBeginIndex);
}
}
}
// 鼠标按键弹起事件
void HexTextEditor::mouseReleaseEvent( QMouseEvent * e )
{
if( e->button( ) == Qt::LeftButton ) {
if( isOnHexTextColumn( e->pos( ) ) ) {
\
mMouseLbuttonStatus = false;
// 将鼠标按下点转换为行列
QPoint pos = e->pos( );
int line = ( pos.y( ) - mLineHeight - 5 ) / mLineHeight + mCurLine;
int row = ( pos.x( ) - mLineNumberWidth + mCharWidth * 2 ) / mCharWidth / 3 ;
mSelectEndIndex = line*LINE_ITEM_COUNT + row ;
if( mSelectBeginIndex > mSelectBeginIndex ) {
setSelection( mSelectEndIndex , mSelectBeginIndex );
}
else
setSelection( mSelectBeginIndex , mSelectEndIndex );
viewport()->update();
}
}
}
void HexTextEditor::keyReleaseEvent( QKeyEvent * e )
{
QPoint pos = QCursor::pos( ) ;
pos = this->mapFromGlobal( pos );
int key = e->key( );
if( key & Qt::Key_Control ) {
mControlKeyStatus = false;
}
}
// 键盘按键按下事件
void HexTextEditor::keyPressEvent( QKeyEvent * e )
{
QPoint pos = QCursor::pos( ) ;
pos = this->mapFromGlobal( pos );
int key = e->key( );
if( e->modifiers( ) == Qt::ControlModifier ) {
switch( key ) {
case Qt::Key_Z: undo( ); break;
case Qt::Key_Y: redo( ); break;
case Qt::Key_G:menuGotoByteTriggered( ); break;
case Qt::Key_E:menuEditTriggered( ); break;
case Qt::Key_C:menuCopyTriggered( ); break;
case Qt::Key_A:menuSelectAllTriggered( ); break;
case Qt::Key_Down:
{
if( mCurLine >= mMaxLine )
return ;
emit wheelDown( mRollStep );
// 向上移动滚动条
mCurLine++;
mScrollBar->setSliderPosition( mCurLine );
}
break;
case Qt::Key_Up:
{
if( mCurLine <= 0 )
return;
emit wheelUp( mRollStep );
// 向下移动滚动条
mCurLine--;
mScrollBar->setSliderPosition( mCurLine );
}
}
}
else if(e->modifiers() == Qt::ShiftModifier){
}
else{
switch (key) {
case Qt::Key_Down:
mCursor.moveNextLine();
viewport()->update();
break;
case Qt::Key_Up:
mCursor.movePreLine();
viewport()->update();
break;
case Qt::Key_Left:
mCursor.movePre();
viewport()->update();
break;
case Qt::Key_Right:
mCursor.moveNext();
viewport()->update();
break;
case Qt::Key_Backspace:
if(mCursor.pos()==0)
return;
mEditUndoStack.push(mCursor.pos(), mHexData[mCursor.pos()],true);
mHexData.remove(mCursor.pos(),1);
mCursor.movePre();
viewport()->update();
break;
case Qt::Key_Delete:
if(mCursor.pos()>=mHexData.size())
return;
mEditUndoStack.push(mCursor.pos()+1, mHexData[mCursor.pos()+1],true);
mHexData.remove(mCursor.pos()+1,1);
viewport()->update();
break;
}
// int data = 0;
// if(key>='0' && key <= '9'){
// data = key -'0';
// }else if(key>='a' && key<='z'){
// data = key -'a';
// }
// else if(key>='A' && key<='Z'){
// data = key-'A';
// }
if(QChar::isPrint(key)){
if( this->posIsOnVisual(mCursor.pos())){
mEditUndoStack.push(mCursor.pos(), mHexData[mCursor.pos()]);
mHexData[mCursor.pos()] = key;
mCursor.moveNext();
viewport()->update();
}
}
}
}
//************************************
// 函数名: contextMenuEvent
// 功 能: 菜单事件
// 返回值: void
// 参 数: QContextMenuEvent * event
//************************************
void HexTextEditor::contextMenuEvent( QContextMenuEvent *event )
{
if( mHexData.isEmpty( ) )
return;
event;
QMenu *menu = new QMenu( this );
emit menuPopup( menu );
if( !mSelect.isEmpty( ) ) {
connect( menu->addAction( tr( "©" ) ) ,
SIGNAL( triggered( ) ) ,
this ,
SLOT( menuCopyTriggered( ) ) );
connect( menu->addAction( tr( "©AsCStyle" ) ) ,
SIGNAL( triggered( ) ) ,
this ,
SLOT( menuCopyAsCStyle() ) );
connect( menu->addAction( tr( "&edit" ) ) ,
SIGNAL( triggered( ) ) ,
this ,
SLOT( menuEditTriggered( ) ) );
}
connect( menu->addAction( tr( "&all select" ) ) ,
SIGNAL( triggered( ) ) ,
this ,
SLOT( menuSelectAllTriggered( ) ) );
connect( menu->addAction( tr( "&goto address" ) ) ,
SIGNAL( triggered( ) ) ,
this ,
SLOT( menuGotoByteTriggered( ) ) );
connect( menu->addAction( tr( "&goto line" ) ) ,
SIGNAL( triggered( ) ) ,
this ,
SLOT( menuGotoLineTriggered( ) ) );
connect( menu->addAction(tr("&parse from string")),
SIGNAL( triggered( ) ) ,
this ,
SLOT( menuParseFromString( ) ) );
connect( menu->addAction(tr("&parse from hex string")),
SIGNAL( triggered( ) ) ,
this ,
SLOT( menuParseFromHexString( ) ) );
//让菜单显示的位置在鼠标的坐标上
menu->move( cursor( ).pos( ) );
menu->show( );
}
void HexTextEditor::menuCopyTriggered( )
{
if( mSelect.isEmpty( ) )
return;
QClipboard *clipboard = QApplication::clipboard( ); //获取系统剪贴板指针
// 获取选中内容
QString buff;
getSelectionText( buff );
clipboard->setText( buff );
}
void HexTextEditor::menuCopyAsCStyle()
{
if( mSelect.isEmpty( ) )
return;
QClipboard *clipboard = QApplication::clipboard( ); //获取系统剪贴板指针
// 获取选中内容
QString buff;
getSelectionText( buff );
buff.replace(" ",",0x");
buff.insert(0,"/*copy by enorez hex editor*/\nunsigned char hexData[]=\n{\n0x");
buff.append("\n};");
clipboard->setText( buff );
}
void HexTextEditor::menuSelectAllTriggered( )
{
if( mHexData.isEmpty( ) )
return;
setSelection( 0 , mHexData.size( ) );
}
void HexTextEditor::menuGotoByteTriggered( )
{
if( mHexData.isEmpty( ) )
return;
bool isOK;
QString text = QInputDialog::getText( NULL ,
tr( "goto" ) ,
tr( "input address" ) ,
QLineEdit::Normal ,
"" ,
&isOK );
if( isOK ) {
int pos = text.toInt( );
setSelection( pos , pos + 1 );
setVisualPos( pos );
}
}
void HexTextEditor::menuGotoLineTriggered( )
{
if( mHexData.isEmpty( ) )
return;
bool isOK;
QString text = QInputDialog::getText( NULL ,
tr("goto") ,
tr( "input linenumber" ) ,
QLineEdit::Normal ,
"" ,
&isOK );
if( isOK ) {
int pos = text.toInt( );
setVisualPos( pos*LINE_ITEM_COUNT );
}
}
void HexTextEditor::menuEditTriggered( )
{
if( mHexData.isEmpty( ) )
return;
// 弹出编辑框
QString text;
if( !getSelectionText( text ) )
return ;
HexEditDialog dlg( this );
if( !dlg.exec( ( getSlectionLength() ) * 3 - 1 , text ) )
return;
QStringList hexList = text.split( ' ');
int j = mSelect.begin( )->mLineNumber *LINE_ITEM_COUNT + mSelect.begin( )->mRow;
for( auto &i : hexList ) {
edit( j , i.toInt( 0,16) );
++j;
}
viewport()->update();
}
void HexTextEditor::menuParseFromHex()
{
QByteArray data = Clipboard::getData();
const char* p = data.data();
int beg = mCursor.pos();
for(int i = 0; i<data.size();++i){
if(!edit(i+beg, p[i])){
break;
}
}
//viewport()->update();
}
void HexTextEditor::menuParseFromString()
{
menuParseFromHex();
}
void HexTextEditor::menuParseFromHexString()
{
QByteArray data = Clipboard::getData();
data.replace(" ","");
const char* p = data.data();
qDebug() <<data;
char buff[3]={0};
quint8 byte=0;
int beg = mCursor.pos();
int size = data.size()/2;
for(int i = 0; i < size ;++i){
buff[0] = *p++;
buff[1] = *p++;
if(sscanf(buff,"%x",&byte) == 1){
qDebug()<<buff<<" "<<hex<<byte;
if(!edit(i+beg,byte))
{
break;
}
}
}
viewport()->update();
}
void HexTextEditor::scrollbarMove( int nStep )
{
if( nStep<0 || nStep>mMaxLine )
return;
if(nStep>mCurLine )
emit wheelDown( nStep - mCurLine );
else
emit wheelUp( mCurLine - nStep );
mCurLine = nStep;
viewport()->update();
}
// 设置字体
void HexTextEditor::setFont(const QFont &font )
{
/**
* 字体的改变, 会影响到行号栏, 十六进制文本栏和字符串栏的宽度
*/
QAbstractScrollArea::setFont( font ) ;
if( mFontMetrics )
delete mFontMetrics;
mFontMetrics = new QFontMetrics( font );
mLineHeight = mFontMetrics->height( ) ;
mCharWidth = mFontMetrics->width( 'W' );
// 可显示的最大行数
mMaxVisualLine = viewport( )->height( ) ;
mMaxVisualLine /= mLineHeight;
// 行号栏的宽度
mLineNumberWidth = 6 * mCharWidth ;
// 十六进制文本栏的宽度
mHexTextWidth = LINE_ITEM_COUNT * 3 * mCharWidth + 5;
// 字符串栏的宽度
mStringWidth = LINE_ITEM_COUNT * mCharWidth;
// 设置部件的最大窗口和最小窗口大小
// setMinimumWidth( getCharWidth( ) * 10 + mHexTextWidth + mStringWidth );
// setMaximumWidth( getCharWidth( ) * 10 + mHexTextWidth + mStringWidth );
}
//************************************
// 函数名: setVisualPos
// 功 能: 将一个地址设置到在显示区域
// 返回值: void
// 参 数: int index
//************************************
void HexTextEditor::setVisualPos( int pos )
{
pos /= LINE_ITEM_COUNT;
if( pos < 0 )
pos = 0;
if( pos >= mMaxLine )
pos = mMaxLine ;
mCurLine = pos;
viewport()->update();
}
int HexTextEditor::getVisualPos( ) const
{
return mCurLine * LINE_ITEM_COUNT;
}
// 返回当前可显示行的开始地址
int HexTextEditor::getVisualLine( ) const
{
return mCurLine;
}
// 将指定行设置为可显示(并处以第一行)
void HexTextEditor::setVisualLine( int line )
{
if( line<0 || line>mMaxLine )
return;
mCurLine = line;
viewport()->update();
}
int HexTextEditor::positionToLineNumber( int pos ) const
{
return pos / LINE_ITEM_COUNT;
}
int HexTextEditor::lineToPosition( int line ) const
{
return line * LINE_ITEM_COUNT;
}
int HexTextEditor::mousePosToIndex( const QPoint& pos )
{
if( isOnHexTextColumn( pos ) ) {
int line = ( pos.y( ) - mLineHeight - 5 ) / mLineHeight + mCurLine;
int row = ( pos.x( ) - mLineNumberWidth + mCharWidth * 2 ) / mCharWidth / 3 ;
return line * LINE_ITEM_COUNT + row;
}
return -1;
}
bool HexTextEditor::posIsOnVisual( int pos ) const
{
return lineIsOnVisual( positionToLineNumber( pos ) );
}
bool HexTextEditor::lineIsOnVisual( int line ) const
{
return line >= mCurLine && line < mCurLine + mMaxVisualLine;
}
// 获取可显示的最大函数
int HexTextEditor::getMaxVisualLine( ) const
{
return mMaxVisualLine;
}
// 获取一行的高度,高度以像素为单位
int HexTextEditor::getLineHeight( ) const
{
return mFontMetrics->height( );
}
// 获取一个字符的宽度(以像素为单位)
int HexTextEditor::getCharWidth( ) const
{
return mFontMetrics->width( 'W' );
}
// 获取当前滚动条滚动一次的行数
int HexTextEditor::getRollStep( ) const
{
return mRollStep;
}
// 设置当前滚动条滚动一次的行数
void HexTextEditor::setRollStep( int rollStep )
{
mRollStep = rollStep;
}
// 设置要显示的二进制数据
void HexTextEditor::setHexData( const QByteArray& data )
{
mHexData = data;
int nSize = 0;
nSize = data.size( );
if( data.size( ) % LINE_ITEM_COUNT >= 0 ) {
mMaxLine = data.size( ) / LINE_ITEM_COUNT + 1;
}
else
mMaxLine = data.size( ) ;
mCurLine = 0;
mScrollBar->setRange( 0 , mMaxLine );
// 重绘
viewport()->update();
}
QByteArray HexTextEditor::hexData() const
{
return mHexData;
}
//************************************
// 函数名: getLineNumberColumnWidth
// 功 能: 获取行号栏的总宽度,宽度以像素为单位
// 返回值: int
//************************************
int HexTextEditor::getLineNumberColumnWidth( ) const
{
return getNumerOfDigit( ( mCurLine + mMaxVisualLine ) * mLineHeight );
}
// 填充被选中的区域的背景色
void HexTextEditor::paintSelectRect( QPainter& painter )
{
int nStartY = mLineHeight + 5;
QRect rect;
QBrush brush( mColorSelect );
int nBegLine = 0 , nCount = 0;
int lineCount = 0;
for( auto &i : mSelect ) {
if( i.mCount == 0 || i.mLineNumber-mCurLine > mMaxVisualLine)
continue;
nBegLine = i.mLineNumber;
nCount = i.mCount;
lineCount = nCount / 16;
if( nCount > LINE_ITEM_COUNT ) { // 字符个数超出一行的个数
nCount = LINE_ITEM_COUNT;
}
if( nBegLine < mCurLine ) {
if( nBegLine + lineCount < mCurLine) {
continue;
}
else {
lineCount = nBegLine + lineCount - mCurLine;
nBegLine = mCurLine;
}
}
if( lineCount > mMaxVisualLine )
lineCount = mMaxVisualLine;
else if(lineCount==0 )
lineCount = 1;
// 绘制十六进制文本去的背景
painter.fillRect( mLineNumberWidth + mCharWidth * i.mRow * 3 ,
nStartY + mLineHeight * ( nBegLine - mCurLine ) ,
mCharWidth * nCount * 3 - mCharWidth , // 行宽
mLineHeight * lineCount , // 行高
brush );
// 绘制字符串区的背景
painter.fillRect( mLineNumberWidth + mHexTextWidth + mCharWidth * i.mRow ,
nStartY + mLineHeight*( nBegLine - mCurLine ) ,
mCharWidth * nCount ,
mLineHeight* lineCount ,
brush );
}
}
void HexTextEditor::paintCursor(QPainter &painter)
{
int pos = mCursor.pos();
QColor color = mColorSelect;
color.setBlue(mColorSelect.blue()+20);
QBrush brush(color );
int nStartY = mLineHeight + 5;
int line = positionToLineNumber(pos );
int row = pos % LINE_ITEM_COUNT;
// 判断光标是否在当前显示范围内.
if( line < mCurLine || line-mCurLine > getMaxVisualLine())
return;
painter.fillRect( mLineNumberWidth + mCharWidth * row * 3 ,
nStartY + mLineHeight * ( line - mCurLine ) ,
mCharWidth,
mLineHeight,
brush);
painter.fillRect( mLineNumberWidth + mHexTextWidth + mCharWidth * row ,
nStartY + mLineHeight*( line - mCurLine ) ,
mCharWidth ,
mLineHeight,
brush );
}
// 绘制出行号
void HexTextEditor::paintLineNumber( QPainter& painter )
{
int nLineNumerDigitCount = getNumerOfDigit( ( mCurLine + mMaxVisualLine ) * LINE_ITEM_COUNT );
// 画刷
painter.setBrush( QBrush( mBKColorLineNumber ) );
// 绘制背景
painter.fillRect( QRect( 0 , 0 , mLineNumberWidth , viewport( )->height( ) ) , QBrush( mBKColorLineNumber ) );
QRect pos( 0 , mLineHeight + 5 , mLineNumberWidth - 5 , mLineHeight );
QString text;
//
painter.setPen( mColorLineNumer );
painter.drawText( QPoint(0,mLineHeight ) , "enorez" );
for( int i = 1; i <= mMaxVisualLine && i + mCurLine <= mMaxLine; ++i ) {
text.sprintf( "%0*X" , nLineNumerDigitCount , ( i + mCurLine - 1 ) * LINE_ITEM_COUNT );
painter.drawText( pos , Qt::AlignRight , text );
pos.setY( pos.y( ) + mLineHeight ) ; // 切换到下一行
pos.setBottom( pos.bottom( ) + mLineHeight );
}
}
// 绘制十六进制字符串
void HexTextEditor::paintHexText( QPainter& painter )
{
// 绘制十六进制文本框
/*
|00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|0123456789ABCDEF
------+-----------------------------------------------+----------------
行号 | |
*/
QBrush brush;
// 绘制分隔符
QPen pen( QColor( 0 , 162 , 232 ) , 3 );
painter.setPen( pen );
painter.drawLine( 0 , mLineHeight + 4 , viewport( )->width( ) , mLineHeight + 4 );
pen.setColor( Qt::black );
// 绘制数字序号
painter.drawText( QPoint( mLineNumberWidth , mLineHeight ) ,
"00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F" );
// 获取需要转换的二进制字串的开始地址
int currentByte = mCurLine * LINE_ITEM_COUNT;
char hexBuff[ 3 * LINE_ITEM_COUNT + 1 ];
int nIndex = mCurLine * LINE_ITEM_COUNT;
char *pData = mHexData.data( );
int nSize = 0;
nSize = mHexData.size( ) - mCurLine * LINE_ITEM_COUNT;
painter.setPen( mColorHexText );
painter.setBrush( QBrush( mBKColorHexText ) );
painter.setFont( this->font( ) );
for( int i = mCurLine , line = 0; line < mMaxVisualLine && i < mMaxLine; ++i , ++line ) {
hexToString( pData + nIndex ,
nSize >= LINE_ITEM_COUNT ? LINE_ITEM_COUNT : nSize ,
hexBuff );
paintHexTextLine( painter ,
line , // 行号
0 , // 列号
hexBuff ,
( nSize >= LINE_ITEM_COUNT ? LINE_ITEM_COUNT : nSize ) * 3 - 1 ,
currentByte + line * LINE_ITEM_COUNT );
nIndex += LINE_ITEM_COUNT;
nSize -= LINE_ITEM_COUNT;
}
}
//************************************
// 函数名: paintHexTextLine
// 功 能: 绘制一行十六进制字符串
// 返回值: void
// 参 数: QPainter & painter
// 参 数: int line 行号
// 参 数: int row 列号
// 参 数: const char * pStr 要绘制的字符串
// 参 数: int nSize 字符串的长度
// 参 数: int absolutePos 这行字符串在二进制字节流中的绝对地址(二进制字节流指的是mHexData)
//************************************
void HexTextEditor::paintHexTextLine( QPainter& painter ,
int line ,
int row ,
const char * pStr ,
int nSize ,
int absolutePos )
{
TokenList::Token* pToken = nullptr;
char buff[ LINE_ITEM_COUNT * 3 + 1 ];
int tokenPos = 0;
nSize /= 3;