Skip to content

Commit 1a770ab

Browse files
committed
Enable customization of default ASCII character
Previously, the character used when a byte falls outside of the ASCII printable range was hardcoded to a period (`.`). This commit adds a getter and setter to enable its customization.
1 parent 3c5127f commit 1a770ab

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
@@ -33,6 +33,7 @@ QHexEdit::QHexEdit(QWidget *parent) : QAbstractScrollArea(parent)
3333
, _addressWidth(4)
3434
, _asciiArea(true)
3535
, _bytesPerLine(16)
36+
, _defaultChar('.')
3637
, _hexCharsInLine(47)
3738
, _highlighting(true)
3839
, _overwriteMode(true)
@@ -154,6 +155,20 @@ int QHexEdit::bytesPerLine()
154155
return _bytesPerLine;
155156
}
156157

158+
char QHexEdit::defaultChar()
159+
{
160+
return _defaultChar;
161+
}
162+
163+
void QHexEdit::setDefaultChar(char defaultChar)
164+
{
165+
_defaultChar = defaultChar;
166+
167+
adjust();
168+
setCursorPosition(_cursorPosition);
169+
viewport()->update();
170+
}
171+
157172
void QHexEdit::setCursorPosition(qint64 position)
158173
{
159174
// 1. delete old cursor
@@ -941,7 +956,7 @@ void QHexEdit::paintEvent(QPaintEvent *event)
941956

942957
int ch = (uchar)_dataShown.at(bPosLine + colIdx);
943958
if ( ch < ' ' || ch > '~' )
944-
ch = '.';
959+
ch = _defaultChar;
945960
rect.setRect(pxPosAsciiX2, pxPosY - _pxCharHeight + _pxSelectionSub, _pxCharWidth, _pxCharHeight);
946961
painter.fillRect(rect, asciiArea.areaStyle());
947962
painter.drawText(pxPosAsciiX2, pxPosY, QChar(ch));
@@ -978,7 +993,7 @@ void QHexEdit::paintEvent(QPaintEvent *event)
978993
// every 2 hex there is 1 ascii
979994
int ch = (uchar)_dataShown.at(hexPos / 2);
980995
if (ch < ' ' || ch > '~')
981-
ch = '.';
996+
ch = _defaultChar;
982997

983998
painter.drawText(_pxCursorX - pxOfsX, _pxCursorY, QChar(ch));
984999
}
@@ -1176,7 +1191,7 @@ QString QHexEdit::toReadable(const QByteArray &ba)
11761191
hexStr.append(" ").append(ba.mid(i+j, 1).toHex());
11771192
char ch = ba[i + j];
11781193
if ((ch < 0x20) || (ch > 0x7e))
1179-
ch = '.';
1194+
ch = _defaultChar;
11801195
ascStr.append(QChar(ch));
11811196
}
11821197
}

src/qhexedit.h

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

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

335+
char defaultChar();
336+
void setDefaultChar(char defaultChar);
337+
331338
qint64 cursorPosition();
332339
void setCursorPosition(qint64 position);
333340

@@ -405,6 +412,7 @@ private slots:
405412
bool _asciiArea;
406413
qint64 _addressOffset;
407414
int _bytesPerLine;
415+
char _defaultChar;
408416
int _hexCharsInLine;
409417
bool _highlighting;
410418
bool _overwriteMode;

0 commit comments

Comments
 (0)