| 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
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
35 class HexDataSourceView : public BaseDataSourceView, public IDataPrintable
36{
37 Q_OBJECT
38 Q_INTERFACES(IDataPrintable)
39
40 public:
41 struct ByteViewAnnotation {
42 int start;
43 int length;
44 QColor color;
45 QString comment;
46 };
47
48 explicit HexDataSourceView(const QByteArray &data, packet_char_enc encoding = PACKET_CHAR_ENC_CHAR_ASCII, QWidget *parent = nullptr);
49 ~HexDataSourceView();
50
51 void setFormat(bytes_view_type format);
52 void setAnnotations(const QVector<ByteViewAnnotation> &annotations);
53
54 bool selectionRange(int *start, int *length) const;
55 int selectionAnchor() const;
56 int selectionEnd() const;
57 int contextByteOffset() const;
58 int dataSize() const {
59 Q_ASSERT(data_.size() <= std::numeric_limits<int>::max());
60 return static_cast<int>(data_.size());
61 }
62 int offsetStart() const { return offset_start_byte_; }
63 int offsetEnd() const { return offset_end_byte_; }
64 int selectedFieldStart() const { return field_start_; }
65 int selectedFieldLength() const { return field_len_; }
66 int selectedProtocolStart() const { return proto_start_; }
67 int selectedProtocolLength() const { return proto_len_; }
68 bool selectedFieldIsProtocol() const { return selected_field_is_protocol_; }
69 bool selectedFieldUsesOwnRange() const { return selected_field_use_own_range_; }
70 void setOffsetStart(int byte);
71 void setOffsetEnd(int byte);
72 void clearOffsetMarkers();
73 void setSelectedFieldIsProtocol(bool is_protocol) { selected_field_is_protocol_ = is_protocol; }
74 void setSelectedFieldUsesOwnRange(bool use_own_range) { selected_field_use_own_range_ = use_own_range; }
75
76signals:
77 void byteViewSettingsChanged();
78 void addAnnotationRequested();
79 void editAnnotationRequested();
80 void removeAnnotationRequested();
81 void offsetStartRequested(int byte);
82 void offsetEndRequested(int byte);
83 void offsetMarkersCleared();
84
85 public slots:
86 void setMonospaceFont(const QFont &mono_font);
87 void updateByteViewSettings();
88
89 void markProtocol(int start, int length);
90 void markField(int start, int length, bool scroll_to = true, bool hover = false);
91 void markAppendix(int start, int length);
92 void unmarkField();
93
94 protected:
95 virtual void paintEvent(QPaintEvent *);
96 virtual void resizeEvent(QResizeEvent *);
97 virtual void showEvent(QShowEvent *);
98 virtual void mousePressEvent (QMouseEvent * event);
99 virtual void mouseMoveEvent (QMouseEvent * event);
100 virtual void mouseReleaseEvent(QMouseEvent *event);
101 virtual void leaveEvent(QEvent *event);
102 virtual void contextMenuEvent(QContextMenuEvent *event);
103 virtual void keyPressEvent(QKeyEvent *event);
104
105 private:
106 // Text highlight modes.
107 typedef enum {
108 ModeNormal,
109 ModeField,
110 ModeProtocol,
111 ModeOffsetNormal,
112 ModeOffsetField,
113 ModeNonPrintable,
114 ModeHover
115 } HighlightMode;
116
117 QTextLayout *layout_;
118
119 void updateLayoutMetrics();
120 int stringWidth(const QString &line);
121 void drawLine(QPainter *painter, const int offset, const int row_y);
122 bool addFormatRange(QList<QTextLayout::FormatRange> &fmt_list, int start, int length, HighlightMode mode);
123 bool addHexFormatRange(QList<QTextLayout::FormatRange> &fmt_list, int mark_start, int mark_length, int tvb_offset, int max_tvb_pos, HighlightMode mode);
124 bool addAsciiFormatRange(QList<QTextLayout::FormatRange> &fmt_list, int mark_start, int mark_length, int tvb_offset, int max_tvb_pos, HighlightMode mode);
125 bool addHexCustomRange(QList<QTextLayout::FormatRange> &fmt_list, int mark_start, int mark_length, int tvb_offset, int max_tvb_pos, const QColor &bg, const QColor &fg);
126 bool addAsciiCustomRange(QList<QTextLayout::FormatRange> &fmt_list, int mark_start, int mark_length, int tvb_offset, int max_tvb_pos, const QColor &bg, const QColor &fg);
127 int annotationIndexAt(int byte_offset) const;
128 int annotationIndexIntersecting(int start, int length) const;
129 void updateSelection(int byte_offset, bool extend, bool emit_signal);
130 void updateAnnotationToolTip(int byte_offset, const QPoint &global_pos);
131 void scrollToByte(int byte);
132 void updateScrollbars();
133 int byteOffsetAtPixel(QPoint pos, bool allow_fuzzy = false);
134
135 void createContextMenu();
136 void updateContextMenu();
137
138 int offsetChars(bool include_pad = true);
139 int offsetPixels();
140 int hexPixels();
141 int asciiPixels();
142 int totalPixels();
143 const QByteArray printableData() { return data_; }
144
145 static const int separator_interval_;
146
147 bool layout_dirty_;
148
149 // Colors
150 QColor offset_normal_fg_;
151 QColor offset_field_fg_;
152
153 // Data
154 packet_char_enc encoding_; // ASCII or EBCDIC
155 QMenu ctx_menu_;
156
157 // Data highlight
158 int hovered_byte_offset_;
159 int proto_start_;
160 int proto_len_;
161 int field_start_;
162 int field_len_;
163 int field_a_start_;
164 int field_a_len_;
165 int field_hover_start_;
166 int field_hover_len_;
167
168 bool show_offset_; // Should we show the byte offset?
169 bool show_hex_; // Should we show the hex display?
170 bool show_ascii_; // Should we show the ASCII display?
171 int row_width_; // Number of bytes per line
172 int em_width_; // Single character width and text margin. NOTE: Use fontMetrics::width for multiple characters.
173 int line_height_; // Font line spacing
174 QList<QRect> hover_outlines_; // Hovered byte outlines.
175
176 bool allow_hover_selection_;
177
178 QVector<ByteViewAnnotation> annotations_;
179
180 int selection_anchor_;
181 int selection_start_;
182 int selection_end_;
183 bool selecting_;
184 int context_byte_offset_;
185 int cursor_byte_;
186 int hovered_annotation_index_;
187 int offset_start_byte_;
188 int offset_end_byte_;
189 bool selected_field_is_protocol_;
190 bool selected_field_use_own_range_;
191
192 // Data selection
193 QVector<int> x_pos_to_column_;
194
195 // Context menu actions
196 QAction *action_allow_hover_selection_;
197 QAction *action_add_annotation_;
198 QAction *action_edit_annotation_;
199 QAction *action_remove_annotation_;
200 QAction *action_set_offset_start_;
201 QAction *action_set_offset_end_;
202 QAction *action_clear_offset_markers_;
203 QAction *action_bytes_hex_;
204 QAction *action_bytes_dec_;
205 QAction *action_bytes_oct_;
206 QAction *action_bytes_bits_;
207 QAction *action_bytes_enc_from_packet_;
208 QAction *action_bytes_enc_ascii_;
209 QAction *action_bytes_enc_ebcdic_;
210
211 private slots:
212 void copyBytes(bool);
213 void setHexDisplayFormat(QAction *action);
214 void setCharacterEncoding(QAction *action);
215 void toggleHoverAllowed(bool);
216 void requestAddAnnotation();
217 void requestEditAnnotation();
218 void requestRemoveAnnotation();
219 void requestSetOffsetStart();
220 void requestSetOffsetEnd();
221 void requestClearOffsetMarkers();
222
223};
base_data_source_view.h
BaseDataSourceView
Definition base_data_source_view.h:17
HexDataSourceView
Definition hex_data_source_view.h:36
IDataPrintable
Definition idata_printable.h:23
data_printer.h
packet_char_enc
packet_char_enc
Definition frame_data.h:42
idata_printable.h
recent.h
HexDataSourceView::ByteViewAnnotation
Definition hex_data_source_view.h:41