| Wireshark 4.7.3 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
hex_data_source_view.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <config.h>
13
14#include "ui/recent.h"
15
16#include <QAbstractScrollArea>
17#include <QFont>
18#include <QColor>
19#include <QVector>
20#include <QMenu>
21#include <QSize>
22#include <QString>
23#include <QTextLayout>
24#include <QVector>
25
26#include <limits>
27
28#include "base_data_source_view.h"
29
30#include <ui/qt/utils/data_printer.h>
31#include <ui/qt/utils/idata_printable.h>
32
33// XXX - Is there any reason we shouldn't add ImageDataSourceView, etc?
34
38 class HexDataSourceView : public BaseDataSourceView, public IDataPrintable
39{
40 Q_OBJECT
41 Q_INTERFACES(IDataPrintable)
42
43 public:
47 struct ByteViewAnnotation {
48 int start;
49 int length;
50 QColor color;
51 QString comment;
52 };
53
62 explicit HexDataSourceView(const QByteArray &data,
63 packet_char_enc encoding = PACKET_CHAR_ENC_CHAR_ASCII,
64 QWidget *parent = nullptr);
65
67 ~HexDataSourceView();
68
74 void setFormat(bytes_view_type format);
75
80 void setAnnotations(const QVector<ByteViewAnnotation> &annotations);
81
82 // ── Selection accessors ───────────────────────────────────────────────
83
91 bool selectionRange(int *start, int *length) const;
92
101 int selectionAnchor() const;
102
107 int selectionEnd() const;
108
118 int contextByteOffset() const;
119
124 int dataSize() const {
125 Q_ASSERT(data_.size() <= std::numeric_limits<int>::max());
126 return static_cast<int>(data_.size());
127 }
128
129 // ── Offset marker accessors ───────────────────────────────────────────
130
135 int offsetStart() const { return offset_start_byte_; }
136
141 int offsetEnd() const { return offset_end_byte_; }
142
147 void setOffsetStart(int byte);
148
153 void setOffsetEnd(int byte);
154
158 void clearOffsetMarkers();
159
160 // ── Selected-field metadata accessors ────────────────────────────────
161
163 int selectedFieldStart() const { return field_start_; }
164
166 int selectedFieldLength() const { return field_len_; }
167
169 int selectedProtocolStart() const { return proto_start_; }
170
172 int selectedProtocolLength() const { return proto_len_; }
173
179 bool selectedFieldIsProtocol() const { return selected_field_is_protocol_; }
180
185 bool selectedFieldUsesOwnRange() const { return selected_field_use_own_range_; }
186
191 void setSelectedFieldIsProtocol(bool is_protocol) {
192 selected_field_is_protocol_ = is_protocol;
193 }
194
199 void setSelectedFieldUsesOwnRange(bool use_own_range) {
200 selected_field_use_own_range_ = use_own_range;
201 }
202
203signals:
210 void byteViewSettingsChanged();
211
213 void addAnnotationRequested();
214
216 void editAnnotationRequested();
217
219 void removeAnnotationRequested();
220
225 void offsetStartRequested(int byte);
226
231 void offsetEndRequested(int byte);
232
234 void offsetMarkersCleared();
235
236 public slots:
241 void setMonospaceFont(const QFont &mono_font) override;
242
246 void updateByteViewSettings();
247
254 void markProtocol(int start, int length) override;
255
264 void markField(int start, int length, bool scroll_to = true, bool hover = false) override;
265
272 void markAppendix(int start, int length) override;
273
277 void unmarkField() override;
278
279 protected:
285 virtual void paintEvent(QPaintEvent *event) override;
286
291 virtual void resizeEvent(QResizeEvent *event) override;
292
298 virtual void showEvent(QShowEvent *event) override;
299
305 virtual void mousePressEvent(QMouseEvent *event) override;
306
312 virtual void mouseMoveEvent(QMouseEvent *event) override;
313
318 virtual void mouseReleaseEvent(QMouseEvent *event) override;
319
324 virtual void leaveEvent(QEvent *event) override;
325
331 virtual void contextMenuEvent(QContextMenuEvent *event) override;
332
338 virtual void keyPressEvent(QKeyEvent *event) override;
339
340 private:
344 typedef enum {
345 ModeNormal,
346 ModeField,
347 ModeProtocol,
348 ModeOffsetNormal,
349 ModeOffsetField,
350 ModeNonPrintable,
351 ModeHover
352 } HighlightMode;
353
354 QTextLayout *layout_;
355
359 void updateLayoutMetrics();
360
366 int stringWidth(const QString &line);
367
375 void drawLine(QPainter *painter, const int offset, const int row_y);
376
386 bool addFormatRange(QList<QTextLayout::FormatRange> &fmt_list,
387 int start, int length, HighlightMode mode);
388
400 bool addHexFormatRange(QList<QTextLayout::FormatRange> &fmt_list,
401 int mark_start, int mark_length,
402 int tvb_offset, int max_tvb_pos,
403 HighlightMode mode);
404
416 bool addAsciiFormatRange(QList<QTextLayout::FormatRange> &fmt_list,
417 int mark_start, int mark_length,
418 int tvb_offset, int max_tvb_pos,
419 HighlightMode mode);
420
433 bool addHexCustomRange(QList<QTextLayout::FormatRange> &fmt_list,
434 int mark_start, int mark_length,
435 int tvb_offset, int max_tvb_pos,
436 const QColor &bg, const QColor &fg);
437
450 bool addAsciiCustomRange(QList<QTextLayout::FormatRange> &fmt_list,
451 int mark_start, int mark_length,
452 int tvb_offset, int max_tvb_pos,
453 const QColor &bg, const QColor &fg);
454
461 int annotationIndexAt(int byte_offset) const;
462
470 int annotationIndexIntersecting(int start, int length) const;
471
479 void updateSelection(int byte_offset, bool extend, bool emit_signal);
480
487 void updateAnnotationToolTip(int byte_offset, const QPoint &global_pos);
488
494 void scrollToByte(int byte);
495
499 void updateScrollbars();
500
510 int byteOffsetAtPixel(QPoint pos, bool allow_fuzzy = false);
511
515 void createContextMenu();
516
520 void updateContextMenu();
521
522 // ── Column layout helpers ─────────────────────────────────────────────
523
529 int offsetChars(bool include_pad = true);
530
532 int offsetPixels();
533
535 int hexPixels();
536
538 int asciiPixels();
539
541 int totalPixels();
542
547 const QByteArray printableData() override { return data_; }
548
549 // ── Constants ─────────────────────────────────────────────────────────
550
552 static const int separator_interval_;
553
554 // ── State ─────────────────────────────────────────────────────────────
555 bool layout_dirty_;
556
557 // Colors
558 QColor offset_normal_fg_;
559 QColor offset_field_fg_;
560
561 // Data
562 packet_char_enc encoding_;
563 QMenu ctx_menu_;
564
565 // Highlight ranges
566 int hovered_byte_offset_;
567 int proto_start_;
568 int proto_len_;
569 int field_start_;
570 int field_len_;
571 int field_a_start_;
572 int field_a_len_;
573 int field_hover_start_;
574 int field_hover_len_;
575
576 // Display settings
577 bool show_offset_;
578 bool show_hex_;
579 bool show_ascii_;
580 int row_width_;
581 int em_width_;
582 int line_height_;
583
585 QList<QRect> hover_outlines_;
586
587 bool allow_hover_selection_;
588
589 QVector<ByteViewAnnotation> annotations_;
590
591 // Selection state
592 int selection_anchor_;
593 int selection_start_;
594 int selection_end_;
595 bool selecting_;
596 int context_byte_offset_;
597 int cursor_byte_;
598 int hovered_annotation_index_;
599 int offset_start_byte_;
600 int offset_end_byte_;
601 bool selected_field_is_protocol_;
602 bool selected_field_use_own_range_;
603
612 QVector<int> x_pos_to_column_;
613
614 // Context menu actions
615 QAction *action_allow_hover_selection_;
616 QAction *action_add_annotation_;
617 QAction *action_edit_annotation_;
618 QAction *action_remove_annotation_;
619 QAction *action_set_offset_start_;
620 QAction *action_set_offset_end_;
621 QAction *action_clear_offset_markers_;
622 QAction *action_bytes_hex_;
623 QAction *action_bytes_dec_;
624 QAction *action_bytes_oct_;
625 QAction *action_bytes_bits_;
626 QAction *action_bytes_enc_from_packet_;
627 QAction *action_bytes_enc_ascii_;
628 QAction *action_bytes_enc_ebcdic_;
629
630 private slots:
635 void copyBytes(bool unused);
636
641 void setHexDisplayFormat(QAction *action);
642
647 void setCharacterEncoding(QAction *action);
648
653 void toggleHoverAllowed(bool allowed);
654
656 void requestAddAnnotation();
657
659 void requestEditAnnotation();
660
662 void requestRemoveAnnotation();
663
665 void requestSetOffsetStart();
666
668 void requestSetOffsetEnd();
669
671 void requestClearOffsetMarkers();
672};
base_data_source_view.h
BaseDataSourceView::data_
QByteArray data_
The raw packet bytes displayed by this view.
Definition base_data_source_view.h:141
BaseDataSourceView::BaseDataSourceView
BaseDataSourceView(const QByteArray &data, QWidget *parent=nullptr)
Construct a BaseDataSourceView.
Definition base_data_source_view.h:31
HexDataSourceView::mousePressEvent
virtual void mousePressEvent(QMouseEvent *event) override
Begin a byte selection or move the cursor on mouse press.
Definition hex_data_source_view.cpp:532
HexDataSourceView::markAppendix
void markAppendix(int start, int length) override
Highlight the appendix (trailing) bytes of the selected field.
Definition hex_data_source_view.cpp:393
HexDataSourceView::setMonospaceFont
void setMonospaceFont(const QFont &mono_font) override
Update the monospace font and recalculate layout metrics.
Definition hex_data_source_view.cpp:413
HexDataSourceView::selectionEnd
int selectionEnd() const
Return the byte offset at the end of the current selection.
Definition hex_data_source_view.cpp:153
HexDataSourceView::leaveEvent
virtual void leaveEvent(QEvent *event) override
Clear hover highlights when the pointer leaves the widget.
Definition hex_data_source_view.cpp:598
HexDataSourceView::selectedFieldIsProtocol
bool selectedFieldIsProtocol() const
Return whether the selected field is itself a protocol layer.
Definition hex_data_source_view.h:179
HexDataSourceView::showEvent
virtual void showEvent(QShowEvent *event) override
Ensure the layout is up to date when the widget first becomes visible.
Definition hex_data_source_view.cpp:522
HexDataSourceView::setFormat
void setFormat(bytes_view_type format)
Set the numeric base used to render hex-panel byte values.
HexDataSourceView::contextMenuEvent
virtual void contextMenuEvent(QContextMenuEvent *event) override
Show the byte-view context menu.
Definition hex_data_source_view.cpp:611
HexDataSourceView::addAnnotationRequested
void addAnnotationRequested()
Emitted when the user triggers "Add annotation" from the context menu.
HexDataSourceView::offsetEnd
int offsetEnd() const
Return the current offset-end marker byte position.
Definition hex_data_source_view.h:141
HexDataSourceView::offsetMarkersCleared
void offsetMarkersCleared()
Emitted when the user requests that both offset markers be cleared.
HexDataSourceView::byteViewSettingsChanged
void byteViewSettingsChanged()
Emitted when any byte-view display setting changes.
HexDataSourceView::selectedProtocolStart
int selectedProtocolStart() const
Definition hex_data_source_view.h:169
HexDataSourceView::updateByteViewSettings
void updateByteViewSettings()
Reload byte-view display preferences and repaint.
Definition hex_data_source_view.cpp:430
HexDataSourceView::dataSize
int dataSize() const
Return the number of bytes in the data buffer.
Definition hex_data_source_view.h:124
HexDataSourceView::selectionAnchor
int selectionAnchor() const
Return the selection anchor byte offset.
Definition hex_data_source_view.cpp:148
HexDataSourceView::removeAnnotationRequested
void removeAnnotationRequested()
Emitted when the user triggers "Remove annotation" from the context menu.
HexDataSourceView::paintEvent
virtual void paintEvent(QPaintEvent *event) override
Paint all visible rows of the hex dump.
Definition hex_data_source_view.cpp:439
HexDataSourceView::clearOffsetMarkers
void clearOffsetMarkers()
Clear both offset markers and repaint.
Definition hex_data_source_view.cpp:184
HexDataSourceView::offsetEndRequested
void offsetEndRequested(int byte)
Emitted when the user requests a new offset-end marker.
HexDataSourceView::editAnnotationRequested
void editAnnotationRequested()
Emitted when the user triggers "Edit annotation" from the context menu.
HexDataSourceView::HexDataSourceView
HexDataSourceView(const QByteArray &data, packet_char_enc encoding=PACKET_CHAR_ENC_CHAR_ASCII, QWidget *parent=nullptr)
Construct a HexDataSourceView.
Definition hex_data_source_view.cpp:61
HexDataSourceView::resizeEvent
virtual void resizeEvent(QResizeEvent *event) override
Recalculate scrollbar ranges and repaint after a resize.
Definition hex_data_source_view.cpp:517
HexDataSourceView::selectedFieldStart
int selectedFieldStart() const
Definition hex_data_source_view.h:163
HexDataSourceView::selectedFieldUsesOwnRange
bool selectedFieldUsesOwnRange() const
Return whether the selected field defines its own byte range.
Definition hex_data_source_view.h:185
HexDataSourceView::~HexDataSourceView
~HexDataSourceView()
Destroy the view and its QTextLayout.
Definition hex_data_source_view.cpp:119
HexDataSourceView::offsetStart
int offsetStart() const
Return the current offset-start marker byte position.
Definition hex_data_source_view.h:135
HexDataSourceView::markProtocol
void markProtocol(int start, int length) override
Highlight the enclosing protocol layer byte range.
Definition hex_data_source_view.cpp:371
HexDataSourceView::contextByteOffset
int contextByteOffset() const
Return the byte offset at which the context menu was invoked.
Definition hex_data_source_view.cpp:158
HexDataSourceView::selectedProtocolLength
int selectedProtocolLength() const
Definition hex_data_source_view.h:172
HexDataSourceView::selectedFieldLength
int selectedFieldLength() const
Definition hex_data_source_view.h:166
HexDataSourceView::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *event) override
Finalise a drag selection on mouse release.
Definition hex_data_source_view.cpp:584
HexDataSourceView::unmarkField
void unmarkField() override
Clear field, appendix, and hover highlights.
Definition hex_data_source_view.cpp:400
HexDataSourceView::selectionRange
bool selectionRange(int *start, int *length) const
Return the current byte selection range.
Definition hex_data_source_view.cpp:132
HexDataSourceView::keyPressEvent
virtual void keyPressEvent(QKeyEvent *event) override
Handle keyboard navigation and copy commands.
Definition hex_data_source_view.cpp:621
HexDataSourceView::setOffsetEnd
void setOffsetEnd(int byte)
Set the offset-end marker to the given byte.
Definition hex_data_source_view.cpp:172
HexDataSourceView::setSelectedFieldIsProtocol
void setSelectedFieldIsProtocol(bool is_protocol)
Set whether the selected field is a protocol-level item.
Definition hex_data_source_view.h:191
HexDataSourceView::setSelectedFieldUsesOwnRange
void setSelectedFieldUsesOwnRange(bool use_own_range)
Set whether the selected field uses its own independent range.
Definition hex_data_source_view.h:199
HexDataSourceView::setAnnotations
void setAnnotations(const QVector< ByteViewAnnotation > &annotations)
Replace the current set of user annotations.
Definition hex_data_source_view.cpp:125
HexDataSourceView::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *event) override
Extend the selection during a mouse drag.
Definition hex_data_source_view.cpp:554
HexDataSourceView::offsetStartRequested
void offsetStartRequested(int byte)
Emitted when the user requests a new offset-start marker.
HexDataSourceView::markField
void markField(int start, int length, bool scroll_to=true, bool hover=false) override
Highlight a specific dissected field byte range.
Definition hex_data_source_view.cpp:378
HexDataSourceView::setOffsetStart
void setOffsetStart(int byte)
Set the offset-start marker to the given byte.
Definition hex_data_source_view.cpp:163
IDataPrintable
An interface for objects that can provide a printable data representation.
Definition idata_printable.h:26
IDataPrintable::printableData
virtual const QByteArray printableData()=0
Retrieves the printable data representation of the object.
data_printer.h
packet_char_enc
packet_char_enc
Types of character encodings.
Definition frame_data.h:39
PACKET_CHAR_ENC_CHAR_ASCII
@ PACKET_CHAR_ENC_CHAR_ASCII
Definition frame_data.h:40
idata_printable.h
recent.h
bytes_view_type
bytes_view_type
Numeric base used to render byte values in the Packet Bytes pane.
Definition recent.h:61
HexDataSourceView::ByteViewAnnotation
A user-defined color annotation over a byte range.
Definition hex_data_source_view.h:47
HexDataSourceView::ByteViewAnnotation::comment
QString comment
Definition hex_data_source_view.h:51
HexDataSourceView::ByteViewAnnotation::length
int length
Definition hex_data_source_view.h:49
HexDataSourceView::ByteViewAnnotation::color
QColor color
Definition hex_data_source_view.h:50
HexDataSourceView::ByteViewAnnotation::start
int start
Definition hex_data_source_view.h:48