Skip to content

Commit 34f5c78

Browse files
authored
Merge pull request #172 from vcastilla/default-char
Enable customization of default ASCII character
2 parents d5c7abf + 1a770ab commit 34f5c78

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/qhexedit.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ QHexEdit::QHexEdit(QWidget *parent) : QAbstractScrollArea(parent)
3636
, _addressWidth(4)
3737
, _asciiArea(true)
3838
, _bytesPerLine(16)
39+
, _defaultChar('.')
3940
, _hexCharsInLine(47)
4041
, _highlighting(true)
4142
, _overwriteMode(true)
@@ -158,6 +159,20 @@ int QHexEdit::bytesPerLine()
158159
return _bytesPerLine;
159160
}
160161

162+
char QHexEdit::defaultChar()
163+
{
164+
return _defaultChar;
165+
}
166+
167+
void QHexEdit::setDefaultChar(char defaultChar)
168+
{
169+
_defaultChar = defaultChar;
170+
171+
adjust();
172+
setCursorPosition(_cursorPosition);
173+
viewport()->update();
174+
}
175+
161176
void QHexEdit::setCursorPosition(qint64 position)
162177
{
163178
// 1. delete old cursor
@@ -945,7 +960,7 @@ void QHexEdit::paintEvent(QPaintEvent *event)
945960

946961
int ch = (uchar)_dataShown.at(bPosLine + colIdx);
947962
if ( ch < ' ' || ch > '~' )
948-
ch = '.';
963+
ch = _defaultChar;
949964
rect.setRect(pxPosAsciiX2, pxPosY - _pxCharHeight + _pxSelectionSub, _pxCharWidth, _pxCharHeight);
950965
painter.fillRect(rect, asciiArea.areaStyle());
951966
painter.drawText(pxPosAsciiX2, pxPosY, QChar(ch));
@@ -982,7 +997,7 @@ void QHexEdit::paintEvent(QPaintEvent *event)
982997
// every 2 hex there is 1 ascii
983998
int ch = (uchar)_dataShown.at(hexPos / 2);
984999
if (ch < ' ' || ch > '~')
985-
ch = '.';
1000+
ch = _defaultChar;
9861001

9871002
painter.drawText(_pxCursorX - pxOfsX, _pxCursorY, QChar(ch));
9881003
}
@@ -1180,7 +1195,7 @@ QString QHexEdit::toReadable(const QByteArray &ba)
11801195
hexStr.append(" ").append(ba.mid(i+j, 1).toHex());
11811196
char ch = ba[i + j];
11821197
if ((ch < 0x20) || (ch > 0x7e))
1183-
ch = '.';
1198+
ch = _defaultChar;
11841199
ascStr.append(QChar(ch));
11851200
}
11861201
}

src/qhexedit.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ class QHEXEDIT_API QHexEdit : public QAbstractScrollArea
104104
/*! Set and get bytes number per line.*/
105105
Q_PROPERTY(int bytesPerLine READ bytesPerLine WRITE setBytesPerLine)
106106

107+
/*! Set and get character to display when outside the printable range.
108+
*/
109+
Q_PROPERTY(int defaultChar READ defaultChar WRITE setDefaultChar)
110+
107111
/*! Property cursorPosition sets or gets the position of the editor cursor
108112
in QHexEdit. Every byte in data has two cursor positions: the lower and upper
109113
Nibble. Maximum cursor position is factor two of data.size().
@@ -330,6 +334,9 @@ public slots:
330334
int bytesPerLine();
331335
void setBytesPerLine(int count);
332336

337+
char defaultChar();
338+
void setDefaultChar(char defaultChar);
339+
333340
qint64 cursorPosition();
334341
void setCursorPosition(qint64 position);
335342

@@ -407,6 +414,7 @@ private slots:
407414
bool _asciiArea;
408415
qint64 _addressOffset;
409416
int _bytesPerLine;
417+
char _defaultChar;
410418
int _hexCharsInLine;
411419
bool _highlighting;
412420
bool _overwriteMode;

0 commit comments

Comments
 (0)