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 dynamicBytesPerLine READ dynamicBytesPerLine WRITE setDynamicBytesPerLine)
112 
115  Q_PROPERTY(bool highlighting READ highlighting WRITE setHighlighting)
116 
121  Q_PROPERTY(QColor highlightingColor READ highlightingColor WRITE setHighlightingColor)
122 
128  Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode)
129 
134  Q_PROPERTY(QColor selectionColor READ selectionColor WRITE setSelectionColor)
135 
141  Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
142 
144  Q_PROPERTY(QFont font READ font WRITE setFont)
145 
146 public:
150  QHexEdit(QWidget *parent=0);
151 
152  // Access to data of qhexedit
153 
158  bool setData(QIODevice &iODevice);
159 
163  QByteArray dataAt(qint64 pos, qint64 count=-1);
164 
168  bool write(QIODevice &iODevice, qint64 pos=0, qint64 count=-1);
169 
170 
171  // Char handling
172 
178  void insert(qint64 pos, char ch);
179 
184  void remove(qint64 pos, qint64 len=1);
185 
191  void replace(qint64 pos, char ch);
192 
193 
194  // ByteArray handling
195 
201  void insert(qint64 pos, const QByteArray &ba);
202 
209  void replace(qint64 pos, qint64 len, const QByteArray &ba);
210 
211 
212  // Utility functions
217  qint64 cursorPosition(QPoint point);
218 
221  void ensureVisible();
222 
228  qint64 indexOf(const QByteArray &ba, qint64 from);
229 
233  bool isModified();
234 
240  qint64 lastIndexOf(const QByteArray &ba, qint64 from);
241 
244  QString selectionToReadableString();
245 
248  QString selectedData();
249 
253  void setFont(const QFont &font);
254 
257  QString toReadableString();
258 
259 
260 public slots:
264  void redo();
265 
269  void undo();
270 
271 signals:
272 
274  void currentAddressChanged(qint64 address);
275 
277  void currentSizeChanged(qint64 size);
278 
280  void dataChanged();
281 
283  void overwriteModeChanged(bool state);
284 
285 
287 public:
288  ~QHexEdit();
289 
290  // Properties
291  bool addressArea();
292  void setAddressArea(bool addressArea);
293 
294  QColor addressAreaColor();
295  void setAddressAreaColor(const QColor &color);
296 
297  qint64 addressOffset();
298  void setAddressOffset(qint64 addressArea);
299 
300  int addressWidth();
301  void setAddressWidth(int addressWidth);
302 
303  bool asciiArea();
304  void setAsciiArea(bool asciiArea);
305 
306  int bytesPerLine();
307  void setBytesPerLine(int count);
308 
309  qint64 cursorPosition();
310  void setCursorPosition(qint64 position);
311 
312  QByteArray data();
313  void setData(const QByteArray &ba);
314 
315  void setHexCaps(const bool isCaps);
316  bool hexCaps();
317 
318  void setDynamicBytesPerLine(const bool isDynamic);
319  bool dynamicBytesPerLine();
320 
321  bool highlighting();
322  void setHighlighting(bool mode);
323 
324  QColor highlightingColor();
325  void setHighlightingColor(const QColor &color);
326 
327  bool overwriteMode();
328  void setOverwriteMode(bool overwriteMode);
329 
330  bool isReadOnly();
331  void setReadOnly(bool readOnly);
332 
333  QColor selectionColor();
334  void setSelectionColor(const QColor &color);
335 
336 protected:
337  // Handle events
338  void keyPressEvent(QKeyEvent *event);
339  void mouseMoveEvent(QMouseEvent * event);
340  void mousePressEvent(QMouseEvent * event);
341  void paintEvent(QPaintEvent *event);
342  void resizeEvent(QResizeEvent *);
343  virtual bool focusNextPrevChild(bool next);
344 private:
345  // Handle selections
346  void resetSelection(qint64 pos); // set selectionStart and selectionEnd to pos
347  void resetSelection(); // set selectionEnd to selectionStart
348  void setSelection(qint64 pos); // set min (if below init) or max (if greater init)
349  qint64 getSelectionBegin();
350  qint64 getSelectionEnd();
351 
352  // Private utility functions
353  void init();
354  void readBuffers();
355  QString toReadable(const QByteArray &ba);
356 
357 private slots:
358  void adjust(); // recalc pixel positions
359  void dataChangedPrivate(int idx=0); // emit dataChanged() signal
360  void refresh(); // ensureVisible() and readBuffers()
361  void updateCursor(); // update blinking cursor
362 
363 private:
364  // Name convention: pixel positions start with _px
365  int _pxCharWidth, _pxCharHeight; // char dimensions (dependend on font)
366  int _pxPosHexX; // X-Pos of HeaxArea
367  int _pxPosAdrX; // X-Pos of Address Area
368  int _pxPosAsciiX; // X-Pos of Ascii Area
369  int _pxGapAdr; // gap left from AddressArea
370  int _pxGapAdrHex; // gap between AddressArea and HexAerea
371  int _pxGapHexAscii; // gap between HexArea and AsciiArea
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 _bSelectionInit; // memory position of Selection
381  qint64 _bPosFirst; // position of first byte shown
382  qint64 _bPosLast; // position of last byte shown
383  qint64 _bPosCurrent; // current position
384 
385  // variables to store the property values
386  bool _addressArea; // left area of QHexEdit
387  QColor _addressAreaColor;
388  int _addressWidth;
389  bool _asciiArea;
390  qint64 _addressOffset;
391  int _bytesPerLine;
392  int _hexCharsInLine;
393  bool _highlighting;
394  bool _overwriteMode;
395  QBrush _brushSelection;
396  QPen _penSelection;
397  QBrush _brushHighlighted;
398  QPen _penHighlighted;
399  bool _readOnly;
400  bool _hexCaps;
401  bool _dynamicBytesPerLine;
402 
403  // other variables
404  bool _editAreaIsAscii; // flag about the ascii mode edited
405  int _addrDigits; // real no of addressdigits, may be > addressWidth
406  bool _blink; // help get cursor blinking
407  QBuffer _bData; // buffer, when setup with QByteArray
408  Chunks *_chunks; // IODevice based access to data
409  QTimer _cursorTimer; // for blinking cursor
410  qint64 _cursorPosition; // absolute position of cursor, 1 Byte == 2 tics
411  QRect _cursorRect; // physical dimensions of cursor
412  QByteArray _data; // QHexEdit's data, when setup with QByteArray
413  QByteArray _dataShown; // data in the current View
414  QByteArray _hexDataShown; // data in view, transformed to hex
415  qint64 _lastEventSize; // size, which was emitted last time
416  QByteArray _markedShown; // marked data in view
417  bool _modified; // Is any data in editor modified?
418  int _rowsShown; // lines of text shown
419  UndoStack * _undoStack; // Stack to store edit actions for undo/redo
421 };
422 
423 #endif // QHEXEDIT_H
Definition: qhexedit.h:59