commands.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #ifndef COMMANDS_H
  2. #define COMMANDS_H
  3. /** \cond docNever */
  4. #include <QUndoCommand>
  5. #include "xbytearray.h"
  6. /*! CharCommand is a class to prived undo/redo functionality in QHexEdit.
  7. A QUndoCommand represents a single editing action on a document. CharCommand
  8. is responsable for manipulations on single chars. It can insert. replace and
  9. remove characters. A manipulation stores allways to actions
  10. 1. redo (or do) action
  11. 2. undo action.
  12. CharCommand also supports command compression via mergeWidht(). This allows
  13. the user to execute a undo command contation e.g. 3 steps in a single command.
  14. If you for example insert a new byt "34" this means for the editor doing 3
  15. steps: insert a "00", replace it with "03" and the replace it with "34". These
  16. 3 steps are combined into a single step, insert a "34".
  17. */
  18. class CharCommand : public QUndoCommand
  19. {
  20. public:
  21. enum { Id = 1234 };
  22. enum Cmd {insert, remove, replace};
  23. CharCommand(XByteArray * xData, Cmd cmd, int charPos, char newChar,
  24. QUndoCommand *parent=0);
  25. void undo();
  26. void redo();
  27. bool mergeWith(const QUndoCommand *command);
  28. int id() const { return Id; }
  29. private:
  30. XByteArray * _xData;
  31. int _charPos;
  32. bool _wasChanged;
  33. char _newChar;
  34. char _oldChar;
  35. Cmd _cmd;
  36. };
  37. /*! ArrayCommand provides undo/redo functionality for handling binary strings. It
  38. can undo/redo insert, replace and remove binary strins (QByteArrays).
  39. */
  40. class ArrayCommand : public QUndoCommand
  41. {
  42. public:
  43. enum Cmd {insert, remove, replace};
  44. ArrayCommand(XByteArray * xData, Cmd cmd, int baPos, QByteArray newBa=QByteArray(), int len=0,
  45. QUndoCommand *parent=0);
  46. void undo();
  47. void redo();
  48. private:
  49. Cmd _cmd;
  50. XByteArray * _xData;
  51. int _baPos;
  52. int _len;
  53. QByteArray _wasChanged;
  54. QByteArray _newBa;
  55. QByteArray _oldBa;
  56. };
  57. /** \endcond docNever */
  58. #endif // COMMANDS_H