Skip to content

Commit 16bbf23

Browse files
committed
feat: 用户体验优化;
1 parent 8f605e5 commit 16bbf23

13 files changed

Lines changed: 1153 additions & 1098 deletions

File tree

3rdparty/QHexView/document/qhexrenderer.cpp

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ QHexRenderer::QHexRenderer(QHexDocument *document, QHexCursor *cursor,
114114
m_asciiVisible = true;
115115
m_addressVisible = true;
116116
m_headerVisible = true;
117+
m_cursorSync = true;
117118

118119
/*===================================*/
119120
}
@@ -375,7 +376,7 @@ qreal QHexRenderer::getCellWidth() const {
375376
}
376377

377378
int QHexRenderer::getNCellsWidth(int n) const {
378-
return qRound(n * getCellWidth());
379+
return qCeil(n * getCellWidth());
379380
}
380381

381382
void QHexRenderer::unprintableChars(QByteArray &ascii) const {
@@ -1104,16 +1105,28 @@ QHexRenderer::asciiCellFormat(qsizetype line, int column, uchar value) const {
11041105

11051106
if (line == m_cursor->currentLine() &&
11061107
column == m_cursor->currentColumn() && m_cursorenabled) {
1107-
if ((m_cursor->insertionMode() == QHexCursor::OverwriteMode) ||
1108-
(m_selectedarea != Areas::AsciiArea)) {
1109-
fmt.foreground = m_bytesBackground;
1110-
fmt.background = (m_selectedarea == Areas::AsciiArea)
1111-
? m_bytesColor
1112-
: m_bytesColor.lighter(250);
1108+
if (m_cursorSync || m_selectedarea == Areas::AsciiArea) {
1109+
if (m_selectedarea == Areas::HexArea) {
1110+
if (m_bytesBackground.lightnessF() < 0.5) {
1111+
fmt.foreground = m_bytesBackground.lighter(95);
1112+
} else {
1113+
fmt.foreground = m_bytesBackground.darker(125);
1114+
}
1115+
if (m_bytesColor.lightnessF() < 0.5) {
1116+
fmt.background = m_bytesColor.lighter(95);
1117+
} else {
1118+
fmt.background = m_bytesColor.darker(125);
1119+
}
1120+
} else {
1121+
fmt.foreground = m_bytesBackground;
1122+
fmt.background = m_bytesColor;
1123+
}
11131124
fmt.fillBackground = true;
1114-
fmt.underline = false;
1115-
} else {
1116-
fmt.underline = true;
1125+
if (m_cursor->insertionMode() == QHexCursor::OverwriteMode) {
1126+
fmt.underline = false;
1127+
} else {
1128+
fmt.underline = true;
1129+
}
11171130
}
11181131
}
11191132

@@ -1150,68 +1163,46 @@ bool QHexRenderer::asciiCellNeedsByteFallback(
11501163
return false;
11511164
}
11521165

1153-
void QHexRenderer::applyCursorAscii(QTextCursor &textcursor,
1154-
qsizetype line) const {
1155-
if ((line != m_cursor->currentLine()) || !m_cursorenabled)
1156-
return;
1157-
1158-
textcursor.clearSelection();
1159-
textcursor.setPosition(m_cursor->currentColumn());
1160-
textcursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
1161-
1162-
QTextCharFormat charformat;
1163-
1164-
if ((m_cursor->insertionMode() == QHexCursor::OverwriteMode) ||
1165-
(m_selectedarea != Areas::AsciiArea)) {
1166-
charformat.setForeground(m_bytesBackground);
1167-
if (m_selectedarea == Areas::AsciiArea)
1168-
charformat.setBackground(m_bytesColor);
1169-
else
1170-
charformat.setBackground(m_bytesColor.lighter(250));
1171-
} else
1172-
charformat.setUnderlineStyle(
1173-
QTextCharFormat::UnderlineStyle::SingleUnderline);
1174-
1175-
textcursor.mergeCharFormat(charformat);
1176-
}
1177-
11781166
void QHexRenderer::applyCursorHex(QTextCursor &textcursor,
11791167
qsizetype line) const {
1180-
if ((line != m_cursor->currentLine()) || !m_cursorenabled)
1168+
if ((line != m_cursor->currentLine()) || !m_cursorenabled ||
1169+
(!m_cursorSync && m_selectedarea != Areas::HexArea))
11811170
return;
11821171

11831172
textcursor.clearSelection();
11841173

11851174
auto col = m_cursor->currentColumn();
11861175
textcursor.setPosition(col * Factor::Hex);
11871176

1188-
if (m_selectedarea == Areas::HexArea) {
1189-
if (m_cursor->currentNibble()) {
1190-
textcursor.movePosition(QTextCursor::Right,
1191-
QTextCursor::MoveAnchor);
1192-
} else {
1193-
textcursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor,
1194-
2);
1195-
}
1177+
if (m_cursor->currentNibble()) {
1178+
textcursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor);
1179+
} else {
1180+
textcursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 2);
11961181
}
11971182

11981183
textcursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
11991184

1200-
if (m_selectedarea == Areas::AsciiArea)
1201-
textcursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
12021185
QTextCharFormat charformat;
12031186

1204-
if ((m_cursor->insertionMode() == QHexCursor::OverwriteMode) ||
1205-
(m_selectedarea != Areas::HexArea)) {
1187+
if (m_selectedarea == Areas::AsciiArea) {
1188+
if (m_bytesBackground.lightnessF() < 0.5) {
1189+
charformat.setForeground(m_bytesBackground.lighter(95));
1190+
} else {
1191+
charformat.setForeground(m_bytesBackground.darker(125));
1192+
}
1193+
if (m_bytesColor.lightnessF() < 0.5) {
1194+
charformat.setBackground(m_bytesColor.lighter(95));
1195+
} else {
1196+
charformat.setBackground(m_bytesColor.darker(125));
1197+
}
1198+
} else {
12061199
charformat.setForeground(m_bytesBackground);
1207-
if (m_selectedarea == Areas::HexArea)
1208-
charformat.setBackground(m_bytesColor);
1209-
else
1210-
charformat.setBackground(m_bytesColor.lighter(250));
1211-
} else
1200+
charformat.setBackground(m_bytesColor);
1201+
}
1202+
if (m_cursor->insertionMode() == QHexCursor::InsertMode) {
12121203
charformat.setUnderlineStyle(
12131204
QTextCharFormat::UnderlineStyle::SingleUnderline);
1214-
1205+
}
12151206
textcursor.setCharFormat(charformat);
12161207
}
12171208

@@ -1443,6 +1434,12 @@ void QHexRenderer::drawAsciiText(QPainter *painter, const QRect &rect,
14431434
painter->restore();
14441435
}
14451436

1437+
bool QHexRenderer::cursorSync() const { return m_cursorSync; }
1438+
1439+
void QHexRenderer::setCursorSync(bool newCursorSync) {
1440+
m_cursorSync = newCursorSync;
1441+
}
1442+
14461443
void QHexRenderer::drawString(QPainter *painter, const QRect &linerect,
14471444
qsizetype line) {
14481445
const QByteArray rawline = this->getLine(line);
@@ -1453,7 +1450,6 @@ void QHexRenderer::drawString(QPainter *painter, const QRect &linerect,
14531450

14541451
painter->save();
14551452
painter->translate(asciirect.topLeft());
1456-
painter->setClipRect(0, 0, asciirect.width(), asciirect.height());
14571453

14581454
const auto cells = buildAsciiCells(rawline);
14591455
const qreal cellWidth = getCellWidth();
@@ -1563,7 +1559,7 @@ void QHexRenderer::drawString(QPainter *painter, const QRect &linerect,
15631559
}
15641560

15651561
const int startX = qRound(start * cellWidth);
1566-
const int endX = qRound((end + 1) * cellWidth) - 1;
1562+
const int endX = qRound((end + 1) * cellWidth);
15671563

15681564
painter->save();
15691565
painter->setPen(QPen(color, 1, Qt::SolidLine));

3rdparty/QHexView/document/qhexrenderer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ class QHexRenderer : public QObject {
113113
QColor borderColor() const;
114114
void setBorderColor(const QColor &newBorderColor);
115115

116+
bool cursorSync() const;
117+
void setCursorSync(bool newCursorSync);
118+
116119
QHexCursor *cursor() const;
117120
void setCursor(QHexCursor *newCursor);
118121
bool asciiCellAt(qsizetype line, int column, int *start, int *length) const;
@@ -209,7 +212,6 @@ class QHexRenderer : public QObject {
209212
bool littleEndian);
210213
static quint32 readUInt32(const QByteArray &rawline, int index,
211214
bool littleEndian);
212-
void applyCursorAscii(QTextCursor &textcursor, qsizetype line) const;
213215
void applyCursorHex(QTextCursor &textcursor, qsizetype line) const;
214216
void drawAddress(QPainter *painter, const QRect &linerect, qsizetype line);
215217
void drawHex(QPainter *painter, const QRect &linerect, qsizetype line);
@@ -239,6 +241,7 @@ class QHexRenderer : public QObject {
239241
bool m_asciiVisible;
240242
bool m_addressVisible;
241243
bool m_headerVisible;
244+
bool m_cursorSync;
242245
QStringConverter::Encoding m_stringEncoding =
243246
QStringConverter::Encoding::Latin1;
244247

3rdparty/QHexView/qhexview.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,10 @@ void QHexView::setDocument(const QSharedPointer<QHexDocument> &document,
235235
this->viewport()->update();
236236
Q_EMIT cursorSelectionChanged();
237237
});
238-
connect(m_cursor, &QHexCursor::insertionModeChanged, this,
239-
&QHexView::renderCurrentLine);
238+
connect(m_cursor, &QHexCursor::insertionModeChanged, this, [this]() {
239+
renderCurrentLine();
240+
Q_EMIT insertionModeChanged();
241+
});
240242

241243
this->adjustScrollBars();
242244
this->viewport()->update();
@@ -333,12 +335,18 @@ QPoint QHexView::absolutePosition(const QPoint &pos) const {
333335

334336
QHexCursor *QHexView::cursor() const { return m_cursor; }
335337

338+
bool QHexView::cursorSync() const { return m_renderer->cursorSync(); }
339+
336340
qsizetype QHexView::copyLimit() const { return m_copylimit; }
337341

338342
void QHexView::setCopyLimit(qsizetype newCopylimit) {
339343
m_copylimit = newCopylimit;
340344
}
341345

346+
void QHexView::setCursorSync(bool newCursorSync) {
347+
m_renderer->setCursorSync(newCursorSync);
348+
}
349+
342350
qreal QHexView::scaleRate() const { return m_scaleRate; }
343351

344352
qsizetype QHexView::findNext(qsizetype begin, const QByteArray &ba) {
@@ -1019,9 +1027,7 @@ bool QHexView::processAction(QHexCursor *cur, QKeyEvent *e) {
10191027
}
10201028

10211029
} else if (e->key() == Qt::Key_Insert) {
1022-
if (!isKeepSize()) {
1023-
cur->switchInsertionMode();
1024-
}
1030+
cur->switchInsertionMode();
10251031
} else
10261032
return false;
10271033

3rdparty/QHexView/qhexview.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class QHexView : public QAbstractScrollArea {
105105

106106
qsizetype copyLimit() const;
107107
QHexCursor *cursor() const;
108+
bool cursorSync() const;
108109

109110
public slots:
110111
void setDocument(const QSharedPointer<QHexDocument> &document,
@@ -146,6 +147,7 @@ public slots:
146147
void Replace(qsizetype offset, const QByteArray &data, int nibbleindex = 0);
147148

148149
void setCopyLimit(qsizetype newCopylimit);
150+
void setCursorSync(bool newCursorSync);
149151

150152
private:
151153
void establishSignal(QHexDocument *doc);
@@ -159,6 +161,7 @@ public slots:
159161
void documentLockedFile(bool locked);
160162
void documentKeepSize(bool keep);
161163
void editableAreaClicked(QHexRenderer::Areas area);
164+
void insertionModeChanged();
162165

163166
void metafgVisibleChanged(bool b);
164167
void metabgVisibleChanged(bool b);

0 commit comments

Comments
 (0)