diff options
author | Peter Wu <peter@lekensteyn.nl> | 2016-03-31 02:06:47 +0200 |
---|---|---|
committer | Michal Labedzki <michal.labedzki@tieto.com> | 2016-04-18 16:41:20 +0000 |
commit | 68ec6735e10bca144120106f82a896eb99b46fa3 (patch) | |
tree | 6b8f201bf582deaabfc2c89f7cb032b5bdf48afa /ui/qt/byte_view_text.cpp | |
parent | abaa076f75f39135144787495eed9e6ebf2bc48a (diff) | |
download | wireshark-68ec6735e10bca144120106f82a896eb99b46fa3.tar.gz |
Qt: highlight hovered byte
Make it easier to link the hex with ascii bytes. The background color
will become yellow for bytes with the mouse focus.
Bug: 11547
Change-Id: Iab87e598a302ecf7bb7b37cd6ad55ea291c02b82
Reviewed-on: https://code.wireshark.org/review/14716
Petri-Dish: Michal Labedzki <michal.labedzki@tieto.com>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Michal Labedzki <michal.labedzki@tieto.com>
Diffstat (limited to 'ui/qt/byte_view_text.cpp')
-rw-r--r-- | ui/qt/byte_view_text.cpp | 58 |
1 files changed, 42 insertions, 16 deletions
diff --git a/ui/qt/byte_view_text.cpp b/ui/qt/byte_view_text.cpp index 58ad98d126..b0d2a10511 100644 --- a/ui/qt/byte_view_text.cpp +++ b/ui/qt/byte_view_text.cpp @@ -55,6 +55,7 @@ ByteViewText::ByteViewText(QWidget *parent, tvbuff_t *tvb, proto_tree *tree, QTr bold_highlight_(false), encoding_(encoding), format_actions_(new QActionGroup(this)), + hovered_byte_offset(-1), p_bound_(0, 0), f_bound_(0, 0), fa_bound_(0, 0), @@ -242,6 +243,7 @@ void ByteViewText::mousePressEvent (QMouseEvent *event) { void ByteViewText::mouseMoveEvent(QMouseEvent *event) { QString field_str; + // XXX can the event really be NULL? if (!event) { emit byteFieldHovered(field_str); p_bound_ = p_bound_save_; @@ -251,6 +253,7 @@ void ByteViewText::mouseMoveEvent(QMouseEvent *event) return; } QPoint pos = event->pos(); + hovered_byte_offset = byteOffsetAtPixel(pos); field_info *fi = fieldAtPixel(pos); if (fi) { if (fi->length < 2) { @@ -280,6 +283,7 @@ void ByteViewText::leaveEvent(QEvent *event) { QString empty; emit byteFieldHovered(empty); + hovered_byte_offset = -1; p_bound_ = p_bound_save_; f_bound_ = f_bound_save_; fa_bound_ = fa_bound_save_; @@ -323,6 +327,7 @@ void ByteViewText::drawOffsetLine(QPainter &painter, const guint offset, const i for (guint tvb_pos = offset; tvb_pos < max_pos; tvb_pos++) { highlight_state hex_state = StateNormal; bool add_space = tvb_pos != offset; + bool highlight_text = tvb_pos == hovered_byte_offset; if ((tvb_pos >= f_bound_.first && tvb_pos < f_bound_.second) || (tvb_pos >= fa_bound_.first && tvb_pos < fa_bound_.second)) { hex_state = StateField; @@ -331,15 +336,15 @@ void ByteViewText::drawOffsetLine(QPainter &painter, const guint offset, const i hex_state = StateProtocol; } - if (hex_state != state) { - if ((state == StateNormal || (state == StateProtocol && hex_state == StateField)) && add_space) { + if (hex_state != state || highlight_text) { + if ((state == StateNormal || (state == StateProtocol && hex_state == StateField) || highlight_text) && add_space) { add_space = false; text += ' '; /* insert a space every separator_interval_ bytes */ if ((tvb_pos % separator_interval_) == 0) text += ' '; } - hex_x += flushOffsetFragment(painter, hex_x, row_y, state, text); + hex_x += flushOffsetFragment(painter, hex_x, row_y, state, false, text); state = hex_state; } @@ -361,10 +366,13 @@ void ByteViewText::drawOffsetLine(QPainter &painter, const guint offset, const i text += (pd[tvb_pos] & (1 << j)) ? '1' : '0'; break; } + if (highlight_text) { + hex_x += flushOffsetFragment(painter, hex_x, row_y, state, true, text); + } } } if (text.length() > 0) { - flushOffsetFragment(painter, hex_x, row_y, state, text); + flushOffsetFragment(painter, hex_x, row_y, state, false, text); } state = StateNormal; @@ -373,6 +381,7 @@ void ByteViewText::drawOffsetLine(QPainter &painter, const guint offset, const i for (guint tvb_pos = offset; tvb_pos < max_pos; tvb_pos++) { highlight_state ascii_state = StateNormal; bool add_space = tvb_pos != offset; + bool highlight_text = tvb_pos == hovered_byte_offset; if ((tvb_pos >= f_bound_.first && tvb_pos < f_bound_.second) || (tvb_pos >= fa_bound_.first && tvb_pos < fa_bound_.second)) { ascii_state = StateField; @@ -381,14 +390,14 @@ void ByteViewText::drawOffsetLine(QPainter &painter, const guint offset, const i ascii_state = StateProtocol; } - if (ascii_state != state) { - if ((state == StateNormal || (state == StateProtocol && ascii_state == StateField)) && add_space) { + if (ascii_state != state || highlight_text) { + if ((state == StateNormal || (state == StateProtocol && ascii_state == StateField) || highlight_text) && add_space) { add_space = false; /* insert a space every separator_interval_ bytes */ if ((tvb_pos % separator_interval_) == 0) text += ' '; } - ascii_x += flushOffsetFragment(painter, ascii_x, row_y, state, text); + ascii_x += flushOffsetFragment(painter, ascii_x, row_y, state, false, text); state = ascii_state; } @@ -403,34 +412,38 @@ void ByteViewText::drawOffsetLine(QPainter &painter, const guint offset, const i pd[tvb_pos]; text += g_ascii_isprint(c) ? c : '.'; + if (highlight_text) { + ascii_x += flushOffsetFragment(painter, ascii_x, row_y, state, true, text); + } } } if (text.length() > 0) { - flushOffsetFragment(painter, ascii_x, row_y, state, text); + flushOffsetFragment(painter, ascii_x, row_y, state, false, text); } // Offset. Must be drawn last in order for offset_state to be set. if (show_offset_) { text = QString("%1").arg(offset, offsetChars(), 16, QChar('0')); - flushOffsetFragment(painter, margin_, row_y, offset_state, text); + flushOffsetFragment(painter, margin_, row_y, offset_state, false, text); } } // Draws a fragment of byte view text at the specifiec location using colors // for the specified state. Clears the text and returns the pixel width of the // drawn text. -qreal ByteViewText::flushOffsetFragment(QPainter &painter, qreal x, int y, highlight_state state, QString &text) +qreal ByteViewText::flushOffsetFragment(QPainter &painter, qreal x, int y, highlight_state state, gboolean extra_highlight, QString &text) { if (text.length() < 1) { return 0; } QFontMetricsF fm(mono_font_); qreal width = fm.width(text); + QRectF area(x, y, width, line_spacing_); // Background if (state == StateField) { - painter.fillRect(QRectF(x, y, width, line_spacing_), palette().highlight()); + painter.fillRect(area, palette().highlight()); } else if (state == StateProtocol) { - painter.fillRect(QRectF(x, y, width, line_spacing_), palette().window()); + painter.fillRect(area, palette().window()); } // Text @@ -452,8 +465,13 @@ qreal ByteViewText::flushOffsetFragment(QPainter &painter, qreal x, int y, highl break; } + if (extra_highlight) { + painter.fillRect(area, QColor("yellow")); + text_brush = QColor("blue"); + } + painter.setPen(QPen(text_brush.color())); - painter.drawText(QRectF(x, y, width, line_spacing_), Qt::AlignTop, text); + painter.drawText(area, Qt::AlignTop, text); text.clear(); return width; } @@ -516,21 +534,29 @@ void ByteViewText::updateScrollbars() horizontalScrollBar()->setRange(0, qMax(0, static_cast<int>((totalPixels() - viewport()->width()) / font_width_))); } -field_info *ByteViewText::fieldAtPixel(QPoint &pos) +int ByteViewText::byteOffsetAtPixel(QPoint &pos) { int byte = (verticalScrollBar()->value() + (pos.y() / line_spacing_)) * row_width_; int x = (horizontalScrollBar()->value() * font_width_) + pos.x(); int col = x_pos_to_column_.value(x, -1); if (col < 0) { - return NULL; + return -1; } byte += col; if ((guint) byte > tvb_captured_length(tvb_)) { - return NULL; + return -1; } + return byte; +} +field_info *ByteViewText::fieldAtPixel(QPoint &pos) +{ + int byte = byteOffsetAtPixel(pos); + if (byte < 0) { + return NULL; + } return proto_find_field_from_offset(proto_tree_, byte, tvb_); } |