~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to plugins/textshape/commands/ChangeTrackedDeleteCommand.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 This file is part of the KDE project
3
3
 * Copyright (C) 2009 Ganesh Paramasivam <ganesh@crystalfab.com>
4
4
 * Copyright (C) 2009 Pierre Stirnweiss <pstirnweiss@googlemail.com>
 
5
 * Copyright (C) 2010 Thomas Zander <zander@kde.org>
5
6
 *
6
7
 * This library is free software; you can redistribute it and/or
7
8
 * modify it under the terms of the GNU Library General Public
46
47
#include <KDebug>
47
48
//#include <iostream>
48
49
#include <QDebug>
 
50
#include <QWeakPointer>
49
51
 
50
52
//A convenience function to get a ListIdType from a format
51
53
 
79
81
    TextCommandBase::undo();
80
82
    UndoRedoFinalizer finalizer(this);
81
83
 
82
 
    QTextDocument *document = m_tool->m_textEditor->document();
 
84
    QTextDocument *document = m_tool->m_textEditor.data()->document();
83
85
    KoTextDocument(document).changeTracker()->elementById(m_addedChangeElement)->setValid(false);
84
86
    foreach (int changeId, m_removedElements) {
85
87
      KoTextDocument(document).changeTracker()->elementById(changeId)->setValid(true);
94
96
    if (!m_first) {
95
97
        TextCommandBase::redo();
96
98
        UndoRedoFinalizer finalizer(this);
97
 
        QTextDocument *document = m_tool->m_textEditor->document();
 
99
        QTextDocument *document = m_tool->m_textEditor.data()->document();
98
100
        KoTextDocument(document).changeTracker()->elementById(m_addedChangeElement)->setValid(true);
99
101
        foreach (int changeId, m_removedElements) {
100
102
          KoTextDocument(document).changeTracker()->elementById(changeId)->setValid(false);
101
103
        }
102
104
    } else {
103
105
        m_first = false;
104
 
        m_tool->m_textEditor->beginEditBlock();
 
106
        m_tool->m_textEditor.data()->beginEditBlock();
105
107
        if(m_mode == PreviousChar)
106
108
            deletePreviousChar();
107
109
        else
108
110
            deleteChar();
109
 
        m_tool->m_textEditor->endEditBlock();
 
111
        m_tool->m_textEditor.data()->endEditBlock();
110
112
    }
111
113
}
112
114
 
