1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
|
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qtextobject.h"
#include "qtextobject_p.h"
#include "qtextdocument.h"
#include "qtextformat_p.h"
#include "qtextdocument_p.h"
#include "qtextcursor.h"
#include "qtextlist.h"
#include "qabstracttextdocumentlayout.h"
#include "qtextengine_p.h"
#include "qdebug.h"
QT_BEGIN_NAMESPACE
// ### DOC: We ought to explain the CONCEPT of objectIndexes if
// relevant to the public API
/*!
\class QTextObject
\reentrant
\brief The QTextObject class is a base class for different kinds
of objects that can group parts of a QTextDocument together.
\ingroup richtext-processing
The common grouping text objects are lists (QTextList), frames
(QTextFrame), and tables (QTextTable). A text object has an
associated format() and document().
There are essentially two kinds of text objects: those that are used
with blocks (block formats), and those that are used with characters
(character formats). The first kind are derived from QTextBlockGroup,
and the second kind from QTextFrame.
You rarely need to use this class directly. When creating custom text
objects, you will also need to reimplement QTextDocument::createObject()
which acts as a factory method for creating text objects.
\sa QTextDocument, {Text Object Example}
*/
/*!
\fn QTextObject::QTextObject(QTextDocument *document)
Creates a new QTextObject for the given \a document.
\warning This function should never be called directly, but only
from QTextDocument::createObject().
*/
QTextObject::QTextObject(QTextDocument *doc)
: QObject(*new QTextObjectPrivate(doc), doc)
{
}
/*!
\fn QTextObject::QTextObject(QTextObjectPrivate &p, QTextDocument *document)
\internal
*/
QTextObject::QTextObject(QTextObjectPrivate &p, QTextDocument *doc)
: QObject(p, doc)
{
}
/*!
Destroys the text object.
\warning Text objects are owned by the document, so you should
never destroy them yourself.
*/
QTextObject::~QTextObject()
{
}
/*!
Returns the text object's format.
\sa setFormat() document()
*/
QTextFormat QTextObject::format() const
{
Q_D(const QTextObject);
return d->pieceTable->formatCollection()->objectFormat(d->objectIndex);
}
/*!
Returns the index of the object's format in the document's internal
list of formats.
\sa QTextDocument::allFormats()
*/
int QTextObject::formatIndex() const
{
Q_D(const QTextObject);
return d->pieceTable->formatCollection()->objectFormatIndex(d->objectIndex);
}
/*!
Sets the text object's \a format.
\sa format()
*/
void QTextObject::setFormat(const QTextFormat &format)
{
Q_D(QTextObject);
int idx = d->pieceTable->formatCollection()->indexForFormat(format);
d->pieceTable->changeObjectFormat(this, idx);
}
/*!
Returns the object index of this object. This can be used together with
QTextFormat::setObjectIndex().
*/
int QTextObject::objectIndex() const
{
Q_D(const QTextObject);
return d->objectIndex;
}
/*!
Returns the document this object belongs to.
\sa format()
*/
QTextDocument *QTextObject::document() const
{
return static_cast<QTextDocument *>(parent());
}
/*!
\internal
*/
QTextDocumentPrivate *QTextObject::docHandle() const
{
return static_cast<const QTextDocument *>(parent())->docHandle();
}
/*!
\class QTextBlockGroup
\reentrant
\brief The QTextBlockGroup class provides a container for text blocks within
a QTextDocument.
\ingroup richtext-processing
Block groups can be used to organize blocks of text within a document.
They maintain an up-to-date list of the text blocks that belong to
them, even when text blocks are being edited.
Each group has a parent document which is specified when the group is
constructed.
Text blocks can be inserted into a group with blockInserted(), and removed
with blockRemoved(). If a block's format is changed, blockFormatChanged()
is called.
The list of blocks in the group is returned by blockList(). Note that the
blocks in the list are not necessarily adjacent elements in the document;
for example, the top-level items in a multi-level list will be separated
by the items in lower levels of the list.
\sa QTextBlock QTextDocument
*/
void QTextBlockGroupPrivate::markBlocksDirty()
{
for (int i = 0; i < blocks.count(); ++i) {
const QTextBlock &block = blocks.at(i);
pieceTable->documentChange(block.position(), block.length());
}
}
/*!
\fn QTextBlockGroup::QTextBlockGroup(QTextDocument *document)
Creates a new new block group for the given \a document.
\warning This function should only be called from
QTextDocument::createObject().
*/
QTextBlockGroup::QTextBlockGroup(QTextDocument *doc)
: QTextObject(*new QTextBlockGroupPrivate(doc), doc)
{
}
/*!
\internal
*/
QTextBlockGroup::QTextBlockGroup(QTextBlockGroupPrivate &p, QTextDocument *doc)
: QTextObject(p, doc)
{
}
/*!
Destroys this block group; the blocks are not deleted, they simply
don't belong to this block anymore.
*/
QTextBlockGroup::~QTextBlockGroup()
{
}
// ### DOC: Shouldn't this be insertBlock()?
/*!
Appends the given \a block to the end of the group.
\warning If you reimplement this function you must call the base
class implementation.
*/
void QTextBlockGroup::blockInserted(const QTextBlock &block)
{
Q_D(QTextBlockGroup);
QTextBlockGroupPrivate::BlockList::Iterator it = qLowerBound(d->blocks.begin(), d->blocks.end(), block);
d->blocks.insert(it, block);
d->markBlocksDirty();
}
// ### DOC: Shouldn't this be removeBlock()?
/*!
Removes the given \a block from the group; the block itself is not
deleted, it simply isn't a member of this group anymore.
*/
void QTextBlockGroup::blockRemoved(const QTextBlock &block)
{
Q_D(QTextBlockGroup);
d->blocks.removeAll(block);
d->markBlocksDirty();
if (d->blocks.isEmpty()) {
document()->docHandle()->deleteObject(this);
return;
}
}
/*!
This function is called whenever the specified \a block of text is changed.
The text block is a member of this group.
The base class implementation does nothing.
*/
void QTextBlockGroup::blockFormatChanged(const QTextBlock &)
{
}
/*!
Returns a (possibly empty) list of all the blocks that are part of
the block group.
*/
QList<QTextBlock> QTextBlockGroup::blockList() const
{
Q_D(const QTextBlockGroup);
return d->blocks;
}
QTextFrameLayoutData::~QTextFrameLayoutData()
{
}
/*!
\class QTextFrame
\reentrant
\brief The QTextFrame class represents a frame in a QTextDocument.
\ingroup richtext-processing
Text frames provide structure for the text in a document. They are used
as generic containers for other document elements.
Frames are usually created by using QTextCursor::insertFrame().
\omit
Each frame in a document consists of a frame start character,
QChar(0xFDD0), followed by the frame's contents, followed by a
frame end character, QChar(0xFDD1). The character formats of the
start and end character contain a reference to the frame object's
objectIndex.
\endomit
Frames can be used to create hierarchical structures in rich text documents.
Each document has a root frame (QTextDocument::rootFrame()), and each frame
beneath the root frame has a parent frame and a (possibly empty) list of
child frames. The parent frame can be found with parentFrame(), and the
childFrames() function provides a list of child frames.
Each frame contains at least one text block to enable text cursors to
insert new document elements within. As a result, the QTextFrame::iterator
class is used to traverse both the blocks and child frames within a given
frame. The first and last child elements in the frame can be found with
begin() and end().
A frame also has a format (specified using QTextFrameFormat) which can be set
with setFormat() and read with format().
Text cursors can be obtained that point to the first and last valid cursor
positions within a frame; use the firstCursorPosition() and
lastCursorPosition() functions for this. The frame's extent in the
document can be found with firstPosition() and lastPosition().
You can iterate over a frame's contents using the
QTextFrame::iterator class: this provides read-only access to its
internal list of text blocks and child frames.
\sa QTextCursor QTextDocument
*/
/*!
\typedef QTextFrame::Iterator
Qt-style synonym for QTextFrame::iterator.
*/
/*!
\fn QTextFrame *QTextFrame::iterator::parentFrame() const
Returns the parent frame of the current frame.
\sa currentFrame() QTextFrame::parentFrame()
*/
/*!
\fn bool QTextFrame::iterator::operator==(const iterator &other) const
Retuns true if the iterator is the same as the \a other iterator;
otherwise returns false.
*/
/*!
\fn bool QTextFrame::iterator::operator!=(const iterator &other) const
Retuns true if the iterator is different from the \a other iterator;
otherwise returns false.
*/
/*!
\fn QTextFrame::iterator QTextFrame::iterator::operator++(int)
The postfix ++ operator (\c{i++}) advances the iterator to the
next item in the text frame, and returns an iterator to the old item.
*/
/*!
\fn QTextFrame::iterator QTextFrame::iterator::operator--(int)
The postfix -- operator (\c{i--}) makes the preceding item in the
current frame, and returns an iterator to the old item.
*/
/*!
\fn void QTextFrame::setFrameFormat(const QTextFrameFormat &format)
Sets the frame's \a format.
\sa frameFormat()
*/
/*!
\fn QTextFrameFormat QTextFrame::frameFormat() const
Returns the frame's format.
\sa setFrameFormat()
*/
/*!
\fn QTextFrame::QTextFrame(QTextDocument *document)
Creates a new empty frame for the text \a document.
*/
QTextFrame::QTextFrame(QTextDocument *doc)
: QTextObject(*new QTextFramePrivate(doc), doc)
{
}
// ### DOC: What does this do to child frames?
/*!
Destroys the frame, and removes it from the document's layout.
*/
QTextFrame::~QTextFrame()
{
Q_D(QTextFrame);
delete d->layoutData;
}
/*!
\internal
*/
QTextFrame::QTextFrame(QTextFramePrivate &p, QTextDocument *doc)
: QTextObject(p, doc)
{
}
/*!
Returns a (possibly empty) list of the frame's child frames.
\sa parentFrame()
*/
QList<QTextFrame *> QTextFrame::childFrames() const
{
Q_D(const QTextFrame);
return d->childFrames;
}
/*!
Returns the frame's parent frame. If the frame is the root frame of a
document, this will return 0.
\sa childFrames() QTextDocument::rootFrame()
*/
QTextFrame *QTextFrame::parentFrame() const
{
Q_D(const QTextFrame);
return d->parentFrame;
}
/*!
Returns the first cursor position inside the frame.
\sa lastCursorPosition() firstPosition() lastPosition()
*/
QTextCursor QTextFrame::firstCursorPosition() const
{
Q_D(const QTextFrame);
return QTextCursor(d->pieceTable, firstPosition());
}
/*!
Returns the last cursor position inside the frame.
\sa firstCursorPosition() firstPosition() lastPosition()
*/
QTextCursor QTextFrame::lastCursorPosition() const
{
Q_D(const QTextFrame);
return QTextCursor(d->pieceTable, lastPosition());
}
/*!
Returns the first document position inside the frame.
\sa lastPosition() firstCursorPosition() lastCursorPosition()
*/
int QTextFrame::firstPosition() const
{
Q_D(const QTextFrame);
if (!d->fragment_start)
return 0;
return d->pieceTable->fragmentMap().position(d->fragment_start) + 1;
}
/*!
Returns the last document position inside the frame.
\sa firstPosition() firstCursorPosition() lastCursorPosition()
*/
int QTextFrame::lastPosition() const
{
Q_D(const QTextFrame);
if (!d->fragment_end)
return d->pieceTable->length() - 1;
return d->pieceTable->fragmentMap().position(d->fragment_end);
}
/*!
\internal
*/
QTextFrameLayoutData *QTextFrame::layoutData() const
{
Q_D(const QTextFrame);
return d->layoutData;
}
/*!
\internal
*/
void QTextFrame::setLayoutData(QTextFrameLayoutData *data)
{
Q_D(QTextFrame);
delete d->layoutData;
d->layoutData = data;
}
void QTextFramePrivate::fragmentAdded(const QChar &type, uint fragment)
{
if (type == QTextBeginningOfFrame) {
Q_ASSERT(!fragment_start);
fragment_start = fragment;
} else if (type == QTextEndOfFrame) {
Q_ASSERT(!fragment_end);
fragment_end = fragment;
} else if (type == QChar::ObjectReplacementCharacter) {
Q_ASSERT(!fragment_start);
Q_ASSERT(!fragment_end);
fragment_start = fragment;
fragment_end = fragment;
} else {
Q_ASSERT(false);
}
}
void QTextFramePrivate::fragmentRemoved(const QChar &type, uint fragment)
{
Q_UNUSED(fragment); // --release warning
if (type == QTextBeginningOfFrame) {
Q_ASSERT(fragment_start == fragment);
fragment_start = 0;
} else if (type == QTextEndOfFrame) {
Q_ASSERT(fragment_end == fragment);
fragment_end = 0;
} else if (type == QChar::ObjectReplacementCharacter) {
Q_ASSERT(fragment_start == fragment);
Q_ASSERT(fragment_end == fragment);
fragment_start = 0;
fragment_end = 0;
} else {
Q_ASSERT(false);
}
remove_me();
}
void QTextFramePrivate::remove_me()
{
Q_Q(QTextFrame);
if (fragment_start == 0 && fragment_end == 0
&& !parentFrame) {
q->document()->docHandle()->deleteObject(q);
return;
}
if (!parentFrame)
return;
int index = parentFrame->d_func()->childFrames.indexOf(q);
// iterator over all children and move them to the parent
for (int i = 0; i < childFrames.size(); ++i) {
QTextFrame *c = childFrames.at(i);
parentFrame->d_func()->childFrames.insert(index, c);
c->d_func()->parentFrame = parentFrame;
++index;
}
Q_ASSERT(parentFrame->d_func()->childFrames.at(index) == q);
parentFrame->d_func()->childFrames.removeAt(index);
childFrames.clear();
parentFrame = 0;
}
/*!
\class QTextFrame::iterator
\reentrant
\brief The iterator class provides an iterator for reading
the contents of a QTextFrame.
\ingroup richtext-processing
A frame consists of an arbitrary sequence of \l{QTextBlock}s and
child \l{QTextFrame}s. This class provides a way to iterate over the
child objects of a frame, and read their contents. It does not provide
a way to modify the contents of the frame.
*/
/*!
\fn bool QTextFrame::iterator::atEnd() const
Returns true if the current item is the last item in the text frame.
*/
/*!
Returns an iterator pointing to the first document element inside the frame.
Please see the document \l{STL-style-Iterators} for more information.
\sa end()
*/
QTextFrame::iterator QTextFrame::begin() const
{
const QTextDocumentPrivate *priv = docHandle();
int b = priv->blockMap().findNode(firstPosition());
int e = priv->blockMap().findNode(lastPosition()+1);
return iterator(const_cast<QTextFrame *>(this), b, b, e);
}
/*!
Returns an iterator pointing to the position past the last document element inside the frame.
Please see the document \l{STL-Style Iterators} for more information.
\sa begin()
*/
QTextFrame::iterator QTextFrame::end() const
{
const QTextDocumentPrivate *priv = docHandle();
int b = priv->blockMap().findNode(firstPosition());
int e = priv->blockMap().findNode(lastPosition()+1);
return iterator(const_cast<QTextFrame *>(this), e, b, e);
}
/*!
Constructs an invalid iterator.
*/
QTextFrame::iterator::iterator()
{
f = 0;
b = 0;
e = 0;
cf = 0;
cb = 0;
}
/*!
\internal
*/
QTextFrame::iterator::iterator(QTextFrame *frame, int block, int begin, int end)
{
f = frame;
b = begin;
e = end;
cf = 0;
cb = block;
}
/*!
Copy constructor. Constructs a copy of the \a other iterator.
*/
QTextFrame::iterator::iterator(const iterator &other)
{
f = other.f;
b = other.b;
e = other.e;
cf = other.cf;
cb = other.cb;
}
/*!
Assigns \a other to this iterator and returns a reference to
this iterator.
*/
QTextFrame::iterator &QTextFrame::iterator::operator=(const iterator &other)
{
f = other.f;
b = other.b;
e = other.e;
cf = other.cf;
cb = other.cb;
return *this;
}
/*!
Returns the current frame pointed to by the iterator, or 0 if the
iterator currently points to a block.
\sa currentBlock()
*/
QTextFrame *QTextFrame::iterator::currentFrame() const
{
return cf;
}
/*!
Returns the current block the iterator points to. If the iterator
points to a child frame, the returned block is invalid.
\sa currentFrame()
*/
QTextBlock QTextFrame::iterator::currentBlock() const
{
if (!f)
return QTextBlock();
return QTextBlock(f->docHandle(), cb);
}
/*!
Moves the iterator to the next frame or block.
\sa currentBlock() currentFrame()
*/
QTextFrame::iterator &QTextFrame::iterator::operator++()
{
const QTextDocumentPrivate *priv = f->docHandle();
const QTextDocumentPrivate::BlockMap &map = priv->blockMap();
if (cf) {
int end = cf->lastPosition() + 1;
cb = map.findNode(end);
cf = 0;
} else if (cb) {
cb = map.next(cb);
if (cb == e)
return *this;
if (!f->d_func()->childFrames.isEmpty()) {
int pos = map.position(cb);
// check if we entered a frame
QTextDocumentPrivate::FragmentIterator frag = priv->find(pos-1);
if (priv->buffer().at(frag->stringPosition) != QChar::ParagraphSeparator) {
QTextFrame *nf = qobject_cast<QTextFrame *>(priv->objectForFormat(frag->format));
if (nf) {
if (priv->buffer().at(frag->stringPosition) == QTextBeginningOfFrame && nf != f) {
cf = nf;
cb = 0;
} else {
Q_ASSERT(priv->buffer().at(frag->stringPosition) != QTextEndOfFrame);
}
}
}
}
}
return *this;
}
/*!
Moves the iterator to the previous frame or block.
\sa currentBlock() currentFrame()
*/
QTextFrame::iterator &QTextFrame::iterator::operator--()
{
const QTextDocumentPrivate *priv = f->docHandle();
const QTextDocumentPrivate::BlockMap &map = priv->blockMap();
if (cf) {
int start = cf->firstPosition() - 1;
cb = map.findNode(start);
cf = 0;
} else {
if (cb == b)
goto end;
if (cb != e) {
int pos = map.position(cb);
// check if we have to enter a frame
QTextDocumentPrivate::FragmentIterator frag = priv->find(pos-1);
if (priv->buffer().at(frag->stringPosition) != QChar::ParagraphSeparator) {
QTextFrame *pf = qobject_cast<QTextFrame *>(priv->objectForFormat(frag->format));
if (pf) {
if (priv->buffer().at(frag->stringPosition) == QTextBeginningOfFrame) {
Q_ASSERT(pf == f);
} else if (priv->buffer().at(frag->stringPosition) == QTextEndOfFrame) {
Q_ASSERT(pf != f);
cf = pf;
cb = 0;
goto end;
}
}
}
}
cb = map.previous(cb);
}
end:
return *this;
}
/*!
\class QTextBlockUserData
\reentrant
\brief The QTextBlockUserData class is used to associate custom data with blocks of text.
\since 4.1
\ingroup richtext-processing
QTextBlockUserData provides an abstract interface for container classes that are used
to associate application-specific user data with text blocks in a QTextDocument.
Generally, subclasses of this class provide functions to allow data to be stored
and retrieved, and instances are attached to blocks of text using
QTextBlock::setUserData(). This makes it possible to store additional data per text
block in a way that can be retrieved safely by the application.
Each subclass should provide a reimplementation of the destructor to ensure that any
private data is automatically cleaned up when user data objects are deleted.
\sa QTextBlock
*/
/*!
Destroys the user data.
*/
QTextBlockUserData::~QTextBlockUserData()
{
}
/*!
\class QTextBlock
\reentrant
\brief The QTextBlock class provides a container for text fragments in a
QTextDocument.
\ingroup richtext-processing
A text block encapsulates a block or paragraph of text in a QTextDocument.
QTextBlock provides read-only access to the block/paragraph structure of
QTextDocuments. It is mainly of use if you want to implement your own
layouts for the visual representation of a QTextDocument, or if you want to
iterate over a document and write out the contents in your own custom
format.
Text blocks are created by their parent documents. If you need to create
a new text block, or modify the contents of a document while examining its
contents, use the cursor-based interface provided by QTextCursor instead.
Each text block is located at a specific position() in a document().
The contents of the block can be obtained by using the text() function.
The length() function determines the block's size within the document
(including formatting characters).
The visual properties of the block are determined by its text layout(),
its charFormat(), and its blockFormat().
The next() and previous() functions enable iteration over consecutive
valid blocks in a document under the condition that the document is not
modified by other means during the iteration process. Note that, although
blocks are returned in sequence, adjacent blocks may come from different
places in the document structure. The validity of a block can be determined
by calling isValid().
QTextBlock provides comparison operators to make it easier to work with
blocks: \l operator==() compares two block for equality, \l operator!=()
compares two blocks for inequality, and \l operator<() determines whether
a block precedes another in the same document.
\img qtextblock-sequence.png
\sa QTextBlockFormat QTextCharFormat QTextFragment
*/
/*!
\fn QTextBlock::QTextBlock(QTextDocumentPrivate *priv, int b)
\internal
*/
/*!
\fn QTextBlock::QTextBlock()
\internal
*/
/*!
\fn QTextBlock::QTextBlock(const QTextBlock &other)
Copies the \a other text block's attributes to this text block.
*/
/*!
\fn bool QTextBlock::isValid() const
Returns true if this text block is valid; otherwise returns false.
*/
/*!
\fn QTextBlock &QTextBlock::operator=(const QTextBlock &other)
Assigns the \a other text block to this text block.
*/
/*!
\fn bool QTextBlock::operator==(const QTextBlock &other) const
Returns true if this text block is the same as the \a other text
block.
*/
/*!
\fn bool QTextBlock::operator!=(const QTextBlock &other) const
Returns true if this text block is different from the \a other
text block.
*/
/*!
\fn bool QTextBlock::operator<(const QTextBlock &other) const
Returns true if this text block occurs before the \a other text
block in the document.
*/
/*!
\class QTextBlock::iterator
\reentrant
\brief The QTextBlock::iterator class provides an iterator for reading
the contents of a QTextBlock.
\ingroup richtext-processing
A block consists of a sequence of text fragments. This class provides
a way to iterate over these, and read their contents. It does not provide
a way to modify the internal structure or contents of the block.
An iterator can be constructed and used to access the fragments within
a text block in the following way:
\snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 4
\snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 7
\sa QTextFragment
*/
/*!
\typedef QTextBlock::Iterator
Qt-style synonym for QTextBlock::iterator.
*/
/*!
\fn QTextBlock::iterator::iterator()
Constructs an iterator for this text block.
*/
/*!
\fn QTextBlock::iterator::iterator(const iterator &other)
Copy constructor. Constructs a copy of the \a other iterator.
*/
/*!
\fn bool QTextBlock::iterator::atEnd() const
Returns true if the current item is the last item in the text block.
*/
/*!
\fn bool QTextBlock::iterator::operator==(const iterator &other) const
Retuns true if this iterator is the same as the \a other iterator;
otherwise returns false.
*/
/*!
\fn bool QTextBlock::iterator::operator!=(const iterator &other) const
Retuns true if this iterator is different from the \a other iterator;
otherwise returns false.
*/
/*!
\fn QTextBlock::iterator QTextBlock::iterator::operator++(int)
The postfix ++ operator (\c{i++}) advances the iterator to the
next item in the text block and returns an iterator to the old current
item.
*/
/*!
\fn QTextBlock::iterator QTextBlock::iterator::operator--(int)
The postfix -- operator (\c{i--}) makes the preceding item current and
returns an iterator to the old current item.
*/
/*!
\fn QTextDocumentPrivate *QTextBlock::docHandle() const
\internal
*/
/*!
\fn int QTextBlock::fragmentIndex() const
\internal
*/
/*!
Returns the index of the block's first character within the document.
*/
int QTextBlock::position() const
{
if (!p || !n)
return 0;
return p->blockMap().position(n);
}
/*!
Returns the length of the block in characters.
\note The length returned includes all formatting characters,
for example, newline.
\sa text() charFormat() blockFormat()
*/
int QTextBlock::length() const
{
if (!p || !n)
return 0;
return p->blockMap().size(n);
}
/*!
Returns true if the given \a position is located within the text
block; otherwise returns false.
*/
bool QTextBlock::contains(int position) const
{
if (!p || !n)
return false;
int pos = p->blockMap().position(n);
int len = p->blockMap().size(n);
return position >= pos && position < pos + len;
}
/*!
Returns the QTextLayout that is used to lay out and display the
block's contents.
Note that the returned QTextLayout object can only be modified from the
documentChanged implementation of a QAbstractTextDocumentLayout subclass.
Any changes applied from the outside cause undefined behavior.
\sa clearLayout()
*/
QTextLayout *QTextBlock::layout() const
{
if (!p || !n)
return 0;
const QTextBlockData *b = p->blockMap().fragment(n);
if (!b->layout)
b->layout = new QTextLayout(*this);
return b->layout;
}
/*!
\since 4.4
Clears the QTextLayout that is used to lay out and display the
block's contents.
\sa layout()
*/
void QTextBlock::clearLayout()
{
if (!p || !n)
return;
const QTextBlockData *b = p->blockMap().fragment(n);
if (b->layout)
b->layout->clearLayout();
}
/*!
Returns the QTextBlockFormat that describes block-specific properties.
\sa charFormat()
*/
QTextBlockFormat QTextBlock::blockFormat() const
{
if (!p || !n)
return QTextFormat().toBlockFormat();
return p->formatCollection()->blockFormat(p->blockMap().fragment(n)->format);
}
/*!
Returns an index into the document's internal list of block formats
for the text block's format.
\sa QTextDocument::allFormats()
*/
int QTextBlock::blockFormatIndex() const
{
if (!p || !n)
return -1;
return p->blockMap().fragment(n)->format;
}
/*!
Returns the QTextCharFormat that describes the block's character
format. The block's character format is used when inserting text into
an empty block.
\sa blockFormat()
*/
QTextCharFormat QTextBlock::charFormat() const
{
if (!p || !n)
return QTextFormat().toCharFormat();
return p->formatCollection()->charFormat(charFormatIndex());
}
/*!
Returns an index into the document's internal list of character formats
for the text block's character format.
\sa QTextDocument::allFormats()
*/
int QTextBlock::charFormatIndex() const
{
if (!p || !n)
return -1;
return p->blockCharFormatIndex(n);
}
/*!
\since 4.7
Returns the resolved text direction.
If the block has no explicit direction set, it will resolve the
direction from the blocks content. Returns either Qt::LeftToRight
or Qt::RightToLeft.
\sa QTextFormat::layoutDirection(), QString::isRightToLeft(), Qt::LayoutDirection
*/
Qt::LayoutDirection QTextBlock::textDirection() const
{
Qt::LayoutDirection dir = blockFormat().layoutDirection();
if (dir != Qt::LayoutDirectionAuto)
return dir;
dir = p->defaultTextOption.textDirection();
if (dir != Qt::LayoutDirectionAuto)
return dir;
const QString buffer = p->buffer();
const int pos = position();
QTextDocumentPrivate::FragmentIterator it = p->find(pos);
QTextDocumentPrivate::FragmentIterator end = p->find(pos + length() - 1); // -1 to omit the block separator char
for (; it != end; ++it) {
const QTextFragmentData * const frag = it.value();
const QChar *p = buffer.constData() + frag->stringPosition;
const QChar * const end = p + frag->size_array[0];
while (p < end) {
switch(QChar::direction(p->unicode()))
{
case QChar::DirL:
return Qt::LeftToRight;
case QChar::DirR:
case QChar::DirAL:
return Qt::RightToLeft;
default:
break;
}
++p;
}
}
return Qt::LeftToRight;
}
/*!
Returns the block's contents as plain text.
\sa length() charFormat() blockFormat()
*/
QString QTextBlock::text() const
{
if (!p || !n)
return QString();
const QString buffer = p->buffer();
QString text;
text.reserve(length());
const int pos = position();
QTextDocumentPrivate::FragmentIterator it = p->find(pos);
QTextDocumentPrivate::FragmentIterator end = p->find(pos + length() - 1); // -1 to omit the block separator char
for (; it != end; ++it) {
const QTextFragmentData * const frag = it.value();
text += QString::fromRawData(buffer.constData() + frag->stringPosition, frag->size_array[0]);
}
return text;
}
/*!
Returns the text document this text block belongs to, or 0 if the
text block does not belong to any document.
*/
const QTextDocument *QTextBlock::document() const
{
return p ? p->document() : 0;
}
/*!
If the block represents a list item, returns the list that the item belongs
to; otherwise returns 0.
*/
QTextList *QTextBlock::textList() const
{
if (!isValid())
return 0;
const QTextBlockFormat fmt = blockFormat();
QTextObject *obj = p->document()->objectForFormat(fmt);
return qobject_cast<QTextList *>(obj);
}
/*!
\since 4.1
Returns a pointer to a QTextBlockUserData object if previously set with
setUserData() or a null pointer.
*/
QTextBlockUserData *QTextBlock::userData() const
{
if (!p || !n)
return 0;
const QTextBlockData *b = p->blockMap().fragment(n);
return b->userData;
}
/*!
\since 4.1
Attaches the given \a data object to the text block.
QTextBlockUserData can be used to store custom settings. The
ownership is passed to the underlying text document, i.e. the
provided QTextBlockUserData object will be deleted if the
corresponding text block gets deleted. The user data object is
not stored in the undo history, so it will not be available after
undoing the deletion of a text block.
For example, if you write a programming editor in an IDE, you may
want to let your user set breakpoints visually in your code for an
integrated debugger. In a programming editor a line of text
usually corresponds to one QTextBlock. The QTextBlockUserData
interface allows the developer to store data for each QTextBlock,
like for example in which lines of the source code the user has a
breakpoint set. Of course this could also be stored externally,
but by storing it inside the QTextDocument, it will for example be
automatically deleted when the user deletes the associated
line. It's really just a way to store custom information in the
QTextDocument without using custom properties in QTextFormat which
would affect the undo/redo stack.
*/
void QTextBlock::setUserData(QTextBlockUserData *data)
{
if (!p || !n)
return;
const QTextBlockData *b = p->blockMap().fragment(n);
if (data != b->userData)
delete b->userData;
b->userData = data;
}
/*!
\since 4.1
Returns the integer value previously set with setUserState() or -1.
*/
int QTextBlock::userState() const
{
if (!p || !n)
return -1;
const QTextBlockData *b = p->blockMap().fragment(n);
return b->userState;
}
/*!
\since 4.1
Stores the specified \a state integer value in the text block. This may be
useful for example in a syntax highlighter to store a text parsing state.
*/
void QTextBlock::setUserState(int state)
{
if (!p || !n)
return;
const QTextBlockData *b = p->blockMap().fragment(n);
b->userState = state;
}
/*!
\since 4.4
Returns the blocks revision.
\sa setRevision(), QTextDocument::revision()
*/
int QTextBlock::revision() const
{
if (!p || !n)
return -1;
const QTextBlockData *b = p->blockMap().fragment(n);
return b->revision;
}
/*!
\since 4.4
Sets a blocks revision to \a rev.
\sa revision(), QTextDocument::revision()
*/
void QTextBlock::setRevision(int rev)
{
if (!p || !n)
return;
const QTextBlockData *b = p->blockMap().fragment(n);
b->revision = rev;
}
/*!
\since 4.4
Returns true if the block is visible; otherwise returns false.
\sa setVisible()
*/
bool QTextBlock::isVisible() const
{
if (!p || !n)
return true;
const QTextBlockData *b = p->blockMap().fragment(n);
return !b->hidden;
}
/*!
\since 4.4
Sets the block's visibility to \a visible.
\sa isVisible()
*/
void QTextBlock::setVisible(bool visible)
{
if (!p || !n)
return;
const QTextBlockData *b = p->blockMap().fragment(n);
b->hidden = !visible;
}
/*!
\since 4.4
Returns the number of this block, or -1 if the block is invalid.
\sa QTextCursor::blockNumber()
*/
int QTextBlock::blockNumber() const
{
if (!p || !n)
return -1;
return p->blockMap().position(n, 1);
}
/*!
\since 4.5
Returns the first line number of this block, or -1 if the block is invalid.
Unless the layout supports it, the line number is identical to the block number.
\sa QTextBlock::blockNumber()
*/
int QTextBlock::firstLineNumber() const
{
if (!p || !n)
return -1;
return p->blockMap().position(n, 2);
}
/*!
\since 4.5
Sets the line count to \a count.
\sa lineCount()
*/
void QTextBlock::setLineCount(int count)
{
if (!p || !n)
return;
p->blockMap().setSize(n, count, 2);
}
/*!
\since 4.5
Returns the line count. Not all document layouts support this feature.
\sa setLineCount()
*/
int QTextBlock::lineCount() const
{
if (!p || !n)
return -1;
return p->blockMap().size(n, 2);
}
/*!
Returns a text block iterator pointing to the beginning of the
text block.
\sa end()
*/
QTextBlock::iterator QTextBlock::begin() const
{
if (!p || !n)
return iterator();
int pos = position();
int len = length() - 1; // exclude the fragment that holds the paragraph separator
int b = p->fragmentMap().findNode(pos);
int e = p->fragmentMap().findNode(pos+len);
return iterator(p, b, e, b);
}
/*!
Returns a text block iterator pointing to the end of the text
block.
\sa begin() next() previous()
*/
QTextBlock::iterator QTextBlock::end() const
{
if (!p || !n)
return iterator();
int pos = position();
int len = length() - 1; // exclude the fragment that holds the paragraph separator
int b = p->fragmentMap().findNode(pos);
int e = p->fragmentMap().findNode(pos+len);
return iterator(p, b, e, e);
}
/*!
Returns the text block in the document after this block, or an empty
text block if this is the last one.
Note that the next block may be in a different frame or table to this block.
\sa previous() begin() end()
*/
QTextBlock QTextBlock::next() const
{
if (!p)
return QTextBlock();
return QTextBlock(p, p->blockMap().next(n));
}
/*!
Returns the text block in the document before this block, or an empty text
block if this is the first one.
Note that the next block may be in a different frame or table to this block.
\sa next() begin() end()
*/
QTextBlock QTextBlock::previous() const
{
if (!p)
return QTextBlock();
return QTextBlock(p, p->blockMap().previous(n));
}
/*!
Returns the text fragment the iterator currently points to.
*/
QTextFragment QTextBlock::iterator::fragment() const
{
int ne = n;
int formatIndex = p->fragmentMap().fragment(n)->format;
do {
ne = p->fragmentMap().next(ne);
} while (ne != e && p->fragmentMap().fragment(ne)->format == formatIndex);
return QTextFragment(p, n, ne);
}
/*!
The prefix ++ operator (\c{++i}) advances the iterator to the
next item in the hash and returns an iterator to the new current
item.
*/
QTextBlock::iterator &QTextBlock::iterator::operator++()
{
int ne = n;
int formatIndex = p->fragmentMap().fragment(n)->format;
do {
ne = p->fragmentMap().next(ne);
} while (ne != e && p->fragmentMap().fragment(ne)->format == formatIndex);
n = ne;
return *this;
}
/*!
The prefix -- operator (\c{--i}) makes the preceding item
current and returns an iterator pointing to the new current item.
*/
QTextBlock::iterator &QTextBlock::iterator::operator--()
{
n = p->fragmentMap().previous(n);
if (n == b)
return *this;
int formatIndex = p->fragmentMap().fragment(n)->format;
int last = n;
while (n != b && p->fragmentMap().fragment(n)->format != formatIndex) {
last = n;
n = p->fragmentMap().previous(n);
}
n = last;
return *this;
}
/*!
\class QTextFragment
\reentrant
\brief The QTextFragment class holds a piece of text in a
QTextDocument with a single QTextCharFormat.
\ingroup richtext-processing
A text fragment describes a piece of text that is stored with a single
character format. Text in which the character format changes can be
represented by sequences of text fragments with different formats.
If the user edits the text in a fragment and introduces a different
character format, the fragment's text will be split at each point where
the format changes, and new fragments will be created.
For example, changing the style of some text in the middle of a
sentence will cause the fragment to be broken into three separate fragments:
the first and third with the same format as before, and the second with
the new style. The first fragment will contain the text from the beginning
of the sentence, the second will contain the text from the middle, and the
third takes the text from the end of the sentence.
\img qtextfragment-split.png
A fragment's text and character format can be obtained with the text()
and charFormat() functions. The length() function gives the length of
the text in the fragment. position() gives the position in the document
of the start of the fragment. To determine whether the fragment contains
a particular position within the document, use the contains() function.
\sa QTextDocument, {Rich Text Document Structure}
*/
/*!
\fn QTextFragment::QTextFragment(const QTextDocumentPrivate *priv, int f, int fe)
\internal
*/
/*!
\fn QTextFragment::QTextFragment()
Creates a new empty text fragment.
*/
/*!
\fn QTextFragment::QTextFragment(const QTextFragment &other)
Copies the content (text and format) of the \a other text fragment
to this text fragment.
*/
/*!
\fn QTextFragment &QTextFragment::operator=(const QTextFragment
&other)
Assigns the content (text and format) of the \a other text fragment
to this text fragment.
*/
/*!
\fn bool QTextFragment::isValid() const
Returns true if this is a valid text fragment (i.e. has a valid
position in a document); otherwise returns false.
*/
/*!
\fn bool QTextFragment::operator==(const QTextFragment &other) const
Returns true if this text fragment is the same (at the same
position) as the \a other text fragment; otherwise returns false.
*/
/*!
\fn bool QTextFragment::operator!=(const QTextFragment &other) const
Returns true if this text fragment is different (at a different
position) from the \a other text fragment; otherwise returns
false.
*/
/*!
\fn bool QTextFragment::operator<(const QTextFragment &other) const
Returns true if this text fragment appears earlier in the document
than the \a other text fragment; otherwise returns false.
*/
/*!
Returns the position of this text fragment in the document.
*/
int QTextFragment::position() const
{
if (!p || !n)
return 0; // ### -1 instead?
return p->fragmentMap().position(n);
}
/*!
Returns the number of characters in the text fragment.
\sa text()
*/
int QTextFragment::length() const
{
if (!p || !n)
return 0;
int len = 0;
int f = n;
while (f != ne) {
len += p->fragmentMap().size(f);
f = p->fragmentMap().next(f);
}
return len;
}
/*!
Returns true if the text fragment contains the text at the given
\a position in the document; otherwise returns false.
*/
bool QTextFragment::contains(int position) const
{
if (!p || !n)
return false;
int pos = this->position();
return position >= pos && position < pos + length();
}
/*!
Returns the text fragment's character format.
\sa text()
*/
QTextCharFormat QTextFragment::charFormat() const
{
if (!p || !n)
return QTextCharFormat();
const QTextFragmentData *data = p->fragmentMap().fragment(n);
return p->formatCollection()->charFormat(data->format);
}
/*!
Returns an index into the document's internal list of character formats
for the text fragment's character format.
\sa QTextDocument::allFormats()
*/
int QTextFragment::charFormatIndex() const
{
if (!p || !n)
return -1;
const QTextFragmentData *data = p->fragmentMap().fragment(n);
return data->format;
}
/*!
Returns the text fragment's as plain text.
\sa length(), charFormat()
*/
QString QTextFragment::text() const
{
if (!p || !n)
return QString();
QString result;
QString buffer = p->buffer();
int f = n;
while (f != ne) {
const QTextFragmentData * const frag = p->fragmentMap().fragment(f);
result += QString(buffer.constData() + frag->stringPosition, frag->size_array[0]);
f = p->fragmentMap().next(f);
}
return result;
}
QT_END_NAMESPACE
|