Skip to content

Commit b0c8e09

Browse files
committed
refactor: 精简 QHexView;
1 parent 3a7c482 commit b0c8e09

2 files changed

Lines changed: 79 additions & 83 deletions

File tree

3rdparty/QHexView/document/qhexrenderer.cpp

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,8 @@ void QHexRenderer::applyDocumentStyles(QPainter *painter,
747747
textdocument->setDefaultTextOption(textopt);
748748
}
749749

750-
void QHexRenderer::applyBasicStyle(QTextCursor &textcursor,
751-
const QByteArray &rawline,
752-
Factor factor) const {
750+
void QHexRenderer::applyHexBasicStyle(QTextCursor &textcursor,
751+
const QByteArray &rawline) const {
753752
QColor color = m_bytesColor;
754753

755754
if (color.lightness() < 50) {
@@ -769,15 +768,15 @@ void QHexRenderer::applyBasicStyle(QTextCursor &textcursor,
769768
if ((rawline[i] != 0x00) && (uchar(rawline[i]) != 0xFF))
770769
continue;
771770

772-
textcursor.setPosition(i * factor);
771+
textcursor.setPosition(i * Hex);
773772
textcursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,
774-
factor);
773+
Hex);
775774
textcursor.mergeCharFormat(charformat);
776775
}
777776
}
778777

779-
void QHexRenderer::applyMetadata(QTextCursor &textcursor, qsizetype line,
780-
Factor factor) const {
778+
void QHexRenderer::applyHexMetadata(QTextCursor &textcursor,
779+
qsizetype line) const {
781780
QHexMetadata *metadata = m_document->metadata();
782781

783782
if (!metadata->lineHasMetadata(line))
@@ -817,37 +816,36 @@ void QHexRenderer::applyMetadata(QTextCursor &textcursor, qsizetype line,
817816
CONTRASTING_COLOR_BORDER, Qt::SolidLine));
818817
}
819818

820-
textcursor.setPosition(int(mi.start * factor));
819+
textcursor.setPosition(int(mi.start * Hex));
821820
textcursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,
822-
int((mi.length * factor)));
821+
int((mi.length * Hex)));
823822
textcursor.mergeCharFormat(charformat);
824823
}
825824
}
826825

