author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
child 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtGui module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <private/qtools_p.h> |
|
43 |
#include <qdebug.h> |
|
44 |
||
45 |
#include "qtextdocument_p.h" |
|
46 |
#include "qtextdocument.h" |
|
47 |
#include <qtextformat.h> |
|
48 |
#include "qtextformat_p.h" |
|
49 |
#include "qtextobject_p.h" |
|
50 |
#include "qtextcursor.h" |
|
51 |
#include "qtextimagehandler_p.h" |
|
52 |
#include "qtextcursor_p.h" |
|
53 |
#include "qtextdocumentlayout_p.h" |
|
54 |
#include "qtexttable.h" |
|
55 |
#include "qtextengine_p.h" |
|
56 |
||
57 |
#include <stdlib.h> |
|
58 |
||
59 |
QT_BEGIN_NAMESPACE |
|
60 |
||
61 |
#define PMDEBUG if(0) qDebug |
|
62 |
||
63 |
// The VxWorks DIAB compiler crashes when initializing the anonymouse union with { a7 } |
|
64 |
#if !defined(Q_CC_DIAB) |
|
65 |
# define QT_INIT_TEXTUNDOCOMMAND(c, a1, a2, a3, a4, a5, a6, a7, a8) \ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
66 |
QTextUndoCommand c = { a1, a2, 0, 0, quint8(a3), a4, a5, a6, { a7 }, a8 } |
0 | 67 |
#else |
68 |
# define QT_INIT_TEXTUNDOCOMMAND(c, a1, a2, a3, a4, a5, a6, a7, a8) \ |
|
69 |
QTextUndoCommand c = { a1, a2, 0, 0, a3, a4, a5, a6 }; c.blockFormat = a7; c.revision = a8 |
|
70 |
#endif |
|
71 |
||
72 |
/* |
|
73 |
Structure of a document: |
|
74 |
||
75 |
DOCUMENT :== FRAME_CONTENTS |
|
76 |
FRAME :== START_OF_FRAME FRAME_CONTENTS END_OF_FRAME |
|
77 |
FRAME_CONTENTS = LIST_OF_BLOCKS ((FRAME | TABLE) LIST_OF_BLOCKS)* |
|
78 |
TABLE :== (START_OF_FRAME TABLE_CELL)+ END_OF_FRAME |
|
79 |
TABLE_CELL = FRAME_CONTENTS |
|
80 |
LIST_OF_BLOCKS :== (BLOCK END_OF_PARA)* BLOCK |
|
81 |
BLOCK :== (FRAGMENT)* |
|
82 |
FRAGMENT :== String of characters |
|
83 |
||
84 |
END_OF_PARA :== 0x2029 # Paragraph separator in Unicode |
|
85 |
START_OF_FRAME :== 0xfdd0 |
|
86 |
END_OF_FRAME := 0xfdd1 |
|
87 |
||
88 |
Note also that LIST_OF_BLOCKS can be empty. Nevertheless, there is |
|
89 |
at least one valid cursor position there where you could start |
|
90 |
typing. The block format is in this case determined by the last |
|
91 |
END_OF_PARA/START_OF_FRAME/END_OF_FRAME (see below). |
|
92 |
||
93 |
Lists are not in here, as they are treated specially. A list is just |
|
94 |
a collection of (not neccessarily connected) blocks, that share the |
|
95 |
same objectIndex() in the format that refers to the list format and |
|
96 |
object. |
|
97 |
||
98 |
The above does not clearly note where formats are. Here's |
|
99 |
how it looks currently: |
|
100 |
||
101 |
FRAGMENT: one charFormat associated |
|
102 |
||
103 |
END_OF_PARA: one charFormat, and a blockFormat for the _next_ block. |
|
104 |
||
105 |
START_OF_FRAME: one char format, and a blockFormat (for the next |
|
106 |
block). The format associated with the objectIndex() of the |
|
107 |
charFormat decides whether this is a frame or table and its |
|
108 |
properties |
|
109 |
||
110 |
END_OF_FRAME: one charFormat and a blockFormat (for the next |
|
111 |
block). The object() of the charFormat is the same as for the |
|
112 |
corresponding START_OF_BLOCK. |
|
113 |
||
114 |
||
115 |
The document is independent of the layout with certain restrictions: |
|
116 |
||
117 |
* Cursor movement (esp. up and down) depend on the layout. |
|
118 |
* You cannot have more than one layout, as the layout data of QTextObjects |
|
119 |
is stored in the text object itself. |
|
120 |
||
121 |
*/ |
|
122 |
||
123 |
void QTextBlockData::invalidate() const |
|
124 |
{ |
|
125 |
if (layout) |
|
126 |
layout->engine()->invalidate(); |
|
127 |
} |
|
128 |
||
129 |
static bool isValidBlockSeparator(const QChar &ch) |
|
130 |
{ |
|
131 |
return ch == QChar::ParagraphSeparator |
|
132 |
|| ch == QTextBeginningOfFrame |
|
133 |
|| ch == QTextEndOfFrame; |
|
134 |
} |
|
135 |
||
136 |
#ifndef QT_NO_DEBUG |
|
137 |
static bool noBlockInString(const QString &str) |
|
138 |
{ |
|
139 |
return !str.contains(QChar::ParagraphSeparator) |
|
140 |
&& !str.contains(QTextBeginningOfFrame) |
|
141 |
&& !str.contains(QTextEndOfFrame); |
|
142 |
} |
|
143 |
#endif |
|
144 |
||
145 |
bool QTextUndoCommand::tryMerge(const QTextUndoCommand &other) |
|
146 |
{ |
|
147 |
if (command != other.command) |
|
148 |
return false; |
|
149 |
||
150 |
if (command == Inserted |
|
151 |
&& (pos + length == other.pos) |
|
152 |
&& (strPos + length == other.strPos) |
|
153 |
&& format == other.format) { |
|
154 |
||
155 |
length += other.length; |
|
156 |
return true; |
|
157 |
} |
|
158 |
||
159 |
// removal to the 'right' using 'Delete' key |
|
160 |
if (command == Removed |
|
161 |
&& pos == other.pos |
|
162 |
&& (strPos + length == other.strPos) |
|
163 |
&& format == other.format) { |
|
164 |
||
165 |
length += other.length; |
|
166 |
return true; |
|
167 |
} |
|
168 |
||
169 |
// removal to the 'left' using 'Backspace' |
|
170 |
if (command == Removed |
|
171 |
&& (other.pos + other.length == pos) |
|
172 |
&& (other.strPos + other.length == strPos) |
|
173 |
&& (format == other.format)) { |
|
174 |
||
175 |
int l = length; |
|
176 |
(*this) = other; |
|
177 |
||
178 |
length += l; |
|
179 |
return true; |
|
180 |
} |
|
181 |
||
182 |
return false; |
|
183 |
} |
|
184 |
||
185 |
QTextDocumentPrivate::QTextDocumentPrivate() |
|
186 |
: wasUndoAvailable(false), |
|
187 |
wasRedoAvailable(false), |
|
188 |
docChangeOldLength(0), |
|
189 |
docChangeLength(0), |
|
190 |
framesDirty(true), |
|
191 |
rtFrame(0), |
|
192 |
initialBlockCharFormatIndex(-1) // set correctly later in init() |
|
193 |
{ |
|
194 |
editBlock = 0; |
|
195 |
docChangeFrom = -1; |
|
196 |
||
197 |
undoState = 0; |
|
198 |
revision = -1; // init() inserts a block, bringing it to 0 |
|
199 |
||
200 |
lout = 0; |
|
201 |
||
202 |
modified = false; |
|
203 |
modifiedState = 0; |
|
204 |
||
205 |
undoEnabled = true; |
|
206 |
inContentsChange = false; |
|
207 |
||
208 |
defaultTextOption.setTabStop(80); // same as in qtextengine.cpp |
|
209 |
defaultTextOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); |
|
210 |
||
211 |
indentWidth = 40; |
|
212 |
documentMargin = 4; |
|
213 |
||
214 |
maximumBlockCount = 0; |
|
215 |
needsEnsureMaximumBlockCount = false; |
|
216 |
unreachableCharacterCount = 0; |
|
217 |
lastBlockCount = 0; |
|
218 |
} |
|
219 |
||
220 |
void QTextDocumentPrivate::init() |
|
221 |
{ |
|
222 |
framesDirty = false; |
|
223 |
||
224 |
bool undoState = undoEnabled; |
|
225 |
undoEnabled = false; |
|
226 |
initialBlockCharFormatIndex = formats.indexForFormat(QTextCharFormat()); |
|
227 |
insertBlock(0, formats.indexForFormat(QTextBlockFormat()), formats.indexForFormat(QTextCharFormat())); |
|
228 |
undoEnabled = undoState; |
|
229 |
modified = false; |
|
230 |
modifiedState = 0; |
|
231 |
} |
|
232 |
||
233 |
void QTextDocumentPrivate::clear() |
|
234 |
{ |
|
235 |
Q_Q(QTextDocument); |
|
236 |
for (int i = 0; i < cursors.count(); ++i) { |
|
237 |
cursors.at(i)->setPosition(0); |
|
238 |
cursors.at(i)->currentCharFormat = -1; |
|
239 |
cursors.at(i)->anchor = 0; |
|
240 |
cursors.at(i)->adjusted_anchor = 0; |
|
241 |
} |
|
242 |
||
243 |
QList<QTextCursorPrivate *>oldCursors = cursors; |
|
244 |
QT_TRY{ |
|
245 |
cursors.clear(); |
|
246 |
changedCursors.clear(); |
|
247 |
||
248 |
QMap<int, QTextObject *>::Iterator objectIt = objects.begin(); |
|
249 |
while (objectIt != objects.end()) { |
|
250 |
if (*objectIt != rtFrame) { |
|
251 |
delete *objectIt; |
|
252 |
objectIt = objects.erase(objectIt); |
|
253 |
} else { |
|
254 |
++objectIt; |
|
255 |
} |
|
256 |
} |
|
257 |
// also clear out the remaining root frame pointer |
|
258 |
// (we're going to delete the object further down) |
|
259 |
objects.clear(); |
|
260 |
||
261 |
title.clear(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
262 |
clearUndoRedoStacks(QTextDocument::UndoAndRedoStacks); |
0 | 263 |
text = QString(); |
264 |
unreachableCharacterCount = 0; |
|
265 |
modifiedState = 0; |
|
266 |
modified = false; |
|
267 |
formats = QTextFormatCollection(); |
|
268 |
int len = fragments.length(); |
|
269 |
fragments.clear(); |
|
270 |
blocks.clear(); |
|
271 |
cachedResources.clear(); |
|
272 |
delete rtFrame; |
|
273 |
rtFrame = 0; |
|
274 |
init(); |
|
275 |
cursors = oldCursors; |
|
276 |
inContentsChange = true; |
|
277 |
q->contentsChange(0, len, 0); |
|
278 |
inContentsChange = false; |
|
279 |
if (lout) |
|
280 |
lout->documentChanged(0, len, 0); |
|
281 |
} QT_CATCH(...) { |
|
282 |
cursors = oldCursors; // at least recover the cursors |
|
283 |
QT_RETHROW; |
|
284 |
} |
|
285 |
} |
|
286 |
||
287 |
QTextDocumentPrivate::~QTextDocumentPrivate() |
|
288 |
{ |
|
289 |
for (int i = 0; i < cursors.count(); ++i) |
|
290 |
cursors.at(i)->priv = 0; |
|
291 |
cursors.clear(); |
|
292 |
undoState = 0; |
|
293 |
undoEnabled = true; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
294 |
clearUndoRedoStacks(QTextDocument::RedoStack); |
0 | 295 |
} |
296 |
||
297 |
void QTextDocumentPrivate::setLayout(QAbstractTextDocumentLayout *layout) |
|
298 |
{ |
|
299 |
Q_Q(QTextDocument); |
|
300 |
if (lout == layout) |
|
301 |
return; |
|
302 |
const bool firstLayout = !lout; |
|
303 |
delete lout; |
|
304 |
lout = layout; |
|
305 |
||
306 |
if (!firstLayout) |
|
307 |
for (BlockMap::Iterator it = blocks.begin(); !it.atEnd(); ++it) |
|
308 |
it->free(); |
|
309 |
||
310 |
emit q->documentLayoutChanged(); |
|
311 |
inContentsChange = true; |
|
312 |
emit q->contentsChange(0, 0, length()); |
|
313 |
inContentsChange = false; |
|
314 |
if (lout) |
|
315 |
lout->documentChanged(0, 0, length()); |
|
316 |
} |
|
317 |
||
318 |
||
319 |
void QTextDocumentPrivate::insert_string(int pos, uint strPos, uint length, int format, QTextUndoCommand::Operation op) |
|
320 |
{ |
|
321 |
// ##### optimise when only appending to the fragment! |
|
322 |
Q_ASSERT(noBlockInString(text.mid(strPos, length))); |
|
323 |
||
324 |
split(pos); |
|
325 |
uint x = fragments.insert_single(pos, length); |
|
326 |
QTextFragmentData *X = fragments.fragment(x); |
|
327 |
X->format = format; |
|
328 |
X->stringPosition = strPos; |
|
329 |
uint w = fragments.previous(x); |
|
330 |
if (w) |
|
331 |
unite(w); |
|
332 |
||
333 |
int b = blocks.findNode(pos); |
|
334 |
blocks.setSize(b, blocks.size(b)+length); |
|
335 |
||
336 |
Q_ASSERT(blocks.length() == fragments.length()); |
|
337 |
||
338 |
QTextFrame *frame = qobject_cast<QTextFrame *>(objectForFormat(format)); |
|
339 |
if (frame) { |
|
340 |
frame->d_func()->fragmentAdded(text.at(strPos), x); |
|
341 |
framesDirty = true; |
|
342 |
} |
|
343 |
||
344 |
adjustDocumentChangesAndCursors(pos, length, op); |
|
345 |
} |
|
346 |
||
347 |
int QTextDocumentPrivate::insert_block(int pos, uint strPos, int format, int blockFormat, QTextUndoCommand::Operation op, int command) |
|
348 |
{ |
|
349 |
split(pos); |
|
350 |
uint x = fragments.insert_single(pos, 1); |
|
351 |
QTextFragmentData *X = fragments.fragment(x); |
|
352 |
X->format = format; |
|
353 |
X->stringPosition = strPos; |
|
354 |
// no need trying to unite, since paragraph separators are always in a fragment of their own |
|
355 |
||
356 |
Q_ASSERT(isValidBlockSeparator(text.at(strPos))); |
|
357 |
Q_ASSERT(blocks.length()+1 == fragments.length()); |
|
358 |
||
359 |
int block_pos = pos; |
|
360 |
if (blocks.length() && command == QTextUndoCommand::BlockRemoved) |
|
361 |
++block_pos; |
|
362 |
int size = 1; |
|
363 |
int n = blocks.findNode(block_pos); |
|
364 |
int key = n ? blocks.position(n) : blocks.length(); |
|
365 |
||
366 |
Q_ASSERT(n || (!n && block_pos == blocks.length())); |
|
367 |
if (key != block_pos) { |
|
368 |
Q_ASSERT(key < block_pos); |
|
369 |
int oldSize = blocks.size(n); |
|
370 |
blocks.setSize(n, block_pos-key); |
|
371 |
size += oldSize - (block_pos-key); |
|
372 |
} |
|
373 |
int b = blocks.insert_single(block_pos, size); |
|
374 |
QTextBlockData *B = blocks.fragment(b); |
|
375 |
B->format = blockFormat; |
|
376 |
||
377 |
Q_ASSERT(blocks.length() == fragments.length()); |
|
378 |
||
379 |
QTextBlockGroup *group = qobject_cast<QTextBlockGroup *>(objectForFormat(blockFormat)); |
|
380 |
if (group) |
|
381 |
group->blockInserted(QTextBlock(this, b)); |
|
382 |
||
383 |
QTextFrame *frame = qobject_cast<QTextFrame *>(objectForFormat(formats.format(format))); |
|
384 |
if (frame) { |
|
385 |
frame->d_func()->fragmentAdded(text.at(strPos), x); |
|
386 |
framesDirty = true; |
|
387 |
} |
|
388 |
||
389 |
adjustDocumentChangesAndCursors(pos, 1, op); |
|
390 |
return x; |
|
391 |
} |
|
392 |
||
393 |
int QTextDocumentPrivate::insertBlock(const QChar &blockSeparator, |
|
394 |
int pos, int blockFormat, int charFormat, QTextUndoCommand::Operation op) |
|
395 |
{ |
|
396 |
Q_ASSERT(formats.format(blockFormat).isBlockFormat()); |
|
397 |
Q_ASSERT(formats.format(charFormat).isCharFormat()); |
|
398 |
Q_ASSERT(pos >= 0 && (pos < fragments.length() || (pos == 0 && fragments.length() == 0))); |
|
399 |
Q_ASSERT(isValidBlockSeparator(blockSeparator)); |
|
400 |
||
401 |
beginEditBlock(); |
|
402 |
||
403 |
int strPos = text.length(); |
|
404 |
text.append(blockSeparator); |
|
405 |
||
406 |
int ob = blocks.findNode(pos); |
|
407 |
bool atBlockEnd = true; |
|
408 |
bool atBlockStart = true; |
|
409 |
int oldRevision = 0; |
|
410 |
if (ob) { |
|
411 |
atBlockEnd = (pos - blocks.position(ob) == blocks.size(ob)-1); |
|
412 |
atBlockStart = ((int)blocks.position(ob) == pos); |
|
413 |
oldRevision = blocks.fragment(ob)->revision; |
|
414 |
} |
|
415 |
||
416 |
const int fragment = insert_block(pos, strPos, charFormat, blockFormat, op, QTextUndoCommand::BlockRemoved); |
|
417 |
||
418 |
Q_ASSERT(blocks.length() == fragments.length()); |
|
419 |
||
420 |
int b = blocks.findNode(pos); |
|
421 |
QTextBlockData *B = blocks.fragment(b); |
|
422 |
||
423 |
QT_INIT_TEXTUNDOCOMMAND(c, QTextUndoCommand::BlockInserted, (editBlock != 0), |
|
424 |
op, charFormat, strPos, pos, blockFormat, |
|
425 |
B->revision); |
|
426 |
||
427 |
appendUndoItem(c); |
|
428 |
Q_ASSERT(undoState == undoStack.size()); |
|
429 |
||
430 |
// update revision numbers of the modified blocks. |
|
431 |
B->revision = (atBlockEnd && !atBlockStart)? oldRevision : revision; |
|
432 |
b = blocks.next(b); |
|
433 |
if (b) { |
|
434 |
B = blocks.fragment(b); |
|
435 |
B->revision = atBlockStart ? oldRevision : revision; |
|
436 |
} |
|
437 |
||
438 |
if (formats.charFormat(charFormat).objectIndex() == -1) |
|
439 |
needsEnsureMaximumBlockCount = true; |
|
440 |
||
441 |
endEditBlock(); |
|
442 |
return fragment; |
|
443 |
} |
|
444 |
||
445 |
int QTextDocumentPrivate::insertBlock(int pos, int blockFormat, int charFormat, QTextUndoCommand::Operation op) |
|
446 |
{ |
|
447 |
return insertBlock(QChar::ParagraphSeparator, pos, blockFormat, charFormat, op); |
|
448 |
} |
|
449 |
||
450 |
void QTextDocumentPrivate::insert(int pos, int strPos, int strLength, int format) |
|
451 |
{ |
|
452 |
if (strLength <= 0) |
|
453 |
return; |
|
454 |
||
455 |
Q_ASSERT(pos >= 0 && pos < fragments.length()); |
|
456 |
Q_ASSERT(formats.format(format).isCharFormat()); |
|
457 |
||
458 |
insert_string(pos, strPos, strLength, format, QTextUndoCommand::MoveCursor); |
|
459 |
if (undoEnabled) { |
|
460 |
int b = blocks.findNode(pos); |
|
461 |
QTextBlockData *B = blocks.fragment(b); |
|
462 |
||
463 |
QT_INIT_TEXTUNDOCOMMAND(c, QTextUndoCommand::Inserted, (editBlock != 0), |
|
464 |
QTextUndoCommand::MoveCursor, format, strPos, pos, strLength, |
|
465 |
B->revision); |
|
466 |
appendUndoItem(c); |
|
467 |
B->revision = revision; |
|
468 |
Q_ASSERT(undoState == undoStack.size()); |
|
469 |
} |
|
470 |
finishEdit(); |
|
471 |
} |
|
472 |
||
473 |
void QTextDocumentPrivate::insert(int pos, const QString &str, int format) |
|
474 |
{ |
|
475 |
if (str.size() == 0) |
|
476 |
return; |
|
477 |
||
478 |
Q_ASSERT(noBlockInString(str)); |
|
479 |
||
480 |
int strPos = text.length(); |
|
481 |
text.append(str); |
|
482 |
insert(pos, strPos, str.length(), format); |
|
483 |
} |
|
484 |
||
485 |
int QTextDocumentPrivate::remove_string(int pos, uint length, QTextUndoCommand::Operation op) |
|
486 |
{ |
|
487 |
Q_ASSERT(pos >= 0); |
|
488 |
Q_ASSERT(blocks.length() == fragments.length()); |
|
489 |
Q_ASSERT(blocks.length() >= pos+(int)length); |
|
490 |
||
491 |
int b = blocks.findNode(pos); |
|
492 |
uint x = fragments.findNode(pos); |
|
493 |
||
494 |
Q_ASSERT(blocks.size(b) > length); |
|
495 |
Q_ASSERT(x && fragments.position(x) == (uint)pos && fragments.size(x) == length); |
|
496 |
Q_ASSERT(noBlockInString(text.mid(fragments.fragment(x)->stringPosition, length))); |
|
497 |
||
498 |
blocks.setSize(b, blocks.size(b)-length); |
|
499 |
||
500 |
QTextFrame *frame = qobject_cast<QTextFrame *>(objectForFormat(fragments.fragment(x)->format)); |
|
501 |
if (frame) { |
|
502 |
frame->d_func()->fragmentRemoved(text.at(fragments.fragment(x)->stringPosition), x); |
|
503 |
framesDirty = true; |
|
504 |
} |
|
505 |
||
506 |
const int w = fragments.erase_single(x); |
|
507 |
||
508 |
if (!undoEnabled) |
|
509 |
unreachableCharacterCount += length; |
|
510 |
||
511 |
adjustDocumentChangesAndCursors(pos, -int(length), op); |
|
512 |
||
513 |
return w; |
|
514 |
} |
|
515 |
||
516 |
int QTextDocumentPrivate::remove_block(int pos, int *blockFormat, int command, QTextUndoCommand::Operation op) |
|
517 |
{ |
|
518 |
Q_ASSERT(pos >= 0); |
|
519 |
Q_ASSERT(blocks.length() == fragments.length()); |
|
520 |
Q_ASSERT(blocks.length() > pos); |
|
521 |
||
522 |
int b = blocks.findNode(pos); |
|
523 |
uint x = fragments.findNode(pos); |
|
524 |
||
525 |
Q_ASSERT(x && (int)fragments.position(x) == pos); |
|
526 |
Q_ASSERT(fragments.size(x) == 1); |
|
527 |
Q_ASSERT(isValidBlockSeparator(text.at(fragments.fragment(x)->stringPosition))); |
|
528 |
Q_ASSERT(b); |
|
529 |
||
530 |
if (blocks.size(b) == 1 && command == QTextUndoCommand::BlockAdded) { |
|
531 |
Q_ASSERT((int)blocks.position(b) == pos); |
|
532 |
// qDebug("removing empty block"); |
|
533 |
// empty block remove the block itself |
|
534 |
} else { |
|
535 |
// non empty block, merge with next one into this block |
|
536 |
// qDebug("merging block with next"); |
|
537 |
int n = blocks.next(b); |
|
538 |
Q_ASSERT((int)blocks.position(n) == pos + 1); |
|
539 |
blocks.setSize(b, blocks.size(b) + blocks.size(n) - 1); |
|
540 |
b = n; |
|
541 |
} |
|
542 |
*blockFormat = blocks.fragment(b)->format; |
|
543 |
||
544 |
QTextBlockGroup *group = qobject_cast<QTextBlockGroup *>(objectForFormat(blocks.fragment(b)->format)); |
|
545 |
if (group) |
|
546 |
group->blockRemoved(QTextBlock(this, b)); |
|
547 |
||
548 |
QTextFrame *frame = qobject_cast<QTextFrame *>(objectForFormat(fragments.fragment(x)->format)); |
|
549 |
if (frame) { |
|
550 |
frame->d_func()->fragmentRemoved(text.at(fragments.fragment(x)->stringPosition), x); |
|
551 |
framesDirty = true; |
|
552 |
} |
|
553 |
||
554 |
blocks.erase_single(b); |
|
555 |
const int w = fragments.erase_single(x); |
|
556 |
||
557 |
adjustDocumentChangesAndCursors(pos, -1, op); |
|
558 |
||
559 |
return w; |
|
560 |
} |
|
561 |
||
562 |
#if !defined(QT_NO_DEBUG) |
|
563 |
static bool isAncestorFrame(QTextFrame *possibleAncestor, QTextFrame *child) |
|
564 |
{ |
|
565 |
while (child) { |
|
566 |
if (child == possibleAncestor) |
|
567 |
return true; |
|
568 |
child = child->parentFrame(); |
|
569 |
} |
|
570 |
return false; |
|
571 |
} |
|
572 |
#endif |
|
573 |
||
574 |
void QTextDocumentPrivate::move(int pos, int to, int length, QTextUndoCommand::Operation op) |
|
575 |
{ |
|
576 |
Q_ASSERT(to <= fragments.length() && to <= pos); |
|
577 |
Q_ASSERT(pos >= 0 && pos+length <= fragments.length()); |
|
578 |
Q_ASSERT(blocks.length() == fragments.length()); |
|
579 |
||
580 |
if (pos == to) |
|
581 |
return; |
|
582 |
||
583 |
const bool needsInsert = to != -1; |
|
584 |
||
585 |
#if !defined(QT_NO_DEBUG) |
|
586 |
const bool startAndEndInSameFrame = (frameAt(pos) == frameAt(pos + length - 1)); |
|
587 |
||
588 |
const bool endIsEndOfChildFrame = (isAncestorFrame(frameAt(pos), frameAt(pos + length - 1)) |
|
589 |
&& text.at(find(pos + length - 1)->stringPosition) == QTextEndOfFrame); |
|
590 |
||
591 |
const bool startIsStartOfFrameAndEndIsEndOfFrameWithCommonParent |
|
592 |
= (text.at(find(pos)->stringPosition) == QTextBeginningOfFrame |
|
593 |
&& text.at(find(pos + length - 1)->stringPosition) == QTextEndOfFrame |
|
594 |
&& frameAt(pos)->parentFrame() == frameAt(pos + length - 1)->parentFrame()); |
|
595 |
||
596 |
const bool isFirstTableCell = (qobject_cast<QTextTable *>(frameAt(pos + length - 1)) |
|
597 |
&& frameAt(pos + length - 1)->parentFrame() == frameAt(pos)); |
|
598 |
||
599 |
Q_ASSERT(startAndEndInSameFrame || endIsEndOfChildFrame || startIsStartOfFrameAndEndIsEndOfFrameWithCommonParent || isFirstTableCell); |
|
600 |
#endif |
|
601 |
||
602 |
split(pos); |
|
603 |
split(pos+length); |
|
604 |
||
605 |
uint dst = needsInsert ? fragments.findNode(to) : 0; |
|
606 |
uint dstKey = needsInsert ? fragments.position(dst) : 0; |
|
607 |
||
608 |
uint x = fragments.findNode(pos); |
|
609 |
uint end = fragments.findNode(pos+length); |
|
610 |
||
611 |
uint w = 0; |
|
612 |
while (x != end) { |
|
613 |
uint n = fragments.next(x); |
|
614 |
||
615 |
uint key = fragments.position(x); |
|
616 |
uint b = blocks.findNode(key+1); |
|
617 |
QTextBlockData *B = blocks.fragment(b); |
|
618 |
int blockRevision = B->revision; |
|
619 |
||
620 |
QTextFragmentData *X = fragments.fragment(x); |
|
621 |
QT_INIT_TEXTUNDOCOMMAND(c, QTextUndoCommand::Removed, (editBlock != 0), |
|
622 |
op, X->format, X->stringPosition, key, X->size_array[0], |
|
623 |
blockRevision); |
|
624 |
QT_INIT_TEXTUNDOCOMMAND(cInsert, QTextUndoCommand::Inserted, (editBlock != 0), |
|
625 |
op, X->format, X->stringPosition, dstKey, X->size_array[0], |
|
626 |
blockRevision); |
|
627 |
||
628 |
if (key+1 != blocks.position(b)) { |
|
629 |
// qDebug("remove_string from %d length %d", key, X->size_array[0]); |
|
630 |
Q_ASSERT(noBlockInString(text.mid(X->stringPosition, X->size_array[0]))); |
|
631 |
w = remove_string(key, X->size_array[0], op); |
|
632 |
||
633 |
if (needsInsert) { |
|
634 |
insert_string(dstKey, X->stringPosition, X->size_array[0], X->format, op); |
|
635 |
dstKey += X->size_array[0]; |
|
636 |
} |
|
637 |
} else { |
|
638 |
// qDebug("remove_block at %d", key); |
|
639 |
Q_ASSERT(X->size_array[0] == 1 && isValidBlockSeparator(text.at(X->stringPosition))); |
|
640 |
b = blocks.previous(b); |
|
641 |
B = 0; |
|
642 |
c.command = blocks.size(b) == 1 ? QTextUndoCommand::BlockDeleted : QTextUndoCommand::BlockRemoved; |
|
643 |
w = remove_block(key, &c.blockFormat, QTextUndoCommand::BlockAdded, op); |
|
644 |
||
645 |
if (needsInsert) { |
|
646 |
insert_block(dstKey++, X->stringPosition, X->format, c.blockFormat, op, QTextUndoCommand::BlockRemoved); |
|
647 |
cInsert.command = blocks.size(b) == 1 ? QTextUndoCommand::BlockAdded : QTextUndoCommand::BlockInserted; |
|
648 |
cInsert.blockFormat = c.blockFormat; |
|
649 |
} |
|
650 |
} |
|
651 |
appendUndoItem(c); |
|
652 |
if (B) |
|
653 |
B->revision = revision; |
|
654 |
x = n; |
|
655 |
||
656 |
if (needsInsert) |
|
657 |
appendUndoItem(cInsert); |
|
658 |
} |
|
659 |
if (w) |
|
660 |
unite(w); |
|
661 |
||
662 |
Q_ASSERT(blocks.length() == fragments.length()); |
|
663 |
||
664 |
finishEdit(); |
|
665 |
} |
|
666 |
||
667 |
void QTextDocumentPrivate::remove(int pos, int length, QTextUndoCommand::Operation op) |
|
668 |
{ |
|
669 |
if (length == 0) |
|
670 |
return; |
|
671 |
move(pos, -1, length, op); |
|
672 |
} |
|
673 |
||
674 |
void QTextDocumentPrivate::setCharFormat(int pos, int length, const QTextCharFormat &newFormat, FormatChangeMode mode) |
|
675 |
{ |
|
676 |
beginEditBlock(); |
|
677 |
||
678 |
Q_ASSERT(newFormat.isValid()); |
|
679 |
||
680 |
int newFormatIdx = -1; |
|
681 |
if (mode == SetFormatAndPreserveObjectIndices) { |
|
682 |
QTextCharFormat cleanFormat = newFormat; |
|
683 |
cleanFormat.clearProperty(QTextFormat::ObjectIndex); |
|
684 |
newFormatIdx = formats.indexForFormat(cleanFormat); |
|
685 |
} else if (mode == SetFormat) { |
|
686 |
newFormatIdx = formats.indexForFormat(newFormat); |
|
687 |
} |
|
688 |
||
689 |
if (pos == -1) { |
|
690 |
if (mode == MergeFormat) { |
|
691 |
QTextFormat format = formats.format(initialBlockCharFormatIndex); |
|
692 |
format.merge(newFormat); |
|
693 |
initialBlockCharFormatIndex = formats.indexForFormat(format); |
|
694 |
} else if (mode == SetFormatAndPreserveObjectIndices |
|
695 |
&& formats.format(initialBlockCharFormatIndex).objectIndex() != -1) { |
|
696 |
QTextCharFormat f = newFormat; |
|
697 |
f.setObjectIndex(formats.format(initialBlockCharFormatIndex).objectIndex()); |
|
698 |
initialBlockCharFormatIndex = formats.indexForFormat(f); |
|
699 |
} else { |
|
700 |
initialBlockCharFormatIndex = newFormatIdx; |
|
701 |
} |
|
702 |
||
703 |
++pos; |
|
704 |
--length; |
|
705 |
} |
|
706 |
||
707 |
const int startPos = pos; |
|
708 |
const int endPos = pos + length; |
|
709 |
||
710 |
split(startPos); |
|
711 |
split(endPos); |
|
712 |
||
713 |
while (pos < endPos) { |
|
714 |
FragmentMap::Iterator it = fragments.find(pos); |
|
715 |
Q_ASSERT(!it.atEnd()); |
|
716 |
||
717 |
QTextFragmentData *fragment = it.value(); |
|
718 |
||
719 |
Q_ASSERT(formats.format(fragment->format).type() == QTextFormat::CharFormat); |
|
720 |
||
721 |
int offset = pos - it.position(); |
|
722 |
int length = qMin(endPos - pos, int(fragment->size_array[0] - offset)); |
|
723 |
int oldFormat = fragment->format; |
|
724 |
||
725 |
if (mode == MergeFormat) { |
|
726 |
QTextFormat format = formats.format(fragment->format); |
|
727 |
format.merge(newFormat); |
|
728 |
fragment->format = formats.indexForFormat(format); |
|
729 |
} else if (mode == SetFormatAndPreserveObjectIndices |
|
730 |
&& formats.format(oldFormat).objectIndex() != -1) { |
|
731 |
QTextCharFormat f = newFormat; |
|
732 |
f.setObjectIndex(formats.format(oldFormat).objectIndex()); |
|
733 |
fragment->format = formats.indexForFormat(f); |
|
734 |
} else { |
|
735 |
fragment->format = newFormatIdx; |
|
736 |
} |
|
737 |
||
738 |
QT_INIT_TEXTUNDOCOMMAND(c, QTextUndoCommand::CharFormatChanged, true, QTextUndoCommand::MoveCursor, oldFormat, |
|
739 |
0, pos, length, 0); |
|
740 |
appendUndoItem(c); |
|
741 |
||
742 |
pos += length; |
|
743 |
Q_ASSERT(pos == (int)(it.position() + fragment->size_array[0]) || pos >= endPos); |
|
744 |
} |
|
745 |
||
746 |
int n = fragments.findNode(startPos - 1); |
|
747 |
if (n) |
|
748 |
unite(n); |
|
749 |
||
750 |
n = fragments.findNode(endPos); |
|
751 |
if (n) |
|
752 |
unite(n); |
|
753 |
||
754 |
QTextBlock blockIt = blocksFind(startPos); |
|
755 |
QTextBlock endIt = blocksFind(endPos); |
|
756 |
if (endIt.isValid()) |
|
757 |
endIt = endIt.next(); |
|
758 |
for (; blockIt.isValid() && blockIt != endIt; blockIt = blockIt.next()) |
|
759 |
QTextDocumentPrivate::block(blockIt)->invalidate(); |
|
760 |
||
761 |
documentChange(startPos, length); |
|
762 |
||
763 |
endEditBlock(); |
|
764 |
} |
|
765 |
||
766 |
void QTextDocumentPrivate::setBlockFormat(const QTextBlock &from, const QTextBlock &to, |
|
767 |
const QTextBlockFormat &newFormat, FormatChangeMode mode) |
|
768 |
{ |
|
769 |
beginEditBlock(); |
|
770 |
||
771 |
Q_ASSERT(mode != SetFormatAndPreserveObjectIndices); // only implemented for setCharFormat |
|
772 |
||
773 |
Q_ASSERT(newFormat.isValid()); |
|
774 |
||
775 |
int newFormatIdx = -1; |
|
776 |
if (mode == SetFormat) |
|
777 |
newFormatIdx = formats.indexForFormat(newFormat); |
|
778 |
QTextBlockGroup *group = qobject_cast<QTextBlockGroup *>(objectForFormat(newFormat)); |
|
779 |
||
780 |
QTextBlock it = from; |
|
781 |
QTextBlock end = to; |
|
782 |
if (end.isValid()) |
|
783 |
end = end.next(); |
|
784 |
||
785 |
for (; it != end; it = it.next()) { |
|
786 |
int oldFormat = block(it)->format; |
|
787 |
QTextBlockFormat format = formats.blockFormat(oldFormat); |
|
788 |
QTextBlockGroup *oldGroup = qobject_cast<QTextBlockGroup *>(objectForFormat(format)); |
|
789 |
if (mode == MergeFormat) { |
|
790 |
format.merge(newFormat); |
|
791 |
newFormatIdx = formats.indexForFormat(format); |
|
792 |
group = qobject_cast<QTextBlockGroup *>(objectForFormat(format)); |
|
793 |
} |
|
794 |
block(it)->format = newFormatIdx; |
|
795 |
||
796 |
block(it)->invalidate(); |
|
797 |
||
798 |
QT_INIT_TEXTUNDOCOMMAND(c, QTextUndoCommand::BlockFormatChanged, true, QTextUndoCommand::MoveCursor, oldFormat, |
|
799 |
0, it.position(), 1, 0); |
|
800 |
appendUndoItem(c); |
|
801 |
||
802 |
if (group != oldGroup) { |
|
803 |
if (oldGroup) |
|
804 |
oldGroup->blockRemoved(it); |
|
805 |
if (group) |
|
806 |
group->blockInserted(it); |
|
807 |
} else if (group) { |
|
808 |
group->blockFormatChanged(it); |
|
809 |
} |
|
810 |
} |
|
811 |
||
812 |
documentChange(from.position(), to.position() + to.length() - from.position()); |
|
813 |
||
814 |
endEditBlock(); |
|
815 |
} |
|
816 |
||
817 |
||
818 |
bool QTextDocumentPrivate::split(int pos) |
|
819 |
{ |
|
820 |
uint x = fragments.findNode(pos); |
|
821 |
if (x) { |
|
822 |
int k = fragments.position(x); |
|
823 |
// qDebug("found fragment with key %d, size_left=%d, size=%d to split at %d", |
|
824 |
// k, (*it)->size_left[0], (*it)->size_array[0], pos); |
|
825 |
if (k != pos) { |
|
826 |
Q_ASSERT(k <= pos); |
|
827 |
// need to resize the first fragment and add a new one |
|
828 |
QTextFragmentData *X = fragments.fragment(x); |
|
829 |
int oldsize = X->size_array[0]; |
|
830 |
fragments.setSize(x, pos-k); |
|
831 |
uint n = fragments.insert_single(pos, oldsize-(pos-k)); |
|
832 |
X = fragments.fragment(x); |
|
833 |
QTextFragmentData *N = fragments.fragment(n); |
|
834 |
N->stringPosition = X->stringPosition + pos-k; |
|
835 |
N->format = X->format; |
|
836 |
return true; |
|
837 |
} |
|
838 |
} |
|
839 |
return false; |
|
840 |
} |
|
841 |
||
842 |
bool QTextDocumentPrivate::unite(uint f) |
|
843 |
{ |
|
844 |
uint n = fragments.next(f); |
|
845 |
if (!n) |
|
846 |
return false; |
|
847 |
||
848 |
QTextFragmentData *ff = fragments.fragment(f); |
|
849 |
QTextFragmentData *nf = fragments.fragment(n); |
|
850 |
||
851 |
if (nf->format == ff->format && (ff->stringPosition + (int)ff->size_array[0] == nf->stringPosition)) { |
|
852 |
if (isValidBlockSeparator(text.at(ff->stringPosition)) |
|
853 |
|| isValidBlockSeparator(text.at(nf->stringPosition))) |
|
854 |
return false; |
|
855 |
||
856 |
fragments.setSize(f, ff->size_array[0] + nf->size_array[0]); |
|
857 |
fragments.erase_single(n); |
|
858 |
return true; |
|
859 |
} |
|
860 |
return false; |
|
861 |
} |
|
862 |
||
863 |
||
864 |
int QTextDocumentPrivate::undoRedo(bool undo) |
|
865 |
{ |
|
866 |
PMDEBUG("%s, undoState=%d, undoStack size=%d", undo ? "undo:" : "redo:", undoState, undoStack.size()); |
|
867 |
if (!undoEnabled || (undo && undoState == 0) || (!undo && undoState == undoStack.size())) |
|
868 |
return -1; |
|
869 |
||
870 |
undoEnabled = false; |
|
871 |
beginEditBlock(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
872 |
int editPos = -1; |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
873 |
int editLength = -1; |
0 | 874 |
while (1) { |
875 |
if (undo) |
|
876 |
--undoState; |
|
877 |
QTextUndoCommand &c = undoStack[undoState]; |
|
878 |
int resetBlockRevision = c.pos; |
|
879 |
||
880 |
switch(c.command) { |
|
881 |
case QTextUndoCommand::Inserted: |
|
882 |
remove(c.pos, c.length, (QTextUndoCommand::Operation)c.operation); |
|
883 |
PMDEBUG(" erase: from %d, length %d", c.pos, c.length); |
|
884 |
c.command = QTextUndoCommand::Removed; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
885 |
editPos = c.pos; |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
886 |
editLength = 0; |
0 | 887 |
break; |
888 |
case QTextUndoCommand::Removed: |
|
889 |
PMDEBUG(" insert: format %d (from %d, length %d, strpos=%d)", c.format, c.pos, c.length, c.strPos); |
|
890 |
insert_string(c.pos, c.strPos, c.length, c.format, (QTextUndoCommand::Operation)c.operation); |
|
891 |
c.command = QTextUndoCommand::Inserted; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
892 |
if (editPos != (int)c.pos) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
893 |
editLength = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
894 |
editPos = c.pos; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
895 |
editLength += c.length; |
0 | 896 |
break; |
897 |
case QTextUndoCommand::BlockInserted: |
|
898 |
case QTextUndoCommand::BlockAdded: |
|
899 |
remove_block(c.pos, &c.blockFormat, c.command, (QTextUndoCommand::Operation)c.operation); |
|
900 |
PMDEBUG(" blockremove: from %d", c.pos); |
|
901 |
if (c.command == QTextUndoCommand::BlockInserted) |
|
902 |
c.command = QTextUndoCommand::BlockRemoved; |
|
903 |
else |
|
904 |
c.command = QTextUndoCommand::BlockDeleted; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
905 |
editPos = c.pos; |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
906 |
editLength = 0; |
0 | 907 |
break; |
908 |
case QTextUndoCommand::BlockRemoved: |
|
909 |
case QTextUndoCommand::BlockDeleted: |
|
910 |
PMDEBUG(" blockinsert: charformat %d blockformat %d (pos %d, strpos=%d)", c.format, c.blockFormat, c.pos, c.strPos); |
|
911 |
insert_block(c.pos, c.strPos, c.format, c.blockFormat, (QTextUndoCommand::Operation)c.operation, c.command); |
|
912 |
resetBlockRevision += 1; |
|
913 |
if (c.command == QTextUndoCommand::BlockRemoved) |
|
914 |
c.command = QTextUndoCommand::BlockInserted; |
|
915 |
else |
|
916 |
c.command = QTextUndoCommand::BlockAdded; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
917 |
if (editPos != (int)c.pos) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
918 |
editLength = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
919 |
editPos = c.pos; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
920 |
editLength += 1; |
0 | 921 |
break; |
922 |
case QTextUndoCommand::CharFormatChanged: { |
|
923 |
resetBlockRevision = -1; // ## TODO |
|
924 |
PMDEBUG(" charFormat: format %d (from %d, length %d)", c.format, c.pos, c.length); |
|
925 |
FragmentIterator it = find(c.pos); |
|
926 |
Q_ASSERT(!it.atEnd()); |
|
927 |
||
928 |
int oldFormat = it.value()->format; |
|
929 |
setCharFormat(c.pos, c.length, formats.charFormat(c.format)); |
|
930 |
c.format = oldFormat; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
931 |
if (editPos != (int)c.pos) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
932 |
editLength = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
933 |
editPos = c.pos; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
934 |
editLength += c.length; |
0 | 935 |
break; |
936 |
} |
|
937 |
case QTextUndoCommand::BlockFormatChanged: { |
|
938 |
resetBlockRevision = -1; // ## TODO |
|
939 |
PMDEBUG(" blockformat: format %d pos %d", c.format, c.pos); |
|
940 |
QTextBlock it = blocksFind(c.pos); |
|
941 |
Q_ASSERT(it.isValid()); |
|
942 |
||
943 |
int oldFormat = block(it)->format; |
|
944 |
block(it)->format = c.format; |
|
945 |
QTextBlockGroup *oldGroup = qobject_cast<QTextBlockGroup *>(objectForFormat(formats.blockFormat(oldFormat))); |
|
946 |
QTextBlockGroup *group = qobject_cast<QTextBlockGroup *>(objectForFormat(formats.blockFormat(c.format))); |
|
947 |
c.format = oldFormat; |
|
948 |
if (group != oldGroup) { |
|
949 |
if (oldGroup) |
|
950 |
oldGroup->blockRemoved(it); |
|
951 |
if (group) |
|
952 |
group->blockInserted(it); |
|
953 |
} else if (group) { |
|
954 |
group->blockFormatChanged(it); |
|
955 |
} |
|
956 |
documentChange(it.position(), it.length()); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
957 |
editPos = -1; |
0 | 958 |
break; |
959 |
} |
|
960 |
case QTextUndoCommand::GroupFormatChange: { |
|
961 |
resetBlockRevision = -1; // ## TODO |
|
962 |
PMDEBUG(" group format change"); |
|
963 |
QTextObject *object = objectForIndex(c.objectIndex); |
|
964 |
int oldFormat = formats.objectFormatIndex(c.objectIndex); |
|
965 |
changeObjectFormat(object, c.format); |
|
966 |
c.format = oldFormat; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
967 |
editPos = -1; |
0 | 968 |
break; |
969 |
} |
|
970 |
case QTextUndoCommand::Custom: |
|
971 |
resetBlockRevision = -1; // ## TODO |
|
972 |
if (undo) |
|
973 |
c.custom->undo(); |
|
974 |
else |
|
975 |
c.custom->redo(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
976 |
editPos = -1; |
0 | 977 |
break; |
978 |
default: |
|
979 |
Q_ASSERT(false); |
|
980 |
} |
|
981 |
||
982 |
if (resetBlockRevision >= 0) { |
|
983 |
int b = blocks.findNode(resetBlockRevision); |
|
984 |
QTextBlockData *B = blocks.fragment(b); |
|
985 |
B->revision = c.revision; |
|
986 |
} |
|
987 |
||
988 |
if (!undo) |
|
989 |
++undoState; |
|
990 |
||
991 |
bool inBlock = ( |
|
992 |
undoState > 0 |
|
993 |
&& undoState < undoStack.size() |
|
994 |
&& undoStack[undoState].block_part |
|
995 |
&& undoStack[undoState-1].block_part |
|
996 |
&& !undoStack[undoState-1].block_end |
|
997 |
); |
|
998 |
if (!inBlock) |
|
999 |
break; |
|
1000 |
} |
|
1001 |
undoEnabled = true; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1002 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1003 |
int newCursorPos = -1; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1004 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1005 |
if (editPos >=0) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1006 |
newCursorPos = editPos + editLength; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1007 |
else if (docChangeFrom >= 0) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1008 |
newCursorPos= qMin(docChangeFrom + docChangeLength, length() - 1); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1009 |
|
0 | 1010 |
endEditBlock(); |
1011 |
emitUndoAvailable(isUndoAvailable()); |
|
1012 |
emitRedoAvailable(isRedoAvailable()); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1013 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1014 |
return newCursorPos; |
0 | 1015 |
} |
1016 |
||
1017 |
/*! |
|
1018 |
Appends a custom undo \a item to the undo stack. |
|
1019 |
*/ |
|
1020 |
void QTextDocumentPrivate::appendUndoItem(QAbstractUndoItem *item) |
|
1021 |
{ |
|
1022 |
if (!undoEnabled) { |
|
1023 |
delete item; |
|
1024 |
return; |
|
1025 |
} |
|
1026 |
||
1027 |
QTextUndoCommand c; |
|
1028 |
c.command = QTextUndoCommand::Custom; |
|
1029 |
c.block_part = editBlock != 0; |
|
1030 |
c.block_end = 0; |
|
1031 |
c.operation = QTextUndoCommand::MoveCursor; |
|
1032 |
c.format = 0; |
|
1033 |
c.strPos = 0; |
|
1034 |
c.pos = 0; |
|
1035 |
c.blockFormat = 0; |
|
1036 |
||
1037 |
c.custom = item; |
|
1038 |
appendUndoItem(c); |
|
1039 |
} |
|
1040 |
||
1041 |
void QTextDocumentPrivate::appendUndoItem(const QTextUndoCommand &c) |
|
1042 |
{ |
|
1043 |
PMDEBUG("appendUndoItem, command=%d enabled=%d", c.command, undoEnabled); |
|
1044 |
if (!undoEnabled) |
|
1045 |
return; |
|
1046 |
if (undoState < undoStack.size()) |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1047 |
clearUndoRedoStacks(QTextDocument::RedoStack); |
0 | 1048 |
|
1049 |
if (!undoStack.isEmpty() && modified) { |
|
1050 |
QTextUndoCommand &last = undoStack[undoState - 1]; |
|
1051 |
||
1052 |
if ( (last.block_part && c.block_part && !last.block_end) // part of the same block => can merge |
|
1053 |
|| (!c.block_part && !last.block_part)) { // two single undo items => can merge |
|
1054 |
||
1055 |
if (last.tryMerge(c)) |
|
1056 |
return; |
|
1057 |
} |
|
1058 |
} |
|
1059 |
if (modifiedState > undoState) |
|
1060 |
modifiedState = -1; |
|
1061 |
undoStack.append(c); |
|
1062 |
undoState++; |
|
1063 |
emitUndoAvailable(true); |
|
1064 |
emitRedoAvailable(false); |
|
1065 |
||
1066 |
if (!c.block_part) |
|
1067 |
emit document()->undoCommandAdded(); |
|
1068 |
} |
|
1069 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1070 |
void QTextDocumentPrivate::clearUndoRedoStacks(QTextDocument::Stacks stacksToClear, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1071 |
bool emitSignals) |
0 | 1072 |
{ |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1073 |
bool undoCommandsAvailable = undoState != 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1074 |
bool redoCommandsAvailable = undoState != undoStack.size(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1075 |
if (stacksToClear == QTextDocument::UndoStack && undoCommandsAvailable) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1076 |
for (int i = 0; i < undoState; ++i) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1077 |
QTextUndoCommand c = undoStack[undoState]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1078 |
if (c.command & QTextUndoCommand::Custom) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1079 |
delete c.custom; |
0 | 1080 |
} |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1081 |
undoStack.remove(0, undoState); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1082 |
undoStack.resize(undoStack.size() - undoState); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1083 |
undoState = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1084 |
if (emitSignals) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1085 |
emitUndoAvailable(false); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1086 |
} else if (stacksToClear == QTextDocument::RedoStack |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1087 |
&& redoCommandsAvailable) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1088 |
for (int i = undoState; i < undoStack.size(); ++i) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1089 |
QTextUndoCommand c = undoStack[i]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1090 |
if (c.command & QTextUndoCommand::Custom) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1091 |
delete c.custom; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1092 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1093 |
undoStack.resize(undoState); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1094 |
if (emitSignals) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1095 |
emitRedoAvailable(false); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1096 |
} else if (stacksToClear == QTextDocument::UndoAndRedoStacks |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1097 |
&& !undoStack.isEmpty()) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1098 |
for (int i = 0; i < undoStack.size(); ++i) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1099 |
QTextUndoCommand c = undoStack[i]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1100 |
if (c.command & QTextUndoCommand::Custom) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1101 |
delete c.custom; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1102 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1103 |
undoState = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1104 |
undoStack.resize(0); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1105 |
if (emitSignals && undoCommandsAvailable) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1106 |
emitUndoAvailable(false); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1107 |
if (emitSignals && redoCommandsAvailable) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1108 |
emitRedoAvailable(false); |
0 | 1109 |
} |
1110 |
} |
|
1111 |
||
1112 |
void QTextDocumentPrivate::emitUndoAvailable(bool available) |
|
1113 |
{ |
|
1114 |
if (available != wasUndoAvailable) { |
|
1115 |
Q_Q(QTextDocument); |
|
1116 |
emit q->undoAvailable(available); |
|
1117 |
wasUndoAvailable = available; |
|
1118 |
} |
|
1119 |
} |
|
1120 |
||
1121 |
void QTextDocumentPrivate::emitRedoAvailable(bool available) |
|
1122 |
{ |
|
1123 |
if (available != wasRedoAvailable) { |
|
1124 |
Q_Q(QTextDocument); |
|
1125 |
emit q->redoAvailable(available); |
|
1126 |
wasRedoAvailable = available; |
|
1127 |
} |
|
1128 |
} |
|
1129 |
||
1130 |
void QTextDocumentPrivate::enableUndoRedo(bool enable) |
|
1131 |
{ |
|
1132 |
if (enable && maximumBlockCount > 0) |
|
1133 |
return; |
|
1134 |
||
1135 |
if (!enable) { |
|
1136 |
undoState = 0; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1137 |
clearUndoRedoStacks(QTextDocument::RedoStack); |
0 | 1138 |
emitUndoAvailable(false); |
1139 |
emitRedoAvailable(false); |
|
1140 |
} |
|
1141 |
modifiedState = modified ? -1 : undoState; |
|
1142 |
undoEnabled = enable; |
|
1143 |
if (!undoEnabled) |
|
1144 |
compressPieceTable(); |
|
1145 |
} |
|
1146 |
||
1147 |
void QTextDocumentPrivate::joinPreviousEditBlock() |
|
1148 |
{ |
|
1149 |
beginEditBlock(); |
|
1150 |
||
1151 |
if (undoEnabled && undoState) |
|
1152 |
undoStack[undoState - 1].block_end = false; |
|
1153 |
} |
|
1154 |
||
1155 |
void QTextDocumentPrivate::endEditBlock() |
|
1156 |
{ |
|
1157 |
Q_ASSERT(editBlock > 0); |
|
1158 |
if (--editBlock) |
|
1159 |
return; |
|
1160 |
||
1161 |
if (undoEnabled && undoState > 0) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1162 |
const bool wasBlocking = !undoStack[undoState - 1].block_end; |
0 | 1163 |
if (undoStack[undoState - 1].block_part) { |
1164 |
undoStack[undoState - 1].block_end = true; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1165 |
if (wasBlocking) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1166 |
emit document()->undoCommandAdded(); |
0 | 1167 |
} |
1168 |
} |
|
1169 |
||
1170 |
finishEdit(); |
|
1171 |
} |
|
1172 |
||
1173 |
void QTextDocumentPrivate::finishEdit() |
|
1174 |
{ |
|
1175 |
Q_Q(QTextDocument); |
|
1176 |
||
1177 |
if (editBlock) |
|
1178 |
return; |
|
1179 |
||
1180 |
if (framesDirty) |
|
1181 |
scan_frames(docChangeFrom, docChangeOldLength, docChangeLength); |
|
1182 |
||
1183 |
if (lout && docChangeFrom >= 0) { |
|
1184 |
if (!inContentsChange) { |
|
1185 |
inContentsChange = true; |
|
1186 |
emit q->contentsChange(docChangeFrom, docChangeOldLength, docChangeLength); |
|
1187 |
inContentsChange = false; |
|
1188 |
} |
|
1189 |
lout->documentChanged(docChangeFrom, docChangeOldLength, docChangeLength); |
|
1190 |
} |
|
1191 |
||
1192 |
docChangeFrom = -1; |
|
1193 |
||
1194 |
if (needsEnsureMaximumBlockCount) { |
|
1195 |
needsEnsureMaximumBlockCount = false; |
|
1196 |
if (ensureMaximumBlockCount()) { |
|
1197 |
// if ensureMaximumBlockCount() returns true |
|
1198 |
// it will have called endEditBlock() and |
|
1199 |
// compressPieceTable() itself, so we return here |
|
1200 |
// to prevent getting two contentsChanged emits |
|
1201 |
return; |
|
1202 |
} |
|
1203 |
} |
|
1204 |
||
1205 |
while (!changedCursors.isEmpty()) { |
|
1206 |
QTextCursorPrivate *curs = changedCursors.takeFirst(); |
|
1207 |
emit q->cursorPositionChanged(QTextCursor(curs)); |
|
1208 |
} |
|
1209 |
||
1210 |
contentsChanged(); |
|
1211 |
||
1212 |
if (blocks.numNodes() != lastBlockCount) { |
|
1213 |
lastBlockCount = blocks.numNodes(); |
|
1214 |
emit q->blockCountChanged(lastBlockCount); |
|
1215 |
} |
|
1216 |
||
1217 |
if (!undoEnabled && unreachableCharacterCount) |
|
1218 |
compressPieceTable(); |
|
1219 |
} |
|
1220 |
||
1221 |
void QTextDocumentPrivate::documentChange(int from, int length) |
|
1222 |
{ |
|
1223 |
// qDebug("QTextDocumentPrivate::documentChange: from=%d,length=%d", from, length); |
|
1224 |
if (docChangeFrom < 0) { |
|
1225 |
docChangeFrom = from; |
|
1226 |
docChangeOldLength = length; |
|
1227 |
docChangeLength = length; |
|
1228 |
return; |
|
1229 |
} |
|
1230 |
int start = qMin(from, docChangeFrom); |
|
1231 |
int end = qMax(from + length, docChangeFrom + docChangeLength); |
|
1232 |
int diff = qMax(0, end - start - docChangeLength); |
|
1233 |
docChangeFrom = start; |
|
1234 |
docChangeOldLength += diff; |
|
1235 |
docChangeLength += diff; |
|
1236 |
} |
|
1237 |
||
1238 |
/* |
|
1239 |
adjustDocumentChangesAndCursors is called whenever there is an insert or remove of characters. |
|
1240 |
param from is the cursor position in the document |
|
1241 |
param addedOrRemoved is the amount of characters added or removed. A negative number means characters are removed. |
|
1242 |
||
1243 |
The function stores information to be emitted when finishEdit() is called. |
|
1244 |
*/ |
|
1245 |
void QTextDocumentPrivate::adjustDocumentChangesAndCursors(int from, int addedOrRemoved, QTextUndoCommand::Operation op) |
|
1246 |
{ |
|
1247 |
if (!editBlock) |
|
1248 |
++revision; |
|
1249 |
||
1250 |
for (int i = 0; i < cursors.size(); ++i) { |
|
1251 |
QTextCursorPrivate *curs = cursors.at(i); |
|
1252 |
if (curs->adjustPosition(from, addedOrRemoved, op) == QTextCursorPrivate::CursorMoved) { |
|
1253 |
if (!changedCursors.contains(curs)) |
|
1254 |
changedCursors.append(curs); |
|
1255 |
} |
|
1256 |
} |
|
1257 |
||
1258 |
// qDebug("QTextDocumentPrivate::adjustDocumentChanges: from=%d,addedOrRemoved=%d", from, addedOrRemoved); |
|
1259 |
if (docChangeFrom < 0) { |
|
1260 |
docChangeFrom = from; |
|
1261 |
if (addedOrRemoved > 0) { |
|
1262 |
docChangeOldLength = 0; |
|
1263 |
docChangeLength = addedOrRemoved; |
|
1264 |
} else { |
|
1265 |
docChangeOldLength = -addedOrRemoved; |
|
1266 |
docChangeLength = 0; |
|
1267 |
} |
|
1268 |
// qDebug("adjustDocumentChanges:"); |
|
1269 |
// qDebug(" -> %d %d %d", docChangeFrom, docChangeOldLength, docChangeLength); |
|
1270 |
return; |
|
1271 |
} |
|
1272 |
||
1273 |
// have to merge the new change with the already existing one. |
|
1274 |
int added = qMax(0, addedOrRemoved); |
|
1275 |
int removed = qMax(0, -addedOrRemoved); |
|
1276 |
||
1277 |
int diff = 0; |
|
1278 |
if(from + removed < docChangeFrom) |
|
1279 |
diff = docChangeFrom - from - removed; |
|
1280 |
else if(from > docChangeFrom + docChangeLength) |
|
1281 |
diff = from - (docChangeFrom + docChangeLength); |
|
1282 |
||
1283 |
int overlap_start = qMax(from, docChangeFrom); |
|
1284 |
int overlap_end = qMin(from + removed, docChangeFrom + docChangeLength); |
|
1285 |
int removedInside = qMax(0, overlap_end - overlap_start); |
|
1286 |
removed -= removedInside; |
|
1287 |
||
1288 |
// qDebug("adjustDocumentChanges: from=%d, addedOrRemoved=%d, diff=%d, removedInside=%d", from, addedOrRemoved, diff, removedInside); |
|
1289 |
docChangeFrom = qMin(docChangeFrom, from); |
|
1290 |
docChangeOldLength += removed + diff; |
|
1291 |
docChangeLength += added - removedInside + diff; |
|
1292 |
// qDebug(" -> %d %d %d", docChangeFrom, docChangeOldLength, docChangeLength); |
|
1293 |
||
1294 |
} |
|
1295 |
||
1296 |
||
1297 |
QString QTextDocumentPrivate::plainText() const |
|
1298 |
{ |
|
1299 |
QString result; |
|
1300 |
result.resize(length()); |
|
1301 |
const QChar *text_unicode = text.unicode(); |
|
1302 |
QChar *data = result.data(); |
|
1303 |
for (QTextDocumentPrivate::FragmentIterator it = begin(); it != end(); ++it) { |
|
1304 |
const QTextFragmentData *f = *it; |
|
1305 |
::memcpy(data, text_unicode + f->stringPosition, f->size_array[0] * sizeof(QChar)); |
|
1306 |
data += f->size_array[0]; |
|
1307 |
} |
|
1308 |
// remove trailing block separator |
|
1309 |
result.chop(1); |
|
1310 |
return result; |
|
1311 |
} |
|
1312 |
||
1313 |
int QTextDocumentPrivate::blockCharFormatIndex(int node) const |
|
1314 |
{ |
|
1315 |
int pos = blocks.position(node); |
|
1316 |
if (pos == 0) |
|
1317 |
return initialBlockCharFormatIndex; |
|
1318 |
||
1319 |
return fragments.find(pos - 1)->format; |
|
1320 |
} |
|
1321 |
||
1322 |
int QTextDocumentPrivate::nextCursorPosition(int position, QTextLayout::CursorMode mode) const |
|
1323 |
{ |
|
1324 |
if (position == length()-1) |
|
1325 |
return position; |
|
1326 |
||
1327 |
QTextBlock it = blocksFind(position); |
|
1328 |
int start = it.position(); |
|
1329 |
int end = start + it.length() - 1; |
|
1330 |
if (position == end) |
|
1331 |
return end + 1; |
|
1332 |
||
1333 |
return it.layout()->nextCursorPosition(position-start, mode) + start; |
|
1334 |
} |
|
1335 |
||
1336 |
int QTextDocumentPrivate::previousCursorPosition(int position, QTextLayout::CursorMode mode) const |
|
1337 |
{ |
|
1338 |
if (position == 0) |
|
1339 |
return position; |
|
1340 |
||
1341 |
QTextBlock it = blocksFind(position); |
|
1342 |
int start = it.position(); |
|
1343 |
if (position == start) |
|
1344 |
return start - 1; |
|
1345 |
||
1346 |
return it.layout()->previousCursorPosition(position-start, mode) + start; |
|
1347 |
} |
|
1348 |
||
1349 |
void QTextDocumentPrivate::changeObjectFormat(QTextObject *obj, int format) |
|
1350 |
{ |
|
1351 |
beginEditBlock(); |
|
1352 |
int objectIndex = obj->objectIndex(); |
|
1353 |
int oldFormatIndex = formats.objectFormatIndex(objectIndex); |
|
1354 |
formats.setObjectFormatIndex(objectIndex, format); |
|
1355 |
||
1356 |
QTextBlockGroup *b = qobject_cast<QTextBlockGroup *>(obj); |
|
1357 |
if (b) { |
|
1358 |
b->d_func()->markBlocksDirty(); |
|
1359 |
} |
|
1360 |
QTextFrame *f = qobject_cast<QTextFrame *>(obj); |
|
1361 |
if (f) |
|
1362 |
documentChange(f->firstPosition(), f->lastPosition() - f->firstPosition()); |
|
1363 |
||
1364 |
QT_INIT_TEXTUNDOCOMMAND(c, QTextUndoCommand::GroupFormatChange, (editBlock != 0), QTextUndoCommand::MoveCursor, oldFormatIndex, |
|
1365 |
0, 0, obj->d_func()->objectIndex, 0); |
|
1366 |
appendUndoItem(c); |
|
1367 |
||
1368 |
endEditBlock(); |
|
1369 |
} |
|
1370 |
||
1371 |
static QTextFrame *findChildFrame(QTextFrame *f, int pos) |
|
1372 |
{ |
|
1373 |
// ##### use binary search |
|
1374 |
QList<QTextFrame *> children = f->childFrames(); |
|
1375 |
for (int i = 0; i < children.size(); ++i) { |
|
1376 |
QTextFrame *c = children.at(i); |
|
1377 |
if (pos >= c->firstPosition() && pos <= c->lastPosition()) |
|
1378 |
return c; |
|
1379 |
} |
|
1380 |
return 0; |
|
1381 |
} |
|
1382 |
||
1383 |
QTextFrame *QTextDocumentPrivate::rootFrame() const |
|
1384 |
{ |
|
1385 |
if (!rtFrame) { |
|
1386 |
QTextFrameFormat defaultRootFrameFormat; |
|
1387 |
defaultRootFrameFormat.setMargin(documentMargin); |
|
1388 |
rtFrame = qobject_cast<QTextFrame *>(const_cast<QTextDocumentPrivate *>(this)->createObject(defaultRootFrameFormat)); |
|
1389 |
} |
|
1390 |
return rtFrame; |
|
1391 |
} |
|
1392 |
||
1393 |
QTextFrame *QTextDocumentPrivate::frameAt(int pos) const |
|
1394 |
{ |
|
1395 |
QTextFrame *f = rootFrame(); |
|
1396 |
||
1397 |
while (1) { |
|
1398 |
QTextFrame *c = findChildFrame(f, pos); |
|
1399 |
if (!c) |
|
1400 |
return f; |
|
1401 |
f = c; |
|
1402 |
} |
|
1403 |
} |
|
1404 |
||
1405 |
void QTextDocumentPrivate::clearFrame(QTextFrame *f) |
|
1406 |
{ |
|
1407 |
for (int i = 0; i < f->d_func()->childFrames.count(); ++i) |
|
1408 |
clearFrame(f->d_func()->childFrames.at(i)); |
|
1409 |
f->d_func()->childFrames.clear(); |
|
1410 |
f->d_func()->parentFrame = 0; |
|
1411 |
} |
|
1412 |
||
1413 |
void QTextDocumentPrivate::scan_frames(int pos, int charsRemoved, int charsAdded) |
|
1414 |
{ |
|
1415 |
// ###### optimise |
|
1416 |
Q_UNUSED(pos); |
|
1417 |
Q_UNUSED(charsRemoved); |
|
1418 |
Q_UNUSED(charsAdded); |
|
1419 |
||
1420 |
QTextFrame *f = rootFrame(); |
|
1421 |
clearFrame(f); |
|
1422 |
||
1423 |
for (FragmentIterator it = begin(); it != end(); ++it) { |
|
1424 |
// QTextFormat fmt = formats.format(it->format); |
|
1425 |
QTextFrame *frame = qobject_cast<QTextFrame *>(objectForFormat(it->format)); |
|
1426 |
if (!frame) |
|
1427 |
continue; |
|
1428 |
||
1429 |
Q_ASSERT(it.size() == 1); |
|
1430 |
QChar ch = text.at(it->stringPosition); |
|
1431 |
||
1432 |
if (ch == QTextBeginningOfFrame) { |
|
1433 |
if (f != frame) { |
|
1434 |
// f == frame happens for tables |
|
1435 |
Q_ASSERT(frame->d_func()->fragment_start == it.n || frame->d_func()->fragment_start == 0); |
|
1436 |
frame->d_func()->parentFrame = f; |
|
1437 |
f->d_func()->childFrames.append(frame); |
|
1438 |
f = frame; |
|
1439 |
} |
|
1440 |
} else if (ch == QTextEndOfFrame) { |
|
1441 |
Q_ASSERT(f == frame); |
|
1442 |
Q_ASSERT(frame->d_func()->fragment_end == it.n || frame->d_func()->fragment_end == 0); |
|
1443 |
f = frame->d_func()->parentFrame; |
|
1444 |
} else if (ch == QChar::ObjectReplacementCharacter) { |
|
1445 |
Q_ASSERT(f != frame); |
|
1446 |
Q_ASSERT(frame->d_func()->fragment_start == it.n || frame->d_func()->fragment_start == 0); |
|
1447 |
Q_ASSERT(frame->d_func()->fragment_end == it.n || frame->d_func()->fragment_end == 0); |
|
1448 |
frame->d_func()->parentFrame = f; |
|
1449 |
f->d_func()->childFrames.append(frame); |
|
1450 |
} else { |
|
1451 |
Q_ASSERT(false); |
|
1452 |
} |
|
1453 |
} |
|
1454 |
Q_ASSERT(f == rtFrame); |
|
1455 |
framesDirty = false; |
|
1456 |
} |
|
1457 |
||
1458 |
void QTextDocumentPrivate::insert_frame(QTextFrame *f) |
|
1459 |
{ |
|
1460 |
int start = f->firstPosition(); |
|
1461 |
int end = f->lastPosition(); |
|
1462 |
QTextFrame *parent = frameAt(start-1); |
|
1463 |
Q_ASSERT(parent == frameAt(end+1)); |
|
1464 |
||
1465 |
if (start != end) { |
|
1466 |
// iterator over the parent and move all children contained in my frame to myself |
|
1467 |
for (int i = 0; i < parent->d_func()->childFrames.size(); ++i) { |
|
1468 |
QTextFrame *c = parent->d_func()->childFrames.at(i); |
|
1469 |
if (start < c->firstPosition() && end > c->lastPosition()) { |
|
1470 |
parent->d_func()->childFrames.removeAt(i); |
|
1471 |
f->d_func()->childFrames.append(c); |
|
1472 |
c->d_func()->parentFrame = f; |
|
1473 |
} |
|
1474 |
} |
|
1475 |
} |
|
1476 |
// insert at the correct position |
|
1477 |
int i = 0; |
|
1478 |
for (; i < parent->d_func()->childFrames.size(); ++i) { |
|
1479 |
QTextFrame *c = parent->d_func()->childFrames.at(i); |
|
1480 |
if (c->firstPosition() > end) |
|
1481 |
break; |
|
1482 |
} |
|
1483 |
parent->d_func()->childFrames.insert(i, f); |
|
1484 |
f->d_func()->parentFrame = parent; |
|
1485 |
} |
|
1486 |
||
1487 |
QTextFrame *QTextDocumentPrivate::insertFrame(int start, int end, const QTextFrameFormat &format) |
|
1488 |
{ |
|
1489 |
Q_ASSERT(start >= 0 && start < length()); |
|
1490 |
Q_ASSERT(end >= 0 && end < length()); |
|
1491 |
Q_ASSERT(start <= end || end == -1); |
|
1492 |
||
1493 |
if (start != end && frameAt(start) != frameAt(end)) |
|
1494 |
return 0; |
|
1495 |
||
1496 |
beginEditBlock(); |
|
1497 |
||
1498 |
QTextFrame *frame = qobject_cast<QTextFrame *>(createObject(format)); |
|
1499 |
Q_ASSERT(frame); |
|
1500 |
||
1501 |
// #### using the default block and char format below might be wrong |
|
1502 |
int idx = formats.indexForFormat(QTextBlockFormat()); |
|
1503 |
QTextCharFormat cfmt; |
|
1504 |
cfmt.setObjectIndex(frame->objectIndex()); |
|
1505 |
int charIdx = formats.indexForFormat(cfmt); |
|
1506 |
||
1507 |
insertBlock(QTextBeginningOfFrame, start, idx, charIdx, QTextUndoCommand::MoveCursor); |
|
1508 |
insertBlock(QTextEndOfFrame, ++end, idx, charIdx, QTextUndoCommand::KeepCursor); |
|
1509 |
||
1510 |
frame->d_func()->fragment_start = find(start).n; |
|
1511 |
frame->d_func()->fragment_end = find(end).n; |
|
1512 |
||
1513 |
insert_frame(frame); |
|
1514 |
||
1515 |
endEditBlock(); |
|
1516 |
||
1517 |
return frame; |
|
1518 |
} |
|
1519 |
||
1520 |
void QTextDocumentPrivate::removeFrame(QTextFrame *frame) |
|
1521 |
{ |
|
1522 |
QTextFrame *parent = frame->d_func()->parentFrame; |
|
1523 |
if (!parent) |
|
1524 |
return; |
|
1525 |
||
1526 |
int start = frame->firstPosition(); |
|
1527 |
int end = frame->lastPosition(); |
|
1528 |
Q_ASSERT(end >= start); |
|
1529 |
||
1530 |
beginEditBlock(); |
|
1531 |
||
1532 |
// remove already removes the frames from the tree |
|
1533 |
remove(end, 1); |
|
1534 |
remove(start-1, 1); |
|
1535 |
||
1536 |
endEditBlock(); |
|
1537 |
} |
|
1538 |
||
1539 |
QTextObject *QTextDocumentPrivate::objectForIndex(int objectIndex) const |
|
1540 |
{ |
|
1541 |
if (objectIndex < 0) |
|
1542 |
return 0; |
|
1543 |
||
1544 |
QTextObject *object = objects.value(objectIndex, 0); |
|
1545 |
if (!object) { |
|
1546 |
QTextDocumentPrivate *that = const_cast<QTextDocumentPrivate *>(this); |
|
1547 |
QTextFormat fmt = formats.objectFormat(objectIndex); |
|
1548 |
object = that->createObject(fmt, objectIndex); |
|
1549 |
} |
|
1550 |
return object; |
|
1551 |
} |
|
1552 |
||
1553 |
QTextObject *QTextDocumentPrivate::objectForFormat(int formatIndex) const |
|
1554 |
{ |
|
1555 |
int objectIndex = formats.format(formatIndex).objectIndex(); |
|
1556 |
return objectForIndex(objectIndex); |
|
1557 |
} |
|
1558 |
||
1559 |
QTextObject *QTextDocumentPrivate::objectForFormat(const QTextFormat &f) const |
|
1560 |
{ |
|
1561 |
return objectForIndex(f.objectIndex()); |
|
1562 |
} |
|
1563 |
||
1564 |
QTextObject *QTextDocumentPrivate::createObject(const QTextFormat &f, int objectIndex) |
|
1565 |
{ |
|
1566 |
QTextObject *obj = document()->createObject(f); |
|
1567 |
||
1568 |
if (obj) { |
|
1569 |
obj->d_func()->objectIndex = objectIndex == -1 ? formats.createObjectIndex(f) : objectIndex; |
|
1570 |
objects[obj->d_func()->objectIndex] = obj; |
|
1571 |
} |
|
1572 |
||
1573 |
return obj; |
|
1574 |
} |
|
1575 |
||
1576 |
void QTextDocumentPrivate::deleteObject(QTextObject *object) |
|
1577 |
{ |
|
1578 |
const int objIdx = object->d_func()->objectIndex; |
|
1579 |
objects.remove(objIdx); |
|
1580 |
delete object; |
|
1581 |
} |
|
1582 |
||
1583 |
void QTextDocumentPrivate::contentsChanged() |
|
1584 |
{ |
|
1585 |
Q_Q(QTextDocument); |
|
1586 |
if (editBlock) |
|
1587 |
return; |
|
1588 |
||
1589 |
bool m = undoEnabled ? (modifiedState != undoState) : true; |
|
1590 |
if (modified != m) { |
|
1591 |
modified = m; |
|
1592 |
emit q->modificationChanged(modified); |
|
1593 |
} |
|
1594 |
||
1595 |
emit q->contentsChanged(); |
|
1596 |
} |
|
1597 |
||
1598 |
void QTextDocumentPrivate::compressPieceTable() |
|
1599 |
{ |
|
1600 |
if (undoEnabled) |
|
1601 |
return; |
|
1602 |
||
1603 |
const uint garbageCollectionThreshold = 96 * 1024; // bytes |
|
1604 |
||
1605 |
//qDebug() << "unreachable bytes:" << unreachableCharacterCount * sizeof(QChar) << " -- limit" << garbageCollectionThreshold << "text size =" << text.size() << "capacity:" << text.capacity(); |
|
1606 |
||
1607 |
bool compressTable = unreachableCharacterCount * sizeof(QChar) > garbageCollectionThreshold |
|
1608 |
&& text.size() >= text.capacity() * 0.9; |
|
1609 |
if (!compressTable) |
|
1610 |
return; |
|
1611 |
||
1612 |
QString newText; |
|
1613 |
newText.resize(text.size()); |
|
1614 |
QChar *newTextPtr = newText.data(); |
|
1615 |
int newLen = 0; |
|
1616 |
||
1617 |
for (FragmentMap::Iterator it = fragments.begin(); !it.atEnd(); ++it) { |
|
1618 |
qMemCopy(newTextPtr, text.constData() + it->stringPosition, it->size_array[0] * sizeof(QChar)); |
|
1619 |
it->stringPosition = newLen; |
|
1620 |
newTextPtr += it->size_array[0]; |
|
1621 |
newLen += it->size_array[0]; |
|
1622 |
} |
|
1623 |
||
1624 |
newText.resize(newLen); |
|
1625 |
newText.squeeze(); |
|
1626 |
//qDebug() << "removed" << text.size() - newText.size() << "characters"; |
|
1627 |
text = newText; |
|
1628 |
unreachableCharacterCount = 0; |
|
1629 |
} |
|
1630 |
||
1631 |
void QTextDocumentPrivate::setModified(bool m) |
|
1632 |
{ |
|
1633 |
Q_Q(QTextDocument); |
|
1634 |
if (m == modified) |
|
1635 |
return; |
|
1636 |
||
1637 |
modified = m; |
|
1638 |
if (!modified) |
|
1639 |
modifiedState = undoState; |
|
1640 |
else |
|
1641 |
modifiedState = -1; |
|
1642 |
||
1643 |
emit q->modificationChanged(modified); |
|
1644 |
} |
|
1645 |
||
1646 |
bool QTextDocumentPrivate::ensureMaximumBlockCount() |
|
1647 |
{ |
|
1648 |
if (maximumBlockCount <= 0) |
|
1649 |
return false; |
|
1650 |
if (blocks.numNodes() <= maximumBlockCount) |
|
1651 |
return false; |
|
1652 |
||
1653 |
beginEditBlock(); |
|
1654 |
||
1655 |
const int blocksToRemove = blocks.numNodes() - maximumBlockCount; |
|
1656 |
QTextCursor cursor(this, 0); |
|
1657 |
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, blocksToRemove); |
|
1658 |
||
1659 |
unreachableCharacterCount += cursor.selectionEnd() - cursor.selectionStart(); |
|
1660 |
||
1661 |
// preserve the char format of the paragraph that is to become the new first one |
|
1662 |
QTextCharFormat charFmt = cursor.blockCharFormat(); |
|
1663 |
cursor.removeSelectedText(); |
|
1664 |
cursor.setBlockCharFormat(charFmt); |
|
1665 |
||
1666 |
endEditBlock(); |
|
1667 |
||
1668 |
compressPieceTable(); |
|
1669 |
||
1670 |
return true; |
|
1671 |
} |
|
1672 |
||
1673 |
/// This method is called from QTextTable when it is about to remove a table-cell to allow cursors to update their selection. |
|
1674 |
void QTextDocumentPrivate::aboutToRemoveCell(int from, int to) |
|
1675 |
{ |
|
1676 |
Q_ASSERT(from <= to); |
|
1677 |
for (int i = 0; i < cursors.size(); ++i) |
|
1678 |
cursors.at(i)->aboutToRemoveCell(from, to); |
|
1679 |
} |
|
1680 |
||
1681 |
QT_END_NAMESPACE |