TDE graphics utilities
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kpcommandhistory.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. Copyright (c) 2003,2004,2005 Clarence Dang <dang@kde.org>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  13. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  17. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #ifndef KP_COMMAND_HISTORY_H
  24. #define KP_COMMAND_HISTORY_H
  25. #include <tqobject.h>
  26. #include <tqstring.h>
  27. #include <tqvaluelist.h>
  28. class TDEActionCollection;
  29. class TDEToolBarPopupAction;
  30. class kpDocument;
  31. class kpMainWindow;
  32. class kpSelection;
  33. class kpViewManager;
  34. class kpCommand
  35. {
  36. public:
  37. kpCommand (kpMainWindow *mainWindow);
  38. virtual ~kpCommand ();
  39. public:
  40. virtual TQString name () const = 0;
  41. // Returns the estimated size in bytes.
  42. //
  43. // You only have to factor in the size of variables that change according
  44. // to the amount of input e.g. pixmap size, text size. There is no need
  45. // to include the size of O(1) variables unless they are huge.
  46. //
  47. // If in doubt, return the largest possible amount of memory that your
  48. // command will take. This is better than making the user unexpectedly
  49. // run out of memory.
  50. virtual int size () const = 0;
  51. virtual void execute () = 0;
  52. virtual void unexecute () = 0;
  53. protected:
  54. kpMainWindow *mainWindow () const;
  55. kpDocument *document () const;
  56. kpSelection *selection () const;
  57. kpViewManager *viewManager () const;
  58. protected:
  59. kpMainWindow *m_mainWindow;
  60. };
  61. class kpNamedCommand : public kpCommand
  62. {
  63. public:
  64. kpNamedCommand (const TQString &name, kpMainWindow *mainWindow);
  65. virtual ~kpNamedCommand ();
  66. virtual TQString name () const;
  67. protected:
  68. TQString m_name;
  69. };
  70. class kpMacroCommand : public kpNamedCommand
  71. {
  72. public:
  73. kpMacroCommand (const TQString &name, kpMainWindow *mainWindow);
  74. virtual ~kpMacroCommand ();
  75. //
  76. // kpCommand Interface
  77. //
  78. virtual int size () const;
  79. virtual void execute ();
  80. virtual void unexecute ();
  81. //
  82. // Interface
  83. //
  84. void addCommand (kpCommand *command);
  85. protected:
  86. TQValueList <kpCommand *> m_commandList;
  87. private:
  88. class kpMacroCommandPrivate *d;
  89. };
  90. // Clone of KCommandHistory with features required by KolourPaint:
  91. // - nextUndoCommand()/nextRedoCommand()
  92. // - undo/redo history limited by both number and size
  93. //
  94. // Features not required by KolourPaint (e.g. commandExecuted()) are not
  95. // implemented and undo limit == redo limit. So compared to
  96. // KCommandHistory, this is only "almost source compatible".
  97. class kpCommandHistoryBase : public TQObject
  98. {
  99. Q_OBJECT
  100. public:
  101. kpCommandHistoryBase (bool doReadConfig, TDEActionCollection *ac);
  102. virtual ~kpCommandHistoryBase ();
  103. public:
  104. // (provided for compatibility with KCommandHistory)
  105. int undoLimit () const;
  106. void setUndoLimit (int limit);
  107. int undoMinLimit () const;
  108. void setUndoMinLimit (int limit);
  109. int undoMaxLimit () const;
  110. void setUndoMaxLimit (int limit);
  111. int undoMaxLimitSizeLimit () const;
  112. void setUndoMaxLimitSizeLimit (int sizeLimit);
  113. public:
  114. // Read and write above config
  115. void readConfig ();
  116. void writeConfig ();
  117. public:
  118. void addCommand (kpCommand *command, bool execute = true);
  119. void clear ();
  120. protected slots:
  121. // (same as undo() & redo() except they don't call
  122. // trimCommandListsUpdateActions())
  123. void undoInternal ();
  124. void redoInternal ();
  125. public slots:
  126. virtual void undo ();
  127. virtual void redo ();
  128. virtual void undoUpToNumber (int which);
  129. virtual void redoUpToNumber (int which);
  130. protected:
  131. TQString undoActionText () const;
  132. TQString redoActionText () const;
  133. void trimCommandListsUpdateActions ();
  134. void trimCommandList (TQValueList <kpCommand *> *commandList);
  135. void trimCommandLists ();
  136. void updateActions ();
  137. public:
  138. kpCommand *nextUndoCommand () const;
  139. kpCommand *nextRedoCommand () const;
  140. void setNextUndoCommand (kpCommand *command);
  141. public slots:
  142. virtual void documentSaved ();
  143. signals:
  144. void documentRestored ();
  145. protected:
  146. TDEToolBarPopupAction *m_actionUndo, *m_actionRedo;
  147. // (Front element is the next one)
  148. TQValueList <kpCommand *> m_undoCommandList;
  149. TQValueList <kpCommand *> m_redoCommandList;
  150. int m_undoMinLimit, m_undoMaxLimit, m_undoMaxLimitSizeLimit;
  151. // What you have to do to get back to the document's unmodified state:
  152. // * -x: must Undo x times
  153. // * 0: unmodified
  154. // * +x: must Redo x times
  155. // * INT_MAX: can never become unmodified again
  156. //
  157. // ASSUMPTION: will never have INT_MAX commands in any list.
  158. int m_documentRestoredPosition;
  159. private:
  160. class kpCommandHistoryBasePrivate *d;
  161. };
  162. // Intercepts Undo/Redo requests:
  163. //
  164. // If the user is currently drawing a shape, it cancels it.
  165. // Else it passes on the Undo/Redo request to kpCommandHistoryBase.
  166. //
  167. // TODO: This is wrong. It won't work if the Undo action is disabled,
  168. // for instance.
  169. //
  170. // Maybe the real solution is to call kpCommandHistoryBase::addCommand()
  171. // as _soon_ as the shape starts - not after it ends. But the
  172. // trouble with this solution is that if the user Undoes/cancels
  173. // the shape s/he's currently drawing, it would replace a Redo
  174. // slot in the history. Arguably you shouldn't be able to Redo
  175. // something you never finished drawing.
  176. //
  177. // The solution is to add this functionality to kpCommandHistoryBase.
  178. class kpCommandHistory : public kpCommandHistoryBase
  179. {
  180. Q_OBJECT
  181. public:
  182. kpCommandHistory (bool doReadConfig, kpMainWindow *mainWindow);
  183. virtual ~kpCommandHistory ();
  184. public slots:
  185. virtual void undo ();
  186. virtual void redo ();
  187. protected:
  188. kpMainWindow *m_mainWindow;
  189. };
  190. #endif // KP_COMMAND_HISTORY_H