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 
107  Q_PROPERTY(bool hexCaps READ hexCaps WRITE setHexCaps)
108 
111  Q_PROPERTY(bool highlighting READ highlighting WRITE setHighlighting)
112 
117  Q_PROPERTY(QColor highlightingColor READ highlightingColor WRITE setHighlightingColor)
118 
124  Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode)
125 
130  Q_PROPERTY(QColor selectionColor READ selectionColor WRITE setSelectionColor)
131 
137  Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
138 
140  Q_PROPERTY(QFont font READ font WRITE setFont)
141 
142 public:
146  QHexEdit(QWidget *parent=0);
147 
148  // Access to data of qhexedit
149 
154  bool setData(QIODevice &iODevice);
155 
159  QByteArray dataAt(qint64 pos, qint64 count=-1);
160 
164  bool write(QIODevice &iODevice, qint64 pos=0, qint64 count=-1);
165 
166 
167  // Char handling
168 
174  void insert(qint64 pos, char ch);
175 
180  void remove(qint64 pos, qint64 len=1);
181 
187  void replace(qint64 pos, char ch);
188 
189 
190  // ByteArray handling
191 
197  void insert(qint64 pos, const QByteArray &ba);
198 
205  void replace(qint64 pos, qint64 len, const QByteArray &ba);
206 
207 
208  // Utility functioins
213  qint64 cursorPosition(QPoint point);
214 
217  void ensureVisible();
218 
224  qint64 indexOf(const QByteArray &ba, qint64 from);
225 
229  bool isModified();
230 
236  qint64 lastIndexOf(const QByteArray &ba, qint64 from);
237 
240  QString selectionToReadableString();
241 
245  void setFont(const QFont &font);
246 
249  QString toReadableString();
250 
251 
252 public slots:
256  void redo();
257 
261  void undo();
262 
263 signals:
264 
266  void currentAddressChanged(qint64 address);
267 
269  void currentSizeChanged(qint64 size);
270 
272  void dataChanged();
273 
275  void overwriteModeChanged(bool state);
276 
277 
279 public:
280  ~QHexEdit();
281 
282  // Properties
283  bool addressArea();
284  void setAddressArea(bool addressArea);
285 
286  QColor addressAreaColor();
287  void setAddressAreaColor(const QColor &color);
288 
289  qint64 addressOffset();
290  void setAddressOffset(qint64 addressArea);
291 
292  int addressWidth();
293  void setAddressWidth(int addressWidth);
294 
295  bool asciiArea();
296  void setAsciiArea(bool asciiArea);
297 
298  int bytesPerLine();
299  void setBytesPerLine(int count);
300 
301  qint64 cursorPosition();
302  void setCursorPosition(qint64 position);
303 
304  QByteArray data();
305  void setData(const QByteArray &ba);
306 
307  void setHexCaps(const bool isCaps);
308  bool hexCaps();
309 
310  bool highlighting();
311  void setHighlighting(bool mode);
312 
313  QColor highlightingColor();
314  void setHighlightingColor(const QColor &color);
315 
316  bool overwriteMode();
317  void setOverwriteMode(bool overwriteMode);
318 
319  bool isReadOnly();
320  void setReadOnly(bool readOnly);
321 
322  QColor selectionColor();
323  void setSelectionColor(const QColor &color);
324 
325 protected:
326  // Handle events
327  void keyPressEvent(QKeyEvent *event);
328  void mouseMoveEvent(QMouseEvent * event);
329  void mousePressEvent(QMouseEvent * event);
330  void paintEvent(QPaintEvent *event);
331  void resizeEvent(QResizeEvent *);
332  virtual bool focusNextPrevChild(bool next);
333 private:
334  // Handle selections
335  void resetSelection(qint64 pos); // set selectionStart and selectionEnd to pos
336  void resetSelection(); // set selectionEnd to selectionStart
337  void setSelection(qint64 pos); // set min (if below init) or max (if greater init)
338  int getSelectionBegin();
339  int getSelectionEnd();
340 
341  // Private utility functions
342  void init();
343  void readBuffers();
344  QString toReadable(const QByteArray &ba);
345 
346 private slots:
347  void adjust(); // recalc pixel positions
348  void dataChangedPrivate(int idx=0); // emit dataChanged() signal
349  void refresh(); // ensureVisible() and readBuffers()
350  void updateCursor(); // update blinking cursor
351 
352 private:
353  // Name convention: pixel positions start with _px
354  int _pxCharWidth, _pxCharHeight; // char dimensions (dpendend on font)
355  int _pxPosHexX; // X-Pos of HeaxArea
356  int _pxPosAdrX; // X-Pos of Address Area
357  int _pxPosAsciiX; // X-Pos of Ascii Area
358  int _pxGapAdr; // gap left from AddressArea
359  int _pxGapAdrHex; // gap between AddressArea and HexAerea
360  int _pxGapHexAscii; // gap between HexArea and AsciiArea
361  int _pxCursorWidth; // cursor width
362  int _pxSelectionSub; // offset selection rect
363  int _pxCursorX; // current cursor pos
364  int _pxCursorY; // current cursor pos
365 
366  // Name convention: absolute byte positions in chunks start with _b
367  qint64 _bSelectionBegin; // first position of Selection
368  qint64 _bSelectionEnd; // end of Selection
369  qint64 _bSelectionInit; // memory position of Selection
370  qint64 _bPosFirst; // position of first byte shown
371  qint64 _bPosLast; // position of last byte shown
372  qint64 _bPosCurrent; // current position
373 
374  // variables to store the property values
375  bool _addressArea; // left area of QHexEdit
376  QColor _addressAreaColor;
377  int _addressWidth;
378  bool _asciiArea;
379  qint64 _addressOffset;
380  int _bytesPerLine;
381  int _hexCharsInLine;
382  bool _highlighting;
383  bool _overwriteMode;
384  QBrush _brushSelection;
385  QPen _penSelection;
386  QBrush _brushHighlighted;
387  QPen _penHighlighted;
388  bool _readOnly;
389  bool _hexCaps;
390 
391  // other variables
392  bool _editAreaIsAscii; // flag about the ascii mode edited
393  int _addrDigits; // real no of addressdigits, may be > addressWidth
394  bool _blink; // help get cursor blinking
395  QBuffer _bData; // buffer, when setup with QByteArray
396  Chunks *_chunks; // IODevice based access to data
397  QTimer _cursorTimer; // for blinking cursor
398  qint64 _cursorPosition; // absolute positioin of cursor, 1 Byte == 2 tics
399  QRect _cursorRect; // physical dimensions of cursor
400  QByteArray _data; // QHexEdit's data, when setup with QByteArray
401  QByteArray _dataShown; // data in the current View
402  QByteArray _hexDataShown; // data in view, transformed to hex
403  qint64 _lastEventSize; // size, which was emitted last time
404  QByteArray _markedShown; // marked data in view
405  bool _modified; // Is any data in editor modified?
406  int _rowsShown; // lines of text shown
407  UndoStack * _undoStack; // Stack to store edit actions for undo/redo
409 };
410 
411 #endif // QHEXEDIT_H
Definition: qhexedit.h:59