1 /****************************************************************************
2 **
3 ** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
4 **
5 ** This file is part of the Edyuk project <http://edyuk.org>
6 **
7 ** This file may be used under the terms of the GNU General Public License
8 ** version 3 as published by the Free Software Foundation and appearing in the
9 ** file GPL.txt included in the packaging of this file.
10 **
11 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 **
14 ****************************************************************************/
15 
16 #ifndef _QDOCUMENT_COMMAND_H_
17 #define _QDOCUMENT_COMMAND_H_
18 
19 #include "qce-config.h"
20 
21 /*!
22 	\file qdocumentcommand.h
23 	\brief Definition of the QDocumentCommand class
24 */
25 
26 #include <QUndoCommand>
27 
28 #include "qdocument.h"
29 
30 class QDocumentLine;
31 class QDocumentLineHandle;
32 class QDocumentCursorHandle;
33 
34 class QCE_EXPORT QDocumentCommand : public QUndoCommand
35 {
36 	public:
37 		enum Command
38 		{
39 			None,
40 			Insert,
41 			Erase,
42 			Replace,
43 			Custom
44 		};
45 
46 		struct TextCommandData
47 		{
48 			QString begin, end;
49 			int lineNumber, startOffset, endOffset;
50 			QList<QDocumentLineHandle*> handles;
51 		};
52 
53 		QDocumentCommand(Command c, QDocument *d, QDocumentCommand *p = 0);
54 		virtual ~QDocumentCommand();
55 
56 		virtual int id() const;
57 
58 		virtual bool mergeWith(const QUndoCommand *command);
59 
60 		virtual void redo();
61 		virtual void undo();
62 
63 		bool isSilent() const;
64 		void setSilent(bool y);
65 
66 		bool keepAnchor() const;
67 		void setKeepAnchor(bool y);
68 
69 		void setTargetCursor(QDocumentCursorHandle *h);
70 
71 		void setRedoOffset(int off);
72 		void setUndoOffset(int off);
73 
74 		static bool isAutoUpdated(const QDocumentCursorHandle *h);
75 		static void enableAutoUpdate(QDocumentCursorHandle *h);
76 		static void disableAutoUpdate(QDocumentCursorHandle *h);
77 		static void discardHandlesFromDocument(QDocument *d);
78 
79 	protected:
80 		bool m_state, m_first;
81 		QDocument *m_doc;
82 		int m_redoOffset, m_undoOffset;
83 
84 		void markRedone(QDocumentLineHandle *h, bool firstTime);
85 		void markUndone(QDocumentLineHandle *h);
86 
87 		void updateTarget(int l, int offset);
88 
89 		void insertText(int line, int pos, const QString& s);
90 		void removeText(int line, int pos, int length);
91 
92 		void insertLines(int after, const QList<QDocumentLineHandle*>& l);
93 		void removeLines(int after, int n);
94 
95 		void updateCursorsOnInsertion(int line, int column, int prefixLength, int numLines, int suffixLength);
96 		void updateCursorsOnDeletion(int line, int column, int prefixLength, int numLines, int suffixLength);
97 
98 	private:
99 		bool m_silent;
100 		bool m_keepAnchor;
101 		Command m_command;
102 		QDocumentCursorHandle *m_cursor;
103 
104 		static QList<QDocumentCursorHandle*> m_autoUpdated;
105 };
106 
107 Q_DECLARE_TYPEINFO(QDocumentCommand::TextCommandData, Q_MOVABLE_TYPE);
108 
109 class QCE_EXPORT QDocumentInsertCommand : public QDocumentCommand
110 {
111 	public:
112 		QDocumentInsertCommand(	int l, int offset,
113 								const QString& text,
114 								QDocument *doc,
115 								QDocumentCommand *p = 0);
116 
117 		virtual ~QDocumentInsertCommand();
118 
119 		virtual bool mergeWith(const QUndoCommand *command);
120 
121 		virtual void redo();
122 		virtual void undo();
123 
124 	private:
125 		TextCommandData m_data;
126 };
127 
128 class QCE_EXPORT QDocumentEraseCommand : public QDocumentCommand
129 {
130 	public:
131 		QDocumentEraseCommand(	int bl, int bo,
132 								int el, int eo,
133 								QDocument *doc,
134 								QDocumentCommand *p = 0);
135 
136 		virtual ~QDocumentEraseCommand();
137 
138 		virtual bool mergeWith(const QUndoCommand *command);
139 
140 		virtual void redo();
141 		virtual void undo();
142 
143 	private:
144 		TextCommandData m_data;
145 };
146 
147 class QCE_EXPORT QDocumentCommandBlock : public QDocumentCommand
148 {
149 	public:
150 		QDocumentCommandBlock(QDocument *d);
151 		virtual ~QDocumentCommandBlock();
152 
153 		virtual void redo();
154 		virtual void undo();
155 
156 		void setWeakLock(bool l);
157 		bool isWeakLocked() const;
158 
159 		virtual void addCommand(QDocumentCommand *c);
160 		virtual void removeCommand(QDocumentCommand *c);
161 
162 	private:
163 		bool m_weakLocked;
164 		QList<QDocumentCommand*> m_commands;
165 };
166 
167 #endif
168