source: trunk/src/gui/text/qtextdocumentfragment.cpp@ 698

Last change on this file since 698 was 651, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 41.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qtextdocumentfragment.h"
43#include "qtextdocumentfragment_p.h"
44#include "qtextcursor_p.h"
45#include "qtextlist.h"
46#include "private/qunicodetables_p.h"
47
48#include <qdebug.h>
49#include <qtextcodec.h>
50#include <qbytearray.h>
51#include <qdatastream.h>
52#include <qdatetime.h>
53
54QT_BEGIN_NAMESPACE
55
56QTextCopyHelper::QTextCopyHelper(const QTextCursor &_source, const QTextCursor &_destination, bool forceCharFormat, const QTextCharFormat &fmt)
57#if defined(Q_CC_DIAB) // compiler bug
58 : formatCollection(*_destination.d->priv->formatCollection()), originalText((const QString)_source.d->priv->buffer())
59#else
60 : formatCollection(*_destination.d->priv->formatCollection()), originalText(_source.d->priv->buffer())
61#endif
62{
63 src = _source.d->priv;
64 dst = _destination.d->priv;
65 insertPos = _destination.position();
66 this->forceCharFormat = forceCharFormat;
67 primaryCharFormatIndex = convertFormatIndex(fmt);
68 cursor = _source;
69}
70
71int QTextCopyHelper::convertFormatIndex(const QTextFormat &oldFormat, int objectIndexToSet)
72{
73 QTextFormat fmt = oldFormat;
74 if (objectIndexToSet != -1) {
75 fmt.setObjectIndex(objectIndexToSet);
76 } else if (fmt.objectIndex() != -1) {
77 int newObjectIndex = objectIndexMap.value(fmt.objectIndex(), -1);
78 if (newObjectIndex == -1) {
79 QTextFormat objFormat = src->formatCollection()->objectFormat(fmt.objectIndex());
80 Q_ASSERT(objFormat.objectIndex() == -1);
81 newObjectIndex = formatCollection.createObjectIndex(objFormat);
82 objectIndexMap.insert(fmt.objectIndex(), newObjectIndex);
83 }
84 fmt.setObjectIndex(newObjectIndex);
85 }
86 int idx = formatCollection.indexForFormat(fmt);
87 Q_ASSERT(formatCollection.format(idx).type() == oldFormat.type());
88 return idx;
89}
90
91int QTextCopyHelper::appendFragment(int pos, int endPos, int objectIndex)
92{
93 QTextDocumentPrivate::FragmentIterator fragIt = src->find(pos);
94 const QTextFragmentData * const frag = fragIt.value();
95
96 Q_ASSERT(objectIndex == -1
97 || (frag->size_array[0] == 1 && src->formatCollection()->format(frag->format).objectIndex() != -1));
98
99 int charFormatIndex;
100 if (forceCharFormat)
101 charFormatIndex = primaryCharFormatIndex;
102 else
103 charFormatIndex = convertFormatIndex(frag->format, objectIndex);
104
105 const int inFragmentOffset = qMax(0, pos - fragIt.position());
106 int charsToCopy = qMin(int(frag->size_array[0] - inFragmentOffset), endPos - pos);
107
108 QTextBlock nextBlock = src->blocksFind(pos + 1);
109
110 int blockIdx = -2;
111 if (nextBlock.position() == pos + 1) {
112 blockIdx = convertFormatIndex(nextBlock.blockFormat());
113 } else if (pos == 0 && insertPos == 0) {
114 dst->setBlockFormat(dst->blocksBegin(), dst->blocksBegin(), convertFormat(src->blocksBegin().blockFormat()).toBlockFormat());
115 dst->setCharFormat(-1, 1, convertFormat(src->blocksBegin().charFormat()).toCharFormat());
116 }
117
118 QString txtToInsert(originalText.constData() + frag->stringPosition + inFragmentOffset, charsToCopy);
119 if (txtToInsert.length() == 1
120 && (txtToInsert.at(0) == QChar::ParagraphSeparator
121 || txtToInsert.at(0) == QTextBeginningOfFrame
122 || txtToInsert.at(0) == QTextEndOfFrame
123 )
124 ) {
125 dst->insertBlock(txtToInsert.at(0), insertPos, blockIdx, charFormatIndex);
126 ++insertPos;
127 } else {
128 if (nextBlock.textList()) {
129 QTextBlock dstBlock = dst->blocksFind(insertPos);
130 if (!dstBlock.textList()) {
131 // insert a new text block with the block and char format from the
132 // source block to make sure that the following text fragments
133 // end up in a list as they should
134 int listBlockFormatIndex = convertFormatIndex(nextBlock.blockFormat());
135 int listCharFormatIndex = convertFormatIndex(nextBlock.charFormat());
136 dst->insertBlock(insertPos, listBlockFormatIndex, listCharFormatIndex);
137 ++insertPos;
138 }
139 }
140 dst->insert(insertPos, txtToInsert, charFormatIndex);
141 const int userState = nextBlock.userState();
142 if (userState != -1)
143 dst->blocksFind(insertPos).setUserState(userState);
144 insertPos += txtToInsert.length();
145 }
146
147 return charsToCopy;