1 /* This file is part of the KDE project
2    Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
3                       Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
4                  2006 Martin Pfeiffer <hubipete@gmx.net>
5                  2009 Jeremias Epperlein <jeeree@web.de>
6 
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public
9    License as published by the Free Software Foundation; either
10    version 2 of the License, or (at your option) any later version.
11 
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16 
17    You should have received a copy of the GNU Library General Public License
18    along with this library; see the file COPYING.LIB.  If not, write to
19    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, USA.
21 */
22 
23 #ifndef FORMULACOMMAND_H
24 #define FORMULACOMMAND_H
25 
26 #include <kundo2command.h>
27 #include <QList>
28 #include <QHash>
29 #include <QMetaType>
30 #include "FormulaCursor.h"
31 class BasicElement;
32 class TokenElement;
33 class FormulaData;
34 class GlyphElement;
35 class TableElement;
36 class TableRowElement;
37 
38 /**
39  *
40  * All FormulaCommands are used to manipulate the formula in various ways.
41  * They all provide a redo and undo method as well as changeCursor method
42  * which sets the cursor after the current action. A extra method for this is necessary,
43  * as there might be no cursor when a undo/redo is done, because the tool was deactivated
44  *
45  **/
46 
47 class FormulaCommand :  public KUndo2Command {
48 public:
49     explicit FormulaCommand(KUndo2Command *parent=0);
50 
51     virtual void changeCursor(FormulaCursor& cursor, bool undo) const;
52 
53     void setUndoCursorPosition(const FormulaCursor& position);
54     void setRedoCursorPosition(const FormulaCursor& position);
55 
56 protected:
57     bool m_done;
58 
59 private:
60     FormulaCursor m_undoCursorPosition;
61     FormulaCursor m_redoCursorPosition;
62 };
63 
Q_DECLARE_METATYPE(FormulaCommand *)64 Q_DECLARE_METATYPE(FormulaCommand*)
65 
66 
67 class FormulaCommandReplaceText : public FormulaCommand {
68 public:
69     FormulaCommandReplaceText( TokenElement* owner, int position,int length, const QString& added , KUndo2Command *parent=0);
70 
71     ~FormulaCommandReplaceText() override;
72 
73     /// Execute the command
74     void redo() override;
75 
76     /// Revert the actions done in redo()
77     void undo() override;
78 
79 private:
80     /// The BasicElement that owns the newly added Text
81     TokenElement* m_ownerElement;
82 
83     /// The position inside m_ownerElement
84     int m_position;
85 
86     int m_length;
87 
88     int m_glyphpos;
89 
90     /// The list of added elements
91     QString m_added;
92 
93     QString m_removed;
94 
95     QList<GlyphElement*> m_removedGlyphs;
96 };
97 
98 class FormulaCommandReplaceElements : public FormulaCommand {
99 public:
100     FormulaCommandReplaceElements( RowElement* owner, int position, int length, QList<BasicElement*> elements , bool wrap=false, KUndo2Command *parent=0);
101 
102     ~FormulaCommandReplaceElements() override;
103 
104     /// Execute the command
105     void redo() override;
106 
107     /// Revert the actions done in redo()
108     void undo() override;
109 
110 private:
111     /// The BasicElement that owns the newly added Text
112     RowElement* m_ownerElement;
113 
114     /// The position inside m_ownerElement
115     int m_position;
116 
117     int m_placeholderPosition;
118 
119     int m_length;
120 
121     bool m_wrap;
122 
123     RowElement* m_placeholderParent;
124 
125 //     BasicElement* m_placeholder;
126 
127     /// The list of added elements
128     QList<BasicElement*> m_added;
129 
130     /// The list of removed elements
131     QList<BasicElement*> m_removed;
132 };
133 
134 class FormulaCommandLoad : public FormulaCommand {
135 public:
136     FormulaCommandLoad( FormulaData* data, FormulaElement* newelement, KUndo2Command *parent=0);
137 
138     ~FormulaCommandLoad () override;
139 
140     /// Execute the command
141     void redo() override;
142 
143     /// Revert the actions done in redo()
144     void undo() override;
145 
146 private:
147     FormulaData* m_data;
148     FormulaElement* m_oldel;
149     FormulaElement* m_newel;
150 };
151 
152 class FormulaCommandReplaceRow : public FormulaCommand {
153 public:
154     FormulaCommandReplaceRow ( FormulaData* data, FormulaCursor oldPosition, TableElement* table, int number, int oldlength, int newlength );
155 
156     ~FormulaCommandReplaceRow () override;
157 
158     /// Execute the command
159     void redo() override;
160 
161     /// Revert the actions done in redo()
162     void undo() override;
163 
164 private:
165     FormulaData* m_data;
166     TableElement* m_table;
167     TableRowElement* m_empty;
168     int m_number;
169     QList<BasicElement*> m_newRows;
170     QList<BasicElement*> m_oldRows;
171 };
172 
173 class FormulaCommandReplaceColumn : public FormulaCommand {
174 public:
175     FormulaCommandReplaceColumn ( FormulaData* data, FormulaCursor oldPosition, TableElement* table, int number, int oldlength, int newlength );
176 
177     ~FormulaCommandReplaceColumn () override;
178 
179     /// Execute the command
180     void redo() override;
181 
182     /// Revert the actions done in redo()
183     void undo() override;
184 
185 private:
186     FormulaData* m_data;
187 
188     ///the table we are manipulating
189     TableElement* m_table;
190 
191     ///used to hold the new empty row, if we remove the whole table
192     TableRowElement* m_empty;
193 
194     ///used to store the old rows, if we remove the whole table
195     QList<BasicElement*> m_oldRows;
196 
197     ///the position where we start to insert / remove rows
198     int m_position;
199 
200     ///used to store the old columns
201     QList< QList<BasicElement*> > m_newColumns;
202 
203     ///used to store the new columns
204     QList< QList<BasicElement*> > m_oldColumns;
205 };
206 
207 
208 
209 // /**
210 //  * @short The command for changes of an element's attributes
211 //  *
212 //  * Whenever the user changes the attributes assigned to an element an instance of this
213 //  * class is created to make it possible to revert the changes. The former attributes
214 //  * are stored in m_oldAttributes.
215 //  */
216 // class FormulaCommandAttribute : public KUndo2Command {
217 // public:
218 //     /**
219 //      * The constructor
220 //      * @param cursor The FormulaCursor where the elements will be replaced
221 //      * @param attributes The list of the old attributes
222 //      */
223 //     FormulaCommandAttribute( FormulaCursor* cursor, QHash<QString,QString> attributes );
224 //
225 //     /// Execute the command
226 //     void redo();
227 //
228 //     /// Revert the actions done in redo()
229 //     void undo();
230 //
231 // private:
232 //     /// The BasicElement whose attributes have been changed
233 //     BasicElement* m_ownerElement;
234 //
235 //     /// All attributes that are set newly
236 //     QHash<QString,QString> m_attributes;
237 //
238 //     /// All attributes the element had before
239 //     QHash<QString,QString> m_oldAttributes;
240 // };
241 
242 #endif // FORMULACOMMAND_H
243