KTextEditor

kateundomanager.h
1 /*
2  SPDX-FileCopyrightText: 2009-2010 Bernhard Beschow <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef KATEUNDOMANAGER_H
8 #define KATEUNDOMANAGER_H
9 
10 #include <QObject>
11 
12 #include <ktexteditor_export.h>
13 
14 #include <QList>
15 
16 namespace KTextEditor
17 {
18 class DocumentPrivate;
19 }
20 class KateUndo;
21 class KateUndoGroup;
22 
23 namespace KTextEditor
24 {
25 class Document;
26 class View;
27 class Cursor;
28 }
29 
30 /**
31  * KateUndoManager implements a document's history. It is in either of the two states:
32  * @li the default state, which allows rolling back and forth the history of a document, and
33  * @li a state in which a new element is being added to the history.
34  *
35  * The state of the KateUndomanager can be switched using editStart() and editEnd().
36  */
37 class KTEXTEDITOR_EXPORT KateUndoManager : public QObject
38 {
39  Q_OBJECT
40 
41 public:
42  /**
43  * Creates a clean undo history.
44  *
45  * @param doc the document the KateUndoManager will belong to
46  */
47  explicit KateUndoManager(KTextEditor::DocumentPrivate *doc);
48 
49  ~KateUndoManager();
50 
51  KTextEditor::Document *document();
52 
53  /**
54  * Returns how many undo() actions can be performed.
55  *
56  * @return the number of undo groups which can be undone
57  */
58  uint undoCount() const;
59 
60  /**
61  * Returns how many redo() actions can be performed.
62  *
63  * @return the number of undo groups which can be redone
64  */
65  uint redoCount() const;
66 
67  /**
68  * Prevent latest KateUndoGroup from being merged with the next one.
69  */
70  void undoSafePoint();
71 
72  /**
73  * Allows or disallows merging of "complex" undo groups.
74  *
75  * When an undo group contains different types of undo items, it is considered
76  * a "complex" group.
77  *
78  * @param allow whether complex merging is allowed
79  */
80  void setAllowComplexMerge(bool allow);
81 
82  bool isActive() const
83  {
84  return m_isActive;
85  }
86 
87  void setModified(bool modified);
88  void updateConfig();
89  void updateLineModifications();
90 
91  /**
92  * Used by the swap file recovery, this function afterwards manipulates
93  * the undo/redo cursors of the last KateUndoGroup.
94  * This function should not be used other than by Kate::SwapFile.
95  * @param undoCursor the undo cursor
96  * @param redoCursor the redo cursor
97  */
98  void setUndoRedoCursorsOfLastGroup(const KTextEditor::Cursor &undoCursor, const KTextEditor::Cursor &redoCursor);
99 
100  /**
101  * Returns the redo cursor of the last undo group.
102  * Needed for the swap file recovery.
103  */
104  KTextEditor::Cursor lastRedoCursor() const;
105 
106 public Q_SLOTS:
107  /**
108  * Undo the latest undo group.
109  *
110  * Make sure isDefaultState() is true when calling this method.
111  */
112  void undo();
113 
114  /**
115  * Redo the latest undo group.
116  *
117  * Make sure isDefaultState() is true when calling this method.
118  */
119  void redo();
120 
121  void clearUndo();
122  void clearRedo();
123 
124  /**
125  * Notify KateUndoManager about the beginning of an edit.
126  */
127  void editStart();
128 
129  /**
130  * Notify KateUndoManager about the end of an edit.
131  */
132  void editEnd();
133 
134  void startUndo();
135  void endUndo();
136 
137  void inputMethodStart();
138  void inputMethodEnd();
139 
140  /**
141  * Notify KateUndoManager that text was inserted.
142  */
143  void slotTextInserted(int line, int col, const QString &s);
144 
145  /**
146  * Notify KateUndoManager that text was removed.
147  */
148  void slotTextRemoved(int line, int col, const QString &s);
149 
150  /**
151  * Notify KateUndoManager that a line was marked as autowrapped.
152  */
153  void slotMarkLineAutoWrapped(int line, bool autowrapped);
154 
155  /**
156  * Notify KateUndoManager that a line was wrapped.
157  */
158  void slotLineWrapped(int line, int col, int length, bool newLine);
159 
160  /**
161  * Notify KateUndoManager that a line was un-wrapped.
162  */
163  void slotLineUnWrapped(int line, int col, int length, bool lineRemoved);
164 
165  /**
166  * Notify KateUndoManager that a line was inserted.
167  */
168  void slotLineInserted(int line, const QString &s);
169 
170  /**
171  * Notify KateUndoManager that a line was removed.
172  */
173  void slotLineRemoved(int line, const QString &s);
174 
175 Q_SIGNALS:
176  void undoChanged();
177  void undoStart(KTextEditor::Document *);
178  void undoEnd(KTextEditor::Document *);
179  void redoStart(KTextEditor::Document *);
180  void redoEnd(KTextEditor::Document *);
181  void isActiveChanged(bool enabled);
182 
183 private Q_SLOTS:
184  /**
185  * @short Add an undo item to the current undo group.
186  *
187  * @param undo undo item to be added, must be non-null
188  */
189  void addUndoItem(KateUndo *undo);
190 
191  void setActive(bool active);
192 
193  void updateModified();
194 
195  void undoCancel();
196  void viewCreated(KTextEditor::Document *, KTextEditor::View *newView);
197 
198 private:
199  KTextEditor::View *activeView();
200 
201 private:
202  KTextEditor::DocumentPrivate *m_document = nullptr;
203  bool m_undoComplexMerge = false;
204  bool m_isActive = true;
205  KateUndoGroup *m_editCurrentUndo = nullptr;
206  QList<KateUndoGroup *> undoItems;
207  QList<KateUndoGroup *> redoItems;
208  // these two variables are for resetting the document to
209  // non-modified if all changes have been undone...
210  KateUndoGroup *lastUndoGroupWhenSaved = nullptr;
211  KateUndoGroup *lastRedoGroupWhenSaved = nullptr;
212  bool docWasSavedWhenUndoWasEmpty = true;
213  bool docWasSavedWhenRedoWasEmpty = true;
214 };
215 
216 #endif
KateUndoManager implements a document&#39;s history.
The Cursor represents a position in a Document.
Definition: cursor.h:71
bool editStart()
Enclose editor actions with editStart() and editEnd() to group them.
Class to manage a group of undo items.
Definition: kateundo.h:419
A KParts derived class representing a text document.
Definition: document.h:185
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Definition: katetextblock.h:20
bool editEnd()
End a editor operation.
Base class for Kate undo commands.
Definition: kateundo.h:33
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:146
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Jun 12 2021 22:57:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.