827-
void QHexRenderer::applySelection(QTextCursor &textcursor, qsizetype line,
828-
Factor factor) const {
826+
void QHexRenderer::applyHexSelection(QTextCursor &textcursor,
827+
qsizetype line) const {
829828
if (!m_cursor->isLineSelected(line))
830829
return;
831830

832831
auto total = m_cursor->selectionCount();
833832
for (int i = 0; i < total; ++i) {
834-
applySelection(m_cursor->selection(i), textcursor, line, factor, false,
835-
false);
833+
applyHexSelection(m_cursor->selection(i), textcursor, line, false,
834+
false);
836835
}
837836

838837
if (m_cursor->hasPreviewSelection()) {
839-
applySelection(
840-
m_cursor->previewSelection(), textcursor, line, factor,
838+
applyHexSelection(
839+
m_cursor->previewSelection(), textcursor, line,
841840
m_cursor->previewSelectionMode() == QHexCursor::SelectionRemove,
842841
m_cursor->previewSelectionMode() == QHexCursor::SelectionNormal &&
843842
m_cursor->hasInternalSelection());
844843
}
845844
}
846845

847-
void QHexRenderer::applySelection(const QHexSelection &selection,
848-
QTextCursor &textcursor, qsizetype line,
849-
Factor factor, bool strikeOut,
850-
bool hasSelection) const {
846+
void QHexRenderer::applyHexSelection(const QHexSelection &selection,
847+
QTextCursor &textcursor, qsizetype line,
848+
bool strikeOut, bool hasSelection) const {
851849
if (!selection.isLineSelected(line)) {
852850
return;
853851
}
@@ -902,16 +900,14 @@ void QHexRenderer::applySelection(const QHexSelection &selection,
902900
end = startsel.lineWidth - 1;
903901
}
904902

905-
applySelection(metas, textcursor, line * startsel.lineWidth, begin, end,
906-
factor, strikeOut, hasSelection);
903+
applyHexSelection(metas, textcursor, line * startsel.lineWidth, begin, end,
904+
strikeOut, hasSelection);
907905
}
908906

909-
void QHexRenderer::applySelection(const QVector<QHexMetadata::MetaInfo> &metas,
910-
QTextCursor &textcursor,
911-
qsizetype startLineOffset,
912-
qsizetype lineStart, qsizetype lineEnd,
913-
Factor factor, bool strikeOut,
914-
bool hasSelection) const {
907+
void QHexRenderer::applyHexSelection(
908+
const QVector<QHexMetadata::MetaInfo> &metas, QTextCursor &textcursor,
909+
qsizetype startLineOffset, qsizetype lineStart, qsizetype lineEnd,
910+
bool strikeOut, bool hasSelection) const {
915911
auto totallen = lineEnd - lineStart + 1;
916912

917913
QTextCharFormat charfmt;
@@ -936,16 +932,16 @@ void QHexRenderer::applySelection(const QVector<QHexMetadata::MetaInfo> &metas,
936932

937933
auto blen = begin - fmtBegin;
938934

939-
textcursor.setPosition(fmtBegin * factor);
935+
textcursor.setPosition(fmtBegin * Hex);
940936
textcursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,
941-
blen * factor);
937+
blen * Hex);
942938

943939
textcursor.mergeCharFormat(charfmt);
944940
textcursor.clearSelection();
945941
totallen -= blen;
946942

947943
textcursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,
948-
mlen * factor);
944+
mlen * Hex);
949945

950946
if (!strikeOut) {
951947
QColor fg, bg = charfmt.background().color();
@@ -981,20 +977,21 @@ void QHexRenderer::applySelection(const QVector<QHexMetadata::MetaInfo> &metas,
981977

982978
if (totallen > 0) {
983979
textcursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,
984-
totallen * factor);
980+
totallen * Hex);
985981
textcursor.mergeCharFormat(charfmt);
986982
textcursor.clearSelection();
987983
}
988984
} else {
989-
textcursor.setPosition(lineStart * factor);
985+
textcursor.setPosition(lineStart * Hex);
990986
textcursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,
991-
totallen * factor);
987+
totallen * Hex);
992988
textcursor.mergeCharFormat(charfmt);
993989
}
994990
}
995991

