QHexEdit
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 
11 #ifdef QHEXEDIT_EXPORTS
12 #define QHEXEDIT_API Q_DECL_EXPORT
13 #elif QHEXEDIT_IMPORTS
14 #define QHEXEDIT_API Q_DECL_IMPORT
15 #else
16 #define QHEXEDIT_API
17 #endif
18 
59 class QHEXEDIT_API QHexEdit : public QAbstractScrollArea
60 {
61  Q_OBJECT
62 
66  Q_PROPERTY(bool addressArea READ addressArea WRITE setAddressArea)
67 
68 
71  Q_PROPERTY(QColor addressAreaColor READ addressAreaColor WRITE setAddressAreaColor)
72 
78  Q_PROPERTY(qint64 addressOffset READ addressOffset WRITE setAddressOffset)
79 
82  Q_PROPERTY(int addressWidth READ addressWidth WRITE setAddressWidth)
83 
86  Q_PROPERTY(bool asciiArea READ asciiArea WRITE setAsciiArea)
87 
89  Q_PROPERTY(int bytesPerLine READ bytesPerLine WRITE setBytesPerLine)
90 
95  Q_PROPERTY(qint64 cursorPosition READ cursorPosition WRITE setCursorPosition)
96 
102  Q_PROPERTY(QByteArray data READ data WRITE setData NOTIFY dataChanged)
103 
106  Q_PROPERTY(bool highlighting READ highlighting WRITE setHighlighting)
107 
112  Q_PROPERTY(QColor highlightingColor READ highlightingColor WRITE setHighlightingColor)
113 
119  Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode)
120 
125  Q_PROPERTY(QColor selectionColor READ selectionColor WRITE setSelectionColor)
126 
132  Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
133 
135  Q_PROPERTY(QFont font READ font WRITE setFont)
136 
137 public:
141  QHexEdit(QWidget *parent=0);
142 
143  // Access to data of qhexedit
144 
149  bool setData(QIODevice &iODevice);
150 
154  QByteArray dataAt(qint64 pos, qint64 count=-1);
155 
159  bool write(QIODevice &iODevice, qint64 pos=0, qint64 count=-1);
160 
161 
162  // Char handling
163 
169  void insert(qint64 pos, char ch);
170 
175  void remove(qint64 pos, qint64 len=1);
176 
182  void replace(qint64 pos, char ch);
183 
184 
185  // ByteArray handling
186 
192  void insert(qint64 pos, const QByteArray &ba);
193 
200  void replace(qint64 pos, qint64 len, const QByteArray &ba);
201 
202 
203  // Utility functioins
208  qint64 cursorPosition(QPoint point);
209 
212  void ensureVisible();
213 
219  qint64 indexOf(const QByteArray &ba, qint64 from);
220 
224  bool isModified();
225 
231  qint64 lastIndexOf(const QByteArray &ba, qint64 from);
232 
235  QString selectionToReadableString();
236 
240  void setFont(const QFont &font);
241 
244  QString toReadableString();
245 
246 
247 public slots:
251  void redo();
252 
256  void undo();
257 
258 signals:
259 
261  void currentAddressChanged(qint64 address);
262 
264  void currentSizeChanged(qint64 size);
265 
267  void dataChanged();
268 
270  void overwriteModeChanged(bool state);
271 
272 
274 public:
275  ~QHexEdit();
276 
277  // Properties
278  bool addressArea();
279  void setAddressArea(bool addressArea);
280 
281  QColor addressAreaColor();
282  void setAddressAreaColor(const QColor &color);
283 
284  qint64 addressOffset();
285  void setAddressOffset(qint64 addressArea);
286 
287  int addressWidth();
288  void setAddressWidth(int addressWidth);
289 
290  bool asciiArea();
291  void setAsciiArea(bool asciiArea);
292 
293  int bytesPerLine();
294  void setBytesPerLine(int count);
295 
296  qint64 cursorPosition();
297  void setCursorPosition(qint64 position);
298 
299  QByteArray data();
300  void setData(const QByteArray &ba);
301 
302  bool highlighting();
303  void setHighlighting(bool mode);
304 
305  QColor highlightingColor();
306  void setHighlightingColor(const QColor &color);
307 
308  bool overwriteMode();
309  void setOverwriteMode(bool overwriteMode);
310 
311  bool isReadOnly();
312  void setReadOnly(bool readOnly);
313 
314  QColor selectionColor();
315  void setSelectionColor(const QColor &color);
316 
317 protected:
318  // Handle events
319  void keyPressEvent(QKeyEvent *event);
320  void mouseMoveEvent(QMouseEvent * event);
321  void mousePressEvent(QMouseEvent * event);
322  void paintEvent(QPaintEvent *event);
323  void resizeEvent(QResizeEvent *);
324 
325 private:
326  // Handle selections
327  void resetSelection(qint64 pos); // set selectionStart and selectionEnd to pos
328  void resetSelection(); // set selectionEnd to selectionStart
329  void setSelection(qint64 pos); // set min (if below init) or max (if greater init)
330  int getSelectionBegin();
331  int getSelectionEnd();
332 
333  // Private utility functions
334  void init();
335  void readBuffers();
336  QString toReadable(const QByteArray &ba);
337 
338 private slots:
339  void adjust(); // recalc pixel positions
340  void dataChangedPrivate(int idx=0); // emit dataChanged() signal
341  void refresh(); // ensureVisible() and readBuffers()
342  void updateCursor(); // update blinking cursor
343 
344 private:
345  // Name convention: pixel positions start with _px
346  int _pxCharWidth, _pxCharHeight; // char dimensions (dpendend on font)
347  int _pxPosHexX; // X-Pos of HeaxArea
348  int _pxPosAdrX; // X-Pos of Address Area
349  int _pxPosAsciiX; // X-Pos of Ascii Area
350  int _pxGapAdr; // gap left from AddressArea
351  int _pxGapAdrHex; // gap between AddressArea and HexAerea
352  int _pxGapHexAscii; // gap between HexArea and AsciiArea
353  int _pxCursorWidth; // cursor width
354  int _pxSelectionSub; // offset selection rect
355  int _pxCursorX; // current cursor pos
356  int _pxCursorY; // current cursor pos
357 
358  // Name convention: absolute byte positions in chunks start with _b
359  qint64 _bSelectionBegin; // first position of Selection
360  qint64 _bSelectionEnd; // end of Selection
361  qint64 _bSelectionInit; // memory position of Selection
362  qint64 _bPosFirst; // position of first byte shown
363  qint64 _bPosLast; // position of last byte shown
364  qint64 _bPosCurrent; // current position
365 
366  // variables to store the property values
367  bool _addressArea; // left area of QHexEdit
368  QColor _addressAreaColor;
369  int _addressWidth;
370  bool _asciiArea;
371  qint64 _addressOffset;
372  int _bytesPerLine;
373  int _hexCharsInLine;
374  bool _highlighting;
375  bool _overwriteMode;
376  QBrush _brushSelection;
377  QPen _penSelection;
378  QBrush _brushHighlighted;
379  QPen _penHighlighted;
380  bool _readOnly;
381 
382  // other variables
383  int _addrDigits; // real no of addressdigits, may be > addressWidth
384  bool _blink; // help get cursor blinking
385  QBuffer _bData; // buffer, when setup with QByteArray
386  Chunks *_chunks; // IODevice based access to data
387  QTimer _cursorTimer; // for blinking cursor
388  qint64 _cursorPosition; // absolute positioin of cursor, 1 Byte == 2 tics
389  QRect _cursorRect; // physical dimensions of cursor
390  QByteArray _data; // QHexEdit's data, when setup with QByteArray
391  QByteArray _dataShown; // data in the current View
392  QByteArray _hexDataShown; // data in view, transformed to hex
393  qint64 _lastEventSize; // size, which was emitted last time
394  QByteArray _markedShown; // marked data in view
395  bool _modified; // Is any data in editor modified?
396  int _rowsShown; // lines of text shown
397  UndoStack * _undoStack; // Stack to store edit actions for undo/redo
399 };
400 
401 #endif // QHEXEDIT_H
Definition: qhexedit.h:59