QHexEdit: /media/winni/1p4/Werkstatt/qt/qhexedit2/src/qhexedit.h Source File

QHexEdit

Loading...
Searching...
No Matches
qhexedit.h
1#ifndef QHEXEDIT_H
2#define QHEXEDIT_H
3
4#include <QAbstractScrollArea>
5#include <QPen>
6#include <QBrush>
7
8#include "chunks.h"
9#include "commands.h"
10#include "color_manager.h"
11
12#ifdef QHEXEDIT_EXPORTS
13#define QHEXEDIT_API Q_DECL_EXPORT
14#elif QHEXEDIT_IMPORTS
15#define QHEXEDIT_API Q_DECL_IMPORT
16#else
17#define QHEXEDIT_API
18#endif
19
60class QHEXEDIT_API QHexEdit : public QAbstractScrollArea
61{
62 Q_OBJECT
63
67 Q_PROPERTY(bool addressArea READ addressArea WRITE setAddressArea)
68
69
74 Q_PROPERTY(qint64 addressOffset READ addressOffset WRITE setAddressOffset)
75
76
78 Q_PROPERTY(int addressWidth READ addressWidth WRITE setAddressWidth)
79
80
82 Q_PROPERTY(bool asciiArea READ asciiArea WRITE setAsciiArea)
83
84
85 Q_PROPERTY(int bytesPerLine READ bytesPerLine WRITE setBytesPerLine)
86
87
91 Q_PROPERTY(qint64 cursorPosition READ cursorPosition WRITE setCursorPosition)
92
93
98 Q_PROPERTY(QByteArray data READ data WRITE setData NOTIFY dataChanged)
99
100
103 Q_PROPERTY(bool hexCaps READ hexCaps WRITE setHexCaps)
104
105
107 Q_PROPERTY(bool dynamicBytesPerLine READ dynamicBytesPerLine WRITE setDynamicBytesPerLine)
108
109
111 Q_PROPERTY(bool highlighting READ highlighting WRITE setHighlighting)
112
113
117 Q_PROPERTY(QColor highlightingColor READ highlightingColor WRITE setHighlightingColor)
118
119
124 Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode)
125
126
131 Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
132
133
134 Q_PROPERTY(QFont font READ font WRITE setFont)
135
136public:
140 QHexEdit(QWidget *parent=NULL);
141
142 // Access to data of qhexedit
143
148 bool setData(QIODevice &iODevice);
149
153 QByteArray dataAt(qint64 pos, qint64 count=-1);
154
158 bool write(QIODevice &iODevice, qint64 pos=0, qint64 count=-1);
159
160
161 // Char handling
162
168 void insert(qint64 pos, char ch);
169
174 void remove(qint64 pos, qint64 len=1);
175
181 void replace(qint64 pos, char ch);
182
183
184 // ByteArray handling
185
191 void insert(qint64 pos, const QByteArray &ba);
192
199 void replace(qint64 pos, qint64 len, const QByteArray &ba);
200
201
202 // User marking areas
203
213 void addUserArea(qint64 posStart, qint64 posEnd, QColor fontColor, QBrush areaStyle);
214
217 void clearUserAreas();
218
219 // Utility functions
220
225 qint64 cursorPosition(QPoint point);
226
229 void ensureVisible();
230
236 qint64 indexOf(const QByteArray &ba, qint64 from);
237
241 bool isModified();
242
248 qint64 lastIndexOf(const QByteArray &ba, qint64 from);
249
252 QString selectionToReadableString();
253
256 QString selectedData();
257
261 void setFont(const QFont &font);
262
265 QString toReadableString();
266
267public slots:
271 void redo();
272
276 void undo();
277
278signals:
279
281 void currentAddressChanged(qint64 address);
282
284 void currentSizeChanged(qint64 size);
285
287 void dataChanged();
288
290 void overwriteModeChanged(bool state);
291
292
294public:
295 ~QHexEdit();
296
297 // Properties
298 bool addressArea();
299 void setAddressArea(bool addressArea);
300
301 qint64 addressOffset();
302 void setAddressOffset(qint64 addressArea);
303
304 int addressWidth();
305 void setAddressWidth(int addressWidth);
306
307 bool asciiArea();
308 void setAsciiArea(bool asciiArea);
309
310 int bytesPerLine();
311 void setBytesPerLine(int count);
312
313 qint64 cursorPosition();
314 void setCursorPosition(qint64 position);
315
316 QByteArray data();
317 void setData(const QByteArray &ba);
318
319 void setHexCaps(const bool isCaps);
320 bool hexCaps();
321
322 void setDynamicBytesPerLine(const bool isDynamic);
323 bool dynamicBytesPerLine();
324
325 bool highlighting();
326 void setHighlighting(bool mode);
327
328 QColor highlightingColor();
329 void setHighlightingColor(const QColor &color);
330
331 bool overwriteMode();
332 void setOverwriteMode(bool overwriteMode);
333
334 bool isReadOnly();
335 void setReadOnly(bool readOnly);
336
337protected:
338 // Handle events
339 bool event(QEvent *event);
340 void keyPressEvent(QKeyEvent *event);
341 void mouseMoveEvent(QMouseEvent * event);
342 void mousePressEvent(QMouseEvent * event);
343 void paintEvent(QPaintEvent *event);
344 void resizeEvent(QResizeEvent *);
345 virtual bool focusNextPrevChild(bool next);
346private:
347 // Handle selections
348 void resetSelection(qint64 pos); // set selectionStart and selectionEnd to pos
349 void resetSelection(); // set selectionEnd to selectionStart
350 void setSelection(qint64 pos); // set min (if below init) or max (if greater init)
351 qint64 getSelectionBegin();
352 qint64 getSelectionEnd();
353
354 // Private utility functions
355 void init();
356 void readBuffers();
357 QString toReadable(const QByteArray &ba);
358
359private slots:
360 void adjust(); // recalc pixel positions
361 void dataChangedPrivate(int idx=0); // emit dataChanged() signal
362 void refresh(); // ensureVisible() and readBuffers()
363 void updateCursor(); // update blinking cursor
364
365private:
366 // Name convention: pixel positions start with _px
367 int _pxCharWidth, _pxCharHeight; // char dimensions (dependend on font)
368 int _pxPosHexX; // X-Pos of HeaxArea
369 int _pxPosAdrX; // X-Pos of Address Area
370 int _pxPosAsciiX; // X-Pos of Ascii Area
371 int _pxAreaMargin; // gap left and right from areas
372 int _pxCursorWidth; // cursor width
373 int _pxSelectionSub; // offset selection rect
374 int _pxCursorX; // current cursor pos
375 int _pxCursorY; // current cursor pos
376
377 // Name convention: absolute byte positions in chunks start with _b
378 qint64 _bSelectionBegin; // first position of Selection
379 qint64 _bSelectionEnd; // end of Selection
380 qint64 _bPosFirst; // position of first byte shown
381 qint64 _bPosLast; // position of last byte shown
382 qint64 _bPosCurrent; // current position
383
384 // variables to store the property values
385 bool _addressArea; // left area of QHexEdit
386 int _addressWidth;
387 bool _asciiArea;
388 qint64 _addressOffset;
389 int _bytesPerLine;
390 int _hexCharsInLine;
391 bool _highlighting;
392 bool _overwriteMode;
393 bool _readOnly;
394 bool _hexCaps;
395 bool _dynamicBytesPerLine;
396
397 // other variables
398 bool _editAreaIsAscii; // flag about the ascii mode edited
399 int _addrDigits; // real no of addressdigits, may be > addressWidth
400 bool _blink; // help get cursor blinking
401 QBuffer _bData; // buffer, when setup with QByteArray
402 Chunks *_chunks; // IODevice based access to data
403 QTimer _cursorTimer; // for blinking cursor
404 qint64 _cursorPosition; // absolute position of cursor, 1 Byte == 2 tics
405 QRect _cursorRect; // physical dimensions of cursor
406 QByteArray _data; // QHexEdit's data, when setup with QByteArray
407 QByteArray _dataShown; // data in the current View
408 QByteArray _hexDataShown; // data in view, transformed to hex
409 qint64 _lastEventSize; // size, which was emitted last time
410 QByteArray _markedShown; // marked data in view
411 bool _modified; // Is any data in editor modified?
412 int _rowsShown; // lines of text shown
413 UndoStack * _undoStack; // stack to store edit actions for undo/redo
414 ColorManager * _colorManager; // holds highlighting, selection and other area colors
416};
417
418#endif // QHEXEDIT_H