source: trunk/src/gui/text/qtextformat.h@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 28.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 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#ifndef QTEXTFORMAT_H
43#define QTEXTFORMAT_H
44
45#include <QtGui/qcolor.h>
46#include <QtGui/qfont.h>
47#include <QtCore/qshareddata.h>
48#include <QtCore/qvector.h>
49#include <QtCore/qvariant.h>
50#include <QtGui/qpen.h>
51#include <QtGui/qbrush.h>
52#include <QtGui/qtextoption.h>
53
54QT_BEGIN_HEADER
55
56QT_BEGIN_NAMESPACE
57
58QT_MODULE(Gui)
59
60class QString;
61class QVariant;
62class QFont;
63
64class QTextFormatCollection;
65class QTextFormatPrivate;
66class QTextBlockFormat;
67class QTextCharFormat;
68class QTextListFormat;
69class QTextTableFormat;
70class QTextFrameFormat;
71class QTextImageFormat;
72class QTextTableCellFormat;
73class QTextFormat;
74class QTextObject;
75class QTextCursor;
76class QTextDocument;
77class QTextLength;
78
79#ifndef QT_NO_DATASTREAM
80Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTextLength &);
81Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTextLength &);
82#endif
83
84class Q_GUI_EXPORT QTextLength
85{
86public:
87 enum Type { VariableLength = 0, FixedLength, PercentageLength };
88
89 inline QTextLength() : lengthType(VariableLength), fixedValueOrPercentage(0) {}
90
91 inline explicit QTextLength(Type type, qreal value);
92
93 inline Type type() const { return lengthType; }
94 inline qreal value(qreal maximumLength) const
95 {
96 switch (lengthType) {
97 case FixedLength: return fixedValueOrPercentage;
98 case VariableLength: return maximumLength;
99 case PercentageLength: return fixedValueOrPercentage * maximumLength / qreal(100);
100 }
101 return -1;
102 }
103
104 inline qreal rawValue() const { return fixedValueOrPercentage; }
105
106 inline bool operator==(const QTextLength &other) const
107 { return lengthType == other.lengthType
108 && qFuzzyCompare(fixedValueOrPercentage, other.fixedValueOrPercentage); }
109 inline bool operator!=(const QTextLength &other) const
110 { return lengthType != other.lengthType
111 || !qFuzzyCompare(fixedValueOrPercentage, other.fixedValueOrPercentage); }
112 operator QVariant() const;
113
114private:
115 Type lengthType;
116 qreal fixedValueOrPercentage;
117 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTextLength &);
118 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTextLength &);
119};
120
121inline QTextLength::QTextLength(Type atype, qreal avalue)
122 : lengthType(atype), fixedValueOrPercentage(avalue) {}
123
124#ifndef QT_NO_DATASTREAM
125Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTextFormat &);
126Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTextFormat &);
127#endif
128
129class Q_GUI_EXPORT QTextFormat
130{
131 Q_GADGET
132 Q_ENUMS(FormatType Property ObjectTypes)
133public:
134 enum FormatType {
135 InvalidFormat = -1,
136 BlockFormat = 1,
137 CharFormat = 2,
138 ListFormat = 3,
139 TableFormat = 4,
140 FrameFormat = 5,
141
142 UserFormat = 100
143 };
144
145 enum Property {
146 ObjectIndex = 0x0,
147
148 // paragraph and char
149 CssFloat = 0x0800,
150 LayoutDirection = 0x0801,
151
152 OutlinePen = 0x810,
153 BackgroundBrush = 0x820,
154 ForegroundBrush = 0x821,
155 // Internal to qtextlayout.cpp: ObjectSelectionBrush = 0x822
156 BackgroundImageUrl = 0x823,
157
158 // paragraph
159 BlockAlignment = 0x1010,
160 BlockTopMargin = 0x1030,
161 BlockBottomMargin = 0x1031,
162 BlockLeftMargin = 0x1032,
163 BlockRightMargin = 0x1033,
164 TextIndent = 0x1034,
165 TabPositions = 0x1035,
166 BlockIndent = 0x1040,
167 BlockNonBreakableLines = 0x1050,
168 BlockTrailingHorizontalRulerWidth = 0x1060,
169
170 // character properties
171 FirstFontProperty = 0x1FE0,
172 FontCapitalization = FirstFontProperty,
173 FontLetterSpacing = 0x1FE1,
174 FontWordSpacing = 0x1FE2,
175 FontStyleHint = 0x1FE3,
176 FontStyleStrategy = 0x1FE4,
177 FontKerning = 0x1FE5,
178 FontFamily = 0x2000,
179 FontPointSize = 0x2001,
180 FontSizeAdjustment = 0x2002,
181 FontSizeIncrement = FontSizeAdjustment, // old name, compat
182 FontWeight = 0x2003,
183 FontItalic = 0x2004,
184 FontUnderline = 0x2005, // deprecated, use TextUnderlineStyle instead
185 FontOverline = 0x2006,
186 FontStrikeOut = 0x2007,
187 FontFixedPitch = 0x2008,
188 FontPixelSize = 0x2009,
189 LastFontProperty = FontPixelSize,
190
191 TextUnderlineColor = 0x2010,
192 TextVerticalAlignment = 0x2021,
193 TextOutline = 0x2022,
194 TextUnderlineStyle = 0x2023,
195 TextToolTip = 0x2024,
196
197 IsAnchor = 0x2030,
198 AnchorHref = 0x2031,
199 AnchorName = 0x2032,
200 ObjectType = 0x2f00,
201
202 // list properties
203 ListStyle = 0x3000,
204 ListIndent = 0x3001,
205
206 // table and frame properties
207 FrameBorder = 0x4000,
208 FrameMargin = 0x4001,
209 FramePadding = 0x4002,
210 FrameWidth = 0x4003,
211 FrameHeight = 0x4004,
212 FrameTopMargin = 0x4005,
213 FrameBottomMargin = 0x4006,
214 FrameLeftMargin = 0x4007,
215 FrameRightMargin = 0x4008,
216 FrameBorderBrush = 0x4009,
217 FrameBorderStyle = 0x4010,
218
219 TableColumns = 0x4100,
220 TableColumnWidthConstraints = 0x4101,
221 TableCellSpacing = 0x4102,
222 TableCellPadding = 0x4103,
223 TableHeaderRowCount = 0x4104,
224
225 // table cell properties
226 TableCellRowSpan = 0x4810,
227 TableCellColumnSpan = 0x4811,
228
229 TableCellTopPadding = 0x4812,
230 TableCellBottomPadding = 0x4813,
231 TableCellLeftPadding = 0x4814,
232 TableCellRightPadding = 0x4815,
233
234 // image properties
235 ImageName = 0x5000,
236 ImageWidth = 0x5010,
237 ImageHeight = 0x5011,
238
239 // internal
240 /*
241 SuppressText = 0x5012,
242 SuppressBackground = 0x513
243 */
244
245 // selection properties
246 FullWidthSelection = 0x06000,
247
248 // page break properties
249 PageBreakPolicy = 0x7000,
250
251 // --
252 UserProperty = 0x100000
253 };
254
255 enum ObjectTypes {
256 NoObject,
257 ImageObject,
258 TableObject,
259 TableCellObject,
260
261 UserObject = 0x1000
262 };
263
264 enum PageBreakFlag {
265 PageBreak_Auto = 0,
266 PageBreak_AlwaysBefore = 0x001,
267 PageBreak_AlwaysAfter = 0x010
268 // PageBreak_AlwaysInside = 0x100
269 };
270 Q_DECLARE_FLAGS(PageBreakFlags, PageBreakFlag)
271
272 QTextFormat();
273
274 explicit QTextFormat(int type);
275
276 QTextFormat(const QTextFormat &rhs);
277 QTextFormat &operator=(const QTextFormat &rhs);
278 ~QTextFormat();
279
280 void merge(const QTextFormat &other);
281
282 inline bool isValid() const { return type() != InvalidFormat; }
283
284 int type() const;
285
286 int objectIndex() const;
287 void setObjectIndex(int object);
288
289 QVariant property(int propertyId) const;
290 void setProperty(int propertyId, const QVariant &value);
291 void clearProperty(int propertyId);
292 bool hasProperty(int propertyId) const;
293
294 bool boolProperty(int propertyId) const;
295 int intProperty(int propertyId) const;
296 qreal doubleProperty(int propertyId) const;
297 QString stringProperty(int propertyId) const;
298 QColor colorProperty(int propertyId) const;
299 QPen penProperty(int propertyId) const;
300 QBrush brushProperty(int propertyId) const;
301 QTextLength lengthProperty(int propertyId) const;
302 QVector<QTextLength> lengthVectorProperty(int propertyId) const;
303
304 void setProperty(int propertyId, const QVector<QTextLength> &lengths);
305
306 QMap<int, QVariant> properties() const;
307 int propertyCount() const;
308
309 inline void setObjectType(int type);
310 inline int objectType() const
311 { return intProperty(ObjectType); }
312
313 inline bool isCharFormat() const { return type() == CharFormat; }
314 inline bool isBlockFormat() const { return type() == BlockFormat; }
315 inline bool isListFormat() const { return type() == ListFormat; }
316 inline bool isFrameFormat() const { return type() == FrameFormat; }
317 inline bool isImageFormat() const { return type() == CharFormat && objectType() == ImageObject; }
318 inline bool isTableFormat() const { return type() == FrameFormat && objectType() == TableObject; }
319 inline bool isTableCellFormat() const { return type() == CharFormat && objectType() == TableCellObject; }
320
321 QTextBlockFormat toBlockFormat() const;
322 QTextCharFormat toCharFormat() const;
323 QTextListFormat toListFormat() const;
324 QTextTableFormat toTableFormat() const;
325 QTextFrameFormat toFrameFormat() const;
326 QTextImageFormat toImageFormat() const;
327 QTextTableCellFormat toTableCellFormat() const;
328
329 bool operator==(const QTextFormat &rhs) const;
330 inline bool operator!=(const QTextFormat &rhs) const { return !operator==(rhs); }
331 operator QVariant() const;
332
333 inline void setLayoutDirection(Qt::LayoutDirection direction)
334 { setProperty(QTextFormat::LayoutDirection, direction); }
335 inline Qt::LayoutDirection layoutDirection() const
336 { return Qt::LayoutDirection(intProperty(QTextFormat::LayoutDirection)); }
337
338 inline void setBackground(const QBrush &brush)
339 { setProperty(BackgroundBrush, brush); }
340 inline QBrush background() const
341 { return brushProperty(BackgroundBrush); }
342 inline void clearBackground()
343 { clearProperty(BackgroundBrush); }
344
345 inline void setForeground(const QBrush &brush)
346 { setProperty(ForegroundBrush, brush); }
347 inline QBrush foreground() const
348 { return brushProperty(ForegroundBrush); }
349 inline void clearForeground()
350 { clearProperty(ForegroundBrush); }
351
352private:
353 QSharedDataPointer<QTextFormatPrivate> d;
354 qint32 format_type;
355
356 friend class QTextFormatCollection;
357 friend class QTextCharFormat;
358 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTextFormat &);
359 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTextFormat &);
360};
361
362inline void QTextFormat::setObjectType(int atype)
363{ setProperty(ObjectType, atype); }
364
365Q_DECLARE_OPERATORS_FOR_FLAGS(QTextFormat::PageBreakFlags)
366
367class Q_GUI_EXPORT QTextCharFormat : public QTextFormat
368{
369public:
370 enum VerticalAlignment {
371 AlignNormal = 0,
372 AlignSuperScript,
373 AlignSubScript,
374 AlignMiddle,
375 AlignTop,
376 AlignBottom
377 };
378 enum UnderlineStyle { // keep in sync with Qt::PenStyle!
379 NoUnderline,
380 SingleUnderline,
381 DashUnderline,
382 DotLine,
383 DashDotLine,
384 DashDotDotLine,
385 WaveUnderline,
386 SpellCheckUnderline
387 };
388
389 QTextCharFormat();
390
391 bool isValid() const { return isCharFormat(); }
392 void setFont(const QFont &font);
393 QFont font() const;
394
395 inline void setFontFamily(const QString &family)
396 { setProperty(FontFamily, family); }
397 inline QString fontFamily() const
398 { return stringProperty(FontFamily); }
399
400 inline void setFontPointSize(qreal size)
401 { setProperty(FontPointSize, size); }
402 inline qreal fontPointSize() const
403 { return doubleProperty(FontPointSize); }
404
405 inline void setFontWeight(int weight)
406 { if (weight == QFont::Normal) weight = 0; setProperty(FontWeight, weight); }
407 inline int fontWeight() const
408 { int weight = intProperty(FontWeight); if (weight == 0) weight = QFont::Normal; return weight; }
409 inline void setFontItalic(bool italic)
410 { setProperty(FontItalic, italic); }
411 inline bool fontItalic() const
412 { return boolProperty(FontItalic); }
413 inline void setFontCapitalization(QFont::Capitalization capitalization)
414 { setProperty(FontCapitalization, capitalization); }
415 inline QFont::Capitalization fontCapitalization() const
416 { return static_cast<QFont::Capitalization>(intProperty(FontCapitalization)); }
417 inline void setFontLetterSpacing(qreal spacing)
418 { setProperty(FontLetterSpacing, spacing); }
419 inline qreal fontLetterSpacing() const
420 { return doubleProperty(FontLetterSpacing); }
421 inline void setFontWordSpacing(qreal spacing)
422 { setProperty(FontWordSpacing, spacing); }
423 inline qreal fontWordSpacing() const
424 { return doubleProperty(FontWordSpacing); }
425
426 inline void setFontUnderline(bool underline)
427 { setProperty(TextUnderlineStyle, underline ? SingleUnderline : NoUnderline); }
428 bool fontUnderline() const;
429
430 inline void setFontOverline(bool overline)
431 { setProperty(FontOverline, overline); }
432 inline bool fontOverline() const
433 { return boolProperty(FontOverline); }
434
435 inline void setFontStrikeOut(bool strikeOut)
436 { setProperty(FontStrikeOut, strikeOut); }
437 inline bool fontStrikeOut() const
438 { return boolProperty(FontStrikeOut); }
439
440 inline void setUnderlineColor(const QColor &color)
441 { setProperty(TextUnderlineColor, color); }
442 inline QColor underlineColor() const
443 { return colorProperty(TextUnderlineColor); }
444
445 inline void setFontFixedPitch(bool fixedPitch)
446 { setProperty(FontFixedPitch, fixedPitch); }
447 inline bool fontFixedPitch() const
448 { return boolProperty(FontFixedPitch); }
449
450 inline void setFontStyleHint(QFont::StyleHint hint, QFont::StyleStrategy strategy = QFont::PreferDefault)
451 { setProperty(FontStyleHint, hint); setProperty(FontStyleStrategy, strategy); }
452 inline void setFontStyleStrategy(QFont::StyleStrategy strategy)
453 { setProperty(FontStyleStrategy, strategy); }
454 QFont::StyleHint fontStyleHint() const
455 { return static_cast<QFont::StyleHint>(intProperty(FontStyleHint)); }
456 QFont::StyleStrategy fontStyleStrategy() const
457 { return static_cast<QFont::StyleStrategy>(intProperty(FontStyleStrategy)); }
458
459 inline void setFontKerning(bool enable)
460 { setProperty(FontKerning, enable); }
461 inline bool fontKerning() const
462 { return boolProperty(FontKerning); }
463
464 void setUnderlineStyle(UnderlineStyle style);
465 inline UnderlineStyle underlineStyle() const
466 { return static_cast<UnderlineStyle>(intProperty(TextUnderlineStyle)); }
467
468 inline void setVerticalAlignment(VerticalAlignment alignment)
469 { setProperty(TextVerticalAlignment, alignment); }
470 inline VerticalAlignment verticalAlignment() const
471 { return static_cast<VerticalAlignment>(intProperty(TextVerticalAlignment)); }
472
473 inline void setTextOutline(const QPen &pen)
474 { setProperty(TextOutline, pen); }
475 inline QPen textOutline() const
476 { return penProperty(TextOutline); }
477
478 inline void setToolTip(const QString &tip)
479 { setProperty(TextToolTip, tip); }
480 inline QString toolTip() const
481 { return stringProperty(TextToolTip); }
482
483 inline void setAnchor(bool anchor)
484 { setProperty(IsAnchor, anchor); }
485 inline bool isAnchor() const
486 { return boolProperty(IsAnchor); }
487
488 inline void setAnchorHref(const QString &value)
489 { setProperty(AnchorHref, value); }
490 inline QString anchorHref() const
491 { return stringProperty(AnchorHref); }
492
493 inline void setAnchorName(const QString &name)
494 { setAnchorNames(QStringList(name)); }
495 QString anchorName() const;
496
497 inline void setAnchorNames(const QStringList &names)
498 { setProperty(AnchorName, names); }
499 QStringList anchorNames() const;
500
501 inline void setTableCellRowSpan(int tableCellRowSpan);
502 inline int tableCellRowSpan() const
503 { int s = intProperty(TableCellRowSpan); if (s == 0) s = 1; return s; }
504 inline void setTableCellColumnSpan(int tableCellColumnSpan);
505 inline int tableCellColumnSpan() const
506 { int s = intProperty(TableCellColumnSpan); if (s == 0) s = 1; return s; }
507
508protected:
509 explicit QTextCharFormat(const QTextFormat &fmt);
510 friend class QTextFormat;
511};
512
513inline void QTextCharFormat::setTableCellRowSpan(int _tableCellRowSpan)