File indexing completed on 2024-04-28 11:45:29
0001 /* 0002 SPDX-FileCopyrightText: 2009-2010 Bernhard Beschow <bbeschow@cs.tu-berlin.de> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #ifndef KATEUNDOMANAGER_H 0008 #define KATEUNDOMANAGER_H 0009 0010 #include <QObject> 0011 0012 #include <ktexteditor_export.h> 0013 0014 #include <QList> 0015 0016 namespace KTextEditor 0017 { 0018 class DocumentPrivate; 0019 } 0020 class KateUndo; 0021 class KateUndoGroup; 0022 0023 namespace KTextEditor 0024 { 0025 class Document; 0026 class View; 0027 class ViewPrivate; 0028 class Cursor; 0029 } 0030 0031 /** 0032 * KateUndoManager implements a document's history. It is in either of the two states: 0033 * @li the default state, which allows rolling back and forth the history of a document, and 0034 * @li a state in which a new element is being added to the history. 0035 * 0036 * The state of the KateUndomanager can be switched using editStart() and editEnd(). 0037 */ 0038 class KTEXTEDITOR_EXPORT KateUndoManager : public QObject 0039 { 0040 Q_OBJECT 0041 0042 public: 0043 /** 0044 * Creates a clean undo history. 0045 * 0046 * @param doc the document the KateUndoManager will belong to 0047 */ 0048 explicit KateUndoManager(KTextEditor::DocumentPrivate *doc); 0049 0050 ~KateUndoManager() override; 0051 0052 KTextEditor::Document *document(); 0053 0054 /** 0055 * Returns how many undo() actions can be performed. 0056 * 0057 * @return the number of undo groups which can be undone 0058 */ 0059 uint undoCount() const; 0060 0061 /** 0062 * Returns how many redo() actions can be performed. 0063 * 0064 * @return the number of undo groups which can be redone 0065 */ 0066 uint redoCount() const; 0067 0068 /** 0069 * Prevent latest KateUndoGroup from being merged with the next one. 0070 */ 0071 void undoSafePoint(); 0072 0073 /** 0074 * Allows or disallows merging of "complex" undo groups. 0075 * 0076 * When an undo group contains different types of undo items, it is considered 0077 * a "complex" group. 0078 * 0079 * @param allow whether complex merging is allowed 0080 */ 0081 void setAllowComplexMerge(bool allow); 0082 0083 bool isActive() const 0084 { 0085 return m_isActive; 0086 } 0087 0088 void setModified(bool modified); 0089 void updateConfig(); 0090 void updateLineModifications(); 0091 0092 /** 0093 * Used by the swap file recovery, this function afterwards manipulates 0094 * the undo/redo cursors of the last KateUndoGroup. 0095 * This function should not be used other than by Kate::SwapFile. 0096 * @param undoCursor the undo cursor 0097 * @param redoCursor the redo cursor 0098 */ 0099 void setUndoRedoCursorsOfLastGroup(const KTextEditor::Cursor undoCursor, const KTextEditor::Cursor redoCursor); 0100 0101 /** 0102 * Returns the redo cursor of the last undo group. 0103 * Needed for the swap file recovery. 0104 */ 0105 KTextEditor::Cursor lastRedoCursor() const; 0106 0107 public Q_SLOTS: 0108 /** 0109 * Undo the latest undo group. 0110 * 0111 * Make sure isDefaultState() is true when calling this method. 0112 */ 0113 void undo(); 0114 0115 /** 0116 * Redo the latest undo group. 0117 * 0118 * Make sure isDefaultState() is true when calling this method. 0119 */ 0120 void redo(); 0121 0122 void clearUndo(); 0123 void clearRedo(); 0124 0125 /** 0126 * Notify KateUndoManager about the beginning of an edit. 0127 */ 0128 void editStart(); 0129 0130 /** 0131 * Notify KateUndoManager about the end of an edit. 0132 */ 0133 void editEnd(); 0134 0135 void startUndo(); 0136 void endUndo(); 0137 0138 void inputMethodStart(); 0139 void inputMethodEnd(); 0140 0141 /** 0142 * Notify KateUndoManager that text was inserted. 0143 */ 0144 void slotTextInserted(int line, int col, const QString &s); 0145 0146 /** 0147 * Notify KateUndoManager that text was removed. 0148 */ 0149 void slotTextRemoved(int line, int col, const QString &s); 0150 0151 /** 0152 * Notify KateUndoManager that a line was marked as autowrapped. 0153 */ 0154 void slotMarkLineAutoWrapped(int line, bool autowrapped); 0155 0156 /** 0157 * Notify KateUndoManager that a line was wrapped. 0158 */ 0159 void slotLineWrapped(int line, int col, int length, bool newLine); 0160 0161 /** 0162 * Notify KateUndoManager that a line was un-wrapped. 0163 */ 0164 void slotLineUnWrapped(int line, int col, int length, bool lineRemoved); 0165 0166 /** 0167 * Notify KateUndoManager that a line was inserted. 0168 */ 0169 void slotLineInserted(int line, const QString &s); 0170 0171 /** 0172 * Notify KateUndoManager that a line was removed. 0173 */ 0174 void slotLineRemoved(int line, const QString &s); 0175 0176 Q_SIGNALS: 0177 void undoChanged(); 0178 void undoStart(KTextEditor::Document *); 0179 void undoEnd(KTextEditor::Document *); 0180 void redoStart(KTextEditor::Document *); 0181 void redoEnd(KTextEditor::Document *); 0182 void isActiveChanged(bool enabled); 0183 0184 private Q_SLOTS: 0185 /** 0186 * @short Add an undo item to the current undo group. 0187 * 0188 * @param undo undo item to be added, must be non-null 0189 */ 0190 void addUndoItem(KateUndo *undo); 0191 0192 void setActive(bool active); 0193 0194 void updateModified(); 0195 0196 void undoCancel(); 0197 void viewCreated(KTextEditor::Document *, KTextEditor::View *newView) const; 0198 0199 private: 0200 KTEXTEDITOR_NO_EXPORT 0201 KTextEditor::ViewPrivate *activeView(); 0202 0203 private: 0204 KTextEditor::DocumentPrivate *m_document = nullptr; 0205 bool m_undoComplexMerge = false; 0206 bool m_isActive = true; 0207 KateUndoGroup *m_editCurrentUndo = nullptr; 0208 QList<KateUndoGroup *> undoItems; 0209 QList<KateUndoGroup *> redoItems; 0210 // these two variables are for resetting the document to 0211 // non-modified if all changes have been undone... 0212 KateUndoGroup *lastUndoGroupWhenSaved = nullptr; 0213 KateUndoGroup *lastRedoGroupWhenSaved = nullptr; 0214 bool docWasSavedWhenUndoWasEmpty = true; 0215 bool docWasSavedWhenRedoWasEmpty = true; 0216 0217 // saved undo items that are used to restore state on doc reload 0218 QList<KateUndoGroup *> savedUndoItems; 0219 QList<KateUndoGroup *> savedRedoItems; 0220 QByteArray docChecksumBeforeReload; 0221 }; 0222 0223 #endif