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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/* This file is part of the KDE project
* Copyright (C) 2007 Thomas Zander <zander@kde.org>
* Copyright (C) 2008 Girish Ramakrishnan <girish@forwardbias.in>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library 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 "ChangeListCommand.h"
#include <KoTextBlockData.h>
#include <KoTextDocument.h>
#include <QTextCursor>
#include <KoParagraphStyle.h>
#include <KoList.h>
#include <klocalizedstring.h>
#include "TextDebug.h"
#define MARGIN_DEFAULT 18 // we consider it the default value
ChangeListCommand::ChangeListCommand(const QTextCursor &cursor, const KoListLevelProperties &levelProperties,
KoTextEditor::ChangeListFlags flags, KUndo2Command *parent)
: KoTextCommandBase(parent),
m_flags(flags),
m_first(true),
m_alignmentMode(false)
{
setText(kundo2_i18n("Change List"));
const bool styleCompletelySetAlready = extractTextBlocks(cursor, levelProperties.level(), levelProperties.labelType());
QSet<int> levels = m_levels.values().toSet();
KoListStyle::LabelType labelType = levelProperties.labelType();
KoListStyle listStyle;
// If the style is already completely set, we unset it instead
if (styleCompletelySetAlready && !(m_flags & KoTextEditor::DontUnsetIfSame))
labelType = KoListStyle::None;
foreach (int lev, levels) {
KoListLevelProperties llp;
llp.setLevel(lev);
llp.setLabelType(labelType);
llp.setNumberFormat(levelProperties.numberFormat());
llp.setListItemPrefix(levelProperties.listItemPrefix());
llp.setListItemSuffix(levelProperties.listItemSuffix());
if (KoListStyle::isNumberingStyle(labelType)) {
llp.setStartValue(1);
llp.setRelativeBulletSize(100); //we take the default value for numbering bullets as 100
if (llp.listItemSuffix().isEmpty()) {
llp.setListItemSuffix(".");
}
}
else if (labelType == KoListStyle::BulletCharLabelType) {
llp.setRelativeBulletSize(100); //we take the default value for numbering bullets as 100
llp.setBulletCharacter(levelProperties.bulletCharacter());
} else if (labelType == KoListStyle::ImageLabelType) {
llp.setBulletImage(levelProperties.bulletImage());
llp.setWidth(levelProperties.width());
llp.setHeight(levelProperties.height());
}
llp.setAlignmentMode(true); // when creating a new list we create it in this mode
llp.setLabelFollowedBy(KoListStyle::ListTab);
llp.setDisplayLevel(levelProperties.displayLevel());
llp.setTabStopPosition(MARGIN_DEFAULT*(lev+1));
llp.setMargin(MARGIN_DEFAULT*(lev+1));
llp.setTextIndent(- MARGIN_DEFAULT);
listStyle.setLevelProperties(llp);
}
initList(&listStyle);
setText(kundo2_i18n("Change List"));
}
ChangeListCommand::ChangeListCommand(const QTextCursor &cursor, KoListStyle *style, int level,
KoTextEditor::ChangeListFlags flags, KUndo2Command *parent)
: KoTextCommandBase(parent),
m_flags(flags),
m_first(true),
m_alignmentMode(false)
{
Q_ASSERT(style);
extractTextBlocks(cursor, level); // don't care about return value
initList(style);
setText(kundo2_i18n("Change List"));
}
ChangeListCommand::~ChangeListCommand()
{
}
bool ChangeListCommand::extractTextBlocks(const QTextCursor &cursor, int level, KoListStyle::LabelType newLabelType)
{
int selectionStart = qMin(cursor.anchor(), cursor.position());
int selectionEnd = qMax(cursor.anchor(), cursor.position());
bool styleCompletelySetAlready = true;
QTextBlock block = cursor.block().document()->findBlock(selectionStart);
bool oneOf = (selectionStart == selectionEnd); //ensures the block containing the cursor is selected in that case
while (block.isValid() && ((block.position() < selectionEnd) || oneOf)) {
m_blocks.append(block);
if (block.textList()) {
KoListLevelProperties prop = KoListLevelProperties::fromTextList(block.textList());
m_alignmentMode=prop.alignmentMode();
m_formerProperties.insert((m_blocks.size() - 1), prop);
m_levels.insert((m_blocks.size() - 1), detectLevel(block, level));
if (prop.labelType() != newLabelType)
styleCompletelySetAlready = false;
}
else {
KoListLevelProperties prop;
prop.setLabelType(KoListStyle::None);
prop.setAlignmentMode(true);
m_alignmentMode=prop.alignmentMode();
m_formerProperties.insert((m_blocks.size() - 1), prop);
m_levels.insert((m_blocks.size() - 1), level);
styleCompletelySetAlready = false;
}
oneOf = false;
block = block.next();
}
return styleCompletelySetAlready;
}
int ChangeListCommand::detectLevel(const QTextBlock &block, int givenLevel)
{
if (givenLevel != 0)
return givenLevel;
if (block.textList()) {
if (block.blockFormat().hasProperty(KoParagraphStyle::ListLevel))
return block.blockFormat().intProperty(KoParagraphStyle::ListLevel);
else
return block.textList()->format().intProperty(KoListStyle::Level);
}
return 1;
}
bool ChangeListCommand::formatsEqual(const KoListLevelProperties &llp, const QTextListFormat &format)
{
if (m_flags & KoTextEditor::MergeExactly) {
QTextListFormat listFormat;
llp.applyStyle(listFormat);
return listFormat == format;
} else {
return (int) llp.labelType() == (int) format.style();
}
}
void ChangeListCommand::initList(KoListStyle *listStyle)
{
KoTextDocument document(m_blocks.first().document());
KoList *mergeableList = 0;
KoList *newList = 0;
//First check if we could merge with previous or next list
if (m_flags & KoTextEditor::MergeWithAdjacentList) {
QSet<int> levels = m_levels.values().toSet();
// attempt to merge with previous block
QTextBlock prev = m_blocks.value(0).previous();
bool isMergeable = true;
foreach (int lev, levels) {
KoListLevelProperties llp = listStyle->levelProperties(lev);
// checks format compatibility
isMergeable = (isMergeable && prev.isValid() && prev.textList() && (formatsEqual(llp, prev.textList()->format())));
}
if (isMergeable)
mergeableList = document.list(prev);
if (!mergeableList) {
// attempt to merge with next block if previous failed
isMergeable = true;
QTextBlock next = m_blocks.value(m_blocks.size()-1).next();
foreach (int lev, levels) {
KoListLevelProperties llp = listStyle->levelProperties(lev);
isMergeable = (isMergeable && next.isValid() && next.textList() && (formatsEqual(llp, next.textList()->format())));
}
if (isMergeable)
mergeableList = document.list(next);
}
}
// Now iterates over the blocks and set-up the various lists
for (int i = 0; i < m_blocks.size(); ++i) {
m_list.insert(i, 0);
m_oldList.insert(i, document.list(m_blocks.at(i)));
m_newProperties.insert(i, listStyle->levelProperties(m_levels.value(i)));
// First check if we want to remove a list
if (m_newProperties.value(i).labelType() == KoListStyle::None) {
m_actions.insert(i, ChangeListCommand::RemoveList);
continue;
}
// Then check if we want to modify an existing list.
// The behaviour chosen for modifying a list is the following. If the selection contains more than one block, a new list is always created. If the selection only contains one block, the behaviour depends on the flag.
if ((m_flags & KoTextEditor::ModifyExistingList) && (m_blocks.size() == 1)) {
m_list.insert(i, document.list(m_blocks.at(i)));
if (m_list.value(i)) {
m_actions.insert(i, ChangeListCommand::ModifyExisting);
continue;
}
}
// Then check if we can merge with an existing list. The actual check was done before, here we just check the result.
if (mergeableList) {
m_list.insert(i, mergeableList);
m_actions.insert(i, ChangeListCommand::MergeList);
continue;
}
// All else failing, we need to create a new list.
KoList::Type type = m_flags & KoTextEditor::CreateNumberedParagraph ? KoList::NumberedParagraph : KoList::TextList;
if (!newList)
newList = new KoList(m_blocks.at(i).document(), listStyle, type);
m_list.insert(i, newList);
m_actions.insert(i, ChangeListCommand::CreateNew);
}
}
void ChangeListCommand::redo()
{
if (!m_first) {
for (int i = 0; i < m_blocks.size(); ++i) { // We invalidate the lists before calling redo on the QTextDocument
if (m_actions.value(i) == ChangeListCommand::RemoveList) {
//if the block is not part of a list continue
if (!m_blocks.at(i).textList()) {
continue;
}
for (int j = 0; j < m_blocks.at(i).textList()->count(); j++) {
if (m_blocks.at(i).textList()->item(j) != m_blocks.at(i)) {
QTextBlock currentBlock = m_blocks.at(i).textList()->item(j);
KoTextBlockData userData(currentBlock);
userData.setCounterWidth(-1.0);
break;
}
}
}
}
KoTextCommandBase::redo();
UndoRedoFinalizer finalizer(this);
for (int i = 0; i < m_blocks.size(); ++i) {
if ((m_actions.value(i) == ChangeListCommand::ModifyExisting) || (m_actions.value(i) == ChangeListCommand::CreateNew)
|| (m_actions.value(i) == ChangeListCommand::MergeList)) {
m_list.value(i)->updateStoredList(m_blocks.at(i));
KoListStyle *listStyle = m_list.value(i)->style();
listStyle->refreshLevelProperties(m_newProperties.value(i));
for (int j = 0; j < m_blocks.at(i).textList()->count(); j++) {
if (m_blocks.at(i).textList()->item(j) != m_blocks.at(i)) {
QTextBlock currentBlock = m_blocks.at(i).textList()->item(j);
KoTextBlockData userData(currentBlock);
userData.setCounterWidth(-1.0);
break;
}
}
}
QTextBlock currentBlock = m_blocks.at(i);
KoTextBlockData userData(currentBlock);
userData.setCounterWidth(-1.0);
}
}
else {
for (int i = 0; i < m_blocks.size(); ++i) {
if (m_actions.value(i) == ChangeListCommand::RemoveList) {
//if the block is not part of a list continue
if (!m_blocks.at(i).textList()) {
continue;
}
KoList::remove(m_blocks.at(i));
}
else if (m_actions.value(i) == ChangeListCommand::ModifyExisting) {
KoListStyle *listStyle = m_list.value(i)->style();
listStyle->setLevelProperties(m_newProperties.value(i));
QTextCursor cursor(m_blocks.at(i));
QTextBlockFormat format = m_blocks.at(i).blockFormat();
format.clearProperty(KoParagraphStyle::UnnumberedListItem);
cursor.setBlockFormat(format);
}
else {
//(ChangeListCommand::CreateNew)
//(ChangeListCommand::MergeList)
m_list.value(i)->add(m_blocks.at(i), m_newProperties.value(i).level());
QTextCursor cursor(m_blocks.at(i));
QTextBlockFormat format = m_blocks.at(i).blockFormat();
format.clearProperty(KoParagraphStyle::UnnumberedListItem);
cursor.setBlockFormat(format);
}
}
}
m_first = false;
}
void ChangeListCommand::undo()
{
KoTextCommandBase::undo();
UndoRedoFinalizer finalizer(this);
for (int i = 0; i < m_blocks.size(); ++i) {
// command to undo:
if (m_actions.value(i) == ChangeListCommand::RemoveList) {
//if the block is not part of a list continue
if (!m_blocks.at(i).textList()) {
continue;
}
m_oldList.value(i)->updateStoredList(m_blocks.at(i));
if ((m_flags & KoTextEditor::ModifyExistingList) && (m_formerProperties.value(i).labelType() != KoListStyle::None)) {
KoListStyle *listStyle = m_oldList.value(i)->style();
listStyle->refreshLevelProperties(m_formerProperties.value(i));
}
for (int j = 0; j < m_blocks.at(i).textList()->count(); j++) {
if (m_blocks.at(i).textList()->item(j) != m_blocks.at(i)) {
QTextBlock currentBlock = m_blocks.at(i).textList()->item(j);
KoTextBlockData userData(currentBlock);
userData.setCounterWidth(-1.0);
break;
}
}
}
else if (m_actions.value(i) == ChangeListCommand::ModifyExisting) {
m_list.value(i)->updateStoredList(m_blocks.at(i));
if ((m_flags & KoTextEditor::ModifyExistingList) && (m_formerProperties.value(i).labelType() != KoListStyle::None)) {
KoListStyle *listStyle = m_oldList.value(i)->style();
listStyle->refreshLevelProperties(m_formerProperties.value(i));
}
for (int j = 0; j < m_blocks.at(i).textList()->count(); j++) {
if (m_blocks.at(i).textList()->item(j) != m_blocks.at(i)) {
QTextBlock currentBlock = m_blocks.at(i).textList()->item(j);
KoTextBlockData userData(currentBlock);
userData.setCounterWidth(-1.0);
break;
}
}
}
else {
//(ChangeListCommand::CreateNew)
//(ChangeListCommand::MergeList)
//if the new/merged list replaced an existing list, the pointer to QTextList in oldList needs updating.
if ((m_oldList.value(i))) {
m_oldList.value(i)->updateStoredList(m_blocks.at(i));
for (int j = 0; j < m_blocks.at(i).textList()->count(); j++) {
if (m_blocks.at(i).textList()->item(j) != m_blocks.at(i)) {
QTextBlock currentBlock = m_blocks.at(i).textList()->item(j);
KoTextBlockData userData(currentBlock);
userData.setCounterWidth(-1.0);
break;
}
}
}
}
QTextBlock currentBlock = m_blocks.at(i);
KoTextBlockData userData(currentBlock);
userData.setCounterWidth(-1.0);
}
}
bool ChangeListCommand::mergeWith(const KUndo2Command *)
{
return false;
}
|