Qt 4.8
qtextdocument_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QTEXTDOCUMENT_P_H
43 #define QTEXTDOCUMENT_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 "QtCore/qglobal.h"
57 #include "QtCore/qstring.h"
58 #include "QtCore/qvector.h"
59 #include "QtCore/qlist.h"
60 #include "private/qobject_p.h"
61 #include "private/qfragmentmap_p.h"
62 #include "QtGui/qtextlayout.h"
63 #include "QtGui/qtextoption.h"
64 #include "private/qtextformat_p.h"
65 #include "QtGui/qtextdocument.h"
66 #include "QtGui/qtextobject.h"
67 #include "QtGui/qtextcursor.h"
68 #include "QtCore/qmap.h"
69 #include "QtCore/qvariant.h"
70 #include "QtCore/qurl.h"
71 #include "private/qcssparser_p.h"
72 
73 // #define QT_QMAP_DEBUG
74 
75 #ifdef QT_QMAP_DEBUG
76 #include <iostream>
77 #endif
78 
80 
82 class QTextFormat;
83 class QTextBlockFormat;
84 class QTextCursorPrivate;
86 class QTextDocument;
87 class QTextFrame;
88 
89 #define QTextBeginningOfFrame QChar(0xfdd0)
90 #define QTextEndOfFrame QChar(0xfdd1)
91 
92 class QTextFragmentData : public QFragment<>
93 {
94 public:
95  inline void initialize() {}
96  inline void invalidate() const {}
97  inline void free() {}
99  int format;
100 };
101 
102 class QTextBlockData : public QFragment<3>
103 {
104 public:
105  inline void initialize()
106  { layout = 0; userData = 0; userState = -1; revision = 0; hidden = 0; }
107  void invalidate() const;
108  inline void free()
109  { delete layout; layout = 0; delete userData; userData = 0; }
110 
111  mutable int format;
112  // ##### probably store a QTextEngine * here!
113  mutable QTextLayout *layout;
115  mutable int userState;
116  mutable int revision : 31;
117  mutable uint hidden : 1;
118 };
119 
120 
121 class QAbstractUndoItem;
122 
124 {
125 public:
126  enum Command {
127  Inserted = 0,
128  Removed = 1,
129  CharFormatChanged = 2,
130  BlockFormatChanged = 3,
131  BlockInserted = 4,
132  BlockRemoved = 5,
133  BlockAdded = 6,
134  BlockDeleted = 7,
135  GroupFormatChange = 8,
136  CursorMoved = 9,
137  Custom = 256
138  };
139  enum Operation {
140  KeepCursor = 0,
141  MoveCursor = 1
142  };
144  uint block_part : 1; // all commands that are part of an undo block (including the first and the last one) have this set to 1
145  uint block_end : 1; // the last command in an undo block has this set to 1.
146  uint block_padding : 6; // padding since block used to be a quint8
148  int format;
151  union {
156  };
158 
159  bool tryMerge(const QTextUndoCommand &other);
160 };
162 
164 {
166 public:
168  typedef FragmentMap::ConstIterator FragmentIterator;
170 
173 
174  void init();
175  void clear();
176 
177  void setLayout(QAbstractTextDocumentLayout *layout);
178 
179  void insert(int pos, const QString &text, int format);
180  void insert(int pos, int strPos, int strLength, int format);
181  int insertBlock(int pos, int blockFormat, int charFormat, QTextUndoCommand::Operation = QTextUndoCommand::MoveCursor);
182  int insertBlock(const QChar &blockSeparator, int pos, int blockFormat, int charFormat,
184 
185  void move(int from, int to, int length, QTextUndoCommand::Operation = QTextUndoCommand::MoveCursor);
186  void remove(int pos, int length, QTextUndoCommand::Operation = QTextUndoCommand::MoveCursor);
187 
188  void aboutToRemoveCell(int cursorFrom, int cursorEnd);
189 
190  QTextFrame *insertFrame(int start, int end, const QTextFrameFormat &format);
191  void removeFrame(QTextFrame *frame);
192 
193  enum FormatChangeMode { MergeFormat, SetFormat, SetFormatAndPreserveObjectIndices };
194 
195  void setCharFormat(int pos, int length, const QTextCharFormat &newFormat, FormatChangeMode mode = SetFormat);
196  void setBlockFormat(const QTextBlock &from, const QTextBlock &to,
197  const QTextBlockFormat &newFormat, FormatChangeMode mode = SetFormat);
198 
199  void emitUndoAvailable(bool available);
200  void emitRedoAvailable(bool available);
201 
202  int undoRedo(bool undo);
203  inline void undo() { undoRedo(true); }
204  inline void redo() { undoRedo(false); }
205  void appendUndoItem(QAbstractUndoItem *);
206  inline void beginEditBlock() { if (0 == editBlock++) ++revision; }
207  void joinPreviousEditBlock();
208  void endEditBlock();
209  void finishEdit();
210  inline bool isInEditBlock() const { return editBlock; }
211  void enableUndoRedo(bool enable);
212  inline bool isUndoRedoEnabled() const { return undoEnabled; }
213 
214  inline bool isUndoAvailable() const { return undoEnabled && undoState > 0; }
215  inline bool isRedoAvailable() const { return undoEnabled && undoState < undoStack.size(); }
216 
217  inline int availableUndoSteps() const { return undoEnabled ? undoState : 0; }
218  inline int availableRedoSteps() const { return undoEnabled ? qMax(undoStack.size() - undoState - 1, 0) : 0; }
219 
220  inline QString buffer() const { return text; }
221  QString plainText() const;
222  inline int length() const { return fragments.length(); }
223 
224  inline QTextFormatCollection *formatCollection() { return &formats; }
225  inline const QTextFormatCollection *formatCollection() const { return &formats; }
226  inline QAbstractTextDocumentLayout *layout() const { return lout; }
227 
228  inline FragmentIterator find(int pos) const { return fragments.find(pos); }
229  inline FragmentIterator begin() const { return fragments.begin(); }
230  inline FragmentIterator end() const { return fragments.end(); }
231 
232  inline QTextBlock blocksBegin() const { return QTextBlock(const_cast<QTextDocumentPrivate *>(this), blocks.firstNode()); }
233  inline QTextBlock blocksEnd() const { return QTextBlock(const_cast<QTextDocumentPrivate *>(this), 0); }
234  inline QTextBlock blocksFind(int pos) const { return QTextBlock(const_cast<QTextDocumentPrivate *>(this), blocks.findNode(pos)); }
235  int blockCharFormatIndex(int node) const;
236 
237  inline int numBlocks() const { return blocks.numNodes(); }
238 
239  const BlockMap &blockMap() const { return blocks; }
240  const FragmentMap &fragmentMap() const { return fragments; }
241  BlockMap &blockMap() { return blocks; }
242  FragmentMap &fragmentMap() { return fragments; }
243 
244  static const QTextBlockData *block(const QTextBlock &it) { return it.p->blocks.fragment(it.n); }
245 
246  int nextCursorPosition(int position, QTextLayout::CursorMode mode) const;
247  int previousCursorPosition(int position, QTextLayout::CursorMode mode) const;
248  int leftCursorPosition(int position) const;
249  int rightCursorPosition(int position) const;
250 
251  void changeObjectFormat(QTextObject *group, int format);
252 
253  void setModified(bool m);
254  inline bool isModified() const { return modified; }
255 
256  inline QFont defaultFont() const { return formats.defaultFont(); }
257  inline void setDefaultFont(const QFont &f) { formats.setDefaultFont(f); }
258 
259  void clearUndoRedoStacks(QTextDocument::Stacks stacksToClear, bool emitSignals = false);
260 
261 private:
262  bool split(int pos);
263  bool unite(uint f);
264 
265  void insert_string(int pos, uint strPos, uint length, int format, QTextUndoCommand::Operation op);
266  int insert_block(int pos, uint strPos, int format, int blockformat, QTextUndoCommand::Operation op, int command);
267  int remove_string(int pos, uint length, QTextUndoCommand::Operation op);
268  int remove_block(int pos, int *blockformat, int command, QTextUndoCommand::Operation op);
269 
270  void insert_frame(QTextFrame *f);
271  void scan_frames(int pos, int charsRemoved, int charsAdded);
272  static void clearFrame(QTextFrame *f);
273 
274  void adjustDocumentChangesAndCursors(int from, int addedOrRemoved, QTextUndoCommand::Operation op);
275 
278 
279 public:
280  void documentChange(int from, int length);
281 
282  inline void addCursor(QTextCursorPrivate *c) { cursors.append(c); }
283  inline void removeCursor(QTextCursorPrivate *c) { cursors.removeAll(c); }
284 
285  QTextFrame *frameAt(int pos) const;
286  QTextFrame *rootFrame() const;
287 
288  QTextObject *objectForIndex(int objectIndex) const;
289  QTextObject *objectForFormat(int formatIndex) const;
290  QTextObject *objectForFormat(const QTextFormat &f) const;
291 
292  QTextObject *createObject(const QTextFormat &newFormat, int objectIndex = -1);
293  void deleteObject(QTextObject *object);
294 
295  QTextDocument *document() { return q_func(); }
296  const QTextDocument *document() const { return q_func(); }
297 
298  bool ensureMaximumBlockCount();
299 
300 private:
302  QTextDocumentPrivate& operator= (const QTextDocumentPrivate& m);
303 
304  void appendUndoItem(const QTextUndoCommand &c);
305 
306  void contentsChanged();
307 
308  void compressPieceTable();
309 
312 
316  int revision;
317  // position in undo stack of the last setModified(false) call
319  bool modified;
320 
327 
329  mutable QTextFrame *rtFrame;
331  FragmentMap fragments;
332  BlockMap blocks;
334 
340 
342 
343 public:
346 #ifndef QT_NO_CSSPARSER
348 #endif
358 
359  void mergeCachedResources(const QTextDocumentPrivate *priv);
360 
361  friend class QTextHtmlExporter;
362  friend class QTextCursor;
363 };
364 
365 class QTextTable;
367 {
368 public:
369  QTextHtmlExporter(const QTextDocument *_doc);
370 
371  enum ExportMode {
373  ExportFragment
374  };
375 
376  QString toHtml(const QByteArray &encoding, ExportMode mode = ExportEntireDocument);
377 
378 private:
379  enum StyleMode { EmitStyleTag, OmitStyleTag };
380  enum FrameType { TextFrame, TableFrame, RootFrame };
381 
382  void emitFrame(QTextFrame::Iterator frameIt);
383  void emitTextFrame(const QTextFrame *frame);
384  void emitBlock(const QTextBlock &block);
385  void emitTable(const QTextTable *table);
386  void emitFragment(const QTextFragment &fragment);
387 
388  void emitBlockAttributes(const QTextBlock &block);
389  bool emitCharFormatStyle(const QTextCharFormat &format);
390  void emitTextLength(const char *attribute, const QTextLength &length);
391  void emitAlignment(Qt::Alignment alignment);
392  void emitFloatStyle(QTextFrameFormat::Position pos, StyleMode mode = EmitStyleTag);
393  void emitMargins(const QString &top, const QString &bottom, const QString &left, const QString &right);
394  void emitAttribute(const char *attribute, const QString &value);
395  void emitFrameStyle(const QTextFrameFormat &format, FrameType frameType);
396  void emitBorderStyle(QTextFrameFormat::BorderStyle style);
397  void emitPageBreakPolicy(QTextFormat::PageBreakFlags policy);
398 
399  void emitFontFamily(const QString &family);
400 
401  void emitBackgroundAttribute(const QTextFormat &format);
402  QString findUrlForImage(const QTextDocument *doc, qint64 cacheKey, bool isPixmap);
403 
408 };
409 
411 
412 #endif // QTEXTDOCUMENT_P_H
QTextDocumentPrivate * p
Definition: qtextobject.h:287
QTextCharFormat defaultCharFormat
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
double qreal
Definition: qglobal.h:1193
unsigned char c[8]
Definition: qnumeric_p.h:62
quint32 left
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
FragmentIterator end() const
QTextFormatCollection formats
#define it(className, varName)
QAbstractTextDocumentLayout * lout
The QTextFrame class represents a frame in a QTextDocument.
Definition: qtextobject.h:122
Position
This enum describes how a frame is located relative to the surrounding text.
Definition: qtextformat.h:734
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QList< QTextCursorPrivate * > cursors
static qreal position(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
const QTextDocument * doc
Q_DECLARE_TYPEINFO(QTextUndoCommand, Q_PRIMITIVE_TYPE)
QFragmentMap< QTextFragmentData > FragmentMap
void setDefaultFont(const QFont &f)
unsigned char quint8
Definition: qglobal.h:934
QAbstractTextDocumentLayout * layout() const
bool isRedoAvailable() const
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QTextOption defaultTextOption
void addCursor(QTextCursorPrivate *c)
FragmentMap::ConstIterator FragmentIterator
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
bool isInEditBlock() const
void invalidate() const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
FragmentIterator find(int pos) const
QTextBlock blocksBegin() const
The QTextFormat class provides formatting information for a QTextDocument.
Definition: qtextformat.h:129
The QTextCursor class offers an API to access and modify QTextDocuments.
Definition: qtextcursor.h:70
static bool init
bool isUndoAvailable() const
QTextFormatCollection * formatCollection()
const char * layout
QAbstractUndoItem * custom
QMap< QUrl, QVariant > cachedResources
unsigned short quint16
Definition: qglobal.h:936
QTextLayout * layout
The QTextBlock class provides a container for text fragments in a QTextDocument.
Definition: qtextobject.h:199
unsigned int uint
Definition: qglobal.h:996
QCss::StyleSheet parsedDefaultStyleSheet
static void split(QT_FT_Vector *b)
static const QTextBlockData * block(const QTextBlock &it)
__int64 qint64
Definition: qglobal.h:942
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
const BlockMap & blockMap() const
bool isUndoRedoEnabled() const
The QTextFragment class holds a piece of text in a QTextDocument with a single QTextCharFormat.
Definition: qtextobject.h:297
const QTextDocument * document() const
QFont defaultFont() const
QFragmentMap< QTextBlockData > BlockMap
The iterator class provides an iterator for reading the contents of a QTextFrame. ...
Definition: qtextobject.h:144
The QTextBlockUserData class is used to associate custom data with blocks of text.
Definition: qtextobject.h:194
Fragment * fragment(uint index)
void removeCursor(QTextCursorPrivate *c)
QTextBlockUserData * userData
The QTextLayout class is used to lay out and render text.
Definition: qtextlayout.h:105
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
FragmentIterator begin() const
CursorMoveStyle
This enum describes the movement style available to text cursors.
Definition: qnamespace.h:1790
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
QString buffer() const
int availableRedoSteps() const
The QTextFrameFormat class provides formatting information for frames in a QTextDocument.
Definition: qtextformat.h:727
unsigned int quint32
Definition: qglobal.h:938
The QTextLength class encapsulates the different types of length used in a QTextDocument.
Definition: qtextformat.h:84
The QTextBlockFormat class provides formatting information for blocks of text in a QTextDocument...
Definition: qtextformat.h:545
static const QMetaObjectPrivate * priv(const uint *data)
#define Q_AUTOTEST_EXPORT
Definition: qglobal.h:1510
The QTextOption class provides a description of general rich text properties.
Definition: qtextoption.h:59
QVector< QTextUndoCommand > undoStack
The QTextDocument class holds formatted text that can be viewed and edited using a QTextEdit...
Qt::CursorMoveStyle defaultCursorMoveStyle
int availableUndoSteps() const
QTextBlock blocksFind(int pos) const
const QTextFormatCollection * formatCollection() const
The QTextObject class is a base class for different kinds of objects that can group parts of a QTextD...
Definition: qtextobject.h:64
QMap< QUrl, QVariant > resources
static const KeyPair *const end
const FragmentMap & fragmentMap() const
BorderStyle
This enum describes different border styles for the text frame.
Definition: qtextformat.h:742
quint32 right
QMap< int, QTextObject * > objects
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
#define text
Definition: qobjectdefs.h:80
QTextDocument * document()
FragmentMap & fragmentMap()
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
QTextBlock blocksEnd() const
The QAbstractTextDocumentLayout class is an abstract base class used to implement custom layouts for ...