changeset.cpp 8.77 KB
Newer Older
Christian Kamm's avatar
Christian Kamm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

42
#include "changeset.h"
Christian Kamm's avatar
Christian Kamm committed
43 44 45

namespace Utils {

46
ChangeSet::ChangeSet()
47
    : m_string(0), m_cursor(0), m_error(false)
Christian Kamm's avatar
Christian Kamm committed
48 49 50 51
{
}

static bool overlaps(int posA, int lengthA, int posB, int lengthB) {
52 53 54 55 56 57 58 59 60 61 62
    if (lengthB > 0) {
        return
                // right edge of B contained in A
                (posA < posB + lengthB && posA + lengthA >= posB + lengthB)
                // left edge of B contained in A
                || (posA <= posB && posA + lengthA > posB)
                // A contained in B
                || (posB < posA && posB + lengthB > posA + lengthA);
    } else {
        return (posB > posA && posB < posA + lengthA);
    }
Christian Kamm's avatar
Christian Kamm committed
63 64
}

65
bool ChangeSet::hasOverlap(int pos, int length)
Christian Kamm's avatar
Christian Kamm committed
66
{
67 68 69 70 71 72
    QListIterator<EditOp> i(m_operationList);
    while (i.hasNext()) {
        const EditOp &cmd = i.next();

        switch (cmd.type) {
        case EditOp::Replace:
73
            if (overlaps(pos, length, cmd.pos1, cmd.length1))
Christian Kamm's avatar
Christian Kamm committed
74
                return true;
75 76 77
            break;

        case EditOp::Move:
78
            if (overlaps(pos, length, cmd.pos1, cmd.length1))
79 80
                return true;
            if (cmd.pos2 > pos && cmd.pos2 < pos + length)
Christian Kamm's avatar
Christian Kamm committed
81
                return true;
82 83 84 85 86 87 88 89
            break;

        case EditOp::Insert:
            if (cmd.pos1 > pos && cmd.pos1 < pos + length)
                return true;
            break;

        case EditOp::Remove:
90
            if (overlaps(pos, length, cmd.pos1, cmd.length1))
91 92 93 94
                return true;
            break;

        case EditOp::Flip:
95
            if (overlaps(pos, length, cmd.pos1, cmd.length1))
96
                return true;
97
            if (overlaps(pos, length, cmd.pos2, cmd.length2))
98 99 100 101
                return true;
            break;

        case EditOp::Copy:
102
            if (overlaps(pos, length, cmd.pos1, cmd.length1))
103 104 105 106 107 108 109
                return true;
            if (cmd.pos2 > pos && cmd.pos2 < pos + length)
                return true;
            break;

        case EditOp::Unset:
            break;
Christian Kamm's avatar
Christian Kamm committed
110 111 112 113 114 115
        }
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
116 117
bool ChangeSet::isEmpty() const
{
118 119
    return m_operationList.isEmpty();
}
Roberto Raggi's avatar
Roberto Raggi committed
120

121 122 123 124 125 126 127 128 129 130 131
QList<ChangeSet::EditOp> ChangeSet::operationList() const
{
    return m_operationList;
}

void ChangeSet::clear()
{
    m_string = 0;
    m_cursor = 0;
    m_operationList.clear();
    m_error = false;
Roberto Raggi's avatar
Roberto Raggi committed
132 133
}

134
bool ChangeSet::replace(int pos, int length, const QString &replacement)
Roberto Raggi's avatar
Roberto Raggi committed
135
{
136 137 138 139 140
    if (hasOverlap(pos, length))
        m_error = true;

    EditOp cmd(EditOp::Replace);
    cmd.pos1 = pos;
141
    cmd.length1 = length;
142 143 144 145
    cmd.text = replacement;
    m_operationList += cmd;

    return !m_error;
Roberto Raggi's avatar
Roberto Raggi committed
146 147
}

148
bool ChangeSet::move(int pos, int length, int to)
Roberto Raggi's avatar
Roberto Raggi committed
149
{
150 151 152 153 154 155 156
    if (hasOverlap(pos, length)
        || hasOverlap(to, 0)
        || overlaps(pos, length, to, 0))
        m_error = true;

    EditOp cmd(EditOp::Move);
    cmd.pos1 = pos;
157
    cmd.length1 = length;
158 159 160 161
    cmd.pos2 = to;
    m_operationList += cmd;

    return !m_error;
Roberto Raggi's avatar
Roberto Raggi committed
162 163
}

164
bool ChangeSet::insert(int pos, const QString &text)
Roberto Raggi's avatar
Roberto Raggi committed
165
{
166 167 168 169 170 171 172 173 174
    if (hasOverlap(pos, 0))
        m_error = true;

    EditOp cmd(EditOp::Insert);
    cmd.pos1 = pos;
    cmd.text = text;
    m_operationList += cmd;

    return !m_error;
Roberto Raggi's avatar
Roberto Raggi committed
175 176
}

177
bool ChangeSet::remove(int pos, int length)
Christian Kamm's avatar
Christian Kamm committed
178
{
179 180
    if (hasOverlap(pos, length))
        m_error = true;
Christian Kamm's avatar
Christian Kamm committed
181

182 183
    EditOp cmd(EditOp::Remove);
    cmd.pos1 = pos;
184
    cmd.length1 = length;
185 186 187
    m_operationList += cmd;

    return !m_error;
Christian Kamm's avatar
Christian Kamm committed
188 189
}

190
bool ChangeSet::flip(int pos1, int length1, int pos2, int length2)
Christian Kamm's avatar
Christian Kamm committed
191
{
192 193 194
    if (hasOverlap(pos1, length1)
        || hasOverlap(pos2, length2)
        || overlaps(pos1, length1, pos2, length2))
195
        m_error = true;
Christian Kamm's avatar
Christian Kamm committed
196

197 198
    EditOp cmd(EditOp::Flip);
    cmd.pos1 = pos1;
199
    cmd.length1 = length1;
200
    cmd.pos2 = pos2;
201
    cmd.length2 = length2;
202 203 204
    m_operationList += cmd;

    return !m_error;
Christian Kamm's avatar
Christian Kamm committed
205 206
}

207
bool ChangeSet::copy(int pos, int length, int to)
Christian Kamm's avatar
Christian Kamm committed
208
{
209 210 211 212 213 214 215
    if (hasOverlap(pos, length)
        || hasOverlap(to, 0)
        || overlaps(pos, length, to, 0))
        m_error = true;

    EditOp cmd(EditOp::Copy);
    cmd.pos1 = pos;
216
    cmd.length1 = length;
217 218 219 220 221 222 223 224 225 226
    cmd.pos2 = to;
    m_operationList += cmd;

    return !m_error;
}

void ChangeSet::doReplace(const EditOp &replace, QList<EditOp> *replaceList)
{
    Q_ASSERT(replace.type == EditOp::Replace);

Christian Kamm's avatar
Christian Kamm committed
227
    {
228
        QMutableListIterator<EditOp> i(*replaceList);
Christian Kamm's avatar
Christian Kamm committed
229
        while (i.hasNext()) {
230 231
            EditOp &c = i.next();
            if (replace.pos1 <= c.pos1)
232 233 234
                c.pos1 += replace.text.size();
            if (replace.pos1 < c.pos1)
                c.pos1 -= replace.length1;
Christian Kamm's avatar
Christian Kamm committed
235 236 237
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
238
    if (m_string) {
239
        m_string->replace(replace.pos1, replace.length1, replace.text);
Roberto Raggi's avatar
Roberto Raggi committed
240
    } else if (m_cursor) {
241
        m_cursor->setPosition(replace.pos1);
242
        m_cursor->setPosition(replace.pos1 + replace.length1, QTextCursor::KeepAnchor);
243
        m_cursor->insertText(replace.text);
Christian Kamm's avatar
Christian Kamm committed
244 245 246
    }
}

247
void ChangeSet::convertToReplace(const EditOp &op, QList<EditOp> *replaceList)
Christian Kamm's avatar
Christian Kamm committed
248
{
249 250 251 252 253 254 255 256 257 258
    EditOp replace1(EditOp::Replace);
    EditOp replace2(EditOp::Replace);

    switch (op.type) {
    case EditOp::Replace:
        replaceList->append(op);
        break;

    case EditOp::Move:
        replace1.pos1 = op.pos1;
259
        replace1.length1 = op.length1;
260 261 262
        replaceList->append(replace1);

        replace2.pos1 = op.pos2;
263
        replace2.text = textAt(op.pos1, op.length1);
264 265 266 267 268 269 270 271
        replaceList->append(replace2);
        break;

    case EditOp::Insert:
        replace1.pos1 = op.pos1;
        replace1.text = op.text;
        replaceList->append(replace1);
        break;
Christian Kamm's avatar
Christian Kamm committed
272

273 274
    case EditOp::Remove:
        replace1.pos1 = op.pos1;
275
        replace1.length1 = op.length1;
276 277 278 279 280
        replaceList->append(replace1);
        break;

    case EditOp::Flip:
        replace1.pos1 = op.pos1;
281 282
        replace1.length1 = op.length1;
        replace1.text = textAt(op.pos2, op.length2);
283 284 285
        replaceList->append(replace1);

        replace2.pos1 = op.pos2;
286 287
        replace2.length1 = op.length2;
        replace2.text = textAt(op.pos1, op.length1);
288 289 290 291 292
        replaceList->append(replace2);
        break;

    case EditOp::Copy:
        replace1.pos1 = op.pos2;
293
        replace1.text = textAt(op.pos1, op.length1);
294 295 296 297 298
        replaceList->append(replace1);
        break;

    case EditOp::Unset:
        break;
Christian Kamm's avatar
Christian Kamm committed
299 300 301
    }
}

302 303 304 305 306 307
bool ChangeSet::hadErrors()
{
    return m_error;
}

void ChangeSet::apply(QString *s)
Christian Kamm's avatar
Christian Kamm committed
308
{
Roberto Raggi's avatar
Roberto Raggi committed
309
    m_string = s;
310
    apply_helper();
Roberto Raggi's avatar
Roberto Raggi committed
311
    m_string = 0;
Christian Kamm's avatar
Christian Kamm committed
312 313
}

314
void ChangeSet::apply(QTextCursor *textCursor)
Christian Kamm's avatar
Christian Kamm committed
315
{
Roberto Raggi's avatar
Roberto Raggi committed
316
    m_cursor = textCursor;
317
    apply_helper();
Roberto Raggi's avatar
Roberto Raggi committed
318
    m_cursor = 0;
Christian Kamm's avatar
Christian Kamm committed
319 320
}

321
QString ChangeSet::textAt(int pos, int length)
Christian Kamm's avatar
Christian Kamm committed
322
{
323 324 325 326 327 328
    if (m_string) {
        return m_string->mid(pos, length);
    } else if (m_cursor) {
        m_cursor->setPosition(pos);
        m_cursor->setPosition(pos + length, QTextCursor::KeepAnchor);
        return m_cursor->selectedText();
Christian Kamm's avatar
Christian Kamm committed
329
    }
330 331 332 333 334 335 336
    return QString();
}

void ChangeSet::apply_helper()
{
    // convert all ops to replace
    QList<EditOp> replaceList;
Christian Kamm's avatar
Christian Kamm committed
337
    {
338 339 340 341
        while (!m_operationList.isEmpty()) {
            const EditOp cmd(m_operationList.first());
            m_operationList.removeFirst();
            convertToReplace(cmd, &replaceList);
Christian Kamm's avatar
Christian Kamm committed
342 343
        }
    }
344 345 346 347 348 349 350 351 352 353 354

    // execute replaces
    if (m_cursor)
        m_cursor->beginEditBlock();

    while (!replaceList.isEmpty()) {
        const EditOp cmd(replaceList.first());
        replaceList.removeFirst();
        doReplace(cmd, &replaceList);
    }

Roberto Raggi's avatar
Roberto Raggi committed
355 356
    if (m_cursor)
        m_cursor->endEditBlock();
Christian Kamm's avatar
Christian Kamm committed
357 358 359
}

} // end namespace Utils