113
115
void ChangeTrackedDeleteCommand::deleteChar()
114
116
{
115
 
    QTextCursor *caret = m_tool->m_textEditor->cursor();
 
117
    QTextCursor *caret = m_tool->m_textEditor.data()->cursor();
116
118
 
117
119
    if (caret->atEnd() && !caret->hasSelection())
118
120
        return;
125
127
 
126
128
void ChangeTrackedDeleteCommand::deletePreviousChar()
127
129
{
128
 
    QTextCursor *caret = m_tool->m_textEditor->cursor();
 
130
    QTextCursor *caret = m_tool->m_textEditor.data()->cursor();
129
131
 
130
132
    if (caret->atStart() && !caret->hasSelection())
131
133
        return;
177
179
    selection.insertBlock();
178
180
    // Mark it as inserted content
179
181
    QTextCharFormat format = selection.charFormat();
180
 
    m_tool->m_textEditor->registerTrackedChange(selection, KoGenChange::insertChange, i18n("Key Press"), format, format, false); 
 
182
    m_tool->m_textEditor.data()->registerTrackedChange(selection, KoGenChange::InsertChange, i18n("Key Press"), format, format, false);
181
183
    //Paste the selected text
182
184
    TextPasteCommand *pasteCommand = new TextPasteCommand(QClipboard::Clipboard, m_tool, this);
183
185
    pasteCommand->redo();
197
199
 
198
200
void ChangeTrackedDeleteCommand::deleteSelection(QTextCursor &selection)
199
201
{
200
 
    QTextDocument *document = m_tool->m_textEditor->document();
 
202
    QTextDocument *document = m_tool->m_textEditor.data()->document();
201
203
    KoTextDocumentLayout *layout = qobject_cast<KoTextDocumentLayout*>(document->documentLayout());
202
204
    Q_ASSERT(layout);
203
205
    Q_ASSERT(layout->inlineTextObjectManager());
239
241
    }
240
242
 
241
243
    checker.setPosition(selectionBegin);
242
 
    
 
244
 
243
245
    if (!KoTextDocument(document).changeTracker()->displayChanges()) {
244
246
        QChar charAtPos = document->characterAt(checker.position() - 1);
245
247
        if (layout->inlineTextObjectManager()->inlineTextObject(checker) && charAtPos == QChar::ObjectReplacementCharacter) {
255
257
 
256
258
    if (KoTextDocument(document).changeTracker()->containsInlineChanges(checker.charFormat())) {
257
259
        int changeId = checker.charFormat().property(KoCharacterStyle::ChangeTrackerId).toInt();
258
 
        if (KoTextDocument(document).changeTracker()->elementById(changeId)->getChangeType() == KoGenChange::deleteChange) {
 
260
        if (KoTextDocument(document).changeTracker()->elementById(changeId)->getChangeType() == KoGenChange::DeleteChange) {
259
261
            QTextDocumentFragment prefix =  KoTextDocument(document).changeTracker()->elementById(changeId)->getDeleteData();
260
262
            selectionBegin -= (fragmentLength(prefix) + 1 );
261
263
            KoTextDocument(document).changeTracker()->elementById(changeId)->setValid(false);
359
361
                } 
360
362
                currentList->add(cursor.block(), deletedListItemLevel);
361
363
            }
 
364
        } else if (tempCursor.currentTable()) {
 
365
            QTextTable *deletedTable = tempCursor.currentTable();
 
366
            int numRows = deletedTable->rows();
 
367
            int numColumns = deletedTable->columns();
 
368
            QTextTable *insertedTable = cursor.insertTable(numRows, numColumns, deletedTable->format());
 
369
            for (int i=0; i<numRows; i++) {
 
370
                for (int j=0; j<numColumns; j++) {
 
371
                    tempCursor.setPosition(deletedTable->cellAt(i,j).firstCursorPosition().position());
 
372
                    tempCursor.setPosition(deletedTable->cellAt(i,j).lastCursorPosition().position(), QTextCursor::KeepAnchor);
 
373
                    insertedTable->cellAt(i,j).setFormat(deletedTable->cellAt(i,j).format().toTableCellFormat());
 
374
                    cursor.setPosition(insertedTable->cellAt(i,j).firstCursorPosition().position());
 
375
                    cursor.insertFragment(tempCursor.selection());
 
376
                }
 
377
            }
 
378
            tempCursor.setPosition(deletedTable->cellAt(numRows-1,numColumns-1).lastCursorPosition().position());
 
379
            currentBlock = tempCursor.block();
 
380
            //Move the cursor outside of table
 
381
            cursor.setPosition(cursor.position() + 1);
 
382
            continue;
362
383
        } else {
363
 
            // This block does not contain a list. So no special work here. 
 
384
            // This block does not contain a list. So no special work here.
364
385
            if (currentBlock != tempDoc.begin())
365
386
                cursor.insertBlock(currentBlock.blockFormat(), currentBlock.charFormat());
366
387
        }
404
425
            deletedListItem = currentBlock.blockFormat().property(KoDeleteChangeMarker::DeletedListItem).toBool();
405
426
            if (currentBlock != tempDoc.begin() && deletedListItem)
406
427
                length += 1; //For the Block separator
 
428
        } else if (tempCursor.currentTable()) {
 
429
            QTextTable *deletedTable = tempCursor.currentTable();
 
430
            int numRows = deletedTable->rows();
 
431
            int numColumns = deletedTable->columns();
 
432
            for (int i=0; i<numRows; i++) {
 
433
                for (int j=0; j<numColumns; j++) {
 
434
                    length += 1;
 
435
                    length += (deletedTable->cellAt(i,j).lastCursorPosition().position() - deletedTable->cellAt(i,j).firstCursorPosition().position());
 
436
                }
 
437
            }
 
438
            tempCursor.setPosition(deletedTable->cellAt(numRows-1,numColumns-1).lastCursorPosition().position());
 
439
            currentBlock = tempCursor.block();
 
440
            length += 1;
 
441
            continue;
407
442
        } else {
408
443
            if (currentBlock != tempDoc.begin())
409
444
                length += 1; //For the Block Separator
437
472
        {}
438
473
 
439
474
        void undo() {
440
 
            if (m_document.isNull())
441
 
                return;
442
 
            m_document->undo(KoTextDocument(m_document).textEditor()->cursor());
 
475
            QTextDocument *doc = m_document.data();
 
476
            if (doc)
 
477
                doc->undo(KoTextDocument(doc).textEditor()->cursor());
443
478
        }
444
479
 
445
480
        void redo() {
446
 
            if (m_document.isNull())
447
 
                return;
448
 
            m_document->redo(KoTextDocument(m_document).textEditor()->cursor());
 
481
            QTextDocument *doc = m_document.data();
 
482
            if (doc)
 
483
                doc->redo(KoTextDocument(doc).textEditor()->cursor());
449
484
        }
450
485
 
451
 
        QPointer<QTextDocument> m_document;
 
486
        QWeakPointer<QTextDocument> m_document;
452
487
    };
453
488
 
454
489
    if (command->id() != id())
473
508
        m_length = other->m_length;
474
509
 
475
510
        for(int i=0; i < command->childCount(); i++)
476
 
            new UndoTextCommand(m_tool->m_textEditor->document(), this);
 
511
            new UndoTextCommand(m_tool->m_textEditor.data()->document(), this);
477
512
 
478
513
        return true;
479
514
    }
483
518
void ChangeTrackedDeleteCommand::updateListIds(QTextCursor &cursor)
484
519
{
485
520
    m_newListIds.clear();
486
 
    QTextDocument *document = m_tool->m_textEditor->document();
 
521
    QTextDocument *document = m_tool->m_textEditor.data()->document();
487
522
    QTextCursor tempCursor(document);
488
523
    QTextBlock startBlock = document->findBlock(cursor.anchor());
489
524
    QTextBlock endBlock = document->findBlock(cursor.position());
500
535
}
501
536
void ChangeTrackedDeleteCommand::updateListChanges()
502
537
{
503
 
    QTextDocument *document = m_tool->m_textEditor->document();
 
538
    QTextDocument *document = m_tool->m_textEditor.data()->document();
504
539
    QTextCursor tempCursor(document);
505
540
    QTextBlock startBlock = document->findBlock(m_position);
506
541
    QTextBlock endBlock = document->findBlock(m_position + m_length);
535
570
 
536
571
void ChangeTrackedDeleteCommand::removeChangeElement(int changeId)
537
572
{
538
 
    QTextDocument *document = m_tool->m_textEditor->document();
 
573
    QTextDocument *document = m_tool->m_textEditor.data()->document();
539
574
    KoTextDocumentLayout *layout = qobject_cast<KoTextDocumentLayout*>(document->documentLayout());
540
575
    KoChangeTrackerElement *element = KoTextDocument(document).changeTracker()->elementById(changeId);
541
576
    KoDeleteChangeMarker *marker = element->getDeleteChangeMarker();