File indexing completed on 2024-05-12 16:34:58

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Thomas Zander <zander@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "TextChanges.h"
0021 #include "TextChange.h"
0022 
0023 
0024 TextChanges::TextChanges()
0025         : m_root(0)
0026 {
0027 }
0028 
0029 TextChanges::~TextChanges()
0030 {
0031     TextChange *change = m_root;
0032     while (change) {
0033         TextChange *prev = change;
0034         change = change->next();
0035         delete prev;
0036     }
0037     m_root = 0;
0038 }
0039 
0040 void TextChanges::inserted(int position, const QString &text)
0041 {
0042     changed(position, QString(), text);
0043 }
0044 
0045 void TextChanges::changed(int position, const QString &former, const QString &latter)
0046 {
0047     TextChange *change = new TextChange();
0048     change->setPosition(position);
0049     change->setNewText(latter);
0050     change->setOldText(former);
0051     if (m_root == 0) {
0052         m_root = change;
0053         return;
0054     }
0055 
0056     TextChange *cursor = m_root;
0057     while (cursor->next()) {
0058         if (cursor->position() + cursor->length() >= position) break;
0059         cursor = cursor->next();
0060     }
0061     Q_ASSERT(cursor);
0062     if (cursor->position() > position) { // insert new one before.
0063         cursor->insertBefore(change);
0064         if (cursor == m_root)
0065             m_root = change;
0066     } else if (position >= cursor->position() && position <= cursor->position() + cursor->length()) {//merge
0067         cursor->merge(change);
0068         delete change;
0069     } else { // insert new one after.
0070         cursor->insertAfter(change);
0071         if (change->next())
0072             change->next()->move(change->length());
0073     }
0074 }
0075 
0076 bool TextChanges::hasText(int position, int length) const
0077 {
0078     Q_UNUSED(position);
0079     Q_UNUSED(length);
0080     return false;
0081 }
0082 
0083 QMap<int, const TextChange*> TextChanges::changes() const
0084 {
0085     QMap<int, const TextChange*> result;
0086     return result;
0087 }