Wireshark 4.7.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
hex_data_source_view.h
Go to the documentation of this file.
1
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
29
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);
242
247
254 void markProtocol(int start, int length);
255
264 void markField(int start, int length, bool scroll_to = true, bool hover = false);
265
272 void markAppendix(int start, int length);
273
277 void unmarkField();
278
279protected:
285 virtual void paintEvent(QPaintEvent *event);
286
291 virtual void resizeEvent(QResizeEvent *event);
292
298 virtual void showEvent(QShowEvent *event);
299
305 virtual void mousePressEvent(QMouseEvent *event);
306
312 virtual void mouseMoveEvent(QMouseEvent *event);
313
318 virtual void mouseReleaseEvent(QMouseEvent *event);
319
324 virtual void leaveEvent(QEvent *event);
325
331 virtual void contextMenuEvent(QContextMenuEvent *event);
332
338 virtual void keyPressEvent(QKeyEvent *event);
339
340private:
344 typedef enum {
345 ModeNormal,
346 ModeField,
347 ModeProtocol,
348 ModeOffsetNormal,
349 ModeOffsetField,
350 ModeNonPrintable,
351 ModeHover
352 } HighlightMode;
353
354 QTextLayout *layout_;
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() { return data_; }
548
549 // ── Constants ─────────────────────────────────────────────────────────
550
552 static const int separator_interval_;
553
554 // ── State ─────────────────────────────────────────────────────────────
555 bool layout_dirty_;
557 // Colors
558 QColor offset_normal_fg_;
559 QColor offset_field_fg_;
561 // Data
562 packet_char_enc encoding_;
563 QMenu ctx_menu_;
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_;
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_;
585 QList<QRect> hover_outlines_;
586
587 bool allow_hover_selection_;
589 QVector<ByteViewAnnotation> annotations_;
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_;
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_;
630private 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};
Abstract base class for widgets that display raw packet byte data.
Definition base_data_source_view.h:20
QByteArray data_
The raw packet bytes displayed by this view.
Definition base_data_source_view.h:141
A fully custom-painted hex dump view for a single packet data source.
Definition hex_data_source_view.h:39
virtual void showEvent(QShowEvent *event)
Ensure the layout is up to date when the widget first becomes visible.
Definition hex_data_source_view.cpp:521
void markField(int start, int length, bool scroll_to=true, bool hover=false)
Highlight a specific dissected field byte range.
Definition hex_data_source_view.cpp:377
virtual void paintEvent(QPaintEvent *event)
Paint all visible rows of the hex dump.
Definition hex_data_source_view.cpp:438
int selectionEnd() const
Return the byte offset at the end of the current selection.
Definition hex_data_source_view.cpp:152
bool selectedFieldIsProtocol() const
Return whether the selected field is itself a protocol layer.
Definition hex_data_source_view.h:179
void setFormat(bytes_view_type format)
Set the numeric base used to render hex-panel byte values.
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
virtual void mouseMoveEvent(QMouseEvent *event)
Extend the selection during a mouse drag.
Definition hex_data_source_view.cpp:553
void updateByteViewSettings()
Reload byte-view display preferences and repaint.
Definition hex_data_source_view.cpp:429
void markAppendix(int start, int length)
Highlight the appendix (trailing) bytes of the selected field.
Definition hex_data_source_view.cpp:392
int dataSize() const
Return the number of bytes in the data buffer.
Definition hex_data_source_view.h:124
virtual void leaveEvent(QEvent *event)
Clear hover highlights when the pointer leaves the widget.
Definition hex_data_source_view.cpp:597
virtual void resizeEvent(QResizeEvent *event)
Recalculate scrollbar ranges and repaint after a resize.
Definition hex_data_source_view.cpp:516
int selectionAnchor() const
Return the selection anchor byte offset.
Definition hex_data_source_view.cpp:147
void removeAnnotationRequested()
Emitted when the user triggers "Remove annotation" from the context menu.
void clearOffsetMarkers()
Clear both offset markers and repaint.
Definition hex_data_source_view.cpp:183
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.
virtual void mousePressEvent(QMouseEvent *event)
Begin a byte selection or move the cursor on mouse press.
Definition hex_data_source_view.cpp:531
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 and its QTextLayout.
Definition hex_data_source_view.cpp:118
void markProtocol(int start, int length)
Highlight the enclosing protocol layer byte range.
Definition hex_data_source_view.cpp:370
int offsetStart() const
Return the current offset-start marker byte position.
Definition hex_data_source_view.h:135
virtual void contextMenuEvent(QContextMenuEvent *event)
Show the byte-view context menu.
Definition hex_data_source_view.cpp:610
void unmarkField()
Clear field, appendix, and hover highlights.
Definition hex_data_source_view.cpp:399
int contextByteOffset() const
Return the byte offset at which the context menu was invoked.
Definition hex_data_source_view.cpp:157
int selectedProtocolLength() const
Definition hex_data_source_view.h:172
int selectedFieldLength() const
Definition hex_data_source_view.h:166
bool selectionRange(int *start, int *length) const
Return the current byte selection range.
Definition hex_data_source_view.cpp:131
void setMonospaceFont(const QFont &mono_font)
Update the monospace font and recalculate layout metrics.
Definition hex_data_source_view.cpp:412
void setOffsetEnd(int byte)
Set the offset-end marker to the given byte.
Definition hex_data_source_view.cpp:171
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:124
virtual void mouseReleaseEvent(QMouseEvent *event)
Finalise a drag selection on mouse release.
Definition hex_data_source_view.cpp:583
virtual void keyPressEvent(QKeyEvent *event)
Handle keyboard navigation and copy commands.
Definition hex_data_source_view.cpp:620
void offsetStartRequested(int byte)
Emitted when the user requests a new offset-start marker.
void setOffsetStart(int byte)
Set the offset-start marker to the given byte.
Definition hex_data_source_view.cpp:162
An interface for objects that can provide a printable data representation.
Definition idata_printable.h:26
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:57
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