Kate
kateundo.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2009-2010 Bernhard Beschow <bbeschow@cs.tu-berlin.de> 00003 Copyright (C) 2002 John Firebaugh <jfirebaugh@kde.org> 00004 Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org> 00005 Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License version 2 as published by the Free Software Foundation. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "kateundo.h" 00023 00024 #include "kateundomanager.h" 00025 #include "katedocument.h" 00026 00027 #include <ktexteditor/cursor.h> 00028 #include <ktexteditor/view.h> 00029 00030 KateUndo::KateUndo (KateDocument *document) 00031 : m_document (document) 00032 { 00033 } 00034 00035 KateUndo::~KateUndo () 00036 { 00037 } 00038 00039 bool KateUndo::isEmpty() const 00040 { 00041 return false; 00042 } 00043 00044 bool KateEditInsertTextUndo::isEmpty() const 00045 { 00046 return len() == 0; 00047 } 00048 00049 bool KateEditRemoveTextUndo::isEmpty() const 00050 { 00051 return len() == 0; 00052 } 00053 00054 bool KateUndo::mergeWith (const KateUndo* /*undo*/) 00055 { 00056 return false; 00057 } 00058 00059 bool KateEditInsertTextUndo::mergeWith (const KateUndo* undo) 00060 { 00061 const KateEditInsertTextUndo *u = dynamic_cast<const KateEditInsertTextUndo *> (undo); 00062 if (u != 0 00063 && m_line == u->m_line 00064 && (m_col + len()) == u->m_col) 00065 { 00066 m_text += u->m_text; 00067 return true; 00068 } 00069 00070 return false; 00071 } 00072 00073 bool KateEditRemoveTextUndo::mergeWith (const KateUndo* undo) 00074 { 00075 const KateEditRemoveTextUndo *u = dynamic_cast<const KateEditRemoveTextUndo *> (undo); 00076 00077 if (u != 0 00078 && m_line == u->m_line 00079 && m_col == (u->m_col + u->len())) 00080 { 00081 m_text.prepend(u->m_text); 00082 m_col = u->m_col; 00083 return true; 00084 } 00085 00086 return false; 00087 } 00088 00089 void KateEditInsertTextUndo::undo () 00090 { 00091 KateDocument *doc = document(); 00092 00093 doc->editRemoveText (m_line, m_col, len()); 00094 } 00095 00096 void KateEditRemoveTextUndo::undo () 00097 { 00098 KateDocument *doc = document(); 00099 00100 doc->editInsertText (m_line, m_col, m_text); 00101 } 00102 00103 void KateEditWrapLineUndo::undo () 00104 { 00105 KateDocument *doc = document(); 00106 00107 doc->editUnWrapLine (m_line, m_newLine, m_len); 00108 } 00109 00110 void KateEditUnWrapLineUndo::undo () 00111 { 00112 KateDocument *doc = document(); 00113 00114 doc->editWrapLine (m_line, m_col, m_removeLine); 00115 } 00116 00117 void KateEditInsertLineUndo::undo () 00118 { 00119 KateDocument *doc = document(); 00120 00121 doc->editRemoveLine (m_line); 00122 } 00123 00124 void KateEditRemoveLineUndo::undo () 00125 { 00126 KateDocument *doc = document(); 00127 00128 doc->editInsertLine (m_line, m_text); 00129 } 00130 00131 void KateEditMarkLineAutoWrappedUndo::undo () 00132 { 00133 KateDocument *doc = document(); 00134 00135 doc->editMarkLineAutoWrapped (m_line, m_autowrapped); 00136 } 00137 00138 void KateEditRemoveTextUndo::redo () 00139 { 00140 KateDocument *doc = document(); 00141 00142 doc->editRemoveText (m_line, m_col, len()); 00143 } 00144 00145 void KateEditInsertTextUndo::redo () 00146 { 00147 KateDocument *doc = document(); 00148 00149 doc->editInsertText (m_line, m_col, m_text); 00150 } 00151 00152 void KateEditUnWrapLineUndo::redo () 00153 { 00154 KateDocument *doc = document(); 00155 00156 doc->editUnWrapLine (m_line, m_removeLine, m_len); 00157 } 00158 00159 void KateEditWrapLineUndo::redo () 00160 { 00161 KateDocument *doc = document(); 00162 00163 doc->editWrapLine (m_line, m_col, m_newLine); 00164 } 00165 00166 void KateEditRemoveLineUndo::redo () 00167 { 00168 KateDocument *doc = document(); 00169 00170 doc->editRemoveLine (m_line); 00171 } 00172 00173 void KateEditInsertLineUndo::redo () 00174 { 00175 KateDocument *doc = document(); 00176 00177 doc->editInsertLine (m_line, m_text); 00178 } 00179 00180 void KateEditMarkLineAutoWrappedUndo::redo () 00181 { 00182 KateDocument *doc = document(); 00183 00184 doc->editMarkLineAutoWrapped (m_line, m_autowrapped); 00185 } 00186 00187 KateUndoGroup::KateUndoGroup (KateUndoManager *manager, const KTextEditor::Cursor &cursorPosition, const KTextEditor::Range &selectionRange) 00188 : m_manager (manager) 00189 , m_safePoint(false) 00190 , m_undoSelection(selectionRange) 00191 , m_redoSelection(-1, -1, -1, -1) 00192 , m_undoCursor(cursorPosition) 00193 , m_redoCursor(-1, -1) 00194 { 00195 } 00196 00197 KateUndoGroup::~KateUndoGroup () 00198 { 00199 qDeleteAll (m_items); 00200 } 00201 00202 void KateUndoGroup::undo (KTextEditor::View *view) 00203 { 00204 if (m_items.isEmpty()) 00205 return; 00206 00207 m_manager->startUndo (); 00208 00209 for (int i=m_items.size()-1; i >= 0; --i) 00210 m_items[i]->undo(); 00211 00212 if (view != 0) { 00213 if (m_undoSelection.isValid()) 00214 view->setSelection(m_undoSelection); 00215 else 00216 view->removeSelection(); 00217 00218 if (m_undoCursor.isValid()) 00219 view->setCursorPosition(m_undoCursor); 00220 } 00221 00222 m_manager->endUndo (); 00223 } 00224 00225 void KateUndoGroup::redo (KTextEditor::View *view) 00226 { 00227 if (m_items.isEmpty()) 00228 return; 00229 00230 m_manager->startUndo (); 00231 00232 for (int i=0; i < m_items.size(); ++i) 00233 m_items[i]->redo(); 00234 00235 if (view != 0) { 00236 if (m_redoSelection.isValid()) 00237 view->setSelection(m_redoSelection); 00238 else 00239 view->removeSelection(); 00240 00241 if (m_redoCursor.isValid()) 00242 view->setCursorPosition(m_redoCursor); 00243 } 00244 00245 m_manager->endUndo (); 00246 } 00247 00248 void KateUndoGroup::editEnd(const KTextEditor::Cursor &cursorPosition, const KTextEditor::Range selectionRange) 00249 { 00250 m_redoCursor = cursorPosition; 00251 m_redoSelection = selectionRange; 00252 } 00253 00254 void KateUndoGroup::addItem(KateUndo* u) 00255 { 00256 if (u->isEmpty()) 00257 delete u; 00258 else if (!m_items.isEmpty() && m_items.last()->mergeWith(u)) 00259 delete u; 00260 else 00261 m_items.append(u); 00262 } 00263 00264 bool KateUndoGroup::merge (KateUndoGroup* newGroup,bool complex) 00265 { 00266 if (m_safePoint) 00267 return false; 00268 00269 if (newGroup->isOnlyType(singleType()) || complex) { 00270 // Take all of its items first -> last 00271 KateUndo* u = newGroup->m_items.isEmpty() ? 0 : newGroup->m_items.takeFirst (); 00272 while (u) { 00273 addItem(u); 00274 u = newGroup->m_items.isEmpty() ? 0 : newGroup->m_items.takeFirst (); 00275 } 00276 00277 if (newGroup->m_safePoint) 00278 safePoint(); 00279 00280 m_redoCursor = newGroup->m_redoCursor; 00281 m_redoSelection = newGroup->m_redoSelection; 00282 00283 return true; 00284 } 00285 00286 return false; 00287 } 00288 00289 void KateUndoGroup::safePoint (bool safePoint) 00290 { 00291 m_safePoint=safePoint; 00292 } 00293 00294 KTextEditor::Document *KateUndoGroup::document() 00295 { 00296 return m_manager->document(); 00297 } 00298 00299 KateUndo::UndoType KateUndoGroup::singleType() const 00300 { 00301 KateUndo::UndoType ret = KateUndo::editInvalid; 00302 00303 Q_FOREACH(const KateUndo *item, m_items) { 00304 if (ret == KateUndo::editInvalid) 00305 ret = item->type(); 00306 else if (ret != item->type()) 00307 return KateUndo::editInvalid; 00308 } 00309 00310 return ret; 00311 } 00312 00313 bool KateUndoGroup::isOnlyType(KateUndo::UndoType type) const 00314 { 00315 if (type == KateUndo::editInvalid) return false; 00316 00317 Q_FOREACH(const KateUndo *item, m_items) 00318 if (item->type() != type) 00319 return false; 00320 00321 return true; 00322 } 00323 00324 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference