Qt: Fetch byte view text font metrics more often.

Add ByteViewText::updateLayoutMetrics, which fetches the character width
and line height.  Call it whenever our font changes and when we're about
to paint. Blind attempt at fixing #15819.
This commit is contained in:
Gerald Combs 2020-11-12 13:37:56 -08:00
parent 580de09849
commit 96eec0beb9
2 changed files with 14 additions and 5 deletions

View File

@ -32,6 +32,9 @@
// - Add recent settings and context menu items to show/hide the offset.
// - Add a UTF-8 and possibly UTF-xx option to the ASCII display.
// - Move more common metrics to DataPrinter.
// - Pre-draw all of our characters and paint our display using pixmap
// copying? That would make this behave like a terminal screen, which
// is what we ultimately want.
Q_DECLARE_METATYPE(bytes_view_type)
Q_DECLARE_METATYPE(bytes_encoding_type)
@ -183,15 +186,11 @@ void ByteViewText::setMonospaceFont(const QFont &mono_font)
QFont int_font(mono_font);
int_font.setStyleStrategy(QFont::ForceIntegerMetrics);
const QFontMetricsF fm(int_font);
font_width_ = fm.width('M');
setFont(int_font);
viewport()->setFont(int_font);
layout_->setFont(int_font);
// We should probably use ProtoTree::rowHeight.
line_height_ = fontMetrics().height();
updateLayoutMetrics();
updateScrollbars();
viewport()->update();
@ -208,6 +207,8 @@ void ByteViewText::updateByteViewSettings()
void ByteViewText::paintEvent(QPaintEvent *)
{
updateLayoutMetrics();
QPainter painter(viewport());
painter.translate(-horizontalScrollBar()->value() * font_width_, 0);
@ -342,6 +343,13 @@ void ByteViewText::contextMenuEvent(QContextMenuEvent *event)
const int ByteViewText::separator_interval_ = DataPrinter::separatorInterval();
void ByteViewText::updateLayoutMetrics()
{
font_width_ = fontMetrics().boundingRect('M').width();
// We might want to match ProtoTree::rowHeight.
line_height_ = fontMetrics().height();
}
// 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)

View File

@ -77,6 +77,7 @@ private:
QTextLayout *layout_;
const QByteArray data_;
void updateLayoutMetrics();
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);