1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "textwriter_p.h"
44 QT_QML_BEGIN_NAMESPACE
46 using namespace QDeclarativeJS;
48 TextWriter::TextWriter()
53 static bool overlaps(int posA, int lengthA, int posB, int lengthB) {
54 return (posA < posB + lengthB && posA + lengthA > posB + lengthB)
55 || (posA < posB && posA + lengthA > posB);
58 bool TextWriter::hasOverlap(int pos, int length)
61 QListIterator<Replace> i(replaceList);
63 const Replace &cmd = i.next();
64 if (overlaps(pos, length, cmd.pos, cmd.length))
69 QListIterator<Move> i(moveList);
71 const Move &cmd = i.next();
72 if (overlaps(pos, length, cmd.pos, cmd.length))
79 bool TextWriter::hasMoveInto(int pos, int length)
81 QListIterator<Move> i(moveList);
83 const Move &cmd = i.next();
84 if (cmd.to >= pos && cmd.to < pos + length)
90 void TextWriter::replace(int pos, int length, const QString &replacement)
92 Q_ASSERT(!hasOverlap(pos, length));
93 Q_ASSERT(!hasMoveInto(pos, length));
98 cmd.replacement = replacement;
102 void TextWriter::move(int pos, int length, int to)
104 Q_ASSERT(!hasOverlap(pos, length));
113 void TextWriter::doReplace(const Replace &replace)
115 int diff = replace.replacement.size() - replace.length;
117 QMutableListIterator<Replace> i(replaceList);
118 while (i.hasNext()) {
119 Replace &c = i.next();
120 if (replace.pos < c.pos)
122 else if (replace.pos + replace.length < c.pos + c.length)
127 QMutableListIterator<Move> i(moveList);
128 while (i.hasNext()) {
130 if (replace.pos < c.pos)
132 else if (replace.pos + replace.length < c.pos + c.length)
135 if (replace.pos < c.to)
141 string->replace(replace.pos, replace.length, replace.replacement);
143 cursor->setPosition(replace.pos);
144 cursor->setPosition(replace.pos + replace.length, QTextCursor::KeepAnchor);
145 cursor->insertText(replace.replacement);
149 void TextWriter::doMove(const Move &move)
153 text = string->mid(move.pos, move.length);
155 cursor->setPosition(move.pos);
156 cursor->setPosition(move.pos + move.length, QTextCursor::KeepAnchor);
157 text = cursor->selectedText();
162 cut.length = move.length;
166 paste.replacement = text;
168 replaceList.append(cut);
169 replaceList.append(paste);
172 while (!replaceList.isEmpty()) {
173 cmd = replaceList.first();
174 replaceList.removeFirst();
179 void TextWriter::write(QString *s)
186 void TextWriter::write(QTextCursor *textCursor)
193 void TextWriter::write_helper()
196 cursor->beginEditBlock();
199 while (!replaceList.isEmpty()) {
200 cmd = replaceList.first();
201 replaceList.removeFirst();
207 while (!moveList.isEmpty()) {
208 cmd = moveList.first();
209 moveList.removeFirst();
214 cursor->endEditBlock();