edbee - Qt Editor Library
textundostack.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <QObject>
9 #include <QMap>
10 #include <QStack>
11 
12 namespace edbee {
13 
14 class Change;
15 class TextDocument;
16 class TextEditorController;
17 class ChangeGroup;
18 
19 
49 
50 class TextUndoStack : public QObject
51 {
52  Q_OBJECT
53 
54 public:
55 
61 
62  };
63 
64 public:
65  explicit TextUndoStack( TextDocument* doc, QObject* parent = 0);
66  virtual ~TextUndoStack();
67  void clear();
68 
69  void registerContoller( TextEditorController* controller );
70  void unregisterController( TextEditorController* controller );
71  bool isControllerRegistered( TextEditorController* controller );
72 
73  void beginUndoGroup( ChangeGroup* group );
75  void endUndoGroup(int coalesceId , bool flatten);
77  int undoGroupLevel();
78 
82 
83  Change* giveChange( Change* change, int coalesceId );
84 
85  bool canUndo( TextEditorController* controller=0 );
86  bool canRedo( TextEditorController* controller=0 );
87 
88  void undo( TextEditorController* controller=0, bool controllerOnly=false );
89  void redo( TextEditorController* controller=0, bool controllerOnly=false );
90 
91  bool isCollectionEnabled() { return collectionEnabled_; }
92  void setCollectionEnabled( bool enabled ) { collectionEnabled_ = enabled; }
93 
94  bool isUndoRunning() { return undoRunning_; }
95  bool isRedoRunning() { return redoRunning_; }
96 
97  int size();
98  Change* at(int idx);
99  int currentIndex( TextEditorController* controller=0 );
100  int lastIndex( TextEditorController* controller=0 );
101  Change* last(TextEditorController* controller=0 );
102 
103  int sizeInDocChanges();
105 
106  Change* findRedoChange( TextEditorController* controller=0);
107  Change* findUndoChange( TextEditorController* controller=0 );
108 
109  void setPersisted(bool enabled);
110  bool isPersisted();
111  int persistedIndex();
112 
113  TextDocument* document() { return documentRef_; }
114 
115  QString dumpStack();
116  void dumpStackInternal();
117 
118 protected:
119  int findRedoIndex( int index, TextEditorController* controller=0 );
120  int findUndoIndex( int index, TextEditorController* controller=0 );
121  void clearRedo( TextEditorController* controller );
122  bool undoControllerChange( TextEditorController* controller );
123  void undoDocumentChange();
124  bool redoControllerChange( TextEditorController* controller );
125  void redoDocumentChange();
126 
127  void setPersistedIndex( int index );
128  void setChangeIndex( int index );
129 
130 signals:
131 
132  void undoGroupStarted( edbee::ChangeGroup* group );
133 
136  void undoGroupEnded( int coalesceId, bool merged, int action );
137  void changeAdded( edbee::Change* change );
138 // void changeMerged( edbee::TextChange* oldChange, edbee::TextChange* change );
139  void undoExecuted( edbee::Change* change );
140  void redoExecuted( edbee::Change* change );
141 
143  void persistedChanged(bool persisted);
144 
145 
146 private:
147  void clearHistoryLists();
148 
149 
150  TextDocument* documentRef_;
151 
152  QList<Change*> changeList_;
153  int changeIndex_;
154  QMap<TextEditorController*,int> controllerIndexMap_;
155  int persistedIndex_;
156 
157  QStack<ChangeGroup*> undoGroupStack_;
158  QStack<int> lastCoalesceIdStack_;
159 
160 // int undoGroupLevel_; ///< The undo group level
161 // TextChangeGroup* undoGroup_; ///< The current undo group
162  bool collectionEnabled_;
163 
164  bool undoRunning_;
165  bool redoRunning_;
166 };
167 
168 } // edbee
void dumpStackInternal()
Dumps the internal stack.
Definition: textundostack.cpp:532
void setLastCoalesceIdAtCurrentLevel(int id)
Sets the last coalesceId at the current level.
Definition: textundostack.cpp:184
This is the base and abstract class of a text document A TextDocument is the model part of the editor...
Definition: textdocument.h:40
An undoable-command-group.
Definition: change.h:84
int size()
returns the number of changes on the stack
Definition: textundostack.cpp:350
TextUndoStack(TextDocument *doc, QObject *parent=0)
Constructs the main undostack.
Definition: textundostack.cpp:20
int findRedoIndex(int index, TextEditorController *controller=0)
This method finds the next redo-item.
Definition: textundostack.cpp:544
void setPersisted(bool enabled)
Marks the current documentIndex as persisted.
Definition: textundostack.cpp:445
int currentIndex(TextEditorController *controller=0)
This method return the index that&#39;s active for the given controller The currentIndex points directly ...
Definition: textundostack.cpp:365
void clear()
clears both stacks
Definition: textundostack.cpp:58
bool isCollectionEnabled()
Definition: textundostack.h:91
void undoExecuted(edbee::Change *change)
Definition: moc_textundostack.cpp:210
bool canRedo(TextEditorController *controller=0)
Should check if a redo operation can be performed. When no controller is given a document-redo is che...
Definition: textundostack.cpp:296
int findUndoIndex(int index, TextEditorController *controller=0)
This method finds the index of the given stackitem from the given index.
Definition: textundostack.cpp:562
bool isRedoRunning()
Definition: textundostack.h:95
bool undoControllerChange(TextEditorController *controller)
This method undos the given controller change. This method does NOT undo document changes...
Definition: textundostack.cpp:616
The group has been ungrouped, the single change has been added to the stack.
Definition: textundostack.h:58
Change * findRedoChange(TextEditorController *controller=0)
This method returns the current redo change. This maybe the change for the given context or the docum...
Definition: textundostack.cpp:381
virtual ~TextUndoStack()
the undo stack items
Definition: textundostack.cpp:38
TextDocument * document()
Definition: textundostack.h:113
EndUndoGroupAction
This enumeration is signaled to the listeners to notify what happend when ending an undo group...
Definition: textundostack.h:57
Change * at(int idx)
returns the change at the given index
Definition: textundostack.cpp:357
bool isUndoRunning()
Definition: textundostack.h:94
void unregisterController(TextEditorController *controller)
Unregisters the given controller.
Definition: textundostack.cpp:82
bool isControllerRegistered(TextEditorController *controller)
Checks if the given controller is registered.
Definition: textundostack.cpp:89
void undoGroupEnded(int coalesceId, bool merged, int action)
This signal is fired when the group is ended. Warning, when the group is merged the group pointer wil...
Definition: moc_textundostack.cpp:196
Copyright 2011-2013 - Reliable Bits Software by Blommers IT.
Definition: commentcommand.cpp:22
void undo(TextEditorController *controller=0, bool controllerOnly=false)
performs an undo operation
Definition: textundostack.cpp:304
void redo(TextEditorController *controller=0, bool controllerOnly=false)
performs the redo operation for the given controller
Definition: textundostack.cpp:329
void setCollectionEnabled(bool enabled)
Definition: textundostack.h:92
void persistedChanged(bool persisted)
This signal is emitted if the persisted state is changed.
Definition: moc_textundostack.cpp:224
void undoGroupStarted(edbee::ChangeGroup *group)
Definition: moc_textundostack.cpp:189
void setPersistedIndex(int index)
Sets the persisted in dex and fires a signal.
Definition: textundostack.cpp:707
Change * giveChange(Change *change, int coalesceId)
This method gives the command and tries to coalesce the command if possible Warning when a change is ...
Definition: textundostack.cpp:209
void resetAllLastCoalesceIds()
Resets all coalsceIds.
Definition: textundostack.cpp:191
void undoDocumentChange()
Performs an document-change undo.
Definition: textundostack.cpp:634
Change * last(TextEditorController *controller=0)
Returns last textchange on the undo stack.
Definition: textundostack.cpp:403
void clearRedo(TextEditorController *controller)
Clears all redo&#39;s for the given controller.
Definition: textundostack.cpp:576
ChangeGroup * currentGroup()
This method returns the current active group.
Definition: textundostack.cpp:107
A basic change.
Definition: change.h:18
int undoGroupLevel()
returns the number of stacked undo-groups
Definition: textundostack.cpp:170
int lastIndex(TextEditorController *controller=0)
This method returns the last index for a given controller The lastIndex points to the index on the st...
Definition: textundostack.cpp:394
void beginUndoGroup(ChangeGroup *group)
Starts an undo group (or increase nest-level-counter)
Definition: textundostack.cpp:96
The texteditor works via the controller. The controller is the central point/mediater which maps/cont...
Definition: texteditorcontroller.h:37
int persistedIndex()
This method returns the current persisted index.
Definition: textundostack.cpp:477
void endUndoGroupAndDiscard()
Ends the undo-group and discards all content.
Definition: textundostack.cpp:154
int sizeInDocChanges()
The number of doc-&#39;undo&#39; items on the stack.
Definition: textundostack.cpp:412
int lastCoalesceIdAtCurrentLevel()
returns the last coalesceId at the given level
Definition: textundostack.cpp:177
void redoExecuted(edbee::Change *change)
Definition: moc_textundostack.cpp:217
void changeAdded(edbee::Change *change)
Definition: moc_textundostack.cpp:203
This is the undo stack for the texteditor. This stack is SHARED by all views of the document The stac...
Definition: textundostack.h:50
bool redoControllerChange(TextEditorController *controller)
undo&#39;s the given controller change
Definition: textundostack.cpp:664
Change * findUndoChange(TextEditorController *controller=0)
This method returns the last change (TOS) for the given controller.
Definition: textundostack.cpp:434
void setChangeIndex(int index)
When setting the change-index we sometimes must emit a persisted change.
Definition: textundostack.cpp:719
bool canUndo(TextEditorController *controller=0)
Should check if a undo operation can be performed. When no controller is given a document-undo is che...
Definition: textundostack.cpp:287
int currentIndexInDocChanges()
This method returns the current doc change index.
Definition: textundostack.cpp:423
bool isPersisted()
returns true if the undo-stack is on a persisted index
Definition: textundostack.cpp:452
A normal group end.
Definition: textundostack.h:60
void endUndoGroup(int coalesceId, bool flatten)
Ends the undogroup.
Definition: textundostack.cpp:117
QString dumpStack()
makes a dump of the current stack
Definition: textundostack.cpp:484
void redoDocumentChange()
redo a document change
Definition: textundostack.cpp:682
void registerContoller(TextEditorController *controller)
Registers acontroller for it&#39;s own view pointer.
Definition: textundostack.cpp:75
The group contained nothing usefull, nothing is added to the stack.
Definition: textundostack.h:59