Wireshark 5.1.0
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 <QVector>
24
25#include <limits>
26
28
32
33// XXX - Is there any reason we shouldn't add ImageDataSourceView, etc?
34
39{
40 Q_OBJECT
41 Q_INTERFACES(IDataPrintable)
42
43public:
48 int start;
49 int length;
50 QColor color;
51 QString comment;
52 };
53
62 explicit HexDataSourceView(const QByteArray &data,
64 QWidget *parent = nullptr);
65
68
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:
211
214
217
220
225 void offsetStartRequested(int byte);
226
231 void offsetEndRequested(int byte);
232
235
236public slots:
241 void setMonospaceFont(const QFont &mono_font) override;
242
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
279protected:
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
340private:
344 struct RowPaintContext {
345 bool has_selection = false;
346 int sel_start = -1;
347 int sel_length = 0;
348 QColor sel_bg;
349 QColor sel_fg;
350 QColor sel_overlay;
351 QColor marker_start_bg;
352 QColor marker_end_bg;
353 int hover_start = -1;
354 int hover_length = 0;
355 QColor invalid_fg;
356 };
357
361 struct ByteStyle {
362 QColor bg;
363 QColor fg;
364 };
365
369 struct Row {
370 int offset;
371 int last;
372 int y;
373 QVector<ByteStyle> styles;
374 QVector<ByteStyle> gaps;
375 };
376
380 void updateLayoutMetrics();
381
387 int stringWidth(const QString &line);
388
396 void drawLine(QPainter *painter, const Row &row, const RowPaintContext &ctx);
397
403 void drawOffsetColumn(QPainter *painter, const Row &row);
404
414 void drawHexPanel(QPainter *painter, const Row &row, const RowPaintContext &ctx);
415
427 void drawTextPanel(QPainter *painter, const Row &row, const RowPaintContext &ctx);
428
439 void drawMultiByteCell(QPainter *painter, const ByteViewTextCells::Cell &cell, int first, int last, const Row &row, const RowPaintContext &ctx);
440
448 QColor cellTextColor(const ByteViewTextCells::Cell &cell, const ByteStyle &style, const RowPaintContext &ctx);
449
463 ByteStyle byteStyle(int offset, const RowPaintContext &ctx, int length = 1);
464
469 QColor annotationBackground(const ByteViewAnnotation &ann, const RowPaintContext &ctx);
470
474 void ensureTextCells();
475
477 ByteViewTextCells::Encoding textEncoding() const;
478
480 int textPanelX();
481
487 QRect textCellRect(int col, int row_y);
488
495 QRect textSpanRect(int first_col, int last_col, int row_y);
496
498 int hexCharsPerByte();
499
504 QString byteText(uint8_t c);
505
507 int hexPanelX();
508
514 QRect hexCellRect(int col, int row_y);
515
522 int hexColumnAtX(int x, bool allow_fuzzy);
523
530 int textColumnAtX(int x, bool allow_fuzzy);
531
538 int annotationIndexAt(int byte_offset) const;
539
547 int annotationIndexIntersecting(int start, int length) const;
548
556 void updateSelection(int byte_offset, bool extend, bool emit_signal);
557
564 void updateAnnotationToolTip(int byte_offset, const QPoint &global_pos);
565
571 void scrollToByte(int byte);
572
576 void updateScrollbars();
577
587 int byteOffsetAtPixel(QPoint pos, bool allow_fuzzy = false);
588
592 void createContextMenu();
593
597 void updateContextMenu();
598
599 // ── Column layout helpers ─────────────────────────────────────────────
600
606 int offsetChars(bool include_pad = true);
607
609 int offsetPixels();
610
612 int hexPixels();
613
615 int asciiPixels();
616
618 int totalPixels();
619
624 const QByteArray printableData() override { return data_; }
625
626 // ── Constants ─────────────────────────────────────────────────────────
627
629 static const int separator_interval_;
630
631 // ── State ─────────────────────────────────────────────────────────────
632 bool layout_dirty_;
633
634 // Colors
635 QColor offset_normal_fg_;
636 QColor offset_field_fg_;
637
638 // Data
639 packet_char_enc encoding_;
640 QMenu ctx_menu_;
641
642 // Highlight ranges
643 int hovered_byte_offset_;
644 int proto_start_;
645 int proto_len_;
646 int field_start_;
647 int field_len_;
648 int field_a_start_;
649 int field_a_len_;
650 int field_hover_start_;
651 int field_hover_len_;
652
653 // Display settings
654 bool show_offset_;
655 bool show_hex_;
656 bool show_ascii_;
657 int row_width_;
658 int em_width_;
659 int line_height_;
660
662 QList<QRect> hover_outlines_;
663
664 bool allow_hover_selection_;
665
666 QVector<ByteViewAnnotation> annotations_;
667
668 // Selection state
669 int selection_anchor_;
670 int selection_start_;
671 int selection_end_;
672 bool selecting_;
673 int context_byte_offset_;
674 int cursor_byte_;
675 int hovered_annotation_index_;
676 int offset_start_byte_;
677 int offset_end_byte_;
678 bool selected_field_is_protocol_;
679 bool selected_field_use_own_range_;
680
681 ByteViewTextCells text_cells_;
682 bool text_cells_dirty_;
683
684 // Context menu actions
685 QAction *action_allow_hover_selection_;
686 QAction *action_add_annotation_;
687 QAction *action_edit_annotation_;
688 QAction *action_remove_annotation_;
689 QAction *action_set_offset_start_;
690 QAction *action_set_offset_end_;
691 QAction *action_clear_offset_markers_;
692 QAction *action_bytes_hex_;
693 QAction *action_bytes_dec_;
694 QAction *action_bytes_oct_;
695 QAction *action_bytes_bits_;
696 QAction *action_bytes_enc_from_packet_;
697 QAction *action_bytes_enc_ascii_;
698 QAction *action_bytes_enc_ebcdic_;
699 QAction *action_bytes_enc_utf8_;
700
701private slots:
706 void copyBytes(bool unused);
707
712 void setHexDisplayFormat(QAction *action);
713
718 void setCharacterEncoding(QAction *action);
719
724 void toggleHoverAllowed(bool allowed);
725
727 void requestAddAnnotation();
728
730 void requestEditAnnotation();
731
733 void requestRemoveAnnotation();
734
736 void requestSetOffsetStart();
737
739 void requestSetOffsetEnd();
740
742 void requestClearOffsetMarkers();
743};
QByteArray data_
The raw packet bytes displayed by this view.
Definition base_data_source_view.h:141
BaseDataSourceView(const QByteArray &data, QWidget *parent=nullptr)
Construct a BaseDataSourceView.
Definition base_data_source_view.h:31
Encoding
Character encoding of the data.
Definition byte_view_text_cells.h:39
virtual void mousePressEvent(QMouseEvent *event) override
Begin a byte selection or move the cursor on mouse press.
Definition hex_data_source_view.cpp:582
void markAppendix(int start, int length) override
Highlight the appendix (trailing) bytes of the selected field.
Definition hex_data_source_view.cpp:404
void setMonospaceFont(const QFont &mono_font) override
Update the monospace font and recalculate layout metrics.
Definition hex_data_source_view.cpp:424
int selectionEnd() const
Return the byte offset at the end of the current selection.
Definition hex_data_source_view.cpp:159
virtual void leaveEvent(QEvent *event) override
Clear hover highlights when the pointer leaves the widget.
Definition hex_data_source_view.cpp:648
bool selectedFieldIsProtocol() const
Return whether the selected field is itself a protocol layer.
Definition hex_data_source_view.h:179
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:572
void setFormat(bytes_view_type format)
Set the numeric base used to render hex-panel byte values.
virtual void contextMenuEvent(QContextMenuEvent *event) override
Show the byte-view context menu.
Definition hex_data_source_view.cpp:661
void addAnnotationRequested()
Emitted when the user triggers "Add annotation" from the context menu.
int offsetEnd() const
Return the current offset-end marker byte position.
Definition hex_data_source_view.h:141
void offsetMarkersCleared()
Emitted when the user requests that both offset markers be cleared.
void byteViewSettingsChanged()
Emitted when any byte-view display setting changes.
int selectedProtocolStart() const
Definition hex_data_source_view.h:169
void updateByteViewSettings()
Reload byte-view display preferences and repaint.
Definition hex_data_source_view.cpp:440
int dataSize() const
Return the number of bytes in the data buffer.
Definition hex_data_source_view.h:124
int selectionAnchor() const
Return the selection anchor byte offset.
Definition hex_data_source_view.cpp:154
void removeAnnotationRequested()
Emitted when the user triggers "Remove annotation" from the context menu.
virtual void paintEvent(QPaintEvent *event) override
Paint all visible rows of the hex dump.
Definition hex_data_source_view.cpp:450
void clearOffsetMarkers()
Clear both offset markers and repaint.
Definition hex_data_source_view.cpp:190
void offsetEndRequested(int byte)
Emitted when the user requests a new offset-end marker.
void editAnnotationRequested()
Emitted when the user triggers "Edit annotation" from the context menu.
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:70
virtual void resizeEvent(QResizeEvent *event) override
Recalculate scrollbar ranges and repaint after a resize.
Definition hex_data_source_view.cpp:567
int selectedFieldStart() const
Definition hex_data_source_view.h:163
bool selectedFieldUsesOwnRange() const
Return whether the selected field defines its own byte range.
Definition hex_data_source_view.h:185
~HexDataSourceView()
Destroy the view.
Definition hex_data_source_view.cpp:126
int offsetStart() const
Return the current offset-start marker byte position.
Definition hex_data_source_view.h:135
void markProtocol(int start, int length) override
Highlight the enclosing protocol layer byte range.
Definition hex_data_source_view.cpp:382
int contextByteOffset() const
Return the byte offset at which the context menu was invoked.
Definition hex_data_source_view.cpp:164
int selectedProtocolLength() const
Definition hex_data_source_view.h:172
int selectedFieldLength() const
Definition hex_data_source_view.h:166
virtual void mouseReleaseEvent(QMouseEvent *event) override
Finalise a drag selection on mouse release.
Definition hex_data_source_view.cpp:634
void unmarkField() override
Clear field, appendix, and hover highlights.
Definition hex_data_source_view.cpp:411
bool selectionRange(int *start, int *length) const
Return the current byte selection range.
Definition hex_data_source_view.cpp:138
virtual void keyPressEvent(QKeyEvent *event) override
Handle keyboard navigation and copy commands.
Definition hex_data_source_view.cpp:671
void setOffsetEnd(int byte)
Set the offset-end marker to the given byte.
Definition hex_data_source_view.cpp:178
void setSelectedFieldIsProtocol(bool is_protocol)
Set whether the selected field is a protocol-level item.
Definition hex_data_source_view.h:191
void setSelectedFieldUsesOwnRange(bool use_own_range)
Set whether the selected field uses its own independent range.
Definition hex_data_source_view.h:199
void setAnnotations(const QVector< ByteViewAnnotation > &annotations)
Replace the current set of user annotations.
Definition hex_data_source_view.cpp:131
virtual void mouseMoveEvent(QMouseEvent *event) override
Extend the selection during a mouse drag.
Definition hex_data_source_view.cpp:604
void offsetStartRequested(int byte)
Emitted when the user requests a new offset-start marker.
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:389
void setOffsetStart(int byte)
Set the offset-start marker to the given byte.
Definition hex_data_source_view.cpp:169
An interface for objects that can provide a printable data representation.
Definition idata_printable.h:26
virtual const QByteArray printableData()=0
Retrieves the printable data representation of the object.
packet_char_enc
Types of character encodings.
Definition frame_data.h:39
@ PACKET_CHAR_ENC_CHAR_ASCII
Definition frame_data.h:40
bytes_view_type
Numeric base used to render byte values in the Packet Bytes pane.
Definition recent.h:61
A user-defined color annotation over a byte range.
Definition hex_data_source_view.h:47
QString comment
Definition hex_data_source_view.h:51
int length
Definition hex_data_source_view.h:49
QColor color
Definition hex_data_source_view.h:50
int start
Definition hex_data_source_view.h:48