1
/****************************************************************************
2
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
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
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#ifndef QTEXTDOCUMENTFRAGMENT_P_H
43
#define QTEXTDOCUMENTFRAGMENT_P_H
44
45
//
46
//  W A R N I N G
47
//  -------------
48
//
49
// This file is not part of the Qt API.  It exists purely as an
50
// implementation detail.  This header file may change from version to
51
// version without notice, or even be removed.
52
//
53
// We mean it.
54
//
55
56
#include "QtGui/qtextdocument.h"
57
#include "private/qtexthtmlparser_p.h"
58
#include "private/qtextdocument_p.h"
59
#include "QtGui/qtexttable.h"
60
#include "QtCore/qatomic.h"
61
#include "QtCore/qlist.h"
62
#include "QtCore/qmap.h"
63
#include "QtCore/qpointer.h"
64
#include "QtCore/qvarlengtharray.h"
65
#include "QtCore/qdatastream.h"
66
67
QT_BEGIN_NAMESPACE
68
69
class QTextDocumentFragmentPrivate;
70
71
class QTextCopyHelper
72
{
73
public:
74
    QTextCopyHelper(const QTextCursor &_source, const QTextCursor &_destination, bool forceCharFormat = false, const QTextCharFormat &fmt = QTextCharFormat());
75
76
    void copy();
77
78
private:
79
    void appendFragments(int pos, int endPos);
80
    int appendFragment(int pos, int endPos, int objectIndex = -1);
81
    int convertFormatIndex(const QTextFormat &oldFormat, int objectIndexToSet = -1);
82
    inline int convertFormatIndex(int oldFormatIndex, int objectIndexToSet = -1)
83
    { return convertFormatIndex(src->formatCollection()->format(oldFormatIndex), objectIndexToSet); }
84
    inline QTextFormat convertFormat(const QTextFormat &fmt)
85
    { return dst->formatCollection()->format(convertFormatIndex(fmt)); }
86
87
    int insertPos;
88
89
    bool forceCharFormat;
90
    int primaryCharFormatIndex;
91
92
    QTextCursor cursor;
93
    QTextDocumentPrivate *dst;
94
    QTextDocumentPrivate *src;
95
    QTextFormatCollection &formatCollection;
96
    const QString originalText;
97
    QMap<int, int> objectIndexMap;
98
};
99
100
class QTextDocumentFragmentPrivate
101
{
102
public:
103
    QTextDocumentFragmentPrivate(const QTextCursor &cursor = QTextCursor());
104
    inline ~QTextDocumentFragmentPrivate() { delete doc; }
105
106
    void insert(QTextCursor &cursor) const;
107
108
    QAtomicInt ref;
109
    QTextDocument *doc;
110
111
    uint importedFromPlainText : 1;
112
private:
113
    Q_DISABLE_COPY(QTextDocumentFragmentPrivate)
114
};
115
116
#ifndef QT_NO_TEXTHTMLPARSER
117
118
class QTextHtmlImporter : public QTextHtmlParser
119
{
120
    struct Table;
121
public:
122
    enum ImportMode {
123
        ImportToFragment,
124
        ImportToDocument
125
    };
126
127
    QTextHtmlImporter(QTextDocument *_doc, const QString &html,
128
                      ImportMode mode,
129
                      const QTextDocument *resourceProvider = 0);
130
131
    void import();
132
133
private:
134
    bool closeTag();
135
136
    Table scanTable(int tableNodeIdx);
137
138
    enum ProcessNodeResult { ContinueWithNextNode, ContinueWithCurrentNode, ContinueWithNextSibling };
139
140
    void appendBlock(const QTextBlockFormat &format, QTextCharFormat charFmt = QTextCharFormat());
141
    bool appendNodeText();
142
143
    ProcessNodeResult processBlockNode();
144
    ProcessNodeResult processSpecialNodes();
145
146
    struct List
147
    {
148
        inline List() : listNode(0) {}
149
        QTextListFormat format;
150
        int listNode;
151
        QPointer<QTextList> list;
152
    };
153
    QVector<List> lists;
154
    int indent;
155
156
    // insert a named anchor the next time we emit a char format,
157
    // either in a block or in regular text
158
    QStringList namedAnchors;
159
160
#ifdef Q_CC_SUN
161
    friend struct QTextHtmlImporter::Table;
162
#endif
163
    struct TableCellIterator
164
    {
165
        inline TableCellIterator(QTextTable *t = 0) : table(t), row(0), column(0) {}
166
167
        inline TableCellIterator &operator++() {
168
            if (atEnd())
169
                return *this;
170
            do {
171
                const QTextTableCell cell = table->cellAt(row, column);
172
                if (!cell.isValid())
173
                    break;
174
                column += cell.columnSpan();
175
                if (column >= table->columns()) {
176
                    column = 0;
177
                    ++row;
178
                }
179
            } while (row < table->rows() && table->cellAt(row, column).row() != row);
180
181
            return *this;
182
        }
183
184
        inline bool atEnd() const { return table == 0 || row >= table->rows(); }
185
186
        QTextTableCell cell() const { return table->cellAt(row, column); }
187
188
        QTextTable *table;
189
        int row;
190
        int column;
191
    };
192
193
    friend struct Table;
194
    struct Table
195
    {
196
        Table() : isTextFrame(false), rows(0), columns(0), currentRow(0), lastIndent(0) {}
197
        QPointer<QTextFrame> frame;
198
        bool isTextFrame;
199
        int rows;
200
        int columns;
201
        int currentRow; // ... for buggy html (see html_skipCell testcase)
202
        TableCellIterator currentCell;
203
        int lastIndent;
204
    };
205
    QVector<Table> tables;
206
207
    struct RowColSpanInfo
208
    {
209
        int row, col;
210
        int rowSpan, colSpan;
211
    };
212
213
    enum WhiteSpace
214
    {
215
        RemoveWhiteSpace,
216
        CollapseWhiteSpace,
217
        PreserveWhiteSpace
218
    };
219
220
    WhiteSpace compressNextWhitespace;
221
222
    QTextDocument *doc;
223
    QTextCursor cursor;
224
    QTextHtmlParserNode::WhiteSpaceMode wsm;
225
    ImportMode importMode;
226
    bool hasBlock;
227
    bool forceBlockMerging;
228
    bool blockTagClosed;
229
    int currentNodeIdx;
230
    const QTextHtmlParserNode *currentNode;
231
};
232
233
QT_END_NAMESPACE
234
#endif // QT_NO_TEXTHTMLPARSER
235
236
#endif // QTEXTDOCUMENTFRAGMENT_P_H