KTextEditor

kateundo.h
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2011 Dominik Haumann <[email protected]>
4  Copyright (C) 2009-2010 Bernhard Beschow <[email protected]>
5  Copyright (C) 2002 John Firebaugh <[email protected]>
6  Copyright (C) 2001 Christoph Cullmann <[email protected]>
7  Copyright (C) 2001 Joseph Wenninger <[email protected]>
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Library General Public
11  License as published by the Free Software Foundation; either
12  version 2 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Library General Public License for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with this library; see the file COPYING.LIB. If not, write to
21  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  Boston, MA 02110-1301, USA.
23 */
24 
25 #ifndef kate_undo_h
26 #define kate_undo_h
27 
28 #include <QList>
29 
30 #include <QBitArray>
31 #include <ktexteditor/range.h>
32 
33 class KateUndoManager;
34 namespace KTextEditor
35 {
36 class DocumentPrivate;
37 }
38 
39 namespace KTextEditor
40 {
41 class View;
42 }
43 
47 class KateUndo
48 {
49 public:
54  explicit KateUndo(KTextEditor::DocumentPrivate *document);
55 
59  virtual ~KateUndo();
60 
61 public:
66 
67 public:
73  virtual bool isEmpty() const;
74 
82  virtual bool mergeWith(const KateUndo *undo);
83 
87  virtual void undo() = 0;
88 
92  virtual void redo() = 0;
93 
98  virtual KateUndo::UndoType type() const = 0;
99 
100 protected:
105  inline KTextEditor::DocumentPrivate *document()
106  {
107  return m_document;
108  }
109 
110 private:
114  KTextEditor::DocumentPrivate *m_document;
115 
116  //
117  // Line modification system
118  //
119 public:
120  enum ModificationFlag { UndoLine1Modified = 1, UndoLine2Modified = 2, UndoLine1Saved = 4, UndoLine2Saved = 8, RedoLine1Modified = 16, RedoLine2Modified = 32, RedoLine1Saved = 64, RedoLine2Saved = 128 };
121 
122  inline void setFlag(ModificationFlag flag)
123  {
124  m_lineModFlags |= flag;
125  }
126 
127  inline void unsetFlag(ModificationFlag flag)
128  {
129  m_lineModFlags &= (~flag);
130  }
131 
132  inline bool isFlagSet(ModificationFlag flag) const
133  {
134  return m_lineModFlags & flag;
135  }
136 
137  virtual void updateUndoSavedOnDiskFlag(QBitArray &lines)
138  {
139  Q_UNUSED(lines)
140  }
141  virtual void updateRedoSavedOnDiskFlag(QBitArray &lines)
142  {
143  Q_UNUSED(lines)
144  }
145 
146 private:
147  uchar m_lineModFlags = 0x0;
148 };
149 
150 class KateEditInsertTextUndo : public KateUndo
151 {
152 public:
153  explicit KateEditInsertTextUndo(KTextEditor::DocumentPrivate *document, int line, int col, const QString &text);
154 
158  bool isEmpty() const override;
159 
163  void undo() override;
164 
168  void redo() override;
169 
173  bool mergeWith(const KateUndo *undo) override;
174 
178  KateUndo::UndoType type() const override
179  {
180  return KateUndo::editInsertText;
181  }
182 
183 protected:
184  inline int len() const
185  {
186  return m_text.length();
187  }
188  inline int line() const
189  {
190  return m_line;
191  }
192 
193 private:
194  const int m_line;
195  const int m_col;
196  QString m_text;
197 };
198 
199 class KateEditRemoveTextUndo : public KateUndo
200 {
201 public:
202  explicit KateEditRemoveTextUndo(KTextEditor::DocumentPrivate *document, int line, int col, const QString &text);
203 
207  bool isEmpty() const override;
208 
212  void undo() override;
213 
217  void redo() override;
218 
222  bool mergeWith(const KateUndo *undo) override;
223 
227  KateUndo::UndoType type() const override
228  {
229  return KateUndo::editRemoveText;
230  }
231 
232 protected:
233  inline int len() const
234  {
235  return m_text.length();
236  }
237  inline int line() const
238  {
239  return m_line;
240  }
241 
242 private:
243  const int m_line;
244  int m_col;
245  QString m_text;
246 };
247 
248 class KateEditMarkLineAutoWrappedUndo : public KateUndo
249 {
250 public:
251  explicit KateEditMarkLineAutoWrappedUndo(KTextEditor::DocumentPrivate *document, int line, bool autowrapped)
252  : KateUndo(document)
253  , m_line(line)
254  , m_autowrapped(autowrapped)
255  {
256  }
257 
261  void undo() override;
262 
266  void redo() override;
267 
271  KateUndo::UndoType type() const override
272  {
273  return KateUndo::editMarkLineAutoWrapped;
274  }
275 
276 private:
277  const int m_line;
278  const bool m_autowrapped;
279 };
280 
281 class KateEditWrapLineUndo : public KateUndo
282 {
283 public:
284  explicit KateEditWrapLineUndo(KTextEditor::DocumentPrivate *document, int line, int col, int len, bool newLine);
285 
289  void undo() override;
290 
294  void redo() override;
295 
299  KateUndo::UndoType type() const override
300  {
301  return KateUndo::editWrapLine;
302  }
303 
304 protected:
305  inline int line() const
306  {
307  return m_line;
308  }
309 
310 private:
311  const int m_line;
312  const int m_col;
313  const int m_len;
314  const bool m_newLine;
315 };
316 
317 class KateEditUnWrapLineUndo : public KateUndo
318 {
319 public:
320  explicit KateEditUnWrapLineUndo(KTextEditor::DocumentPrivate *document, int line, int col, int len, bool removeLine);
321 
325  void undo() override;
326 
330  void redo() override;
331 
335  KateUndo::UndoType type() const override
336  {
337  return KateUndo::editUnWrapLine;
338  }
339 
340 protected:
341  inline int line() const
342  {
343  return m_line;
344  }
345 
346 private:
347  const int m_line;
348  const int m_col;
349  const int m_len;
350  const bool m_removeLine;
351 };
352 
353 class KateEditInsertLineUndo : public KateUndo
354 {
355 public:
356  explicit KateEditInsertLineUndo(KTextEditor::DocumentPrivate *document, int line, const QString &text);
357 
361  void undo() override;
362 
366  void redo() override;
367 
371  KateUndo::UndoType type() const override
372  {
373  return KateUndo::editInsertLine;
374  }
375 
376 protected:
377  inline int line() const
378  {
379  return m_line;
380  }
381 
382 private:
383  const int m_line;
384  const QString m_text;
385 };
386 
387 class KateEditRemoveLineUndo : public KateUndo
388 {
389 public:
390  explicit KateEditRemoveLineUndo(KTextEditor::DocumentPrivate *document, int line, const QString &text);
391 
395  void undo() override;
396 
400  void redo() override;
401 
405  KateUndo::UndoType type() const override
406  {
407  return KateUndo::editRemoveLine;
408  }
409 
410 protected:
411  inline int line() const
412  {
413  return m_line;
414  }
415 
416 private:
417  const int m_line;
418  const QString m_text;
419 };
420 
425 {
426 public:
431  explicit KateUndoGroup(KateUndoManager *manager, const KTextEditor::Cursor &cursorPosition, const KTextEditor::Range &selectionRange);
432 
436  ~KateUndoGroup();
437 
438  KateUndoGroup(const KateUndoGroup &) = delete;
439  KateUndoGroup &operator=(const KateUndoGroup &) = delete;
440 
441 public:
445  void undo(KTextEditor::View *view);
446 
450  void redo(KTextEditor::View *view);
451 
452  void editEnd(const KTextEditor::Cursor &cursorPosition, const KTextEditor::Range &selectionRange);
453 
460  bool merge(KateUndoGroup *newGroup, bool complex);
461 
465  void safePoint(bool safePoint = true);
466 
470  bool isEmpty() const
471  {
472  return m_items.isEmpty();
473  }
474 
478  void flagSavedAsModified();
479 
480  void markUndoAsSaved(QBitArray &lines);
481  void markRedoAsSaved(QBitArray &lines);
482 
486  inline void setUndoCursor(const KTextEditor::Cursor &cursor)
487  {
488  m_undoCursor = cursor;
489  }
490 
494  inline void setRedoCursor(const KTextEditor::Cursor &cursor)
495  {
496  m_redoCursor = cursor;
497  }
498 
499  inline const KTextEditor::Cursor &redoCursor() const
500  {
501  return m_redoCursor;
502  }
503 
504 private:
505  KTextEditor::Document *document();
506 
511  KateUndo::UndoType singleType() const;
512 
518  bool isOnlyType(KateUndo::UndoType type) const;
519 
520 public:
525  void addItem(KateUndo *u);
526 
527 private:
528  KateUndoManager *const m_manager;
529 
533  QList<KateUndo *> m_items;
534 
538  bool m_safePoint = false;
539 
543  const KTextEditor::Range m_undoSelection;
544 
548  KTextEditor::Range m_redoSelection;
549 
553  KTextEditor::Cursor m_undoCursor;
554 
558  KTextEditor::Cursor m_redoCursor;
559 };
560 
561 #endif
bool editWrapLine(int line, int col, bool newLine=true, bool *newLineAdded=nullptr)
Wrap line.
void setUndoCursor(const KTextEditor::Cursor &cursor)
Set the undo cursor to cursor.
Definition: kateundo.h:486
UndoType
Types for undo items.
Definition: kateundo.h:65
bool isEmpty() const
is this undogroup empty?
Definition: kateundo.h:470
bool editRemoveLine(int line)
Remove a line.
KTextEditor::DocumentPrivate * document()
Return the document the undo item belongs to.
Definition: kateundo.h:105
KateUndoManager implements a document&#39;s history.
The Cursor represents a position in a Document.
Definition: cursor.h:85
Class to manage a group of undo items.
Definition: kateundo.h:424
void setRedoCursor(const KTextEditor::Cursor &cursor)
Set the redo cursor to cursor.
Definition: kateundo.h:494
bool editUnWrapLine(int line, bool removeLine=true, int length=0)
Unwrap line.
A KParts derived class representing a text document.
Definition: document.h:199
bool editMarkLineAutoWrapped(int line, bool autowrapped)
Mark line as autowrapped.
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
An object representing a section of text, from one Cursor to another.
bool editEnd()
End a editor operation.
bool editInsertLine(int line, const QString &s)
Insert a string at the given line.
Base class for Kate undo commands.
Definition: kateundo.h:47
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:155
bool editInsertText(int line, int col, const QString &s)
Add a string in the given line/column.
bool editRemoveText(int line, int col, int len)
Remove a string in the given line/column.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 7 2020 22:55:45 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.