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 
249  void setFont(const QFont &font);
250 
253  QString toReadableString();
254 
255 
256 public slots:
260  void redo();
261 
265  void undo();
266 
267 signals:
268 
270  void currentAddressChanged(qint64 address);
271 
273  void currentSizeChanged(qint64 size);
274 
276  void dataChanged();
277 
279  void overwriteModeChanged(bool state);
280 
281 
283 public:
284  ~QHexEdit();
285 
286  // Properties
287  bool addressArea();
288  void setAddressArea(bool addressArea);
289 
290  QColor addressAreaColor();
291  void setAddressAreaColor(const QColor &color);
292 
293  qint64 addressOffset();
294  void setAddressOffset(qint64 addressArea);
295 
296  int addressWidth();
297  void setAddressWidth(int addressWidth);
298 
299  bool asciiArea();
300  void setAsciiArea(bool asciiArea);
301 
302  int bytesPerLine();
303  void setBytesPerLine(int count);
304 
305  qint64 cursorPosition();
306  void setCursorPosition(qint64 position);
307 
308  QByteArray data();
309  void setData(const QByteArray &ba);
310 
311  void setHexCaps(const bool isCaps);
312  bool hexCaps();
313 
314  void setDynamicBytesPerLine(const bool isDynamic);
315  bool dynamicBytesPerLine();
316 
317  bool highlighting();
318  void setHighlighting(bool mode);
319 
320  QColor highlightingColor();
321  void setHighlightingColor(const QColor &color);
322 
323  bool overwriteMode();
324  void setOverwriteMode(bool overwriteMode);
325 
326  bool isReadOnly();
327  void setReadOnly(bool readOnly);
328 
329  QColor selectionColor();
330  void setSelectionColor(const QColor &color);
331 
332 protected:
333  // Handle events
334  void keyPressEvent(QKeyEvent *event);
335  void mouseMoveEvent(QMouseEvent * event);
336  void mousePressEvent(QMouseEvent * event);
337  void paintEvent(QPaintEvent *event);
338  void resizeEvent(QResizeEvent *);
339  virtual bool focusNextPrevChild(bool next);
340 private:
341  // Handle selections
342  void resetSelection(qint64 pos); // set selectionStart and selectionEnd to pos
343  void resetSelection(); // set selectionEnd to selectionStart
344  void setSelection(qint64 pos); // set min (if below init) or max (if greater init)
345  int getSelectionBegin();
346  int getSelectionEnd();
347 
348  // Private utility functions
349  void init();
350  void readBuffers();
351  QString toReadable(const QByteArray &ba);
352 
353 private slots:
354  void adjust(); // recalc pixel positions
355  void dataChangedPrivate(int idx=0); // emit dataChanged() signal
356  void refresh(); // ensureVisible() and readBuffers()
357  void updateCursor(); // update blinking cursor
358 
359 private:
360  // Name convention: pixel positions start with _px
361  int _pxCharWidth, _pxCharHeight; // char dimensions (dependend on font)
362  int _pxPosHexX; // X-Pos of HeaxArea
363  int _pxPosAdrX; // X-Pos of Address Area
364  int _pxPosAsciiX; // X-Pos of Ascii Area
365  int _pxGapAdr; // gap left from AddressArea
366  int _pxGapAdrHex; // gap between AddressArea and HexAerea
367  int _pxGapHexAscii; // gap between HexArea and AsciiArea
368  int _pxCursorWidth; // cursor width
369  int _pxSelectionSub; // offset selection rect
370  int _pxCursorX; // current cursor pos
371  int _pxCursorY; // current cursor pos
372 
373  // Name convention: absolute byte positions in chunks start with _b
374  qint64 _bSelectionBegin; // first position of Selection
375  qint64 _bSelectionEnd; // end of Selection
376  qint64 _bSelectionInit; // memory position of Selection
377  qint64 _bPosFirst; // position of first byte shown
378  qint64 _bPosLast; // position of last byte shown
379  qint64 _bPosCurrent; // current position
380 
381  // variables to store the property values
382  bool _addressArea; // left area of QHexEdit
383  QColor _addressAreaColor;
384  int _addressWidth;
385  bool _asciiArea;
386  qint64 _addressOffset;
387  int _bytesPerLine;
388  int _hexCharsInLine;
389  bool _highlighting;
390  bool _overwriteMode;
391  QBrush _brushSelection;
392  QPen _penSelection;
393  QBrush _brushHighlighted;
394  QPen _penHighlighted;
395  bool _readOnly;
396  bool _hexCaps;
397  bool _dynamicBytesPerLine;
398 
399  // other variables
400  bool _editAreaIsAscii; // flag about the ascii mode edited
401  int _addrDigits; // real no of addressdigits, may be > addressWidth
402  bool _blink; // help get cursor blinking
403  QBuffer _bData; // buffer, when setup with QByteArray
404  Chunks *_chunks; // IODevice based access to data
405  QTimer _cursorTimer; // for blinking cursor
406  qint64 _cursorPosition; // absolute position of cursor, 1 Byte == 2 tics
407  QRect _cursorRect; // physical dimensions of cursor
408  QByteArray _data; // QHexEdit's data, when setup with QByteArray
409  QByteArray _dataShown; // data in the current View
410  QByteArray _hexDataShown; // data in view, transformed to hex
411  qint64 _lastEventSize; // size, which was emitted last time
412  QByteArray _markedShown; // marked data in view
413  bool _modified; // Is any data in editor modified?
414  int _rowsShown; // lines of text shown
415  UndoStack * _undoStack; // Stack to store edit actions for undo/redo
417 };
418 
419 #endif // QHEXEDIT_H
Definition: qhexedit.h:59