• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Kate

kateedit.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2005 Hamish Rodda <rodda@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "kateedit.h"
00020 #include "katedocument.h"
00021 
00022 KateEditInfo::KateEditInfo(Kate::EditSource source, const KTextEditor::Range& oldRange, const QStringList& oldText, const KTextEditor::Range& newRange, const QStringList& newText)
00023   : m_editSource(source)
00024   , m_oldRange(oldRange)
00025   , m_oldText(oldText)
00026   , m_newRange(newRange)
00027   , m_newText(newText)
00028   , m_revisionTokenCounter(0)
00029 {
00030   m_translate = (m_newRange.end() - m_newRange.start()) - (m_oldRange.end() - m_oldRange.start());
00031 }
00032 
00033 KateEditInfo::~KateEditInfo()
00034 {
00035 }
00036 
00037 Kate::EditSource KateEditInfo::editSource() const
00038 {
00039   return m_editSource;
00040 }
00041 
00042 const KTextEditor::Range & KateEditInfo::oldRange( ) const
00043 {
00044   return m_oldRange;
00045 }
00046 
00047 QStringList KateEditInfo::oldText( const KTextEditor::Range & range ) const
00048 {
00049   QStringList ret;
00050   for (int i = range.start().line(); i <= range.end().line(); ++i) {
00051     QString original = m_oldText[range.start().line() - m_oldRange.start().line()];
00052 
00053     int startCol = 0, length = -1;
00054     if (range.start().line() == m_oldRange.start().line())
00055       startCol = range.start().column() - m_oldRange.start().column();
00056     if (range.end().line() == m_oldRange.end().line())
00057       length = range.end().column() - startCol;
00058 
00059     ret << original.mid(startCol, length);
00060   }
00061   return ret;
00062 }
00063 
00064 const QStringList & KateEditInfo::oldText( ) const
00065 {
00066   return m_oldText;
00067 }
00068 
00069 const KTextEditor::Range & KateEditInfo::newRange( ) const
00070 {
00071   return m_newRange;
00072 }
00073 
00074 QStringList KateEditInfo::newText( const KTextEditor::Range & range ) const
00075 {
00076   QStringList ret;
00077   for (int i = range.start().line(); i <= range.end().line(); ++i) {
00078     QString original = m_newText[range.start().line() - m_newRange.start().line()];
00079 
00080     int startCol = 0, length = -1;
00081     if (range.start().line() == m_newRange.start().line())
00082       startCol = range.start().column() - m_oldRange.start().column();
00083     if (range.end().line() == m_newRange.end().line())
00084       length = range.end().column() - startCol;
00085 
00086     ret << original.mid(startCol, length);
00087   }
00088   return ret;
00089 }
00090 
00091 bool KateEditInfo::isReferenced() const
00092 {
00093   return m_revisionTokenCounter;
00094 }
00095 
00096 void KateEditInfo::dereferenceRevision()
00097 {
00098   --m_revisionTokenCounter;
00099 }
00100 
00101 void KateEditInfo::referenceRevision()
00102 {
00103   ++m_revisionTokenCounter;
00104 }
00105 
00106 const QStringList & KateEditInfo::newText() const
00107 {
00108   return m_newText;
00109 }
00110 
00111 bool KateEditInfo::isRemoval() const
00112 {
00113   return !m_oldRange.isEmpty() && m_newRange.isEmpty();
00114 }
00115 
00116 KateEditHistory::KateEditHistory( KateDocument * doc )
00117   : QObject(doc)
00118   , m_revision(0)
00119 {
00120 }
00121 
00122 KateEditHistory::~KateEditHistory()
00123 {
00124 }
00125 
00126 int KateEditHistory::revision()
00127 {
00128   QMutexLocker lock(&m_mutex);
00129   if (!m_edits.isEmpty()) {
00130     KateEditInfo* edit = m_edits.last();
00131     if (!edit->isReferenced())
00132       m_revisions.insert(++m_revision, edit);
00133 
00134     edit->referenceRevision();
00135     return m_revision;
00136   }
00137 
00138   return 0;
00139 }
00140 
00141 void KateEditHistory::releaseRevision(int revision)
00142 {
00143   QMutexLocker lock(&m_mutex);
00144   if (m_revisions.contains(revision)) {
00145     KateEditInfo* edit = m_revisions[revision];
00146     edit->dereferenceRevision();
00147     if (!edit->isReferenced())
00148       m_revisions.remove(revision);
00149     return;
00150   }
00151 
00152   kWarning() << "Unknown revision token " << revision;
00153 }
00154 
00155 void KateEditHistory::doEdit(KateEditInfo* edit) {
00156   
00157   m_mutex.lock();
00158   m_edits.append(edit);
00159   m_mutex.unlock();
00160   
00161   emit editDone(edit);
00162 }
00163 
00164 QList<KateEditInfo*> KateEditHistory::editsBetweenRevisions(int from, int to) const
00165 {
00166   QMutexLocker lock(&m_mutex);
00167 
00168   QList<KateEditInfo*> ret;
00169 
00170   if (from == -1)
00171     return ret;
00172 
00173   if (m_edits.isEmpty())
00174     return ret;
00175 
00176   if (to != -1) {
00177     Q_ASSERT(from <= to);
00178     Q_ASSERT(m_revisions.contains(to));
00179   }
00180 
00181   int fromIndex = 0;
00182   if (from != 0) {
00183     Q_ASSERT(m_revisions.contains(from));
00184     KateEditInfo* fromEdit = m_revisions[from];
00185     Q_ASSERT(fromEdit);
00186 
00187     fromIndex = m_edits.indexOf(fromEdit);
00188     if(fromIndex != -1) {
00189         //Since the "from" edit already known, we need to start one behind it
00190         ++fromIndex;
00191     }
00192   }
00193 
00194   KateEditInfo* toEdit = to == -1 ? m_edits.last() : m_revisions[to];
00195   Q_ASSERT(toEdit);
00196 
00197   int toIndex = m_edits.indexOf(toEdit);
00198   Q_ASSERT(fromIndex != -1);
00199   Q_ASSERT(toIndex != -1);
00200 
00201   for (int i = fromIndex; i <= toIndex; ++i)
00202     ret.append(m_edits.at(i));
00203 
00204   return ret;
00205 }
00206 
00207 #include "kateedit.moc"

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal