1
/* This file is part of the KDE project
2
* Copyright (C) 2010 Pierre Stirnweiss \pstirnweiss@googlemail.com>
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.
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.
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.
20
#include "RejectChangeCommand.h"
22
#include <KoGenChange.h>
23
#include <KoInlineTextObjectManager.h>
24
#include <KoTextDocument.h>
25
#include <KoTextDocumentLayout.h>
27
#include <changetracker/KoChangeTracker.h>
28
#include <changetracker/KoChangeTrackerElement.h>
29
#include <styles/KoCharacterStyle.h>
36
#include <QTextCharFormat>
37
#include <QTextCursor>
38
#include <QTextDocument>
39
#include <QTextFragment>
41
RejectChangeCommand::RejectChangeCommand (int changeId, QList<QPair<int, int> > changeRanges, QTextDocument *document, QUndoCommand* parent) : TextCommandBase(parent),
44
m_changeRanges(changeRanges),
47
setText(i18n("Reject change"));
49
m_changeTracker = KoTextDocument(m_document).changeTracker();
50
m_layout = dynamic_cast<KoTextDocumentLayout*>(document->documentLayout());
53
RejectChangeCommand::~RejectChangeCommand()
57
void RejectChangeCommand::redo()
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));
68
while (!deleteRanges.isEmpty()) {
69
QPair<int, int> range = deleteRanges.pop();
70
cursor.setPosition(range.first);
71
cursor.setPosition(range.second, QTextCursor::KeepAnchor);
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);
87
format.clearProperty(KoCharacterStyle::ChangeTrackerId);
89
cursor.setCharFormat(format);
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));
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))) {
108
QTextCharFormat format = cursor.charFormat();
109
format.clearProperty(KoCharacterStyle::ChangeTrackerId);
110
cursor.setCharFormat(format);
114
m_changeTracker->acceptRejectChange(m_changeId, true);
117
m_changeTracker->acceptRejectChange(m_changeId, true);
118
TextCommandBase::redo();
119
UndoRedoFinalizer finalizer(this);
121
emit acceptRejectChange();}
123
void RejectChangeCommand::undo()
125
m_changeTracker->acceptRejectChange(m_changeId, false);
126
TextCommandBase::undo();
127
UndoRedoFinalizer finalizer(this);
128
emit acceptRejectChange();
131
#include <RejectChangeCommand.moc>