@@ -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
377378int QHexRenderer::getNCellsWidth (int n) const {
378- return qRound (n * getCellWidth ());
379+ return qCeil (n * getCellWidth ());
379380}
380381
381382void 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-
11781166void 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+
14461443void 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));
0 commit comments