Kate
kateundo.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kateundo.h"
00023
00024 #include "katedocument.h"
00025 #include "kateview.h"
00026 #include "katecursor.h"
00027
00028 KateUndo::KateUndo (KateDocument *document)
00029 : m_document (document)
00030 {
00031 }
00032
00033 KateUndo::~KateUndo ()
00034 {
00035 }
00036
00037 bool KateUndo::isEmpty() const
00038 {
00039 return false;
00040 }
00041
00042 bool KateEditInsertTextUndo::isEmpty() const
00043 {
00044 return len() == 0;
00045 }
00046
00047 bool KateEditRemoveTextUndo::isEmpty() const
00048 {
00049 return len() == 0;
00050 }
00051
00052 bool KateUndo::mergeWith (const KateUndo* )
00053 {
00054 return false;
00055 }
00056
00057 bool KateEditInsertTextUndo::mergeWith (const KateUndo* undo)
00058 {
00059 const KateEditInsertTextUndo *u = dynamic_cast<const KateEditInsertTextUndo *> (undo);
00060 if (u != 0
00061 && m_line == u->m_line
00062 && (m_col + len()) == u->m_col)
00063 {
00064 m_text += u->m_text;
00065 return true;
00066 }
00067
00068 return false;
00069 }
00070
00071 bool KateEditRemoveTextUndo::mergeWith (const KateUndo* undo)
00072 {
00073 const KateEditRemoveTextUndo *u = dynamic_cast<const KateEditRemoveTextUndo *> (undo);
00074
00075 if (u != 0
00076 && m_line == u->m_line
00077 && m_col == (u->m_col + u->len()))
00078 {
00079 m_text.prepend(u->m_text);
00080 m_col = u->m_col;
00081 return true;
00082 }
00083
00084 return false;
00085 }
00086
00087 void KateEditInsertTextUndo::undo ()
00088 {
00089 KateDocument *doc = document();
00090
00091 doc->editRemoveText (m_line, m_col, len());
00092 }
00093
00094 void KateEditRemoveTextUndo::undo ()
00095 {
00096 KateDocument *doc = document();
00097
00098 doc->editInsertText (m_line, m_col, m_text);
00099 }
00100
00101 void KateEditWrapLineUndo::undo ()
00102 {
00103 KateDocument *doc = document();
00104
00105 doc->editUnWrapLine (m_line, m_newLine, m_len);
00106 }
00107
00108 void KateEditUnWrapLineUndo::undo ()
00109 {
00110 KateDocument *doc = document();
00111
00112 doc->editWrapLine (m_line, m_col, m_removeLine);
00113 }
00114
00115 void KateEditInsertLineUndo::undo ()
00116 {
00117 KateDocument *doc = document();
00118
00119 doc->editRemoveLine (m_line);
00120 }
00121
00122 void KateEditRemoveLineUndo::undo ()
00123 {
00124 KateDocument *doc = document();
00125
00126 doc->editInsertLine (m_line, m_text);
00127 }
00128
00129 void KateEditMarkLineAutoWrappedUndo::undo ()
00130 {
00131 KateDocument *doc = document();
00132
00133 doc->editMarkLineAutoWrapped (m_line, m_autowrapped);
00134 }
00135
00136 void KateEditRemoveTextUndo::redo ()
00137 {
00138 KateDocument *doc = document();
00139
00140 doc->editRemoveText (m_line, m_col, len());
00141 }
00142
00143 void KateEditInsertTextUndo::redo ()
00144 {
00145 KateDocument *doc = document();
00146
00147 doc->editInsertText (m_line, m_col, m_text);
00148 }
00149
00150 void KateEditUnWrapLineUndo::redo ()
00151 {
00152 KateDocument *doc = document();
00153
00154 doc->editUnWrapLine (m_line, m_removeLine, m_len);
00155 }
00156
00157 void KateEditWrapLineUndo::redo ()
00158 {
00159 KateDocument *doc = document();
00160
00161 doc->editWrapLine (m_line, m_col, m_newLine);
00162 }
00163
00164 void KateEditRemoveLineUndo::redo ()
00165 {
00166 KateDocument *doc = document();
00167
00168 doc->editRemoveLine (m_line);
00169 }
00170
00171 void KateEditInsertLineUndo::redo ()
00172 {
00173 KateDocument *doc = document();
00174
00175 doc->editInsertLine (m_line, m_text);
00176 }
00177
00178 void KateEditMarkLineAutoWrappedUndo::redo ()
00179 {
00180 KateDocument *doc = document();
00181
00182 doc->editMarkLineAutoWrapped (m_line, m_autowrapped);
00183 }
00184
00185 KateUndoGroup::KateUndoGroup (KateDocument *document)
00186 : m_document (document)
00187 , m_safePoint(false)
00188 , m_undoSelection(-1, -1, -1, -1)
00189 , m_redoSelection(-1, -1, -1, -1)
00190 , m_undoCursor(-1, -1)
00191 , m_redoCursor(-1, -1)
00192 {
00193 if (document->activeKateView())
00194 {
00195 m_undoCursor = document->activeKateView()->cursorPosition();
00196 m_undoSelection = document->activeKateView()->selectionRange();
00197 }
00198 }
00199
00200 KateUndoGroup::~KateUndoGroup ()
00201 {
00202 qDeleteAll (m_items);
00203 }
00204
00205 void KateUndoGroup::undo ()
00206 {
00207 if (m_items.isEmpty())
00208 return;
00209
00210 m_document->editStart (false);
00211
00212 for (int i=m_items.size()-1; i >= 0; --i)
00213 m_items[i]->undo();
00214
00215 if (KateView *view = activeKateView()) {
00216 if (m_undoSelection.isValid())
00217 view->setSelection(m_undoSelection);
00218 else
00219 view->clearSelection();
00220
00221 if (m_undoCursor.isValid())
00222 view->editSetCursor(m_undoCursor);
00223 }
00224
00225 m_document->editEnd ();
00226 }
00227
00228 void KateUndoGroup::redo ()
00229 {
00230 if (m_items.isEmpty())
00231 return;
00232
00233 m_document->editStart (false);
00234
00235 for (int i=0; i < m_items.size(); ++i)
00236 m_items[i]->redo();
00237
00238 if (KateView *view = activeKateView()) {
00239 if (m_redoSelection.isValid())
00240 view->setSelection(m_redoSelection);
00241 else
00242 view->clearSelection();
00243
00244 if (m_redoCursor.isValid())
00245 view->editSetCursor(m_redoCursor);
00246 }
00247
00248 m_document->editEnd ();
00249 }
00250
00251 void KateUndoGroup::editEnd()
00252 {
00253 if (activeKateView())
00254 {
00255 m_redoCursor = activeKateView()->cursorPosition();
00256 m_redoSelection = activeKateView()->selectionRange();
00257 }
00258 }
00259
00260 void KateUndoGroup::addItem(KateUndo* u)
00261 {
00262 if (u->isEmpty())
00263 delete u;
00264 else if (!m_items.isEmpty() && m_items.last()->mergeWith(u))
00265 delete u;
00266 else
00267 m_items.append(u);
00268 }
00269
00270 bool KateUndoGroup::merge (KateUndoGroup* newGroup,bool complex)
00271 {
00272 if (m_safePoint)
00273 return false;
00274
00275 if (newGroup->isOnlyType(singleType()) || complex) {
00276
00277 KateUndo* u = newGroup->m_items.isEmpty() ? 0 : newGroup->m_items.takeFirst ();
00278 while (u) {
00279 addItem(u);
00280 u = newGroup->m_items.isEmpty() ? 0 : newGroup->m_items.takeFirst ();
00281 }
00282
00283 if (newGroup->m_safePoint)
00284 safePoint();
00285
00286 m_redoCursor = newGroup->m_redoCursor;
00287 m_redoSelection = newGroup->m_redoSelection;
00288
00289 return true;
00290 }
00291
00292 return false;
00293 }
00294
00295 void KateUndoGroup::safePoint (bool safePoint)
00296 {
00297 m_safePoint=safePoint;
00298 }
00299
00300 KateView *KateUndoGroup::activeKateView()
00301 {
00302 return m_document->activeKateView();
00303 }
00304
00305 KateUndo::UndoType KateUndoGroup::singleType() const
00306 {
00307 KateUndo::UndoType ret = KateUndo::editInvalid;
00308
00309 Q_FOREACH(const KateUndo *item, m_items) {
00310 if (ret == KateUndo::editInvalid)
00311 ret = item->type();
00312 else if (ret != item->type())
00313 return KateUndo::editInvalid;
00314 }
00315
00316 return ret;
00317 }
00318
00319 bool KateUndoGroup::isOnlyType(KateUndo::UndoType type) const
00320 {
00321 if (type == KateUndo::editInvalid) return false;
00322
00323 Q_FOREACH(const KateUndo *item, m_items)
00324 if (item->type() != type)
00325 return false;
00326
00327 return true;
00328 }
00329
00330