1 /* This file is part of the KDE project
2 * Copyright (C) 2007 Thomas Zander <zander@kde.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "TextChanges.h"
21 #include "TextChange.h"
22
TextChanges()23 TextChanges::TextChanges()
24 : m_root(0)
25 {
26 }
27
~TextChanges()28 TextChanges::~TextChanges()
29 {
30 TextChange *change = m_root;
31 while (change) {
32 TextChange *prev = change;
33 change = change->next();
34 delete prev;
35 }
36 m_root = 0;
37 }
38
inserted(int position,const QString & text)39 void TextChanges::inserted(int position, const QString &text)
40 {
41 changed(position, QString(), text);
42 }
43
changed(int position,const QString & former,const QString & latter)44 void TextChanges::changed(int position, const QString &former, const QString &latter)
45 {
46 TextChange *change = new TextChange();
47 change->setPosition(position);
48 change->setNewText(latter);
49 change->setOldText(former);
50 if (m_root == 0) {
51 m_root = change;
52 return;
53 }
54
55 TextChange *cursor = m_root;
56 while (cursor->next()) {
57 if (cursor->position() + cursor->length() >= position) {
58 break;
59 }
60 cursor = cursor->next();
61 }
62 Q_ASSERT(cursor);
63 if (cursor->position() > position) { // insert new one before.
64 cursor->insertBefore(change);
65 if (cursor == m_root) {
66 m_root = change;
67 }
68 } else if (position >= cursor->position() && position <= cursor->position() + cursor->length()) {//merge
69 cursor->merge(change);
70 delete change;
71 } else { // insert new one after.
72 cursor->insertAfter(change);
73 if (change->next()) {
74 change->next()->move(change->length());
75 }
76 }
77 }
78
hasText(int position,int length) const79 bool TextChanges::hasText(int position, int length) const
80 {
81 Q_UNUSED(position);
82 Q_UNUSED(length);
83 return false;
84 }
85
changes() const86 QMap<int, const TextChange *> TextChanges::changes() const
87 {
88 QMap<int, const TextChange *> result;
89 return result;
90 }
91