Qt: Fix packet bytes hover rectangle position

For Qt 5.11 and newer use horizontalAdvance() instead of boundingRect().width()
to calculate the width of a QString to position the hover rectangle position,
and to select which byte(s) to highlight.

Closes #17033.

(cherry picked from commit cb3b469d7f)
This commit is contained in:
Stig Bjørlykke 2020-11-30 12:31:55 +01:00 committed by AndersBroman
parent 3cd179d007
commit d5ea403f8d
2 changed files with 16 additions and 6 deletions

View File

@ -341,6 +341,15 @@ void ByteViewText::contextMenuEvent(QContextMenuEvent *event)
const int ByteViewText::separator_interval_ = DataPrinter::separatorInterval();
int ByteViewText::stringWidth(const QString &line)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
return fontMetrics().horizontalAdvance(line);
#else
return fontMetrics().boundingRect(line).width();
#endif
}
// Draw a line of byte view text for a given offset.
// Text highlighting is handled using QTextLayout::FormatRange.
void ByteViewText::drawLine(QPainter *painter, const int offset, const int row_y)
@ -366,7 +375,7 @@ void ByteViewText::drawLine(QPainter *painter, const int offset, const int row_y
if (show_offset_) {
line = QString(" %1 ").arg(offset, offsetChars(false), 16, QChar('0'));
if (build_x_pos) {
x_pos_to_column_.fill(-1, fontMetrics().boundingRect(line).width());
x_pos_to_column_.fill(-1, stringWidth(line));
}
}
@ -401,19 +410,19 @@ void ByteViewText::drawLine(QPainter *painter, const int offset, const int row_y
break;
}
if (build_x_pos) {
x_pos_to_column_ += QVector<int>().fill(tvb_pos - offset, fontMetrics().boundingRect(line).width() - x_pos_to_column_.size() + slop);
x_pos_to_column_ += QVector<int>().fill(tvb_pos - offset, stringWidth(line) - x_pos_to_column_.size() + slop);
}
if (tvb_pos == hovered_byte_offset_ || tvb_pos == marked_byte_offset_) {
int ho_len = recent.gui_bytes_view == BYTES_HEX ? 2 : 8;
QRect ho_rect = painter->boundingRect(QRect(), Qt::AlignHCenter|Qt::AlignVCenter, line.right(ho_len));
ho_rect.moveRight(fontMetrics().boundingRect(line).width());
ho_rect.moveRight(stringWidth(line));
ho_rect.moveTop(row_y);
hover_outlines_.append(ho_rect);
}
}
line += QString(ascii_start - line.length(), ' ');
if (build_x_pos) {
x_pos_to_column_ += QVector<int>().fill(-1, fontMetrics().boundingRect(line).width() - x_pos_to_column_.size());
x_pos_to_column_ += QVector<int>().fill(-1, stringWidth(line) - x_pos_to_column_.size());
}
addHexFormatRange(fmt_list, proto_start_, proto_len_, offset, max_tvb_pos, ModeProtocol);
@ -462,11 +471,11 @@ void ByteViewText::drawLine(QPainter *painter, const int offset, const int row_y
}
}
if (build_x_pos) {
x_pos_to_column_ += QVector<int>().fill(tvb_pos - offset, fontMetrics().boundingRect(line).width() - x_pos_to_column_.size());
x_pos_to_column_ += QVector<int>().fill(tvb_pos - offset, stringWidth(line) - x_pos_to_column_.size());
}
if (tvb_pos == hovered_byte_offset_ || tvb_pos == marked_byte_offset_) {
QRect ho_rect = painter->boundingRect(QRect(), 0, line.right(1));
ho_rect.moveRight(fontMetrics().boundingRect(line).width());
ho_rect.moveRight(stringWidth(line));
ho_rect.moveTop(row_y);
hover_outlines_.append(ho_rect);
}

View File

@ -77,6 +77,7 @@ private:
QTextLayout *layout_;
const QByteArray data_;
int stringWidth(const QString &line);
void drawLine(QPainter *painter, const int offset, const int row_y);
bool addFormatRange(QList<QTextLayout::FormatRange> &fmt_list, int start, int length, HighlightMode mode);
bool addHexFormatRange(QList<QTextLayout::FormatRange> &fmt_list, int mark_start, int mark_length, int tvb_offset, int max_tvb_pos, HighlightMode mode);