1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the text module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
32
#include <QtGui/qcolor.h>
33
#include <QtGui/qfont.h>
34
#include <QtCore/qshareddata.h>
35
#include <QtCore/qvector.h>
36
#include <QtCore/qvariant.h>
37
#include <QtGui/qpen.h>
38
#include <QtGui/qbrush.h>
44
class QTextFormatCollection;
45
class QTextFormatPrivate;
46
class QTextBlockFormat;
47
class QTextCharFormat;
48
class QTextListFormat;
49
class QTextTableFormat;
50
class QTextFrameFormat;
51
class QTextImageFormat;
58
Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTextLength &);
59
Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTextLength &);
61
class Q_GUI_EXPORT QTextLength
64
enum Type { VariableLength = 0, FixedLength, PercentageLength };
66
inline QTextLength() : lengthType(VariableLength), fixedValueOrPercentage(0) {}
68
inline explicit QTextLength(Type type, qreal value);
70
inline Type type() const { return lengthType; }
71
inline qreal value(qreal maximumLength) const
74
case FixedLength: return fixedValueOrPercentage;
75
case VariableLength: return maximumLength;
76
case PercentageLength: return fixedValueOrPercentage * maximumLength / qreal(100);
81
inline qreal rawValue() const { return fixedValueOrPercentage; }
83
inline bool operator==(const QTextLength &other) const
84
{ return lengthType == other.lengthType && fixedValueOrPercentage == other.fixedValueOrPercentage; }
85
inline bool operator!=(const QTextLength &other) const
86
{ return lengthType != other.lengthType || fixedValueOrPercentage != other.fixedValueOrPercentage; }
87
operator QVariant() const;
91
qreal fixedValueOrPercentage;
92
friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTextLength &);
93
friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTextLength &);
96
inline QTextLength::QTextLength(Type atype, qreal avalue)
97
: lengthType(atype), fixedValueOrPercentage(avalue) {}
99
Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTextFormat &);
100
Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTextFormat &);
102
class Q_GUI_EXPORT QTextFormat
119
// paragraph and char
121
LayoutDirection = 0x0801,
124
BackgroundBrush = 0x820,
125
ForegroundBrush = 0x821,
128
BlockAlignment = 0x1010,
129
BlockTopMargin = 0x1030,
130
BlockBottomMargin = 0x1031,
131
BlockLeftMargin = 0x1032,
132
BlockRightMargin = 0x1033,
134
BlockIndent = 0x1040,
135
BlockNonBreakableLines = 0x1050,
137
// character properties
139
FontPointSize = 0x2001,
140
FontSizeIncrement = 0x2002,
143
FontUnderline = 0x2005,
144
FontOverline = 0x2006,
145
FontStrikeOut = 0x2007,
146
FontFixedPitch = 0x2008,
148
TextUnderlineColor = 0x2010,
150
TextVerticalAlignment = 0x2021,
162
// table and frame properties
163
FrameBorder = 0x4000,
164
FrameMargin = 0x4001,
165
FramePadding = 0x4002,
167
FrameHeight = 0x4004,
168
TableColumns = 0x4100,
169
TableColumnWidthConstraints = 0x4101,
170
TableCellSpacing = 0x4102,
171
TableCellPadding = 0x4103,
173
// table cell properties
174
TableCellRowSpan = 0x4810,
175
TableCellColumnSpan = 0x4811,
180
ImageHeight = 0x5011,
182
// internal properties
183
DocumentFragmentMark = 0x6000,
186
UserProperty = 0x100000
197
explicit QTextFormat(int type);
199
QTextFormat(const QTextFormat &rhs);
200
QTextFormat &operator=(const QTextFormat &rhs);
203
void merge(const QTextFormat &other);
205
inline bool isValid() const { return type() != InvalidFormat; }
209
int objectIndex() const;
210
void setObjectIndex(int object);
212
QVariant property(int propertyId) const;
213
void setProperty(int propertyId, const QVariant &value);
214
void clearProperty(int propertyId);
215
bool hasProperty(int propertyId) const;
217
bool boolProperty(int propertyId) const;
218
int intProperty(int propertyId) const;
219
qreal doubleProperty(int propertyId) const;
220
QString stringProperty(int propertyId) const;
221
QColor colorProperty(int propertyId) const;
222
QPen penProperty(int propertyId) const;
223
QBrush brushProperty(int propertyId) const;
224
QTextLength lengthProperty(int propertyId) const;
225
QVector<QTextLength> lengthVectorProperty(int propertyId) const;
227
void setProperty(int propertyId, const QVector<QTextLength> &lengths);
230
QMap<int, QVariant> properties() const;
232
inline void setObjectType(int type);
233
inline int objectType() const
234
{ return intProperty(ObjectType); }
236
inline bool isCharFormat() const { return type() == CharFormat; }
237
inline bool isBlockFormat() const { return type() == BlockFormat; }
238
inline bool isListFormat() const { return type() == ListFormat; }
239
inline bool isFrameFormat() const { return type() == FrameFormat; }
240
inline bool isImageFormat() const { return type() == CharFormat && objectType() == ImageObject; }
241
inline bool isTableFormat() const { return type() == FrameFormat && objectType() == TableObject; }
243
QTextBlockFormat toBlockFormat() const;
244
QTextCharFormat toCharFormat() const;
245
QTextListFormat toListFormat() const;
246
QTextTableFormat toTableFormat() const;
247
QTextFrameFormat toFrameFormat() const;
248
QTextImageFormat toImageFormat() const;
250
bool operator==(const QTextFormat &rhs) const;
251
inline bool operator!=(const QTextFormat &rhs) const { return !operator==(rhs); }
252
operator QVariant() const;
254
inline void setLayoutDirection(Qt::LayoutDirection direction)
255
{ setProperty(QTextFormat::LayoutDirection, direction); }
256
inline Qt::LayoutDirection layoutDirection() const
257
{ return Qt::LayoutDirection(intProperty(QTextFormat::LayoutDirection)); }
259
inline void setBackground(const QBrush &brush)
260
{ setProperty(BackgroundBrush, brush); }
261
inline QBrush background() const
262
{ return brushProperty(BackgroundBrush); }
263
inline void clearBackground()
264
{ clearProperty(BackgroundBrush); }
266
inline void setForeground(const QBrush &brush)
267
{ setProperty(ForegroundBrush, brush); }
268
inline QBrush foreground() const
269
{ return brushProperty(ForegroundBrush); }
270
inline void clearForeground()
271
{ clearProperty(ForegroundBrush); }
274
QSharedDataPointer<QTextFormatPrivate> d;
277
friend class QTextFormatCollection;
278
friend class QTextCharFormat;
279
friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTextFormat &);
280
friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTextFormat &);
283
inline void QTextFormat::setObjectType(int atype)
284
{ setProperty(ObjectType, atype); }
286
class Q_GUI_EXPORT QTextCharFormat : public QTextFormat
289
enum VerticalAlignment { AlignNormal = 0, AlignSuperScript, AlignSubScript };
293
bool isValid() const { return isCharFormat(); }
294
void setFont(const QFont &font);
297
inline void setFontFamily(const QString &family)
298
{ setProperty(FontFamily, family); }
299
inline QString fontFamily() const
300
{ return stringProperty(FontFamily); }
302
inline void setFontPointSize(qreal size)
303
{ setProperty(FontPointSize, size); }
304
inline qreal fontPointSize() const
305
{ return doubleProperty(FontPointSize); }
307
inline void setFontWeight(int weight)
308
{ if (weight == QFont::Normal) weight = 0; setProperty(FontWeight, weight); }
309
inline int fontWeight() const
310
{ int weight = intProperty(FontWeight); if (weight == 0) weight = QFont::Normal; return weight; }
311
inline void setFontItalic(bool italic)
312
{ setProperty(FontItalic, italic); }
313
inline bool fontItalic() const
314
{ return boolProperty(FontItalic); }
316
inline void setFontUnderline(bool underline)
317
{ setProperty(FontUnderline, underline); }
318
inline bool fontUnderline() const
319
{ return boolProperty(FontUnderline); }
321
inline void setFontOverline(bool overline)
322
{ setProperty(FontOverline, overline); }
323
inline bool fontOverline() const
324
{ return boolProperty(FontOverline); }
326
inline void setFontStrikeOut(bool strikeOut)
327
{ setProperty(FontStrikeOut, strikeOut); }
328
inline bool fontStrikeOut() const
329
{ return boolProperty(FontStrikeOut); }
331
inline void setUnderlineColor(const QColor &color)
332
{ setProperty(TextUnderlineColor, color); }
333
inline QColor underlineColor() const
334
{ return colorProperty(TextUnderlineColor); }
336
inline void setFontFixedPitch(bool fixedPitch)
337
{ setProperty(FontFixedPitch, fixedPitch); }
338
inline bool fontFixedPitch() const
339
{ return boolProperty(FontFixedPitch); }
341
inline void setVerticalAlignment(VerticalAlignment alignment)
342
{ setProperty(TextVerticalAlignment, alignment); }
343
inline VerticalAlignment verticalAlignment() const
344
{ return static_cast<VerticalAlignment>(intProperty(TextVerticalAlignment)); }
346
inline void setAnchor(bool anchor)
347
{ setProperty(IsAnchor, anchor); }
348
inline bool isAnchor() const
349
{ return boolProperty(IsAnchor); }
351
inline void setAnchorHref(const QString &value)
352
{ setProperty(AnchorHref, value); }
353
inline QString anchorHref() const
354
{ return stringProperty(AnchorHref); }
356
inline void setAnchorName(const QString &name)
357
{ setProperty(AnchorName, name); }
358
inline QString anchorName() const
359
{ return stringProperty(AnchorName); }
361
inline void setTableCellRowSpan(int tableCellRowSpan);
362
inline int tableCellRowSpan() const
363
{ int s = intProperty(TableCellRowSpan); if (s == 0) s = 1; return s; }
364
inline void setTableCellColumnSpan(int tableCellColumnSpan);
365
inline int tableCellColumnSpan() const
366
{ int s = intProperty(TableCellColumnSpan); if (s == 0) s = 1; return s; }
369
inline void QTextCharFormat::setTableCellRowSpan(int atableCellRowSpan)
371
if (atableCellRowSpan == 1)
372
atableCellRowSpan = 0;
373
setProperty(TableCellRowSpan, atableCellRowSpan);
376
inline void QTextCharFormat::setTableCellColumnSpan(int atableCellColumnSpan)
378
if (atableCellColumnSpan == 1)
379
atableCellColumnSpan = 0;
380
setProperty(TableCellColumnSpan, atableCellColumnSpan);
383
class Q_GUI_EXPORT QTextBlockFormat : public QTextFormat
388
bool isValid() const { return isBlockFormat(); }
390
inline void setAlignment(Qt::Alignment alignment);
391
inline Qt::Alignment alignment() const
392
{ return QFlag(intProperty(BlockAlignment)); }
394
inline void setTopMargin(qreal margin)
395
{ setProperty(BlockTopMargin, margin); }
396
inline qreal topMargin() const
397
{ return doubleProperty(BlockTopMargin); }
399
inline void setBottomMargin(qreal margin)
400
{ setProperty(BlockBottomMargin, margin); }
401
inline qreal bottomMargin() const
402
{ return doubleProperty(BlockBottomMargin); }
404
inline void setLeftMargin(qreal margin)
405
{ setProperty(BlockLeftMargin, margin); }
406
inline qreal leftMargin() const
407
{ return doubleProperty(BlockLeftMargin); }
409
inline void setRightMargin(qreal margin)
410
{ setProperty(BlockRightMargin, margin); }
411
inline qreal rightMargin() const
412
{ return doubleProperty(BlockRightMargin); }
414
inline void setTextIndent(qreal margin)
415
{ setProperty(TextIndent, margin); }
416
inline qreal textIndent() const
417
{ return doubleProperty(TextIndent); }
419
inline void setIndent(int indent);
420
inline int indent() const
421
{ return intProperty(BlockIndent); }
423
inline void setNonBreakableLines(bool b)
424
{ setProperty(BlockNonBreakableLines, b); }
425
inline bool nonBreakableLines() const
426
{ return boolProperty(BlockNonBreakableLines); }
429
inline void QTextBlockFormat::setAlignment(Qt::Alignment aalignment)
430
{ setProperty(BlockAlignment, int(aalignment)); }
432
inline void QTextBlockFormat::setIndent(int aindent)
433
{ setProperty(BlockIndent, aindent); }
435
class Q_GUI_EXPORT QTextListFormat : public QTextFormat
440
bool isValid() const { return isListFormat(); }
449
ListStyleUndefined = 0
452
inline void setStyle(Style style);
453
inline Style style() const
454
{ return static_cast<Style>(intProperty(ListStyle)); }
456
inline void setIndent(int indent);
457
inline int indent() const
458
{ return intProperty(ListIndent); }
462
inline void QTextListFormat::setStyle(Style astyle)
463
{ setProperty(ListStyle, astyle); }
465
inline void QTextListFormat::setIndent(int aindent)
466
{ setProperty(ListIndent, aindent); }
468
class Q_GUI_EXPORT QTextImageFormat : public QTextCharFormat
473
bool isValid() const { return isImageFormat(); }
475
inline void setName(const QString &name);
476
inline QString name() const
477
{ return stringProperty(ImageName); }
479
inline void setWidth(qreal width);
480
inline qreal width() const
481
{ return doubleProperty(ImageWidth); }
483
inline void setHeight(qreal height);
484
inline qreal height() const
485
{ return doubleProperty(ImageHeight); }
488
inline void QTextImageFormat::setName(const QString &aname)
489
{ setProperty(ImageName, aname); }
491
inline void QTextImageFormat::setWidth(qreal awidth)
492
{ setProperty(ImageWidth, awidth); }
494
inline void QTextImageFormat::setHeight(qreal aheight)
495
{ setProperty(ImageHeight, aheight); }
497
class Q_GUI_EXPORT QTextFrameFormat : public QTextFormat
502
bool isValid() const { return isFrameFormat(); }
512
inline void setPosition(Position f)
513
{ setProperty(CssFloat, f); }
514
inline Position position() const
515
{ return static_cast<Position>(intProperty(CssFloat)); }
517
inline void setBorder(qreal border);
518
inline qreal border() const
519
{ return doubleProperty(FrameBorder); }
521
inline void setMargin(qreal margin);
522
inline qreal margin() const
523
{ return doubleProperty(FrameMargin); }
525
inline void setPadding(qreal padding);
526
inline qreal padding() const
527
{ return doubleProperty(FramePadding); }
529
inline void setWidth(qreal width);
530
inline void setWidth(const QTextLength &length)
531
{ setProperty(FrameWidth, length); }
532
inline QTextLength width() const
533
{ return lengthProperty(FrameWidth); }
535
inline void setHeight(qreal height);
536
inline void setHeight(const QTextLength &height);
537
inline QTextLength height() const
538
{ return lengthProperty(FrameHeight); }
541
inline void QTextFrameFormat::setBorder(qreal aborder)
542
{ setProperty(FrameBorder, aborder); }
544
inline void QTextFrameFormat::setMargin(qreal amargin)
545
{ setProperty(FrameMargin, amargin); }
547
inline void QTextFrameFormat::setPadding(qreal apadding)
548
{ setProperty(FramePadding, apadding); }
550
inline void QTextFrameFormat::setWidth(qreal awidth)
551
{ setProperty(FrameWidth, QTextLength(QTextLength::FixedLength, awidth)); }
553
inline void QTextFrameFormat::setHeight(qreal aheight)
554
{ setProperty(FrameHeight, QTextLength(QTextLength::FixedLength, aheight)); }
555
inline void QTextFrameFormat::setHeight(const QTextLength &aheight)
556
{ setProperty(FrameHeight, aheight); }
558
class Q_GUI_EXPORT QTextTableFormat : public QTextFrameFormat
563
inline bool isValid() const { return isTableFormat(); }
565
inline int columns() const
566
{ int cols = intProperty(TableColumns); if (cols == 0) cols = 1; return cols; }
567
inline void setColumns(int columns);
569
inline void setColumnWidthConstraints(const QVector<QTextLength> &constraints)
570
{ setProperty(TableColumnWidthConstraints, constraints); }
572
inline QVector<QTextLength> columnWidthConstraints() const
573
{ return lengthVectorProperty(TableColumnWidthConstraints); }
575
inline void clearColumnWidthConstraints()
576
{ clearProperty(TableColumnWidthConstraints); }
578
inline qreal cellSpacing() const
579
{ return doubleProperty(TableCellSpacing); }
580
inline void setCellSpacing(qreal spacing)
581
{ setProperty(TableCellSpacing, spacing); }
583
inline qreal cellPadding() const
584
{ return doubleProperty(TableCellPadding); }
585
inline void setCellPadding(qreal padding);
587
inline void setAlignment(Qt::Alignment alignment);
588
inline Qt::Alignment alignment() const
589
{ return QFlag(intProperty(BlockAlignment)); }
592
inline void QTextTableFormat::setColumns(int acolumns)
596
setProperty(TableColumns, acolumns);
599
inline void QTextTableFormat::setCellPadding(qreal apadding)
600
{ setProperty(TableCellPadding, apadding); }
602
inline void QTextTableFormat::setAlignment(Qt::Alignment aalignment)
603
{ setProperty(BlockAlignment, int(aalignment)); }
605
#endif // QTEXTFORMAT_H