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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/*
* Copyright (c) 2012 C. Boemann <cbo@boemann.dk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "ChangeAnchorPropertiesCommand.h"
#include "KoAnchorInlineObject.h"
#include "KoAnchorTextRange.h"
#include <KoTextEditor.h>
#include <KoShapeContainer.h>
#include <KoTextShapeDataBase.h>
#include <KoTextDocument.h>
#include <KoInlineTextObjectManager.h>
#include <KoTextRangeManager.h>
ChangeAnchorPropertiesCommand::ChangeAnchorPropertiesCommand(KoShapeAnchor *anchor, const KoShapeAnchor &newAnchorData, KoShapeContainer *newParent, KUndo2Command *parent)
: KUndo2Command(kundo2_noi18n("Change Anchor Properties"), parent)
, m_anchor(anchor)
, m_oldAnchor(0)
, m_newAnchor(0)
, m_oldParent(anchor->shape()->parent())
, m_newParent(newParent)
, m_oldLocation(anchor->textLocation())
, m_newLocation(0)
, m_first(true)
, m_undone(false)
{
copyLayoutProperties(anchor, &m_oldAnchor);
copyLayoutProperties(&newAnchorData, &m_newAnchor);
}
ChangeAnchorPropertiesCommand::~ChangeAnchorPropertiesCommand()
{
if (m_undone) {
delete m_newLocation;
} else {
delete m_oldLocation;
}
}
void ChangeAnchorPropertiesCommand::copyLayoutProperties(const KoShapeAnchor *from, KoShapeAnchor *to)
{
to->setAnchorType(from->anchorType());
to->setOffset(from->offset());
to->setVerticalPos(from->verticalPos());
to->setVerticalRel(from->verticalRel());
to->setHorizontalPos(from->horizontalPos());
to->setHorizontalRel(from->horizontalRel());
}
void ChangeAnchorPropertiesCommand::redo()
{
KoTextShapeDataBase *textData = 0;
if (m_oldParent) {
textData = qobject_cast<KoTextShapeDataBase*>(m_oldParent->userData());
} else if (m_newParent) {
textData = qobject_cast<KoTextShapeDataBase*>(m_newParent->userData());
}
KUndo2Command::redo();
copyLayoutProperties(&m_newAnchor, m_anchor);
m_anchor->shape()->update();
if (m_first) {
m_oldAbsPos = m_anchor->shape()->absolutePosition();
m_anchor->shape()->setParent(m_newParent);
// let's just set the old absolute position so it doesn't look like it's moving around
m_anchor->shape()->setAbsolutePosition(m_oldAbsPos);
} else {
m_anchor->shape()->setParent(m_newParent);
m_anchor->shape()->setAbsolutePosition(m_newAbsPos);
m_anchor->shape()->update();
}
if (m_newAnchor.anchorType() != m_oldAnchor.anchorType()) {
Q_ASSERT(textData);
KoTextDocument doc(textData->document());
KoInlineTextObjectManager *inlineManager = doc.inlineTextObjectManager();
Q_ASSERT(inlineManager);
KoTextRangeManager *rangeManager = doc.textRangeManager();
Q_ASSERT(rangeManager);
// First remove from the old location
switch (m_oldAnchor.anchorType()) {
case KoShapeAnchor::AnchorPage:
// nothing we need to do to clean up old
break;
case KoShapeAnchor::AnchorAsCharacter:
if (m_first) {
//first time we need to remove the character manually
QTextCursor cursor(textData->document());
cursor.setPosition(m_oldLocation->position());
cursor.deleteChar();
}
inlineManager->removeInlineObject(dynamic_cast<KoInlineObject *>(m_oldLocation));
break;
case KoShapeAnchor::AnchorParagraph:
case KoShapeAnchor::AnchorToCharacter:
rangeManager->remove(dynamic_cast<KoTextRange *>(m_oldLocation));
// we need to mark dirty manually as it's a textrange
textData->document()->markContentsDirty(m_oldLocation->position(), 0);
break;
}
// And then set the new location
switch (m_newAnchor.anchorType()) {
case KoShapeAnchor::AnchorPage:
m_anchor->setTextLocation(0);
break;
case KoShapeAnchor::AnchorAsCharacter:
if (m_first) {
KoTextEditor *editor = doc.textEditor();
QTextCursor cursor(textData->document());
cursor.setPosition(editor->position());
m_newLocation = new KoAnchorInlineObject(m_anchor);
inlineManager->insertInlineObject(cursor, dynamic_cast<KoInlineObject *>(m_newLocation));
} else {
// only insert in manager as qt re-inserts the character
inlineManager->addInlineObject(dynamic_cast<KoInlineObject *>(m_newLocation));
}
m_anchor->setTextLocation(m_newLocation);
break;
case KoShapeAnchor::AnchorParagraph:
case KoShapeAnchor::AnchorToCharacter:
if (m_first) {
KoTextEditor *editor = doc.textEditor();
QTextCursor cursor(textData->document());
cursor.setPosition(editor->position());
KoAnchorTextRange *anchorRange = new KoAnchorTextRange(m_anchor, cursor);
anchorRange->setManager(rangeManager);
rangeManager->insert(anchorRange);
m_newLocation = anchorRange;
rangeManager->insert(anchorRange);
} else {
rangeManager->insert(dynamic_cast<KoTextRange *>(m_newLocation));
}
m_anchor->setTextLocation(m_newLocation);
// we need to mark dirty manually as it's a textrange
textData->document()->markContentsDirty(m_newLocation->position(), 0);
break;
}
} else if (m_newAnchor.anchorType() != KoShapeAnchor::AnchorPage) {
if (textData) {
Q_ASSERT(m_anchor->textLocation());
textData->document()->markContentsDirty(m_anchor->textLocation()->position(), 0);
}
}
m_first = false;
m_undone = false;
m_anchor->shape()->notifyChanged();
}
void ChangeAnchorPropertiesCommand::undo()
{
KoTextShapeDataBase *textData = 0;
if (m_oldParent) {
textData = qobject_cast<KoTextShapeDataBase*>(m_oldParent->userData());
} else if (m_newParent) {
textData = qobject_cast<KoTextShapeDataBase*>(m_newParent->userData());
}
copyLayoutProperties(&m_oldAnchor, m_anchor);
m_newAbsPos = m_anchor->shape()->absolutePosition();
m_anchor->shape()->update();
m_anchor->shape()->setParent(m_oldParent);
m_anchor->shape()->setAbsolutePosition(m_oldAbsPos);
m_anchor->shape()->update();
if (m_newAnchor.anchorType() != m_oldAnchor.anchorType()) {
Q_ASSERT(textData);
KoTextDocument doc(textData->document());
KoInlineTextObjectManager *inlineManager = doc.inlineTextObjectManager();
Q_ASSERT(inlineManager);
KoTextRangeManager *rangeManager = doc.textRangeManager();
Q_ASSERT(rangeManager);
// First unset the 'new' (current) location
switch (m_newAnchor.anchorType()) {
case KoShapeAnchor::AnchorPage:
// nothing we need to do to clean up old
break;
case KoShapeAnchor::AnchorAsCharacter:
// only remove in manager as qt removes the character
inlineManager->removeInlineObject(dynamic_cast<KoInlineObject *>(m_newLocation));
break;
case KoShapeAnchor::AnchorParagraph:
case KoShapeAnchor::AnchorToCharacter:
rangeManager->remove(dynamic_cast<KoTextRange *>(m_newLocation));
// we need to mark dirty manually as it's a textrange
textData->document()->markContentsDirty(m_newLocation->position(), 0);
break;
}
// Them re-insert the old (about to be restored) location
switch (m_oldAnchor.anchorType()) {
case KoShapeAnchor::AnchorPage:
// nothing we need to do to clean up old
m_anchor->setTextLocation(0);
break;
case KoShapeAnchor::AnchorAsCharacter:
// only insert in manager as qt re-inserts the character
inlineManager->addInlineObject(dynamic_cast<KoInlineObject *>(m_oldLocation));
m_anchor->setTextLocation(m_oldLocation);
break;
case KoShapeAnchor::AnchorParagraph:
case KoShapeAnchor::AnchorToCharacter:
rangeManager->insert(dynamic_cast<KoTextRange *>(m_oldLocation));
// we need to mark dirty manually as it's a textrange
textData->document()->markContentsDirty(m_oldLocation->position(), 0);
m_anchor->setTextLocation(m_oldLocation);
break;
}
} else if (m_newAnchor.anchorType() != KoShapeAnchor::AnchorPage) {
if (textData) {
Q_ASSERT(m_anchor->textLocation());
textData->document()->markContentsDirty(m_anchor->textLocation()->position(), 0);
}
}
KUndo2Command::undo();
m_undone = true;
m_anchor->shape()->notifyChanged();
}
|