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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
* Copyright (C) 2010 Pierre Stirnweiss \pstirnweiss@googlemail.com>
 
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 "RejectChangeCommand.h"
 
21
 
 
22
#include <KoGenChange.h>
 
23
#include <KoInlineTextObjectManager.h>
 
24
#include <KoTextDocument.h>
 
25
#include <KoTextDocumentLayout.h>
 
26
 
 
27
#include <changetracker/KoChangeTracker.h>
 
28
#include <changetracker/KoChangeTrackerElement.h>
 
29
#include <styles/KoCharacterStyle.h>
 
30
 
 
31
#include <KLocale>
 
32
 
 
33
#include <QPair>
 
34
#include <QStack>
 
35
#include <QTextBlock>
 
36
#include <QTextCharFormat>
 
37
#include <QTextCursor>
 
38
#include <QTextDocument>
 
39
#include <QTextFragment>
 
40
 
 
41
RejectChangeCommand::RejectChangeCommand (int changeId, QList<QPair<int, int> > changeRanges, QTextDocument *document, QUndoCommand* parent) : TextCommandBase(parent),
 
42
    m_first(true),
 
43
    m_changeId(changeId),
 
44
    m_changeRanges(changeRanges),
 
45
    m_document(document)
 
46
{
 
47
    setText(i18n("Reject change"));
 
48
 
 
49
    m_changeTracker = KoTextDocument(m_document).changeTracker();
 
50
    m_layout = dynamic_cast<KoTextDocumentLayout*>(document->documentLayout());
 
51
}
 
52
 
 
53
RejectChangeCommand::~RejectChangeCommand()
 
54
{
 
55
}
 
56
 
 
57
void RejectChangeCommand::redo()
 
58
{
 
59
    if (m_first) {
 
60
        m_first = false;
 
61
        QTextCursor cursor(m_document);
 
62
        if (m_changeTracker->elementById(m_changeId)->getChangeType() == KoGenChange::insertChange) {
 
63
            QList<QPair<int, int> >::const_iterator it;
 
64
            QStack<QPair<int, int> > deleteRanges;
 
65
            for (it = m_changeRanges.constBegin(); it != m_changeRanges.constEnd(); it++) {
 
66
                deleteRanges.push(QPair<int, int>((*it).first, (*it).second));
 
67
            }
 
68
            while (!deleteRanges.isEmpty()) {
 
69
                QPair<int, int> range = deleteRanges.pop();
 
70
                cursor.setPosition(range.first);
 
71
                cursor.setPosition(range.second, QTextCursor::KeepAnchor);
 
72
                cursor.deleteChar();
 
73
            }
 
74
        }
 
75
        else if (m_changeTracker->elementById(m_changeId)->getChangeType() == KoGenChange::formatChange) {
 
76
            QList<QPair<int, int> >::const_iterator it;
 
77
            for (it = m_changeRanges.constBegin(); it != m_changeRanges.constEnd(); it++) {
 
78
                cursor.setPosition((*it).first);
 
79
                cursor.setPosition((*it).second, QTextCursor::KeepAnchor);
 
80
                int changeId = cursor.charFormat().property(KoCharacterStyle::ChangeTrackerId).toInt();
 
81
                QTextCharFormat format = m_changeTracker->elementById(m_changeId)->getPrevFormat().toCharFormat();
 
82
                if (changeId == m_changeId) {
 
83
                    if (int parentChangeId = m_changeTracker->parent(m_changeId)) {
 
84
                        format.setProperty(KoCharacterStyle::ChangeTrackerId, parentChangeId);
 
85
                    }
 
86
                    else {
 
87
                        format.clearProperty(KoCharacterStyle::ChangeTrackerId);
 
88
                    }
 
89
                    cursor.setCharFormat(format);
 
90
                }
 
91
            }
 
92
        } else if (m_changeTracker->elementById(m_changeId)->getChangeType() == KoGenChange::deleteChange){
 
93
            QList<QPair<int, int> >::const_iterator it;
 
94
            QStack<QPair<int, int> > deleteRanges;
 
95
            for (it = m_changeRanges.constBegin(); it != m_changeRanges.constEnd(); it++) {
 
96
                cursor.setPosition((*it).first);
 
97
                cursor.setPosition((*it).second, QTextCursor::KeepAnchor);
 
98
                deleteRanges.push(QPair<int, int>((*it).first, (*it).second));
 
99
            }
 
100
            while (!deleteRanges.isEmpty()) {
 
101
                QPair<int, int> range = deleteRanges.pop();
 
102
                cursor.setPosition(range.first);
 
103
                cursor.setPosition(range.second, QTextCursor::KeepAnchor);
 
104
                if (dynamic_cast<KoDeleteChangeMarker*>(m_layout->inlineTextObjectManager()->inlineTextObject(cursor))) {
 
105
                    cursor.deleteChar();
 
106
                }
 
107
                else {
 
108
                    QTextCharFormat format = cursor.charFormat();
 
109
                    format.clearProperty(KoCharacterStyle::ChangeTrackerId);
 
110
                    cursor.setCharFormat(format);
 
111
                }
 
112
            }
 
113
        }
 
114
        m_changeTracker->acceptRejectChange(m_changeId, true);
 
115
    }
 
116
    else {
 
117
        m_changeTracker->acceptRejectChange(m_changeId, true);
 
118
        TextCommandBase::redo();
 
119
        UndoRedoFinalizer finalizer(this);
 
120
    }
 
121
    emit acceptRejectChange();}
 
122
 
 
123
void RejectChangeCommand::undo()
 
124
{
 
125
    m_changeTracker->acceptRejectChange(m_changeId, false);
 
126
    TextCommandBase::undo();
 
127
    UndoRedoFinalizer finalizer(this);
 
128
    emit acceptRejectChange();
 
129
}
 
130
 
 
131
#include <RejectChangeCommand.moc>