qhexedit.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Original author: Winfried Simon
  2. // See http://code.google.com/p/qhexedit2/
  3. // Huge thanks!
  4. #ifndef QHEXEDIT_H
  5. #define QHEXEDIT_H
  6. #include <QtGui>
  7. #include "qhexedit_p.h"
  8. /** \mainpage
  9. QHexEdit is a binary editor widget for Qt.
  10. \version Version 0.6.3
  11. \image html hexedit.png
  12. */
  13. /*! QHexEdit is a hex editor widget written in C++ for the Qt (Qt4) framework.
  14. It is a simple editor for binary data, just like QPlainTextEdit is for text
  15. data. There are sip configuration files included, so it is easy to create
  16. bindings for PyQt and you can use this widget also in python.
  17. QHexEdit takes the data of a QByteArray (setData()) and shows it. You can use
  18. the mouse or the keyboard to navigate inside the widget. If you hit the keys
  19. (0..9, a..f) you will change the data. Changed data is highlighted and can be
  20. accessed via data().
  21. Normaly QHexEdit works in the overwrite Mode. You can set overwriteMode(false)
  22. and insert data. In this case the size of data() increases. It is also possible
  23. to delete bytes (del or backspace), here the size of data decreases.
  24. You can select data with keyboard hits or mouse movements. The copy-key will
  25. copy the selected data into the clipboard. The cut-key copies also but delets
  26. it afterwards. In overwrite mode, the paste function overwrites the content of
  27. the (does not change the length) data. In insert mode, clipboard data will be
  28. inserted. The clipboard content is expected in ASCII Hex notation. Unknown
  29. characters will be ignored.
  30. QHexEdit comes with undo/redo functionality. All changes can be undone, by
  31. pressing the undo-key (usually ctr-z). They can also be redone afterwards.
  32. The undo/redo framework is cleared, when setData() sets up a new
  33. content for the editor. You can search data inside the content with indexOf()
  34. and lastIndexOf(). The replace() function is to change located subdata. This
  35. 'replaced' data can also be undone by the undo/redo framework.
  36. This widget can only handle small amounts of data. The size has to be below 10
  37. megabytes, otherwise the scroll sliders ard not shown and you can't scroll any
  38. more.
  39. */
  40. class QHexEdit : public QScrollArea
  41. {
  42. Q_OBJECT
  43. /*! Property data holds the content of QHexEdit. Call setData() to set the
  44. content of QHexEdit, data() returns the actual content.
  45. */
  46. Q_PROPERTY(QByteArray data READ data WRITE setData)
  47. /*! Property addressOffset is added to the Numbers of the Address Area.
  48. A offset in the address area (left side) is sometimes usefull, whe you show
  49. only a segment of a complete memory picture. With setAddressOffset() you set
  50. this property - with addressOffset() you get the actual value.
  51. */
  52. Q_PROPERTY(int addressOffset READ addressOffset WRITE setAddressOffset)
  53. /*! Property address area color sets (setAddressAreaColor()) the backgorund
  54. color of address areas. You can also read the color (addressaAreaColor()).
  55. */
  56. Q_PROPERTY(QColor addressAreaColor READ addressAreaColor WRITE setAddressAreaColor)
  57. /*! Porperty cursorPosition sets or gets the position of the editor cursor
  58. in QHexEdit.
  59. */
  60. Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition)
  61. /*! Property highlighting color sets (setHighlightingColor()) the backgorund
  62. color of highlighted text areas. You can also read the color
  63. (highlightingColor()).
  64. */
  65. Q_PROPERTY(QColor highlightingColor READ highlightingColor WRITE setHighlightingColor)
  66. /*! Property selection color sets (setSelectionColor()) the backgorund
  67. color of selected text areas. You can also read the color
  68. (selectionColor()).
  69. */
  70. Q_PROPERTY(QColor selectionColor READ selectionColor WRITE setSelectionColor)
  71. /*! Porperty overwrite mode sets (setOverwriteMode()) or gets (overwriteMode()) the mode
  72. in which the editor works. In overwrite mode the user will overwrite existing data. The
  73. size of data will be constant. In insert mode the size will grow, when inserting
  74. new data.
  75. */
  76. Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode)
  77. /*! Porperty readOnly sets (setReadOnly()) or gets (isReadOnly) the mode
  78. in which the editor works. In readonly mode the the user can only navigate
  79. through the data and select data; modifying is not possible. This
  80. property's default is false.
  81. */
  82. Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
  83. /*! Set the font of the widget. Please use fixed width fonts like Mono or Courier.*/
  84. Q_PROPERTY(QFont font READ font WRITE setFont)
  85. public:
  86. /*! Creates an instance of QHexEdit.
  87. \param parent Parent widget of QHexEdit.
  88. */
  89. QHexEdit(QWidget *parent = 0);
  90. /*! Returns the index position of the first occurrence
  91. of the byte array ba in this byte array, searching forward from index position
  92. from. Returns -1 if ba could not be found. In addition to this functionality
  93. of QByteArray the cursorposition is set to the end of found bytearray and
  94. it will be selected.
  95. */
  96. int indexOf(const QByteArray & ba, int from = 0) const;
  97. /*! Inserts a byte array.
  98. \param i Index position, where to insert
  99. \param ba byte array, which is to insert
  100. In overwrite mode, the existing data will be overwritten, in insertmode ba will be
  101. inserted and size of data grows.
  102. */
  103. void insert(int i, const QByteArray & ba);
  104. /*! Inserts a char.
  105. \param i Index position, where to insert
  106. \param ch Char, which is to insert
  107. In overwrite mode, the existing data will be overwritten, in insertmode ba will be
  108. inserted and size of data grows.
  109. */
  110. void insert(int i, char ch);
  111. /*! Returns the index position of the last occurrence
  112. of the byte array ba in this byte array, searching backwards from index position
  113. from. Returns -1 if ba could not be found. In addition to this functionality
  114. of QByteArray the cursorposition is set to the beginning of found bytearray and
  115. it will be selected.
  116. */
  117. int lastIndexOf(const QByteArray & ba, int from = 0) const;
  118. /*! Removes len bytes from the content.
  119. \param pos Index position, where to remove
  120. \param len Amount of bytes to remove
  121. In overwrite mode, the existing bytes will be overwriten with 0x00.
  122. */
  123. void remove(int pos, int len=1);
  124. /*! Replaces len bytes from index position pos with the byte array after.
  125. */
  126. void replace( int pos, int len, const QByteArray & after);
  127. /*! Gives back a formatted image of the content of QHexEdit
  128. */
  129. QString toReadableString();
  130. /*! Gives back a formatted image of the selected content of QHexEdit
  131. */
  132. QString selectionToReadableString();
  133. /*! \cond docNever */
  134. void setAddressOffset(int offset);
  135. int addressOffset();
  136. void setCursorPosition(int cusorPos);
  137. int cursorPosition();
  138. void setData(QByteArray const &data);
  139. QByteArray data();
  140. void setAddressAreaColor(QColor const &color);
  141. QColor addressAreaColor();
  142. void setHighlightingColor(QColor const &color);
  143. QColor highlightingColor();
  144. void setSelectionColor(QColor const &color);
  145. QColor selectionColor();
  146. void setOverwriteMode(bool);
  147. bool overwriteMode();
  148. void setReadOnly(bool);
  149. bool isReadOnly();
  150. const QFont &font() const;
  151. void setFont(const QFont &);
  152. /*! \endcond docNever */
  153. public slots:
  154. /*! Redoes the last operation. If there is no operation to redo, i.e.
  155. there is no redo step in the undo/redo history, nothing happens.
  156. */
  157. void redo();
  158. /*! Set the minimum width of the address area.
  159. \param addressWidth Width in characters.
  160. */
  161. void setAddressWidth(int addressWidth);
  162. /*! Switch the address area on or off.
  163. \param addressArea true (show it), false (hide it).
  164. */
  165. void setAddressArea(bool addressArea);
  166. /*! Switch the ascii area on or off.
  167. \param asciiArea true (show it), false (hide it).
  168. */
  169. void setAsciiArea(bool asciiArea);
  170. /*! Switch the highlighting feature on or of.
  171. \param mode true (show it), false (hide it).
  172. */
  173. void setHighlighting(bool mode);
  174. /*! Undoes the last operation. If there is no operation to undo, i.e.
  175. there is no undo step in the undo/redo history, nothing happens.
  176. */
  177. void undo();
  178. signals:
  179. /*! Contains the address, where the cursor is located. */
  180. void currentAddressChanged(int address);
  181. /*! Contains the size of the data to edit. */
  182. void currentSizeChanged(int size);
  183. /*! The signal is emited every time, the data is changed. */
  184. void dataChanged();
  185. /*! The signal is emited every time, the overwrite mode is changed. */
  186. void overwriteModeChanged(bool state);
  187. private:
  188. /*! \cond docNever */
  189. QHexEditPrivate *qHexEdit_p;
  190. QHBoxLayout *layout;
  191. QScrollArea *scrollArea;
  192. /*! \endcond docNever */
  193. };
  194. #endif