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