Ignore:
Timestamp:
May 5, 2011, 5:36:53 AM (14 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/gui/text/qtextdocument_p.cpp

    r651 r846  
    11/****************************************************************************
    22**
    3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
    44** All rights reserved.
    55** Contact: Nokia Corporation (qt-info@nokia.com)
     
    6464#if !defined(Q_CC_DIAB)
    6565#  define QT_INIT_TEXTUNDOCOMMAND(c, a1, a2, a3, a4, a5, a6, a7, a8) \
    66           QTextUndoCommand c = { a1, a2, 0, 0, a3, a4, a5, a6, { a7 }, a8 }
     66          QTextUndoCommand c = { a1, a2, 0, 0, quint8(a3), a4, a5, a6, { a7 }, a8 }
    6767#else
    6868#  define QT_INIT_TEXTUNDOCOMMAND(c, a1, a2, a3, a4, a5, a6, a7, a8) \
     
    9292
    9393  Lists are not in here, as they are treated specially. A list is just
    94   a collection of (not neccessarily connected) blocks, that share the
     94  a collection of (not necessarily connected) blocks, that share the
    9595  same objectIndex() in the format that refers to the list format and
    9696  object.
     
    193193{
    194194    editBlock = 0;
     195    editBlockCursorPosition = -1;
    195196    docChangeFrom = -1;
    196197
     
    205206    undoEnabled = true;
    206207    inContentsChange = false;
     208    blockCursorAdjustment = false;
    207209
    208210    defaultTextOption.setTabStop(80); // same as in qtextengine.cpp
     
    234236{
    235237    Q_Q(QTextDocument);
    236     for (int i = 0; i < cursors.count(); ++i) {
    237         cursors.at(i)->setPosition(0);
    238         cursors.at(i)->currentCharFormat = -1;
    239         cursors.at(i)->anchor = 0;
    240         cursors.at(i)->adjusted_anchor = 0;
     238
     239    foreach (QTextCursorPrivate *curs, cursors) {
     240        curs->setPosition(0);
     241        curs->currentCharFormat = -1;
     242        curs->anchor = 0;
     243        curs->adjusted_anchor = 0;
    241244    }
    242245
     
    244247    QT_TRY{
    245248        cursors.clear();
    246         changedCursors.clear();
    247249
    248250        QMap<int, QTextObject *>::Iterator objectIt = objects.begin();
     
    260262
    261263        title.clear();
    262         undoState = 0;
    263         truncateUndoStack();
     264        clearUndoRedoStacks(QTextDocument::UndoAndRedoStacks);
    264265        text = QString();
    265266        unreachableCharacterCount = 0;
     
    288289QTextDocumentPrivate::~QTextDocumentPrivate()
    289290{
    290     for (int i = 0; i < cursors.count(); ++i)
    291         cursors.at(i)->priv = 0;
     291    foreach (QTextCursorPrivate *curs, cursors)
     292        curs->priv = 0;
    292293    cursors.clear();
    293294    undoState = 0;
    294295    undoEnabled = true;
    295     truncateUndoStack();
     296    clearUndoRedoStacks(QTextDocument::RedoStack);
    296297}
    297298
     
    320321void QTextDocumentPrivate::insert_string(int pos, uint strPos, uint length, int format, QTextUndoCommand::Operation op)
    321322{
    322     // ##### optimise when only appending to the fragment!
     323    // ##### optimize when only appending to the fragment!
    323324    Q_ASSERT(noBlockInString(text.mid(strPos, length)));
    324325
     
    663664    Q_ASSERT(blocks.length() == fragments.length());
    664665
    665     finishEdit();
     666    if (!blockCursorAdjustment)
     667        finishEdit();
    666668}
    667669
     
    670672    if (length == 0)
    671673        return;
     674    blockCursorAdjustment = true;
    672675    move(pos, -1, length, op);
     676    blockCursorAdjustment = false;
     677    foreach (QTextCursorPrivate *curs, cursors) {
     678        if (curs->adjustPosition(pos, -length, op) == QTextCursorPrivate::CursorMoved) {
     679            curs->changed = true;
     680        }
     681    }
     682    finishEdit();
    673683}
    674684
     
    872882    beginEditBlock();
    873883    int editPos = -1;
     884    int editLength = -1;
    874885    while (1) {
    875886        if (undo)
     
    884895            c.command = QTextUndoCommand::Removed;
    885896            editPos = c.pos;
     897            editLength = 0;
    886898            break;
    887899        case QTextUndoCommand::Removed:
     
    889901            insert_string(c.pos, c.strPos, c.length, c.format, (QTextUndoCommand::Operation)c.operation);
    890902            c.command = QTextUndoCommand::Inserted;
    891             editPos = c.pos + c.length;
     903            if (editPos != (int)c.pos)
     904                editLength = 0;
     905            editPos = c.pos;
     906            editLength += c.length;
    892907            break;
    893908        case QTextUndoCommand::BlockInserted:
     
    900915                c.command = QTextUndoCommand::BlockDeleted;
    901916            editPos = c.pos;
     917            editLength = 0;
    902918            break;
    903919        case QTextUndoCommand::BlockRemoved:
     
    910926            else
    911927                c.command = QTextUndoCommand::BlockAdded;
    912             editPos = c.pos + 1;
     928            if (editPos != (int)c.pos)
     929                editLength = 0;
     930            editPos = c.pos;
     931            editLength += 1;
    913932            break;
    914933        case QTextUndoCommand::CharFormatChanged: {
     
    921940            setCharFormat(c.pos, c.length, formats.charFormat(c.format));
    922941            c.format = oldFormat;
    923             editPos = c.pos + c.length;
     942            if (editPos != (int)c.pos)
     943                editLength = 0;
     944            editPos = c.pos;
     945            editLength += c.length;
    924946            break;
    925947        }
     
    957979            break;
    958980        }
     981        case QTextUndoCommand::CursorMoved:
     982            editPos = c.pos;
     983            editLength = 0;
     984            break;
    959985        case QTextUndoCommand::Custom:
    960986            resetBlockRevision = -1; // ## TODO
     
    9891015    }
    9901016    undoEnabled = true;
    991     if (editPos < 0 && docChangeFrom >= 0) {
    992         editPos = qMin(docChangeFrom + docChangeLength, length() - 1);
    993     }
     1017
     1018    int newCursorPos = -1;
     1019
     1020    if (editPos >=0)
     1021        newCursorPos = editPos + editLength;
     1022    else if (docChangeFrom >= 0)
     1023        newCursorPos= qMin(docChangeFrom + docChangeLength, length() - 1);
     1024
    9941025    endEditBlock();
    9951026    emitUndoAvailable(isUndoAvailable());
    9961027    emitRedoAvailable(isRedoAvailable());
    997     return editPos;
     1028
     1029    return newCursorPos;
    9981030}
    9991031
     
    10281060        return;
    10291061    if (undoState < undoStack.size())
    1030         truncateUndoStack();
     1062        clearUndoRedoStacks(QTextDocument::RedoStack);
     1063
     1064    if (editBlock != 0 && editBlockCursorPosition >= 0) { // we had a beginEditBlock() with a cursor position
     1065        if (c.pos != (quint32) editBlockCursorPosition) { // and that cursor position is different from the command
     1066            // generate a CursorMoved undo item
     1067            QT_INIT_TEXTUNDOCOMMAND(cc, QTextUndoCommand::CursorMoved, true, QTextUndoCommand::MoveCursor,
     1068                                    0, 0, editBlockCursorPosition, 0, 0);
     1069            undoStack.append(cc);
     1070            undoState++;
     1071            editBlockCursorPosition = -1;
     1072        }
     1073    }
     1074
    10311075
    10321076    if (!undoStack.isEmpty() && modified) {
     
    10511095}
    10521096
    1053 void QTextDocumentPrivate::truncateUndoStack()
    1054 {
    1055     if (undoState == undoStack.size())
    1056         return;
    1057 
    1058     for (int i = undoState; i < undoStack.size(); ++i) {
    1059         QTextUndoCommand c = undoStack[i];
    1060         if (c.command & QTextUndoCommand::Removed) {
    1061             // ########
    1062 //             QTextFragment *f = c.fragment_list;
    1063 //             while (f) {
    1064 //                 QTextFragment *n = f->right;
    1065 //                 delete f;
    1066 //                 f = n;
    1067 //             }
    1068         } else if (c.command & QTextUndoCommand::Custom) {
    1069             delete c.custom;
    1070         }
    1071     }
    1072     undoStack.resize(undoState);
     1097void QTextDocumentPrivate::clearUndoRedoStacks(QTextDocument::Stacks stacksToClear,
     1098                                               bool emitSignals)
     1099{
     1100    bool undoCommandsAvailable = undoState != 0;
     1101    bool redoCommandsAvailable = undoState != undoStack.size();
     1102    if (stacksToClear == QTextDocument::UndoStack && undoCommandsAvailable) {
     1103        for (int i = 0; i < undoState; ++i) {
     1104            QTextUndoCommand c = undoStack[undoState];
     1105            if (c.command & QTextUndoCommand::Custom)
     1106                delete c.custom;
     1107        }
     1108        undoStack.remove(0, undoState);
     1109        undoStack.resize(undoStack.size() - undoState);
     1110        undoState = 0;
     1111        if (emitSignals)
     1112            emitUndoAvailable(false);
     1113    } else if (stacksToClear == QTextDocument::RedoStack
     1114               && redoCommandsAvailable) {
     1115        for (int i = undoState; i < undoStack.size(); ++i) {
     1116            QTextUndoCommand c = undoStack[i];
     1117            if (c.command & QTextUndoCommand::Custom)
     1118                delete c.custom;
     1119        }
     1120        undoStack.resize(undoState);
     1121        if (emitSignals)
     1122            emitRedoAvailable(false);
     1123    } else if (stacksToClear == QTextDocument::UndoAndRedoStacks
     1124               && !undoStack.isEmpty()) {
     1125        for (int i = 0; i < undoStack.size(); ++i) {
     1126            QTextUndoCommand c = undoStack[i];
     1127            if (c.command & QTextUndoCommand::Custom)
     1128                delete c.custom;
     1129        }
     1130        undoState = 0;
     1131        undoStack.resize(0);
     1132        if (emitSignals && undoCommandsAvailable)
     1133            emitUndoAvailable(false);
     1134        if (emitSignals && redoCommandsAvailable)
     1135            emitRedoAvailable(false);
     1136    }
    10731137}
    10741138
     
    10981162    if (!enable) {
    10991163        undoState = 0;
    1100         truncateUndoStack();
     1164        clearUndoRedoStacks(QTextDocument::RedoStack);
    11011165        emitUndoAvailable(false);
    11021166        emitRedoAvailable(false);
     
    11301194        }
    11311195    }
     1196
     1197    editBlockCursorPosition = -1;
    11321198
    11331199    finishEdit();
     
    11661232    }
    11671233
    1168     while (!changedCursors.isEmpty()) {
    1169         QTextCursorPrivate *curs = changedCursors.takeFirst();
    1170         emit q->cursorPositionChanged(QTextCursor(curs));
    1171     }
     1234    QList<QTextCursor> changedCursors;
     1235    foreach (QTextCursorPrivate *curs, cursors) {
     1236        if (curs->changed) {
     1237            curs->changed = false;
     1238            changedCursors.append(QTextCursor(curs));
     1239        }
     1240    }
     1241    foreach (const QTextCursor &cursor, changedCursors)
     1242        emit q->cursorPositionChanged(cursor);
    11721243
    11731244    contentsChanged();
     
    12111282        ++revision;
    12121283
    1213     for (int i = 0; i < cursors.size(); ++i) {
    1214         QTextCursorPrivate *curs = cursors.at(i);
    1215         if (curs->adjustPosition(from, addedOrRemoved, op) == QTextCursorPrivate::CursorMoved) {
    1216             if (!changedCursors.contains(curs))
    1217                 changedCursors.append(curs);
     1284    if (blockCursorAdjustment)  {
     1285        ; // postpone, will be called again from QTextDocumentPrivate::remove()
     1286    } else {
     1287        foreach (QTextCursorPrivate *curs, cursors) {
     1288            if (curs->adjustPosition(from, addedOrRemoved, op) == QTextCursorPrivate::CursorMoved) {
     1289                curs->changed = true;
     1290            }
    12181291        }
    12191292    }
     
    13761449void QTextDocumentPrivate::scan_frames(int pos, int charsRemoved, int charsAdded)
    13771450{
    1378     // ###### optimise
     1451    // ###### optimize
    13791452    Q_UNUSED(pos);
    13801453    Q_UNUSED(charsRemoved);
     
    15791652
    15801653    for (FragmentMap::Iterator it = fragments.begin(); !it.atEnd(); ++it) {
    1581         qMemCopy(newTextPtr, text.constData() + it->stringPosition, it->size_array[0] * sizeof(QChar));
     1654        memcpy(newTextPtr, text.constData() + it->stringPosition, it->size_array[0] * sizeof(QChar));
    15821655        it->stringPosition = newLen;
    15831656        newTextPtr += it->size_array[0];
     
    16381711{
    16391712    Q_ASSERT(from <= to);
    1640     for (int i = 0; i < cursors.size(); ++i)
    1641         cursors.at(i)->aboutToRemoveCell(from, to);
     1713    foreach (QTextCursorPrivate *curs, cursors)
     1714        curs->aboutToRemoveCell(from, to);
    16421715}
    16431716
Note: See TracChangeset for help on using the changeset viewer.