996-
bool QHexRenderer::isByteSelected(qsizetype line, int column, bool *strikeOut,
997-
bool *hasSelection) const {
992+
bool QHexRenderer::isStrByteSelected(qsizetype line, int column,
993+
bool *strikeOut,
994+
bool *hasSelection) const {
998995
if (strikeOut) {
999996
*strikeOut = false;
1000997
}
@@ -1092,7 +1089,7 @@ QHexRenderer::asciiCellFormat(qsizetype line, int column, uchar value) const {
10921089

10931090
bool strikeOut = false;
10941091
bool hasSelection = false;
1095-
if (isByteSelected(line, column, &strikeOut, &hasSelection)) {
1092+
if (isStrByteSelected(line, column, &strikeOut, &hasSelection)) {
10961093
fmt.background = (strikeOut || hasSelection)
10971094
? m_selBackgroundColor.darker()
10981095
: m_selBackgroundColor;
@@ -1239,14 +1236,15 @@ void QHexRenderer::drawHex(QPainter *painter, const QRect &linerect,
12391236
hexrect.setX(this->getHexColumnX() + this->borderSize());
12401237

12411238
this->applyDocumentStyles(painter, &textdocument);
1242-
this->applyBasicStyle(textcursor, rawline, Hex);
1239+
this->applyHexBasicStyle(textcursor, rawline);
12431240

12441241
auto dis = !m_document->metabgVisible() && !m_document->metafgVisible() &&
12451242
!m_document->metaCommentVisible();
1246-
if (!dis)
1247-
this->applyMetadata(textcursor, line, Hex);
1243+
if (!dis) {
1244+
this->applyHexMetadata(textcursor, line);
1245+
}
12481246

1249-
this->applySelection(textcursor, line, Hex);
1247+
this->applyHexSelection(textcursor, line);
12501248
this->applyCursorHex(textcursor, line);
12511249

12521250
painter->save();
@@ -1256,12 +1254,12 @@ void QHexRenderer::drawHex(QPainter *painter, const QRect &linerect,
12561254
ctx.palette.setColor(QPalette::Text, m_bytesColor);
12571255
textdocument.documentLayout()->draw(painter, ctx);
12581256

1259-
this->applyBookMark(painter, textcursor, line, Hex);
1257+
this->applyHexBookMark(painter, textcursor, line);
12601258
painter->restore();
12611259
}
12621260

1263-
void QHexRenderer::applyBookMark(QPainter *painter, QTextCursor &textcursor,
1264-
qsizetype line, Factor factor) {
1261+
void QHexRenderer::applyHexBookMark(QPainter *painter, QTextCursor &textcursor,
1262+
qsizetype line) {
12651263

12661264
if (!m_document->lineHasBookMark(line))
12671265
return;
@@ -1275,18 +1273,11 @@ void QHexRenderer::applyBookMark(QPainter *painter, QTextCursor &textcursor,
12751273
auto off = item % hexLineWidth();
12761274

12771275
qreal begin, width;
1278-
12791276
// add some paddings
1280-
if (factor == Hex) {
1281-
begin = getCellWidth() * off * 3 + 1;
1282-
begin += getCellWidth() / 2;
1283-
width = getCellWidth() * 2;
1284-
textcursor.setPosition(off * factor);
1285-
} else {
1286-
begin = getCellWidth() * off + 1;
1287-
width = getCellWidth();
1288-
textcursor.setPosition(m_cursor->currentColumn());
1289-
}
1277+
begin = getCellWidth() * off * 3 + 1;
1278+
begin += getCellWidth() / 2;
1279+
width = getCellWidth() * 2;
1280+
textcursor.setPosition(off * Hex);
12901281

12911282
textcursor.movePosition(QTextCursor::NextCharacter);
12921283
auto charformat = textcursor.charFormat();
@@ -1343,7 +1334,7 @@ QRect QHexRenderer::byteRectAt(qreal cellWidth, int height,
13431334
return QRect(x, 0, w, height);
13441335
}
13451336

1346-
void QHexRenderer::drawBookmarkRect(QPainter *painter, qreal cellWidth,
1337+
void QHexRenderer::applyStrBookmark(QPainter *painter, qreal cellWidth,
13471338
int height, int byteIndex,
13481339
const QColor &fallbackColor) const {
13491340
QRect byteRect = byteRectAt(cellWidth, height, byteIndex);
@@ -1514,7 +1505,7 @@ void QHexRenderer::drawString(QPainter *painter, const QRect &linerect,
15141505
if (!hasBookmark(line, byteIndex)) {
15151506
continue;
15161507
}
1517-
drawBookmarkRect(painter, cellWidth, height, byteIndex,
1508+
applyStrBookmark(painter, cellWidth, height, byteIndex,
15181509
prepared.formats.at(i).foreground);
15191510
}
15201511

@@ -1531,7 +1522,7 @@ void QHexRenderer::drawString(QPainter *painter, const QRect &linerect,
15311522
if (!hasBookmark(line, byteIndex)) {
15321523
continue;
15331524
}
1534-
drawBookmarkRect(painter, cellWidth, height, byteIndex,
1525+
applyStrBookmark(painter, cellWidth, height, byteIndex,
15351526
prepared.formats.at(i).foreground);
15361527
}
15371528
}

3rdparty/QHexView/document/qhexrenderer.h

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -160,27 +160,32 @@ class QHexRenderer : public QObject {
160160
// modified by wingsummer
161161
enum Factor { String = 1, Hex = 4 };
162162

163+
private:
164+
void drawAddress(QPainter *painter, const QRect &linerect, qsizetype line);
165+
void drawHex(QPainter *painter, const QRect &linerect, qsizetype line);
166+
void drawString(QPainter *painter, const QRect &linerect, qsizetype line);
167+
void drawHeader(QPainter *painter);
168+
169+
private:
163170
void applyDocumentStyles(QPainter *painter,
164171
QTextDocument *textdocument) const;
165-
void applyBasicStyle(QTextCursor &textcursor, const QByteArray &rawline,
166-
Factor factor) const;
167-
void applyMetadata(QTextCursor &textcursor, qsizetype line,
168-
Factor factor) const;
169-
170-
void applySelection(QTextCursor &textcursor, qsizetype line,
171-
Factor factor) const;
172-
void applySelection(const QHexSelection &selection, QTextCursor &textcursor,
173-
qsizetype line, Factor factor, bool strikeOut,
174-
bool hasSelection) const;
175-
void applySelection(const QVector<QHexMetadata::MetaInfo> &metas,
176-
QTextCursor &textcursor, qsizetype startLine,
177-
qsizetype lineStart, qsizetype lineEnd, Factor factor,
178-
bool strikeOut, bool hasSelection) const;
179-
180-
// added by wingsummer
181-
void applyBookMark(QPainter *painter, QTextCursor &textcursor,
182-
qsizetype line, Factor factor);
172+
void applyHexBasicStyle(QTextCursor &textcursor,
173+
const QByteArray &rawline) const;
174+
void applyHexMetadata(QTextCursor &textcursor, qsizetype line) const;
175+
176+
void applyHexSelection(QTextCursor &textcursor, qsizetype line) const;
177+
void applyHexSelection(const QHexSelection &selection,
178+
QTextCursor &textcursor, qsizetype line,
179+
bool strikeOut, bool hasSelection) const;
180+
void applyHexSelection(const QVector<QHexMetadata::MetaInfo> &metas,
181+
QTextCursor &textcursor, qsizetype startLine,
182+
qsizetype lineStart, qsizetype lineEnd,
183+
bool strikeOut, bool hasSelection) const;
184+
void applyHexBookMark(QPainter *painter, QTextCursor &textcursor,
185+
qsizetype line);
186+
void applyCursorHex(QTextCursor &textcursor, qsizetype line) const;
183187

188+
private:
184189
QVector<AsciiCell> buildAsciiCells(const QByteArray &rawline) const;
185190
QVector<AsciiCell> buildAsciiCellsLatin1(const QByteArray &rawline) const;
186191
QVector<AsciiCell> buildAsciiCellsUtf8(const QByteArray &rawline) const;
@@ -192,14 +197,18 @@ class QHexRenderer : public QObject {
192197
bool useBom) const;
193198
AsciiCellFormat asciiCellFormat(qsizetype line, int column,
194199
uchar value) const;
200+
201+
private:
195202
bool selectionCoversByte(const QHexSelection &selection, qsizetype line,
196203
int column) const;
197204
bool
198205
asciiCellNeedsByteFallback(qsizetype line, const AsciiCell &cell,
199206
const QVector<AsciiCellFormat> &formats) const;
200-
bool isByteSelected(qsizetype line, int column, bool *strikeOut,
201-
bool *hasSelection) const;
207+
bool isStrByteSelected(qsizetype line, int column, bool *strikeOut,
208+
bool *hasSelection) const;
202209
bool hasBookmark(qsizetype line, int column) const;
210+
211+
private:
203212
static bool decodeUtf8At(const QByteArray &rawline, int index, int *length,
204213
char32_t *codepoint);
205214
static bool decodeUtf16At(const QByteArray &rawline, int index,
@@ -212,15 +221,11 @@ class QHexRenderer : public QObject {
212221
bool littleEndian);
213222
static quint32 readUInt32(const QByteArray &rawline, int index,
214223
bool littleEndian);
215-
void applyCursorHex(QTextCursor &textcursor, qsizetype line) const;
216-
void drawAddress(QPainter *painter, const QRect &linerect, qsizetype line);
217-
void drawHex(QPainter *painter, const QRect &linerect, qsizetype line);
218-
void drawString(QPainter *painter, const QRect &linerect, qsizetype line);
219-
void drawHeader(QPainter *painter);
220224

221225
private:
222226
QRect byteRectAt(qreal cellWidth, int height, int byteIndex) const;
223-
void drawBookmarkRect(QPainter *painter, qreal cellWidth, int height,
227+
228+
void applyStrBookmark(QPainter *painter, qreal cellWidth, int height,
224229
int byteIndex, const QColor &fallbackColor) const;
225230
void drawAsciiText(QPainter *painter, const QRect &rect,
226231
const QString &text, const AsciiCellFormat &fmt) const;

0 commit comments

Comments
 (0)