source: trunk/src/gui/text/qtextformat.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 81.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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** 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 qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qtextformat.h"
43#include "qtextformat_p.h"
44
45#include <qvariant.h>
46#include <qdatastream.h>
47#include <qdebug.h>
48#include <qmap.h>
49#include <qhash.h>
50
51QT_BEGIN_NAMESPACE
52
53/*!
54 \class QTextLength
55 \reentrant
56
57 \brief The QTextLength class encapsulates the different types of length
58 used in a QTextDocument.
59
60 \ingroup richtext-processing
61
62 When we specify a value for the length of an element in a text document,
63 we often need to provide some other information so that the length is
64 used in the way we expect. For example, when we specify a table width,
65 the value can represent a fixed number of pixels, or it can be a percentage
66 value. This information changes both the meaning of the value and the way
67 it is used.
68
69 Generally, this class is used to specify table widths. These can be
70 specified either as a fixed amount of pixels, as a percentage of the
71 containing frame's width, or by a variable width that allows it to take
72 up just the space it requires.
73
74 \sa QTextTable
75*/
76
77/*!
78 \fn explicit QTextLength::QTextLength()
79
80 Constructs a new length object which represents a variable size.
81*/
82
83/*!
84 \fn QTextLength::QTextLength(Type type, qreal value)
85
86 Constructs a new length object of the given \a type and \a value.
87*/
88
89/*!
90 \fn Type QTextLength::type() const
91
92 Returns the type of this length object.
93
94 \sa QTextLength::Type
95*/
96
97/*!
98 \fn qreal QTextLength::value(qreal maximumLength) const
99
100 Returns the effective length, constrained by the type of the length object
101 and the specified \a maximumLength.
102
103 \sa type()
104*/
105
106/*!
107 \fn qreal QTextLength::rawValue() const
108
109 Returns the constraint value that is specific for the type of the length.
110 If the length is QTextLength::PercentageLength then the raw value is in
111 percent, in the range of 0 to 100. If the length is QTextLength::FixedLength
112 then that fixed amount is returned. For variable lengths, zero is returned.
113*/
114
115/*!
116 \fn bool QTextLength::operator==(const QTextLength &other) const
117
118 Returns true if this text length is the same as the \a other text
119 length.
120*/
121
122/*!
123 \fn bool QTextLength::operator!=(const QTextLength &other) const
124
125 Returns true if this text length is different from the \a other text
126 length.
127*/
128
129/*!
130 \enum QTextLength::Type
131
132 This enum describes the different types a length object can
133 have.
134
135 \value VariableLength The width of the object is variable
136 \value FixedLength The width of the object is fixed
137 \value PercentageLength The width of the object is in
138 percentage of the maximum width
139
140 \sa type()
141*/
142
143/*!
144 Returns the text length as a QVariant
145*/
146QTextLength::operator QVariant() const
147{
148 return QVariant(QVariant::TextLength, this);
149}
150
151#ifndef QT_NO_DATASTREAM
152QDataStream &operator<<(QDataStream &stream, const QTextLength &length)
153{
154 return stream << qint32(length.lengthType) << double(length.fixedValueOrPercentage);
155}
156
157QDataStream &operator>>(QDataStream &stream, QTextLength &length)
158{
159 qint32 type;
160 double fixedValueOrPercentage;
161 stream >> type >> fixedValueOrPercentage;
162 length.fixedValueOrPercentage = fixedValueOrPercentage;
163 length.lengthType = QTextLength::Type(type);
164 return stream;
165}
166#endif // QT_NO_DATASTREAM
167
168class QTextFormatPrivate : public QSharedData
169{
170public:
171 QTextFormatPrivate() : hashDirty(true), fontDirty(true), hashValue(0) {}
172
173 struct Property
174 {
175 inline Property(qint32 k, const QVariant &v) : key(k), value(v) {}
176 inline Property() {}
177
178 qint32 key;
179 QVariant value;
180
181 inline bool operator==(const Property &other) const
182 { return key == other.key && value == other.value; }
183 inline bool operator!=(const Property &other) const
184 { return key != other.key || value != other.value; }
185 };
186
187 inline uint hash() const
188 {
189 if (!hashDirty)
190 return hashValue;
191 return recalcHash();
192 }
193
194 inline bool operator==(const QTextFormatPrivate &rhs) const {
195 if (hash() != rhs.hash())
196 return false;
197
198 return props == rhs.props;
199 }
200
201 inline void insertProperty(qint32 key, const QVariant &value)
202 {
203 hashDirty = true;
204 if (key >= QTextFormat::FirstFontProperty && key <= QTextFormat::LastFontProperty)
205 fontDirty = true;
206 for (int i = 0; i < props.count(); ++i)
207 if (props.at(i).key == key) {
208 props[i].value = value;
209 return;
210 }
211 props.append(Property(key, value));
212 }
213
214 inline void clearProperty(qint32 key)
215 {
216 for (int i = 0; i < props.count(); ++i)
217 if (props.at(i).key == key) {
218 hashDirty = true;
219 if (key >= QTextFormat::FirstFontProperty && key <= QTextFormat::LastFontProperty)
220 fontDirty = true;
221 props.remove(i);
222 return;
223 }
224 }
225
226 inline int propertyIndex(qint32 key) const
227 {
228 for (int i = 0; i < props.count(); ++i)
229 if (props.at(i).key == key)
230 return i;
231 return -1;
232 }
233
234 inline QVariant property(qint32 key) const
235 {
236 const int idx = propertyIndex(key);
237 if (idx < 0)
238 return QVariant();
239 return props.at(idx).value;
240 }
241
242 inline bool hasProperty(qint32 key) const
243 { return propertyIndex(key) != -1; }
244
245 void resolveFont(const QFont &defaultFont);
246
247 inline const QFont &font() const {
248 if (fontDirty)
249 recalcFont();
250 return fnt;
251 }
252
253 QVector<Property> props;
254private:
255
256 uint recalcHash() const;
257 void recalcFont() const;
258
259 mutable bool hashDirty;
260 mutable bool fontDirty;
261 mutable uint hashValue;
262 mutable QFont fnt;
263
264 friend QDataStream &operator<<(QDataStream &, const QTextFormat &);
265 friend QDataStream &operator>>(QDataStream &, QTextFormat &);
266};
267
268// this is only safe because sizeof(int) == sizeof(float)
269static inline uint hash(float d)
270{
271#ifdef Q_CC_GNU
272 // this is a GCC extension and isn't guaranteed to work in other compilers
273 // the reinterpret_cast below generates a strict-aliasing warning with GCC
274 union { float f; uint u; } cvt;
275 cvt.f = d;
276 return cvt.u;
277#else
278 return reinterpret_cast<uint&>(d);
279#endif
280}
281
282static inline uint hash(const QColor &color)
283{
284 return (color.isValid()) ? color.rgba() : 0x234109;
285}
286
287static inline uint hash(const QPen &pen)
288{
289 return hash(pen.color()) + hash(pen.widthF());
290}
291
292static inline uint hash(const QBrush &brush)
293{
294 return hash(brush.color()) + (brush.style() << 3);
295}
296
297static inline uint variantHash(const QVariant &variant)
298{
299 // simple and fast hash functions to differentiate between type and value
300 switch (variant.userType()) { // sorted by occurrence frequency
301 case QVariant::String: return qHash(variant.toString());
302 case QVariant::Double: return hash(variant.toDouble());
303 case QVariant::Int: return 0x811890 + variant.toInt();
304 case QVariant::Brush:
305 return 0x01010101 + hash(qvariant_cast<QBrush>(variant));
306 case QVariant::Bool: return 0x371818 + variant.toBool();
307 case QVariant::Pen: return 0x02020202 + hash(qvariant_cast<QPen>(variant));
308 case QVariant::List:
309 return 0x8377 + qvariant_cast<QVariantList>(variant).count();
310 case QVariant::Color: return hash(qvariant_cast<QColor>(variant));
311 case QVariant::TextLength:
312 return 0x377 + hash(qvariant_cast<QTextLength>(variant).rawValue());
313 case QMetaType::Float: return hash(variant.toFloat());
314 case QVariant::Invalid: return 0;
315 default: break;
316 }
317 return qHash(variant.typeName());
318}
319
320static inline int getHash(const QTextFormatPrivate *d, int format)
321{
322 return (d ? d->hash() : 0) + format;
323}
324
325uint QTextFormatPrivate::recalcHash() const
326{
327 hashValue = 0;
328 for (QVector<Property>::ConstIterator it = props.constBegin(); it != props.constEnd(); ++it)
329 hashValue += (it->key << 16) + variantHash(it->value);
330
331 hashDirty = false;
332
333 return hashValue;
334}
335
336void QTextFormatPrivate::resolveFont(const QFont &defaultFont)
337{
338 recalcFont();
339 const uint oldMask = fnt.resolve();
340 fnt = fnt.resolve(defaultFont);
341
342 if (hasProperty(QTextFormat::FontSizeAdjustment)) {
343 const qreal scaleFactors[7] = {qreal(0.7), qreal(0.8), qreal(1.0), qreal(1.2), qreal(1.5), qreal(2), qreal(2.4)};
344
345 const int htmlFontSize = qBound(0, property(QTextFormat::FontSizeAdjustment).toInt() + 3 - 1, 6);
346
347
348 if (defaultFont.pointSize() <= 0) {
349 qreal pixelSize = scaleFactors[htmlFontSize] * defaultFont.pixelSize();
350 fnt.setPixelSize(qRound(pixelSize));
351 } else {
352 qreal pointSize = scaleFactors[htmlFontSize] * defaultFont.pointSizeF();
353 fnt.setPointSizeF(pointSize);
354 }
355 }
356
357 fnt.resolve(oldMask);
358}
359
360void QTextFormatPrivate::recalcFont() const
361{
362 // update cached font as well
363 QFont f;
364
365 for (int i = 0; i < props.count(); ++i) {
366 switch (props.at(i).key) {
367 case QTextFormat::FontFamily:
368 f.setFamily(props.at(i).value.toString());
369 break;
370 case QTextFormat::FontPointSize:
371 f.setPointSizeF(props.at(i).value.toReal());
372 break;
373 case QTextFormat::FontPixelSize:
374 f.setPixelSize(props.at(i).value.toInt());
375 break;
376 case QTextFormat::FontWeight: {
377 int weight = props.at(i).value.toInt();
378 if (weight == 0) weight = QFont::Normal;
379 f.setWeight(weight);
380 break; }
381 case QTextFormat::FontItalic:
382 f.setItalic(props.at(i).value.toBool());
383 break;
384 case QTextFormat::FontUnderline:
385 if (! hasProperty(QTextFormat::TextUnderlineStyle)) // don't use the old one if the new one is there.
386 f.setUnderline(props.at(i).value.toBool());
387 break;
388 case QTextFormat::TextUnderlineStyle:
389 f.setUnderline(static_cast<QTextCharFormat::UnderlineStyle>(props.at(i).value.toInt()) == QTextCharFormat::SingleUnderline);
390 break;
391 case QTextFormat::FontOverline:
392 f.setOverline(props.at(i).value.toBool());
393 break;
394 case QTextFormat::FontStrikeOut:
395 f.setStrikeOut(props.at(i).value.toBool());
396 break;
397 case QTextFormat::FontLetterSpacing:
398 f.setLetterSpacing(QFont::PercentageSpacing, props.at(i).value.toReal());
399 break;
400 case QTextFormat::FontWordSpacing:
401 f.setWordSpacing(props.at(i).value.toReal());
402 break;
403 case QTextFormat::FontCapitalization:
404 f.setCapitalization(static_cast<QFont::Capitalization> (props.at(i).value.toInt()));
405 break;
406 case QTextFormat::FontFixedPitch: {
407 const bool value = props.at(i).value.toBool();
408 if (f.fixedPitch() != value)
409 f.setFixedPitch(value);
410 break; }
411 case QTextFormat::FontStyleHint:
412 f.setStyleHint(static_cast<QFont::StyleHint>(props.at(i).value.toInt()), f.styleStrategy());
413 break;
414 case QTextFormat::FontStyleStrategy:
415 f.setStyleStrategy(static_cast<QFont::StyleStrategy>(props.at(i).value.toInt()));
416 break;
417 case QTextFormat::FontKerning:
418 f.setKerning(props.at(i).value.toBool());
419 break;
420 default:
421 break;
422 }
423 }
424 fnt = f;
425 fontDirty = false;
426}
427
428#ifndef QT_NO_DATASTREAM
429Q_GUI_EXPORT QDataStream &operator<<(QDataStream &stream, const QTextFormat &fmt)
430{
431 stream << fmt.format_type << fmt.properties();
432 return stream;
433}
434
435Q_GUI_EXPORT QDataStream &operator>>(QDataStream &stream, QTextFormat &fmt)
436{
437 QMap<qint32, QVariant> properties;
438 stream >> fmt.format_type >> properties;
439
440 // QTextFormat's default constructor doesn't allocate the private structure, so
441 // we have to do this, in case fmt is a default constructed value.
442 if(!fmt.d)
443 fmt.d = new QTextFormatPrivate();
444
445 for (QMap<qint32, QVariant>::ConstIterator it = properties.constBegin();
446 it != properties.constEnd(); ++it)
447 fmt.d->insertProperty(it.key(), it.value());
448
449 return stream;
450}
451#endif // QT_NO_DATASTREAM
452
453/*!
454 \class QTextFormat
455 \reentrant
456
457 \brief The QTextFormat class provides formatting information for a
458 QTextDocument.
459
460 \ingroup richtext-processing
461 \ingroup shared
462
463 A QTextFormat is a generic class used for describing the format of
464 parts of a QTextDocument. The derived classes QTextCharFormat,
465 QTextBlockFormat, QTextListFormat, and QTextTableFormat are usually
466 more useful, and describe the formatting that is applied to
467 specific parts of the document.
468
469 A format has a \c FormatType which specifies the kinds of text item it
470 can format; e.g. a block of text, a list, a table, etc. A format
471 also has various properties (some specific to particular format
472 types), as described by the Property enum. Every property has a
473 corresponding Property.
474
475 The format type is given by type(), and the format can be tested
476 with isCharFormat(), isBlockFormat(), isListFormat(),
477 isTableFormat(), isFrameFormat(), and isImageFormat(). If the
478 type is determined, it can be retrieved with toCharFormat(),
479 toBlockFormat(), toListFormat(), toTableFormat(), toFrameFormat(),
480 and toImageFormat().
481
482 A format's properties can be set with the setProperty() functions,
483 and retrieved with boolProperty(), intProperty(), doubleProperty(),
484 and stringProperty() as appropriate. All the property IDs used in
485 the format can be retrieved with allPropertyIds(). One format can
486 be merged into another using merge().
487
488 A format's object index can be set with setObjectIndex(), and
489 retrieved with objectIndex(). These methods can be used to
490 associate the format with a QTextObject. It is used to represent
491 lists, frames, and tables inside the document.
492
493 \sa {Rich Text Processing}
494*/
495
496/*!
497 \enum QTextFormat::FormatType
498
499 This enum describes the text item a QTextFormat object is formatting.
500
501 \value InvalidFormat An invalid format as created by the default
502 constructor
503 \value BlockFormat The object formats a text block
504 \value CharFormat The object formats a single character
505 \value ListFormat The object formats a list
506 \value TableFormat The object formats a table
507 \value FrameFormat The object formats a frame
508
509 \value UserFormat
510
511 \sa QTextCharFormat, QTextBlockFormat, QTextListFormat,
512 QTextTableFormat, type()
513*/
514
515/*!
516 \enum QTextFormat::Property
517
518 This enum describes the different properties a format can have.
519
520 \value ObjectIndex The index of the formatted object. See objectIndex().
521
522 Paragraph and character properties
523
524 \value CssFloat How a frame is located relative to the surrounding text
525 \value LayoutDirection The layout direction of the text in the document
526 (Qt::LayoutDirection).
527
528 \value OutlinePen
529 \value ForegroundBrush
530 \value BackgroundBrush
531 \value BackgroundImageUrl
532
533 Paragraph properties
534
535 \value BlockAlignment
536 \value BlockTopMargin
537 \value BlockBottomMargin
538 \value BlockLeftMargin
539 \value BlockRightMargin
540 \value TextIndent
541 \value TabPositions Specifies the tab positions. The tab positions are structs of QTextOption::Tab which are stored in
542 a QList (internally, in a QList<QVariant>).
543 \value BlockIndent
544 \value BlockNonBreakableLines
545 \value BlockTrailingHorizontalRulerWidth The width of a horizontal ruler element.
546
547 Character properties
548
549 \value FontFamily
550 \value FontPointSize
551 \value FontPixelSize
552 \value FontSizeAdjustment Specifies the change in size given to the fontsize already set using
553 FontPointSize or FontPixelSize.
554 \value FontFixedPitch
555 \omitvalue FontSizeIncrement
556 \value FontWeight
557 \value FontItalic
558 \value FontUnderline \e{This property has been deprecated.} Use QTextFormat::TextUnderlineStyle instead.
559 \value FontOverline
560 \value FontStrikeOut
561 \value FontCapitalization Specifies the capitalization type that is to be applied to the text.
562 \value FontLetterSpacing Changes the default spacing between individual letters in the font. The value is
563 specified in percentage, with 100 as the default value.
564 \value FontWordSpacing Changes the default spacing between individual words. A positive value increases the word spacing
565 by the corresponding pixels; a negative value decreases the spacing.
566 \value FontStyleHint Corresponds to the QFont::StyleHint property
567 \value FontStyleStrategy Corresponds to the QFont::StyleStrategy property
568 \value FontKerning Specifies whether the font has kerning turned on.
569
570 \omitvalue FirstFontProperty
571 \omitvalue LastFontProperty
572
573 \value TextUnderlineColor
574 \value TextVerticalAlignment
575 \value TextOutline
576 \value TextUnderlineStyle
577 \value TextToolTip Specifies the (optional) tool tip to be displayed for a fragment of text.
578
579 \value IsAnchor
580 \value AnchorHref
581 \value AnchorName
582 \value ObjectType
583
584 List properties
585
586 \value ListStyle
587 \value ListIndent
588
589 Table and frame properties
590
591 \value FrameBorder
592 \value FrameBorderBrush
593 \value FrameBorderStyle See the \l{QTextFrameFormat::BorderStyle}{BorderStyle} enum.
594 \value FrameBottomMargin
595 \value FrameHeight
596 \value FrameLeftMargin
597 \value FrameMargin
598 \value FramePadding
599 \value FrameRightMargin
600 \value FrameTopMargin
601 \value FrameWidth
602 \value TableCellSpacing
603 \value TableCellPadding
604 \value TableColumns
605 \value TableColumnWidthConstraints
606 \value TableHeaderRowCount
607
608 Table cell properties
609
610 \value TableCellRowSpan
611 \value TableCellColumnSpan
612 \value TableCellLeftPadding
613 \value TableCellRightPadding
614 \value TableCellTopPadding
615 \value TableCellBottomPadding
616
617 Image properties
618
619 \value ImageName
620 \value ImageWidth
621 \value ImageHeight
622
623 Selection properties
624
625 \value FullWidthSelection When set on the characterFormat of a selection,
626 the whole width of the text will be shown selected.
627
628 Page break properties
629
630 \value PageBreakPolicy Specifies how pages are broken. See the PageBreakFlag enum.
631
632 \value UserProperty
633
634 \sa property(), setProperty()
635*/
636
637/*!
638 \enum QTextFormat::ObjectTypes
639
640 This enum describes what kind of QTextObject this format is associated with.
641
642 \value NoObject
643 \value ImageObject
644 \value TableObject
645 \value TableCellObject
646 \value UserObject The first object that can be used for application-specific purposes.
647
648 \sa QTextObject, QTextTable, QTextObject::format()
649*/
650
651/*!
652 \enum QTextFormat::PageBreakFlag
653 \since 4.2
654
655 This enum describes how page breaking is performed when printing. It maps to the
656 corresponding css properties.
657
658 \value PageBreak_Auto The page break is determined automatically depending on the
659 available space on the current page
660 \value PageBreak_AlwaysBefore The page is always broken before the paragraph/table
661 \value PageBreak_AlwaysAfter A new page is always started after the paragraph/table
662
663 \sa QTextBlockFormat::pageBreakPolicy(), QTextFrameFormat::pageBreakPolicy(),
664 PageBreakPolicy
665*/
666
667/*!
668 \fn bool QTextFormat::isValid() const
669
670 Returns true if the format is valid (i.e. is not
671 InvalidFormat); otherwise returns false.
672*/
673
674/*!
675 \fn bool QTextFormat::isCharFormat() const
676
677 Returns true if this text format is a \c CharFormat; otherwise
678 returns false.
679*/
680
681
682/*!
683 \fn bool QTextFormat::isBlockFormat() const
684
685 Returns true if this text format is a \c BlockFormat; otherwise
686 returns false.
687*/
688
689
690/*!
691 \fn bool QTextFormat::isListFormat() const
692
693 Returns true if this text format is a \c ListFormat; otherwise
694 returns false.
695*/
696
697
698/*!
699 \fn bool QTextFormat::isTableFormat() const
700
701 Returns true if this text format is a \c TableFormat; otherwise
702 returns false.
703*/
704
705
706/*!
707 \fn bool QTextFormat::isFrameFormat() const
708
709 Returns true if this text format is a \c FrameFormat; otherwise
710 returns false.
711*/
712
713
714/*!
715 \fn bool QTextFormat::isImageFormat() const
716
717 Returns true if this text format is an image format; otherwise
718 returns false.
719*/
720
721
722/*!
723 \fn bool QTextFormat::isTableCellFormat() const
724 \since 4.4
725
726 Returns true if this text format is a \c TableCellFormat; otherwise
727 returns false.
728*/
729
730
731/*!
732 Creates a new text format with an \c InvalidFormat.
733
734 \sa FormatType
735*/
736QTextFormat::QTextFormat()
737 : format_type(InvalidFormat)
738{
739}
740
741/*!
742 Creates a new text format of the given \a type.
743
744 \sa FormatType
745*/
746QTextFormat::QTextFormat(int type)
747 : format_type(type)
748{
749}
750
751
752/*!
753 \fn QTextFormat::QTextFormat(const QTextFormat &other)
754
755 Creates a new text format with the same attributes as the \a other
756 text format.
757*/
758QTextFormat::QTextFormat(const QTextFormat &rhs)
759 : d(rhs.d), format_type(rhs.format_type)
760{
761}
762
763/*!
764 \fn QTextFormat &QTextFormat::operator=(const QTextFormat &other)
765
766 Assigns the \a other text format to this text format, and returns a
767 reference to this text format.
768*/
769QTextFormat &QTextFormat::operator=(const QTextFormat &rhs)
770{
771 d = rhs.d;
772 format_type = rhs.format_type;
773 return *this;
774}
775
776/*!
777 Destroys this text format.
778*/
779QTextFormat::~QTextFormat()
780{
781}
782
783
784/*!
785 Returns the text format as a QVariant
786*/
787QTextFormat::operator QVariant() const
788{
789 return QVariant(QVariant::TextFormat, this);
790}
791
792/*!
793 Merges the \a other format with this format; where there are
794 conflicts the \a other format takes precedence.
795*/
796void QTextFormat::merge(const QTextFormat &other)
797{
798 if (format_type != other.format_type)
799 return;
800
801 if (!d) {
802 d = other.d;
803 return;
804 }
805
806 if (!other.d)
807 return;
808
809 QTextFormatPrivate *d = this->d;
810
811 const QVector<QTextFormatPrivate::Property> &otherProps = other.d->props;
812 d->props.reserve(d->props.size() + otherProps.size());
813 for (int i = 0; i < otherProps.count(); ++i) {
814 const QTextFormatPrivate::Property &p = otherProps.at(i);
815 d->insertProperty(p.key, p.value);
816 }
817}
818
819/*!
820 Returns the type of this format.
821
822 \sa FormatType
823*/
824int QTextFormat::type() const
825{
826 return format_type;
827}
828
829/*!
830 Returns this format as a block format.
831*/
832QTextBlockFormat QTextFormat::toBlockFormat() const
833{
834 return QTextBlockFormat(*this);
835}
836
837/*!
838 Returns this format as a character format.
839*/
840QTextCharFormat QTextFormat::toCharFormat() const
841{
842 return QTextCharFormat(*this);
843}
844
845/*!
846 Returns this format as a list format.
847*/
848QTextListFormat QTextFormat::toListFormat() const
849{
850 return QTextListFormat(*this);
851}
852
853/*!
854 Returns this format as a table format.
855*/
856QTextTableFormat QTextFormat::toTableFormat() const
857{
858 return QTextTableFormat(*this);
859}
860
861/*!
862 Returns this format as a frame format.
863*/
864QTextFrameFormat QTextFormat::toFrameFormat() const
865{
866 return QTextFrameFormat(*this);
867}
868
869/*!
870 Returns this format as an image format.
871*/
872QTextImageFormat QTextFormat::toImageFormat() const
873{
874 return QTextImageFormat(*this);
875}
876
877/*!
878 \since 4.4
879
880 Returns this format as a table cell format.
881*/
882QTextTableCellFormat QTextFormat::toTableCellFormat() const
883{
884 return QTextTableCellFormat(*this);
885}
886
887/*!
888 Returns the value of the property specified by \a propertyId. If the
889 property isn't of QTextFormat::Bool type, false is returned instead.
890
891 \sa setProperty() intProperty() doubleProperty() stringProperty() colorProperty() lengthProperty() lengthVectorProperty() Property
892*/
893bool QTextFormat::boolProperty(int propertyId) const
894{
895 if (!d)
896 return false;
897 const QVariant prop = d->property(propertyId);
898 if (prop.userType() != QVariant::Bool)
899 return false;
900 return prop.toBool();
901}
902
903/*!
904 Returns the value of the property specified by \a propertyId. If the
905 property is not of QTextFormat::Integer type, 0 is returned instead.
906
907 \sa setProperty() boolProperty() doubleProperty() stringProperty() colorProperty() lengthProperty() lengthVectorProperty() Property
908*/
909int QTextFormat::intProperty(int propertyId) const
910{
911 // required, since the default layout direction has to be LayoutDirectionAuto, which is not integer 0
912 int def = (propertyId == QTextFormat::LayoutDirection) ? int(Qt::LayoutDirectionAuto) : 0;
913
914 if (!d)
915 return def;
916 const QVariant prop = d->property(propertyId);
917 if (prop.userType() != QVariant::Int)
918 return def;
919 return prop.toInt();
920}
921
922/*!
923 Returns the value of the property specified by \a propertyId. If the
924 property isn't of QVariant::Double or QMetaType::Float type, 0 is
925 returned instead.
926
927 \sa setProperty() boolProperty() intProperty() stringProperty() colorProperty() lengthProperty() lengthVectorProperty() Property
928*/
929qreal QTextFormat::doubleProperty(int propertyId) const
930{
931 if (!d)
932 return 0.;
933 const QVariant prop = d->property(propertyId);
934 if (prop.userType() != QVariant::Double && prop.userType() != QMetaType::Float)
935 return 0.;
936 return qVariantValue<qreal>(prop);
937}
938
939/*!
940 Returns the value of the property given by \a propertyId; if the
941 property isn't of QVariant::String type, an empty string is
942 returned instead.
943
944 \sa setProperty() boolProperty() intProperty() doubleProperty() colorProperty() lengthProperty() lengthVectorProperty() Property
945*/
946QString QTextFormat::stringProperty(int propertyId) const
947{
948 if (!d)
949 return QString();
950 const QVariant prop = d->property(propertyId);
951 if (prop.userType() != QVariant::String)
952 return QString();
953 return prop.toString();
954}
955
956/*!
957 Returns the value of the property given by \a propertyId; if the
958 property isn't of QVariant::Color type, an invalid color is
959 returned instead.
960
961 \sa setProperty(), boolProperty(), intProperty(), doubleProperty(),
962 stringProperty(), lengthProperty(), lengthVectorProperty(), Property
963*/
964QColor QTextFormat::colorProperty(int propertyId) const
965{
966 if (!d)
967 return QColor();
968 const QVariant prop = d->property(propertyId);
969 if (prop.userType() != QVariant::Color)
970 return QColor();
971 return qvariant_cast<QColor>(prop);
972}
973
974/*!
975 Returns the value of the property given by \a propertyId; if the
976 property isn't of QVariant::Pen type, Qt::NoPen is
977 returned instead.
978
979 \sa setProperty() boolProperty() intProperty() doubleProperty() stringProperty() lengthProperty() lengthVectorProperty() Property
980*/
981QPen QTextFormat::penProperty(int propertyId) const
982{
983 if (!d)
984 return QPen(Qt::NoPen);
985 const QVariant prop = d->property(propertyId);
986 if (prop.userType() != QVariant::Pen)
987 return QPen(Qt::NoPen);
988 return qvariant_cast<QPen>(prop);
989}
990
991/*!
992 Returns the value of the property given by \a propertyId; if the
993 property isn't of QVariant::Brush type, Qt::NoBrush is
994 returned instead.
995
996 \sa setProperty() boolProperty() intProperty() doubleProperty() stringProperty() lengthProperty() lengthVectorProperty() Property
997*/
998QBrush QTextFormat::brushProperty(int propertyId) const
999{
1000 if (!d)
1001 return QBrush(Qt::NoBrush);
1002 const QVariant prop = d->property(propertyId);
1003 if (prop.userType() != QVariant::Brush)
1004 return QBrush(Qt::NoBrush);
1005 return qvariant_cast<QBrush>(prop);
1006}
1007
1008/*!
1009 Returns the value of the property given by \a propertyId.
1010
1011 \sa setProperty() boolProperty() intProperty() doubleProperty() stringProperty() colorProperty() lengthVectorProperty() Property
1012*/
1013QTextLength QTextFormat::lengthProperty(int propertyId) const
1014{
1015 if (!d)
1016 return QTextLength();
1017 return qvariant_cast<QTextLength>(d->property(propertyId));
1018}
1019
1020/*!
1021 Returns the value of the property given by \a propertyId. If the
1022 property isn't of QTextFormat::LengthVector type, an empty length
1023 vector is returned instead.
1024
1025 \sa setProperty() boolProperty() intProperty() doubleProperty() stringProperty() colorProperty() lengthProperty() Property
1026*/
1027QVector<QTextLength> QTextFormat::lengthVectorProperty(int propertyId) const
1028{
1029 QVector<QTextLength> vector;
1030 if (!d)
1031 return vector;
1032 const QVariant prop = d->property(propertyId);
1033 if (prop.userType() != QVariant::List)
1034 return vector;
1035
1036 QList<QVariant> propertyList = prop.toList();
1037 for (int i=0; i<propertyList.size(); ++i) {
1038 QVariant var = propertyList.at(i);
1039 if (var.userType() == QVariant::TextLength)
1040 vector.append(qvariant_cast<QTextLength>(var));
1041 }
1042
1043 return vector;
1044}
1045
1046/*!
1047 Returns the property specified by the given \a propertyId.
1048
1049 \sa Property
1050*/
1051QVariant QTextFormat::property(int propertyId) const
1052{
1053 return d ? d->property(propertyId) : QVariant();
1054}
1055
1056/*!
1057 Sets the property specified by the \a propertyId to the given \a value.
1058
1059 \sa Property
1060*/
1061void QTextFormat::setProperty(int propertyId, const QVariant &value)
1062{
1063 if (!d)
1064 d = new QTextFormatPrivate;
1065 if (!value.isValid())
1066 clearProperty(propertyId);
1067 else
1068 d->insertProperty(propertyId, value);
1069}
1070
1071/*!
1072 Sets the value of the property given by \a propertyId to \a value.
1073
1074 \sa lengthVectorProperty() Property
1075*/
1076void QTextFormat::setProperty(int propertyId, const QVector<QTextLength> &value)
1077{
1078 if (!d)
1079 d = new QTextFormatPrivate;
1080 QVariantList list;
1081 for (int i=0; i<value.size(); ++i)
1082 list << value.at(i);
1083 d->insertProperty(propertyId, list);
1084}
1085
1086/*!
1087 Clears the value of the property given by \a propertyId
1088
1089 \sa Property
1090*/
1091void QTextFormat::clearProperty(int propertyId)
1092{
1093 if (!d)
1094 return;
1095 d->clearProperty(propertyId);
1096}
1097
1098
1099/*!
1100 \fn void QTextFormat::setObjectType(int type)
1101
1102 Sets the text format's object type to \a type.
1103
1104 \sa ObjectTypes, objectType()
1105*/
1106
1107
1108/*!
1109 \fn int QTextFormat::objectType() const
1110
1111 Returns the text format's object type.
1112
1113 \sa ObjectTypes, setObjectType()
1114*/
1115
1116
1117/*!
1118 Returns the index of the format object, or -1 if the format object is invalid.
1119
1120 \sa setObjectIndex()
1121*/
1122int QTextFormat::objectIndex() const
1123{
1124 if (!d)
1125 return -1;
1126 const QVariant prop = d->property(ObjectIndex);
1127 if (prop.userType() != QVariant::Int) // ####
1128 return -1;
1129 return prop.toInt();
1130}
1131
1132/*!
1133 \fn void QTextFormat::setObjectIndex(int index)
1134
1135 Sets the format object's object \a index.
1136
1137 \sa objectIndex()
1138*/
1139void QTextFormat::setObjectIndex(int o)
1140{
1141 if (o == -1) {
1142 if (d)
1143 d->clearProperty(ObjectIndex);
1144 } else {
1145 if (!d)
1146 d = new QTextFormatPrivate;
1147 // ### type
1148 d->insertProperty(ObjectIndex, o);
1149 }
1150}
1151
1152/*!
1153 Returns true if the text format has a property with the given \a
1154 propertyId; otherwise returns false.
1155
1156 \sa properties() Property
1157*/
1158bool QTextFormat::hasProperty(int propertyId) const
1159{
1160 return d ? d->hasProperty(propertyId) : false;
1161}
1162
1163/*
1164 Returns the property type for the given \a propertyId.
1165
1166 \sa hasProperty() allPropertyIds() Property
1167*/
1168
1169/*!
1170 Returns a map with all properties of this text format.
1171*/
1172QMap<int, QVariant> QTextFormat::properties() const
1173{
1174 QMap<int, QVariant> map;
1175 if (d) {
1176 for (int i = 0; i < d->props.count(); ++i)
1177 map.insert(d->props.at(i).key, d->props.at(i).value);
1178 }
1179 return map;
1180}
1181
1182/*!
1183 \since 4.3
1184 Returns the number of properties stored in the format.
1185*/
1186int QTextFormat::propertyCount() const
1187{
1188 return d ? d->props.count() : 0;
1189}
1190
1191/*!
1192 \fn bool QTextFormat::operator!=(const QTextFormat &other) const
1193
1194 Returns true if this text format is different from the \a other text
1195 format.
1196*/
1197
1198
1199/*!
1200 \fn bool QTextFormat::operator==(const QTextFormat &other) const
1201
1202 Returns true if this text format is the same as the \a other text
1203 format.
1204*/
1205bool QTextFormat::operator==(const QTextFormat &rhs) const
1206{
1207 if (format_type != rhs.format_type)
1208 return false;
1209
1210 if (d == rhs.d)
1211 return true;
1212
1213 if (d && d->props.isEmpty() && !rhs.d)
1214 return true;
1215
1216 if (!d && rhs.d && rhs.d->props.isEmpty())
1217 return true;
1218
1219 if (!d || !rhs.d)
1220 return false;
1221
1222 return *d == *rhs.d;
1223}
1224
1225/*!
1226 \class QTextCharFormat
1227 \reentrant
1228
1229 \brief The QTextCharFormat class provides formatting information for
1230 characters in a QTextDocument.
1231
1232 \ingroup richtext-processing
1233
1234 The character format of text in a document specifies the visual properties
1235 of the text, as well as information about its role in a hypertext document.
1236
1237 The font used can be set by supplying a font to the setFont() function, and
1238 each aspect of its appearance can be adjusted to give the desired effect.
1239 setFontFamily() and setFontPointSize() define the font's family (e.g. Times)
1240 and printed size; setFontWeight() and setFontItalic() provide control over
1241 the style of the font. setFontUnderline(), setFontOverline(),
1242 setFontStrikeOut(), and setFontFixedPitch() provide additional effects for
1243 text.
1244
1245 The color is set with setForeground(). If the text is intended to be used
1246 as an anchor (for hyperlinks), this can be enabled with setAnchor(). The
1247 setAnchorHref() and setAnchorNames() functions are used to specify the
1248 information about the hyperlink's destination and the anchor's name.
1249
1250 \sa QTextFormat QTextBlockFormat QTextTableFormat QTextListFormat
1251*/
1252
1253/*!
1254 \enum QTextCharFormat::VerticalAlignment
1255
1256 This enum describes the ways that adjacent characters can be vertically
1257 aligned.
1258
1259 \value AlignNormal Adjacent characters are positioned in the standard
1260 way for text in the writing system in use.
1261 \value AlignSuperScript Characters are placed above the baseline for
1262 normal text.
1263 \value AlignSubScript Characters are placed below the baseline for
1264 normal text.
1265 \value AlignMiddle The center of the object is vertically aligned with the base line.
1266 Currently, this is only implemented for inline objects.
1267 \value AlignBottom The bottom edge of the object is vertically aligned with
1268 the base line.
1269 \value AlignTop The top edge of the object is vertically aligned with
1270 the base line.
1271*/
1272
1273/*!
1274 \enum QTextCharFormat::UnderlineStyle
1275
1276 This enum describes the different ways drawing underlined text.
1277
1278 \value NoUnderline Text is draw without any underlining decoration.
1279 \value SingleUnderline A line is drawn using Qt::SolidLine.
1280 \value DashUnderline Dashes are drawn using Qt::DashLine.
1281 \value DotLine Dots are drawn using Qt::DotLine;
1282 \value DashDotLine Dashs and dots are drawn using Qt::DashDotLine.
1283 \value DashDotDotLine Underlines draw drawn using Qt::DashDotDotLine.
1284 \value WaveUnderline The text is underlined using a wave shaped line.
1285 \value SpellCheckUnderline The underline is drawn depending on the QStyle::SH_SpellCeckUnderlineStyle
1286 style hint of the QApplication style. By default this is mapped to
1287 WaveUnderline, on Mac OS X it is mapped to DashDotLine.
1288
1289 \sa Qt::PenStyle
1290*/
1291
1292/*!
1293 \fn QTextCharFormat::QTextCharFormat()
1294
1295 Constructs a new character format object.
1296*/
1297QTextCharFormat::QTextCharFormat() : QTextFormat(CharFormat) {}
1298
1299/*!
1300 \internal
1301 \fn QTextCharFormat::QTextCharFormat(const QTextFormat &other)
1302
1303 Creates a new character format with the same attributes as the \a given
1304 text format.
1305*/
1306QTextCharFormat::QTextCharFormat(const QTextFormat &fmt)
1307 : QTextFormat(fmt)
1308{
1309}
1310
1311/*!
1312 \fn bool QTextCharFormat::isValid() const
1313
1314 Returns true if this character format is valid; otherwise returns
1315 false.
1316*/
1317
1318
1319/*!
1320 \fn void QTextCharFormat::setFontFamily(const QString &family)
1321
1322 Sets the text format's font \a family.
1323
1324 \sa setFont()
1325*/
1326
1327
1328/*!
1329 \fn QString QTextCharFormat::fontFamily() const
1330
1331 Returns the text format's font family.
1332
1333 \sa font()
1334*/
1335
1336
1337/*!
1338 \fn void QTextCharFormat::setFontPointSize(qreal size)
1339
1340 Sets the text format's font \a size.
1341
1342 \sa setFont()
1343*/
1344
1345
1346/*!
1347 \fn qreal QTextCharFormat::fontPointSize() const
1348
1349 Returns the font size used to display text in this format.
1350
1351 \sa font()
1352*/
1353
1354
1355/*!
1356 \fn void QTextCharFormat::setFontWeight(int weight)
1357
1358 Sets the text format's font weight to \a weight.
1359
1360 \sa setFont(), QFont::Weight
1361*/
1362
1363
1364/*!
1365 \fn int QTextCharFormat::fontWeight() const
1366
1367 Returns the text format's font weight.
1368
1369 \sa font(), QFont::Weight
1370*/
1371
1372
1373/*!
1374 \fn void QTextCharFormat::setFontItalic(bool italic)
1375
1376 If \a italic is true, sets the text format's font to be italic; otherwise
1377 the font will be non-italic.
1378
1379 \sa setFont()
1380*/
1381
1382
1383/*!
1384 \fn bool QTextCharFormat::fontItalic() const
1385
1386 Returns true if the text format's font is italic; otherwise
1387 returns false.
1388
1389 \sa font()
1390*/
1391
1392
1393/*!
1394 \fn void QTextCharFormat::setFontUnderline(bool underline)
1395
1396 If \a underline is true, sets the text format's font to be underlined;
1397 otherwise it is displayed non-underlined.
1398
1399 \sa setFont()
1400*/
1401
1402
1403/*!
1404 \fn bool QTextCharFormat::fontUnderline() const
1405
1406 Returns true if the text format's font is underlined; otherwise
1407 returns false.
1408
1409 \sa font()
1410*/
1411bool QTextCharFormat::fontUnderline() const
1412{
1413 if (hasProperty(TextUnderlineStyle))
1414 return underlineStyle() == SingleUnderline;
1415 return boolProperty(FontUnderline);
1416}
1417
1418/*!
1419 \fn UnderlineStyle QTextCharFormat::underlineStyle() const
1420 \since 4.2
1421
1422 Returns the style of underlining the text.
1423*/
1424
1425/*!
1426 \fn void QTextCharFormat::setUnderlineStyle(UnderlineStyle style)
1427 \since 4.2
1428
1429 Sets the style of underlining the text to \a style.
1430*/
1431void QTextCharFormat::setUnderlineStyle(UnderlineStyle style)
1432{
1433 setProperty(TextUnderlineStyle, style);
1434 // for compatibility
1435 setProperty(FontUnderline, style == SingleUnderline);
1436}
1437
1438/*!
1439 \fn void QTextCharFormat::setFontOverline(bool overline)
1440
1441 If \a overline is true, sets the text format's font to be overlined;
1442 otherwise the font is displayed non-overlined.
1443
1444 \sa setFont()
1445*/
1446
1447
1448/*!
1449 \fn bool QTextCharFormat::fontOverline() const
1450
1451 Returns true if the text format's font is overlined; otherwise
1452 returns false.
1453
1454 \sa font()
1455*/
1456
1457
1458/*!
1459 \fn void QTextCharFormat::setFontStrikeOut(bool strikeOut)
1460
1461 If \a strikeOut is true, sets the text format's font with strike-out
1462 enabled (with a horizontal line through it); otherwise it is displayed
1463 without strikeout.
1464
1465 \sa setFont()
1466*/
1467
1468
1469/*!
1470 \fn bool QTextCharFormat::fontStrikeOut() const
1471
1472 Returns true if the text format's font is struck out (has a horizontal line
1473 drawn through it); otherwise returns false.
1474
1475 \sa font()
1476*/
1477
1478
1479/*!
1480 \since 4.5
1481 \fn void QTextCharFormat::setFontStyleHint(QFont::StyleHint hint, QFont::StyleStrategy strategy)
1482
1483 Sets the font style \a hint and \a strategy.
1484
1485 Qt does not support style hints on X11 since this information is not provided by the window system.
1486
1487 \sa setFont()
1488 \sa QFont::setStyleHint()
1489*/
1490
1491
1492/*!
1493 \since 4.5
1494 \fn void QTextCharFormat::setFontStyleStrategy(QFont::StyleStrategy strategy)
1495
1496 Sets the font style \a strategy.
1497
1498 \sa setFont()
1499 \sa QFont::setStyleStrategy()
1500*/
1501
1502
1503/*!
1504 \since 4.5
1505 \fn void QTextCharFormat::setFontKerning(bool enable)
1506 Enables kerning for this font if \a enable is true; otherwise disables it.
1507
1508 When kerning is enabled, glyph metrics do not add up anymore, even for
1509 Latin text. In other words, the assumption that width('a') + width('b')
1510 is equal to width("ab") is not neccesairly true.
1511
1512 \sa setFont()
1513*/
1514
1515
1516/*!
1517 \fn QTextCharFormat::StyleHint QTextCharFormat::fontStyleHint() const
1518 \since 4.5
1519
1520 Returns the font style hint.
1521
1522 \sa setFontStyleHint(), font()
1523*/
1524
1525
1526/*!
1527 \since 4.5
1528 \fn QTextCharFormat::StyleStrategy QTextCharFormat::fontStyleStrategy() const
1529
1530 Returns the current font style strategy.
1531
1532 \sa setFontStyleStrategy()
1533 \sa font()
1534*/
1535
1536
1537/*!
1538 \since 4.5
1539 \fn bool QTextCharFormat::fontKerning() const
1540 Returns true if the font kerning is enabled.
1541
1542 \sa setFontKerning()
1543 \sa font()
1544*/
1545
1546
1547/*!
1548 \fn void QTextCharFormat::setFontFixedPitch(bool fixedPitch)
1549
1550 If \a fixedPitch is true, sets the text format's font to be fixed pitch;
1551 otherwise a non-fixed pitch font is used.
1552
1553 \sa setFont()
1554*/
1555
1556
1557/*!
1558 \fn bool QTextCharFormat::fontFixedPitch() const
1559
1560 Returns true if the text format's font is fixed pitch; otherwise
1561 returns false.
1562
1563 \sa font()
1564*/
1565
1566
1567/*!
1568 \fn QPen QTextCharFormat::textOutline() const
1569
1570 Returns the pen used to draw the outlines of characters in this format.
1571*/
1572
1573
1574/*!
1575 \fn void QTextCharFormat::setTextOutline(const QPen &pen)
1576
1577 Sets the pen used to draw the outlines of characters to the given \a pen.
1578*/
1579
1580/*!
1581 \fn void QTextCharFormat::setToolTip(const QString &text)
1582 \since 4.3
1583
1584 Sets the tool tip for a fragment of text to the given \a text.
1585*/
1586
1587/*!
1588 \fn QString QTextCharFormat::toolTip() const
1589 \since 4.3
1590
1591 Returns the tool tip that is displayed for a fragment of text.
1592*/
1593
1594/*!
1595 \fn void QTextFormat::setForeground(const QBrush &brush)
1596
1597 Sets the foreground brush to the specified \a brush. The foreground
1598 brush is mostly used to render text.
1599
1600 \sa foreground() clearForeground() setBackground()
1601*/
1602
1603
1604/*!
1605 \fn QBrush QTextFormat::foreground() const
1606
1607 Returns the brush used to render foreground details, such as text,
1608 frame outlines, and table borders.
1609
1610 \sa setForeground() clearForeground() background()
1611*/
1612
1613/*!
1614 \fn void QTextFormat::clearForeground()
1615
1616 Clears the brush used to paint the document's foreground. The default
1617 brush will be used.
1618
1619 \sa foreground() setForeground() clearBackground()
1620*/
1621
1622
1623/*!
1624 \fn void QTextCharFormat::setAnchor(bool anchor)
1625
1626 If \a anchor is true, text with this format represents an anchor, and is
1627 formatted in the appropriate way; otherwise the text is formatted normally.
1628 (Anchors are hyperlinks which are often shown underlined and in a different
1629 color from plain text.)
1630
1631 The way the text is rendered is independent of whether or not the format
1632 has a valid anchor defined. Use setAnchorHref(), and optionally
1633 setAnchorNames() to create a hypertext link.
1634
1635 \sa isAnchor()
1636*/
1637
1638
1639/*!
1640 \fn bool QTextCharFormat::isAnchor() const
1641
1642 Returns true if the text is formatted as an anchor; otherwise
1643 returns false.
1644
1645 \sa setAnchor() setAnchorHref() setAnchorNames()
1646*/
1647
1648
1649/*!
1650 \fn void QTextCharFormat::setAnchorHref(const QString &value)
1651
1652 Sets the hypertext link for the text format to the given \a value.
1653 This is typically a URL like "http://example.com/index.html".
1654
1655 The anchor will be displayed with the \a value as its display text;
1656 if you want to display different text call setAnchorNames().
1657
1658 To format the text as a hypertext link use setAnchor().
1659*/
1660
1661
1662/*!
1663 \fn QString QTextCharFormat::anchorHref() const
1664
1665 Returns the text format's hypertext link, or an empty string if
1666 none has been set.
1667*/
1668
1669
1670/*!
1671 \fn void QTextCharFormat::setAnchorName(const QString &name)
1672 \obsolete
1673
1674 This function is deprecated. Use setAnchorNames() instead.
1675
1676 Sets the text format's anchor \a name. For the anchor to work as a
1677 hyperlink, the destination must be set with setAnchorHref() and
1678 the anchor must be enabled with setAnchor().
1679*/
1680
1681/*!
1682 \fn void QTextCharFormat::setAnchorNames(const QStringList &names)
1683 \since 4.3
1684
1685 Sets the text format's anchor \a names. For the anchor to work as a
1686 hyperlink, the destination must be set with setAnchorHref() and
1687 the anchor must be enabled with setAnchor().
1688*/
1689
1690/*!
1691 \fn QString QTextCharFormat::anchorName() const
1692 \obsolete
1693
1694 This function is deprecated. Use anchorNames() instead.
1695
1696 Returns the anchor name associated with this text format, or an empty
1697 string if none has been set. If the anchor name is set, text with this
1698 format can be the destination of a hypertext link.
1699*/
1700QString QTextCharFormat::anchorName() const
1701{
1702 QVariant prop = property(AnchorName);
1703 if (prop.userType() == QVariant::StringList)
1704 return prop.toStringList().value(0);
1705 else if (prop.userType() != QVariant::String)
1706 return QString();
1707 return prop.toString();
1708}
1709
1710/*!
1711 \fn QStringList QTextCharFormat::anchorNames() const
1712 \since 4.3
1713
1714 Returns the anchor names associated with this text format, or an empty
1715 string list if none has been set. If the anchor names are set, text with this
1716 format can be the destination of a hypertext link.
1717*/
1718QStringList QTextCharFormat::anchorNames() const
1719{
1720 QVariant prop = property(AnchorName);
1721 if (prop.userType() == QVariant::StringList)
1722 return prop.toStringList();
1723 else if (prop.userType() != QVariant::String)
1724 return QStringList();
1725 return QStringList(prop.toString());
1726}
1727
1728
1729/*!
1730 \fn void QTextCharFormat::setTableCellRowSpan(int tableCellRowSpan)
1731 \internal
1732
1733 If this character format is applied to characters in a table cell,
1734 the cell will span \a tableCellRowSpan rows.
1735*/
1736
1737
1738/*!
1739 \fn int QTextCharFormat::tableCellRowSpan() const
1740 \internal
1741
1742 If this character format is applied to characters in a table cell,
1743 this function returns the number of rows spanned by the text (this may
1744 be 1); otherwise it returns 1.
1745*/
1746
1747/*!
1748 \fn void QTextCharFormat::setTableCellColumnSpan(int tableCellColumnSpan)
1749 \internal
1750
1751 If this character format is applied to characters in a table cell,
1752 the cell will span \a tableCellColumnSpan columns.
1753*/
1754
1755
1756/*!
1757 \fn int QTextCharFormat::tableCellColumnSpan() const
1758 \internal
1759
1760 If this character format is applied to characters in a table cell,
1761 this function returns the number of columns spanned by the text (this
1762 may be 1); otherwise it returns 1.
1763*/
1764
1765/*!
1766 \fn void QTextCharFormat::setUnderlineColor(const QColor &color)
1767
1768 Sets the underline color used for the characters with this format to
1769 the \a color specified.
1770
1771 \sa underlineColor()
1772*/
1773
1774/*!
1775 \fn QColor QTextCharFormat::underlineColor() const
1776
1777 Returns the color used to underline the characters with this format.
1778
1779 \sa setUnderlineColor()
1780*/
1781
1782/*!
1783 \fn void QTextCharFormat::setVerticalAlignment(VerticalAlignment alignment)
1784
1785 Sets the vertical alignment used for the characters with this format to
1786 the \a alignment specified.
1787
1788 \sa verticalAlignment()
1789*/
1790
1791/*!
1792 \fn VerticalAlignment QTextCharFormat::verticalAlignment() const
1793
1794 Returns the vertical alignment used for characters with this format.
1795
1796 \sa setVerticalAlignment()
1797*/
1798
1799/*!
1800 Sets the text format's \a font.
1801*/
1802void QTextCharFormat::setFont(const QFont &font)
1803{
1804 setFontFamily(font.family());
1805
1806 const qreal pointSize = font.pointSizeF();
1807 if (pointSize > 0) {
1808 setFontPointSize(pointSize);
1809 } else {
1810 const int pixelSize = font.pixelSize();
1811 if (pixelSize > 0)
1812 setProperty(QTextFormat::FontPixelSize, pixelSize);
1813 }
1814
1815 setFontWeight(font.weight());
1816 setFontItalic(font.italic());
1817 setUnderlineStyle(font.underline() ? SingleUnderline : NoUnderline);
1818 setFontOverline(font.overline());
1819 setFontStrikeOut(font.strikeOut());
1820 setFontFixedPitch(font.fixedPitch());
1821 setFontCapitalization(font.capitalization());
1822 setFontWordSpacing(font.wordSpacing());
1823 if (font.letterSpacingType() == QFont::PercentageSpacing)
1824 setFontLetterSpacing(font.letterSpacing());
1825 setFontStyleHint(font.styleHint());
1826 setFontStyleStrategy(font.styleStrategy());
1827 setFontKerning(font.kerning());
1828}
1829
1830/*!
1831 Returns the font for this character format.
1832*/
1833QFont QTextCharFormat::font() const
1834{
1835 return d ? d->font() : QFont();
1836}
1837
1838/*!
1839 \class QTextBlockFormat
1840 \reentrant
1841
1842 \brief The QTextBlockFormat class provides formatting information for
1843 blocks of text in a QTextDocument.
1844
1845 \ingroup richtext-processing
1846
1847 A document is composed of a list of blocks, represented by QTextBlock
1848 objects. Each block can contain an item of some kind, such as a
1849 paragraph of text, a table, a list, or an image. Every block has an
1850 associated QTextBlockFormat that specifies its characteristics.
1851
1852 To cater for left-to-right and right-to-left languages you can set
1853 a block's direction with setDirection(). Paragraph alignment is
1854 set with setAlignment(). Margins are controlled by setTopMargin(),
1855 setBottomMargin(), setLeftMargin(), setRightMargin(). Overall
1856 indentation is set with setIndent(), the indentation of the first
1857 line with setTextIndent().
1858
1859 Line breaking can be enabled and disabled with setNonBreakableLines().
1860
1861 The brush used to paint the paragraph's background
1862 is set with \l{QTextFormat::setBackground()}{setBackground()}, and other
1863 aspects of the text's appearance can be customized by using the
1864 \l{QTextFormat::setProperty()}{setProperty()} function with the
1865 \c OutlinePen, \c ForegroundBrush, and \c BackgroundBrush
1866 \l{QTextFormat::Property} values.
1867
1868 If a text block is part of a list, it can also have a list format that
1869 is accessible with the listFormat() function.
1870
1871 \sa QTextBlock, QTextCharFormat
1872*/
1873
1874/*!
1875 \fn QTextBlockFormat::QTextBlockFormat()
1876
1877 Constructs a new QTextBlockFormat.
1878*/
1879QTextBlockFormat::QTextBlockFormat() : QTextFormat(BlockFormat) {}
1880
1881/*!
1882 \internal
1883 \fn QTextBlockFormat::QTextBlockFormat(const QTextFormat &other)
1884
1885 Creates a new block format with the same attributes as the \a given
1886 text format.
1887*/
1888QTextBlockFormat::QTextBlockFormat(const QTextFormat &fmt)
1889 : QTextFormat(fmt)
1890{
1891}
1892
1893/*!
1894 \since 4.4
1895 Sets the tab positions for the text block to those specified by
1896 \a tabs.
1897
1898 \sa tabPositions()
1899*/
1900void QTextBlockFormat::setTabPositions(const QList<QTextOption::Tab> &tabs)
1901{
1902 QList<QVariant> list;
1903 QList<QTextOption::Tab>::ConstIterator iter = tabs.constBegin();
1904 while (iter != tabs.constEnd()) {
1905 QVariant v;
1906 qVariantSetValue<QTextOption::Tab>(v, *iter);
1907 list.append(v);
1908 ++iter;
1909 }
1910 setProperty(TabPositions, list);
1911}
1912
1913/*!
1914 \since 4.4
1915 Returns a list of tab positions defined for the text block.
1916
1917 \sa setTabPositions()
1918*/
1919QList<QTextOption::Tab> QTextBlockFormat::tabPositions() const
1920{
1921 QVariant variant = property(TabPositions);
1922 if(variant.isNull())
1923 return QList<QTextOption::Tab>();
1924 QList<QTextOption::Tab> answer;
1925 QList<QVariant> variantsList = qvariant_cast<QList<QVariant> >(variant);
1926 QList<QVariant>::Iterator iter = variantsList.begin();
1927 while(iter != variantsList.end()) {
1928 answer.append( qVariantValue<QTextOption::Tab>(*iter));
1929 ++iter;
1930 }
1931 return answer;
1932}
1933
1934/*!
1935 \fn QTextBlockFormat::isValid() const
1936
1937 Returns true if this block format is valid; otherwise returns
1938 false.
1939*/
1940
1941/*!
1942 \fn void QTextFormat::setLayoutDirection(Qt::LayoutDirection direction)
1943
1944 Sets the document's layout direction to the specified \a direction.
1945
1946 \sa layoutDirection()
1947*/
1948
1949
1950/*!
1951 \fn Qt::LayoutDirection QTextFormat::layoutDirection() const
1952
1953 Returns the document's layout direction.
1954
1955 \sa setLayoutDirection()
1956*/
1957
1958
1959/*!
1960 \fn void QTextBlockFormat::setAlignment(Qt::Alignment alignment)
1961
1962 Sets the paragraph's \a alignment.
1963
1964 \sa alignment()
1965*/
1966
1967
1968/*!
1969 \fn Qt::Alignment QTextBlockFormat::alignment() const
1970
1971 Returns the paragraph's alignment.
1972
1973 \sa setAlignment()
1974*/
1975
1976
1977/*!
1978 \fn void QTextBlockFormat::setTopMargin(qreal margin)
1979
1980 Sets the paragraph's top \a margin.
1981
1982 \sa topMargin() setBottomMargin() setLeftMargin() setRightMargin()
1983*/
1984
1985
1986/*!
1987 \fn qreal QTextBlockFormat::topMargin() const
1988
1989 Returns the paragraph's top margin.
1990
1991 \sa setTopMargin() bottomMargin()
1992*/
1993
1994
1995/*!
1996 \fn void QTextBlockFormat::setBottomMargin(qreal margin)
1997
1998 Sets the paragraph's bottom \a margin.
1999
2000 \sa bottomMargin() setTopMargin() setLeftMargin() setRightMargin()
2001*/
2002
2003
2004/*!
2005 \fn qreal QTextBlockFormat::bottomMargin() const
2006
2007 Returns the paragraph's bottom margin.
2008
2009 \sa setBottomMargin() topMargin()
2010*/
2011
2012
2013/*!
2014 \fn void QTextBlockFormat::setLeftMargin(qreal margin)
2015
2016 Sets the paragraph's left \a margin. Indentation can be applied separately
2017 with setIndent().
2018
2019 \sa leftMargin() setRightMargin() setTopMargin() setBottomMargin()
2020*/
2021
2022
2023/*!
2024 \fn qreal QTextBlockFormat::leftMargin() const
2025
2026 Returns the paragraph's left margin.
2027
2028 \sa setLeftMargin() rightMargin() indent()
2029*/
2030
2031
2032/*!
2033 \fn void QTextBlockFormat::setRightMargin(qreal margin)
2034
2035 Sets the paragraph's right \a margin.
2036
2037 \sa rightMargin() setLeftMargin() setTopMargin() setBottomMargin()
2038*/
2039
2040
2041/*!
2042 \fn qreal QTextBlockFormat::rightMargin() const
2043
2044 Returns the paragraph's right margin.
2045
2046 \sa setRightMargin() leftMargin()
2047*/
2048
2049
2050/*!
2051 \fn void QTextBlockFormat::setTextIndent(qreal indent)
2052
2053 Sets the \a indent for the first line in the block. This allows the first
2054 line of a paragraph to be indented differently to the other lines,
2055 enhancing the readability of the text.
2056
2057 \sa textIndent() setLeftMargin() setRightMargin() setTopMargin() setBottomMargin()
2058*/
2059
2060
2061/*!
2062 \fn qreal QTextBlockFormat::textIndent() const
2063
2064 Returns the paragraph's text indent.
2065
2066 \sa setTextIndent()
2067*/
2068
2069
2070/*!
2071 \fn void QTextBlockFormat::setIndent(int indentation)
2072
2073 Sets the paragraph's \a indentation. Margins are set independently of
2074 indentation with setLeftMargin() and setTextIndent().
2075 The \a indentation is an integer that is multiplied with the document-wide
2076 standard indent, resulting in the actual indent of the paragraph.
2077
2078 \sa indent() QTextDocument::indentWidth()
2079*/
2080
2081
2082/*!
2083 \fn int QTextBlockFormat::indent() const
2084
2085 Returns the paragraph's indent.
2086
2087 \sa setIndent()
2088*/
2089
2090
2091/*!
2092 \fn void QTextBlockFormat::setNonBreakableLines(bool b)
2093
2094 If \a b is true, the lines in the paragraph are treated as
2095 non-breakable; otherwise they are breakable.
2096
2097 \sa nonBreakableLines()
2098*/
2099
2100
2101/*!
2102 \fn bool QTextBlockFormat::nonBreakableLines() const
2103
2104 Returns true if the lines in the paragraph are non-breakable;
2105 otherwise returns false.
2106
2107 \sa setNonBreakableLines()
2108*/
2109
2110/*!
2111 \fn QTextFormat::PageBreakFlags QTextBlockFormat::pageBreakPolicy() const
2112 \since 4.2
2113
2114 Returns the currently set page break policy for the paragraph. The default is
2115 QTextFormat::PageBreak_Auto.
2116
2117 \sa setPageBreakPolicy()
2118*/
2119
2120/*!
2121 \fn void QTextBlockFormat::setPageBreakPolicy(PageBreakFlags policy)
2122 \since 4.2
2123
2124 Sets the page break policy for the paragraph to \a policy.
2125
2126 \sa pageBreakPolicy()
2127*/
2128
2129/*!
2130 \class QTextListFormat
2131 \reentrant
2132
2133 \brief The QTextListFormat class provides formatting information for
2134 lists in a QTextDocument.
2135
2136 \ingroup richtext-processing
2137
2138 A list is composed of one or more items, represented as text blocks.
2139 The list's format specifies the appearance of items in the list.
2140 In particular, it determines the indentation and the style of each item.
2141
2142 The indentation of the items is an integer value that causes each item to
2143 be offset from the left margin by a certain amount. This value is read with
2144 indent() and set with setIndent().
2145
2146 The style used to decorate each item is set with setStyle() and can be read
2147 with the style() function. The style controls the type of bullet points and
2148 numbering scheme used for items in the list. Note that lists that use the
2149 decimal numbering scheme begin counting at 1 rather than 0.
2150
2151 \sa QTextList
2152*/
2153
2154/*!
2155 \enum QTextListFormat::Style
2156
2157 This enum describes the symbols used to decorate list items:
2158
2159 \value ListDisc a filled circle
2160 \value ListCircle an empty circle
2161 \value ListSquare a filled square
2162 \value ListDecimal decimal values in ascending order
2163 \value ListLowerAlpha lower case Latin characters in alphabetical order
2164 \value ListUpperAlpha upper case Latin characters in alphabetical order
2165 \value ListLowerRoman lower case roman numerals (supports up to 4999 items only)
2166 \value ListUpperRoman upper case roman numerals (supports up to 4999 items only)
2167 \omitvalue ListStyleUndefined
2168*/
2169
2170/*!
2171 \fn QTextListFormat::QTextListFormat()
2172
2173 Constructs a new list format object.
2174*/
2175QTextListFormat::QTextListFormat()
2176 : QTextFormat(ListFormat)
2177{
2178 setIndent(1);
2179}
2180
2181/*!
2182 \internal
2183 \fn QTextListFormat::QTextListFormat(const QTextFormat &other)
2184
2185 Creates a new list format with the same attributes as the \a given
2186 text format.
2187*/
2188QTextListFormat::QTextListFormat(const QTextFormat &fmt)
2189 : QTextFormat(fmt)
2190{
2191}
2192
2193/*!
2194 \fn bool QTextListFormat::isValid() const
2195
2196 Returns true if this list format is valid; otherwise
2197 returns false.
2198*/
2199
2200/*!
2201 \fn void QTextListFormat::setStyle(Style style)
2202
2203 Sets the list format's \a style.
2204
2205 \sa style() Style
2206*/
2207
2208/*!
2209 \fn Style QTextListFormat::style() const
2210
2211 Returns the list format's style.
2212
2213 \sa setStyle() Style
2214*/
2215
2216
2217/*!
2218 \fn void QTextListFormat::setIndent(int indentation)
2219
2220 Sets the list format's \a indentation.
2221 The indentation is multiplied by the QTextDocument::indentWidth
2222 property to get the effective indent in pixels.
2223
2224 \sa indent()
2225*/
2226
2227
2228/*!
2229 \fn int QTextListFormat::indent() const
2230
2231 Returns the list format's indentation.
2232 The indentation is multiplied by the QTextDocument::indentWidth
2233 property to get the effective indent in pixels.
2234
2235 \sa setIndent()
2236*/
2237
2238
2239/*!
2240 \class QTextFrameFormat
2241 \reentrant
2242
2243 \brief The QTextFrameFormat class provides formatting information for
2244 frames in a QTextDocument.
2245
2246 \ingroup richtext-processing
2247
2248 A text frame groups together one or more blocks of text, providing a layer
2249 of structure larger than the paragraph. The format of a frame specifies
2250 how it is rendered and positioned on the screen. It does not directly
2251 specify the behavior of the text formatting within, but provides
2252 constraints on the layout of its children.
2253
2254 The frame format defines the width() and height() of the frame on the
2255 screen. Each frame can have a border() that surrounds its contents with
2256 a rectangular box. The border is surrounded by a margin() around the frame,
2257 and the contents of the frame are kept separate from the border by the
2258 frame's padding(). This scheme is similar to the box model used by Cascading
2259 Style Sheets for HTML pages.
2260
2261 \img qtextframe-style.png
2262
2263 The position() of a frame is set using setPosition() and determines how it
2264 is located relative to the surrounding text.
2265
2266 The validity of a QTextFrameFormat object can be determined with the
2267 isValid() function.
2268
2269 \sa QTextFrame QTextBlockFormat
2270*/
2271
2272/*!
2273 \enum QTextFrameFormat::Position
2274
2275 This enum describes how a frame is located relative to the surrounding text.
2276
2277 \value InFlow
2278 \value FloatLeft
2279 \value FloatRight
2280
2281 \sa position() CssFloat
2282*/
2283
2284/*!
2285 \enum QTextFrameFormat::BorderStyle
2286 \since 4.3
2287
2288 This enum describes different border styles for the text frame.
2289
2290 \value BorderStyle_None
2291 \value BorderStyle_Dotted
2292 \value BorderStyle_Dashed
2293 \value BorderStyle_Solid
2294 \value BorderStyle_Double
2295 \value BorderStyle_DotDash
2296 \value BorderStyle_DotDotDash
2297 \value BorderStyle_Groove
2298 \value BorderStyle_Ridge
2299 \value BorderStyle_Inset
2300 \value BorderStyle_Outset
2301
2302 \sa borderStyle() FrameBorderStyle
2303*/
2304
2305/*!
2306 \fn QTextFrameFormat::QTextFrameFormat()
2307
2308 Constructs a text frame format object with the default properties.
2309*/
2310QTextFrameFormat::QTextFrameFormat() : QTextFormat(FrameFormat)
2311{
2312 setBorderStyle(BorderStyle_Outset);
2313 setBorderBrush(Qt::darkGray);
2314}
2315
2316/*!
2317 \internal
2318 \fn QTextFrameFormat::QTextFrameFormat(const QTextFormat &other)
2319
2320 Creates a new frame format with the same attributes as the \a given
2321 text format.
2322*/
2323QTextFrameFormat::QTextFrameFormat(const QTextFormat &fmt)
2324 : QTextFormat(fmt)
2325{
2326}
2327
2328/*!
2329 \fn QTextFrameFormat::isValid() const
2330
2331 Returns true if the format description is valid; otherwise returns false.
2332*/
2333
2334/*!
2335 \fn QTextFrameFormat::setPosition(Position policy)
2336
2337 Sets the \a policy for positioning frames with this frame format.
2338
2339*/
2340
2341/*!
2342 \fn Position QTextFrameFormat::position() const
2343
2344 Returns the positioning policy for frames with this frame format.
2345*/
2346
2347/*!
2348 \fn QTextFrameFormat::setBorder(qreal width)
2349
2350 Sets the \a width (in pixels) of the frame's border.
2351*/
2352
2353/*!
2354 \fn qreal QTextFrameFormat::border() const
2355
2356 Returns the width of the border in pixels.
2357*/
2358
2359/*!
2360 \fn QTextFrameFormat::setBorderBrush(const QBrush &brush)
2361 \since 4.3
2362
2363 Sets the \a brush used for the frame's border.
2364*/
2365
2366/*!
2367 \fn QBrush QTextFrameFormat::borderBrush() const
2368 \since 4.3
2369
2370 Returns the brush used for the frame's border.
2371*/
2372
2373/*!
2374 \fn QTextFrameFormat::setBorderStyle(BorderStyle style)
2375 \since 4.3
2376
2377 Sets the \a style of the frame's border.
2378*/
2379
2380/*!
2381 \fn BorderStyle QTextFrameFormat::borderStyle() const
2382 \since 4.3
2383
2384 Returns the style of the frame's border.
2385*/
2386
2387/*!
2388 \fn QTextFrameFormat::setMargin(qreal margin)
2389
2390 Sets the frame's \a margin in pixels.
2391 This method also sets the left, right, top and bottom margins
2392 of the frame to the same value. The individual margins override
2393 the general margin.
2394*/
2395void QTextFrameFormat::setMargin(qreal amargin)
2396{
2397 setProperty(FrameMargin, amargin);
2398 setProperty(FrameTopMargin, amargin);
2399 setProperty(FrameBottomMargin, amargin);
2400 setProperty(FrameLeftMargin, amargin);
2401 setProperty(FrameRightMargin, amargin);
2402}
2403
2404
2405/*!
2406 \fn qreal QTextFrameFormat::margin() const
2407
2408 Returns the width of the frame's external margin in pixels.
2409*/
2410
2411/*!
2412 \fn QTextFrameFormat::setTopMargin(qreal margin)
2413 \since 4.3
2414
2415 Sets the frame's top \a margin in pixels.
2416*/
2417
2418/*!
2419 \fn qreal QTextFrameFormat::topMargin() const
2420 \since 4.3
2421
2422 Returns the width of the frame's top margin in pixels.
2423*/
2424qreal QTextFrameFormat::topMargin() const
2425{
2426 if (!hasProperty(FrameTopMargin))
2427 return margin();
2428 return doubleProperty(FrameTopMargin);
2429}
2430
2431/*!
2432 \fn QTextFrameFormat::setBottomMargin(qreal margin)
2433 \since 4.3
2434
2435 Sets the frame's bottom \a margin in pixels.
2436*/
2437
2438/*!
2439 \fn qreal QTextFrameFormat::bottomMargin() const
2440 \since 4.3
2441
2442 Returns the width of the frame's bottom margin in pixels.
2443*/
2444qreal QTextFrameFormat::bottomMargin() const
2445{
2446 if (!hasProperty(FrameBottomMargin))
2447 return margin();
2448 return doubleProperty(FrameBottomMargin);
2449}
2450
2451/*!
2452 \fn QTextFrameFormat::setLeftMargin(qreal margin)
2453 \since 4.3
2454
2455 Sets the frame's left \a margin in pixels.
2456*/
2457
2458/*!
2459 \fn qreal QTextFrameFormat::leftMargin() const
2460 \since 4.3
2461
2462 Returns the width of the frame's left margin in pixels.
2463*/
2464qreal QTextFrameFormat::leftMargin() const
2465{
2466 if (!hasProperty(FrameLeftMargin))
2467 return margin();
2468 return doubleProperty(FrameLeftMargin);
2469}
2470
2471/*!
2472 \fn QTextFrameFormat::setRightMargin(qreal margin)
2473 \since 4.3
2474
2475 Sets the frame's right \a margin in pixels.
2476*/
2477
2478/*!
2479 \fn qreal QTextFrameFormat::rightMargin() const
2480 \since 4.3
2481
2482 Returns the width of the frame's right margin in pixels.
2483*/
2484qreal QTextFrameFormat::rightMargin() const
2485{
2486 if (!hasProperty(FrameRightMargin))
2487 return margin();
2488 return doubleProperty(FrameRightMargin);
2489}
2490
2491/*!
2492 \fn QTextFrameFormat::setPadding(qreal width)
2493
2494 Sets the \a width of the frame's internal padding in pixels.
2495*/
2496
2497/*!
2498 \fn qreal QTextFrameFormat::padding() const
2499
2500 Returns the width of the frame's internal padding in pixels.
2501*/
2502
2503/*!
2504 \fn QTextFrameFormat::setWidth(const QTextLength &width)
2505
2506 Sets the frame's border rectangle's \a width.
2507
2508 \sa QTextLength
2509*/
2510
2511/*!
2512 \fn QTextFrameFormat::setWidth(qreal width)
2513 \overload
2514
2515 Convenience method that sets the width of the frame's border
2516 rectangle's width to the specified fixed \a width.
2517*/
2518
2519/*!
2520 \fn QTextFormat::PageBreakFlags QTextFrameFormat::pageBreakPolicy() const
2521 \since 4.2
2522
2523 Returns the currently set page break policy for the frame/table. The default is
2524 QTextFormat::PageBreak_Auto.
2525
2526 \sa setPageBreakPolicy()
2527*/
2528
2529/*!
2530 \fn void QTextFrameFormat::setPageBreakPolicy(PageBreakFlags policy)
2531 \since 4.2
2532
2533 Sets the page break policy for the frame/table to \a policy.
2534
2535 \sa pageBreakPolicy()
2536*/
2537
2538/*!
2539 \fn QTextLength QTextFrameFormat::width() const
2540
2541 Returns the width of the frame's border rectangle.
2542
2543 \sa QTextLength
2544*/
2545
2546/*!
2547 \fn void QTextFrameFormat::setHeight(const QTextLength &height)
2548
2549 Sets the frame's \a height.
2550*/
2551
2552/*!
2553 \fn void QTextFrameFormat::setHeight(qreal height)
2554 \overload
2555
2556 Sets the frame's \a height.
2557*/
2558
2559/*!
2560 \fn qreal QTextFrameFormat::height() const
2561
2562 Returns the height of the frame's border rectangle.
2563*/
2564
2565/*!
2566 \class QTextTableFormat
2567 \reentrant
2568
2569 \brief The QTextTableFormat class provides formatting information for
2570 tables in a QTextDocument.
2571
2572 \ingroup richtext-processing
2573
2574 A table is a group of cells ordered into rows and columns. Each table
2575 contains at least one row and one column. Each cell contains a block.
2576 Tables in rich text documents are formatted using the properties
2577 defined in this class.
2578
2579 Tables are horizontally justified within their parent frame according to the
2580 table's alignment. This can be read with the alignment() function and set
2581 with setAlignment().
2582
2583 Cells within the table are separated by cell spacing. The number of pixels
2584 between cells is set with setCellSpacing() and read with cellSpacing().
2585 The contents of each cell is surrounded by cell padding. The number of pixels
2586 between each cell edge and its contents is set with setCellPadding() and read
2587 with cellPadding().
2588
2589 \image qtexttableformat-cell.png
2590
2591 The table's background color can be read with the background() function,
2592 and can be specified with setBackground(). The background color of each
2593 cell can be set independently, and will control the color of the cell within
2594 the padded area.
2595
2596 The table format also provides a way to constrain the widths of the columns
2597 in the table. Columns can be assigned a fixed width, a variable width, or
2598 a percentage of the available width (see QTextLength). The columns() function
2599 returns the number of columns with constraints, and the
2600 columnWidthConstraints() function returns the constraints defined for the
2601 table. These quantities can also be set by calling setColumnWidthConstraints()
2602 with a vector containing new constraints. If no constraints are
2603 required, clearColumnWidthConstraints() can be used to remove them.
2604
2605 \sa QTextTable QTextTableCell QTextLength
2606*/
2607
2608/*!
2609 \fn QTextTableFormat::QTextTableFormat()
2610
2611 Constructs a new table format object.
2612*/
2613QTextTableFormat::QTextTableFormat()
2614 : QTextFrameFormat()
2615{
2616 setObjectType(TableObject);
2617 setCellSpacing(2);
2618 setBorder(1);
2619}
2620
2621/*!
2622 \internal
2623 \fn QTextTableFormat::QTextTableFormat(const QTextFormat &other)
2624
2625 Creates a new table format with the same attributes as the \a given
2626 text format.
2627*/
2628QTextTableFormat::QTextTableFormat(const QTextFormat &fmt)
2629 : QTextFrameFormat(fmt)
2630{
2631}
2632
2633/*!
2634 \fn bool QTextTableFormat::isValid() const
2635
2636 Returns true if this table format is valid; otherwise
2637 returns false.
2638*/
2639
2640
2641/*!
2642 \fn int QTextTableFormat::columns() const
2643
2644 Returns the number of columns specified by the table format.
2645*/
2646
2647
2648/*!
2649 \internal
2650 \fn void QTextTableFormat::setColumns(int columns)
2651
2652 Sets the number of \a columns required by the table format.
2653
2654 \sa columns()
2655*/
2656
2657/*!
2658 \fn void QTextTableFormat::clearColumnWidthConstraints()
2659
2660 Clears the column width constraints for the table.
2661
2662 \sa columnWidthConstraints() setColumnWidthConstraints()
2663*/
2664
2665/*!
2666 \fn void QTextTableFormat::setColumnWidthConstraints(const QVector<QTextLength> &constraints)
2667
2668 Sets the column width \a constraints for the table.
2669
2670 \sa columnWidthConstraints() clearColumnWidthConstraints()
2671*/
2672
2673/*!
2674 \fn QVector<QTextLength> QTextTableFormat::columnWidthConstraints() const
2675
2676 Returns a list of constraints used by this table format to control the
2677 appearance of columns in a table.
2678
2679 \sa setColumnWidthConstraints()
2680*/
2681
2682/*!
2683 \fn qreal QTextTableFormat::cellSpacing() const
2684
2685 Returns the table's cell spacing. This describes the distance between
2686 adjacent cells.
2687*/
2688
2689/*!
2690 \fn void QTextTableFormat::setCellSpacing(qreal spacing)
2691
2692 Sets the cell \a spacing for the table. This determines the distance
2693 between adjacent cells.
2694*/
2695
2696/*!
2697 \fn qreal QTextTableFormat::cellPadding() const
2698
2699 Returns the table's cell padding. This describes the distance between
2700 the border of a cell and its contents.
2701*/
2702
2703/*!
2704 \fn void QTextTableFormat::setCellPadding(qreal padding)
2705
2706 Sets the cell \a padding for the table. This determines the distance
2707 between the border of a cell and its contents.
2708*/
2709
2710/*!
2711 \fn void QTextTableFormat::setAlignment(Qt::Alignment alignment)
2712
2713 Sets the table's \a alignment.
2714
2715 \sa alignment()
2716*/
2717
2718/*!
2719 \fn Qt::Alignment QTextTableFormat::alignment() const
2720
2721 Returns the table's alignment.
2722
2723 \sa setAlignment()
2724*/
2725
2726/*!
2727 \fn void QTextTableFormat::setHeaderRowCount(int count)
2728 \since 4.2
2729
2730 Declares the first \a count rows of the table as table header.
2731 The table header rows get repeated when a table is broken
2732 across a page boundary.
2733*/
2734
2735/*!
2736 \fn int QTextTableFormat::headerRowCount() const
2737 \since 4.2
2738
2739 Returns the number of rows in the table that define the header.
2740
2741 \sa setHeaderRowCount()
2742*/
2743
2744/*!
2745 \fn void QTextFormat::setBackground(const QBrush &brush)
2746
2747 Sets the brush use to paint the document's background to the
2748 \a brush specified.
2749
2750 \sa background() clearBackground() setForeground()
2751*/
2752
2753/*!
2754 \fn QColor QTextFormat::background() const
2755
2756 Returns the brush used to paint the document's background.
2757
2758 \sa setBackground() clearBackground() foreground()
2759*/
2760
2761/*!
2762 \fn void QTextFormat::clearBackground()
2763
2764 Clears the brush used to paint the document's background. The default
2765 brush will be used.
2766
2767 \sa background() setBackground() clearForeground()
2768*/
2769
2770
2771/*!
2772 \class QTextImageFormat
2773 \reentrant
2774
2775 \brief The QTextImageFormat class provides formatting information for
2776 images in a QTextDocument.
2777
2778 \ingroup richtext-processing
2779
2780 Inline images are represented by an object replacement character
2781 (0xFFFC in Unicode) which has an associated QTextImageFormat. The
2782 image format specifies a name with setName() that is used to
2783 locate the image. The size of the rectangle that the image will
2784 occupy is specified using setWidth() and setHeight().
2785
2786 Images can be supplied in any format for which Qt has an image
2787 reader, so SVG drawings can be included alongside PNG, TIFF and
2788 other bitmap formats.
2789
2790 \sa QImage, QImageReader
2791*/
2792
2793/*!
2794 \fn QTextImageFormat::QTextImageFormat()
2795
2796 Creates a new image format object.
2797*/
2798QTextImageFormat::QTextImageFormat() : QTextCharFormat() { setObjectType(ImageObject); }
2799
2800/*!
2801 \internal
2802 \fn QTextImageFormat::QTextImageFormat(const QTextFormat &other)
2803
2804 Creates a new image format with the same attributes as the \a given
2805 text format.
2806*/
2807QTextImageFormat::QTextImageFormat(const QTextFormat &fmt)
2808 : QTextCharFormat(fmt)
2809{
2810}
2811
2812/*!
2813 \fn bool QTextImageFormat::isValid() const
2814
2815 Returns true if this image format is valid; otherwise returns false.
2816*/
2817
2818
2819/*!
2820 \fn void QTextImageFormat::setName(const QString &name)
2821
2822 Sets the \a name of the image. The \a name is used to locate the image
2823 in the application's resources.
2824
2825 \sa name()
2826*/
2827
2828
2829/*!
2830 \fn QString QTextImageFormat::name() const
2831
2832 Returns the name of the image. The name refers to an entry in the
2833 application's resources file.
2834
2835 \sa setName()
2836*/
2837
2838/*!
2839 \fn void QTextImageFormat::setWidth(qreal width)
2840
2841 Sets the \a width of the rectangle occupied by the image.
2842
2843 \sa width() setHeight()
2844*/
2845
2846
2847// ### Qt5 qreal replace with a QTextLength
2848/*!
2849 \fn qreal QTextImageFormat::width() const
2850
2851 Returns the width of the rectangle occupied by the image.
2852
2853 \sa height() setWidth()
2854*/
2855
2856
2857/*!
2858 \fn void QTextImageFormat::setHeight(qreal height)
2859
2860 Sets the \a height of the rectangle occupied by the image.
2861
2862 \sa height() setWidth()
2863*/
2864
2865
2866// ### Qt5 qreal replace with a QTextLength
2867/*!
2868 \fn qreal QTextImageFormat::height() const
2869
2870 Returns the height of the rectangle occupied by the image.
2871
2872 \sa width() setHeight()
2873*/
2874
2875/*!
2876 \fn void QTextCharFormat::setFontCapitalization(QFont::Capitalization capitalization)
2877 \since 4.4
2878
2879 Sets the capitalization of the text that apppears in this font to \a capitalization.
2880
2881 A font's capitalization makes the text appear in the selected capitalization mode.
2882
2883 \sa fontCapitalization()
2884*/
2885
2886/*!
2887 \fn Capitalization QTextCharFormat::fontCapitalization() const
2888 \since 4.4
2889
2890 Returns the current capitalization type of the font.
2891*/
2892
2893/*!
2894 \fn void QTextCharFormat::setFontLetterSpacing(qreal spacing)
2895 \since 4.4
2896
2897 Sets the letter spacing of this format to the given \a spacing, in percent.
2898 A value of 100 indicates default spacing; a value of 200 doubles the amount
2899 of space a letter takes.
2900
2901 \sa fontLetterSpacing()
2902*/
2903
2904/*!
2905 \fn qreal QTextCharFormat::fontLetterSpacing() const
2906 \since 4.4
2907
2908 Returns the current letter spacing percentage.
2909*/
2910
2911/*!
2912 \fn void QTextCharFormat::setFontWordSpacing(qreal spacing)
2913 \since 4.4
2914
2915 Sets the word spacing of this format to the given \a spacing, in pixels.
2916
2917 \sa fontWordSpacing()
2918*/
2919
2920/*!
2921 \fn qreal QTextCharFormat::fontWordSpacing() const
2922 \since 4.4
2923
2924 Returns the current word spacing value.
2925*/
2926
2927/*!
2928 \fn qreal QTextTableCellFormat::topPadding() const
2929 \since 4.4
2930
2931 Gets the top padding of the table cell.
2932
2933 \sa setTopPadding(), leftPadding(), rightPadding(), bottomPadding()
2934*/
2935
2936/*!
2937 \fn qreal QTextTableCellFormat::bottomPadding() const
2938 \since 4.4
2939
2940 Gets the bottom padding of the table cell.
2941
2942 \sa setBottomPadding(), leftPadding(), rightPadding(), topPadding()
2943*/
2944
2945/*!
2946 \fn qreal QTextTableCellFormat::leftPadding() const
2947 \since 4.4
2948
2949 Gets the left padding of the table cell.
2950
2951 \sa setLeftPadding(), rightPadding(), topPadding(), bottomPadding()
2952*/
2953
2954/*!
2955 \fn qreal QTextTableCellFormat::rightPadding() const
2956 \since 4.4
2957
2958 Gets the right padding of the table cell.
2959
2960 \sa setRightPadding(), leftPadding(), topPadding(), bottomPadding()
2961*/
2962
2963/*!
2964 \fn void QTextTableCellFormat::setTopPadding(qreal padding)
2965 \since 4.4
2966
2967 Sets the top \a padding of the table cell.
2968
2969 \sa topPadding(), setLeftPadding(), setRightPadding(), setBottomPadding()
2970*/
2971
2972/*!
2973 \fn void QTextTableCellFormat::setBottomPadding(qreal padding)
2974 \since 4.4
2975
2976 Sets the bottom \a padding of the table cell.
2977
2978 \sa bottomPadding(), setLeftPadding(), setRightPadding(), setTopPadding()
2979*/
2980
2981/*!
2982 \fn void QTextTableCellFormat::setLeftPadding(qreal padding)
2983 \since 4.4
2984
2985 Sets the left \a padding of the table cell.
2986
2987 \sa leftPadding(), setRightPadding(), setTopPadding(), setBottomPadding()
2988*/
2989
2990/*!
2991 \fn void QTextTableCellFormat::setRightPadding(qreal padding)
2992 \since 4.4
2993
2994 Sets the right \a padding of the table cell.
2995
2996 \sa rightPadding(), setLeftPadding(), setTopPadding(), setBottomPadding()
2997*/
2998
2999/*!
3000 \fn void QTextTableCellFormat::setPadding(qreal padding)
3001 \since 4.4
3002
3003 Sets the left, right, top, and bottom \a padding of the table cell.
3004
3005 \sa setLeftPadding(), setRightPadding(), setTopPadding(), setBottomPadding()
3006*/
3007
3008/*!
3009 \fn bool QTextTableCellFormat::isValid() const
3010 \since 4.4
3011
3012 Returns true if this table cell format is valid; otherwise returns false.
3013*/
3014
3015/*!
3016 \fn QTextTableCellFormat::QTextTableCellFormat()
3017 \since 4.4
3018
3019 Constructs a new table cell format object.
3020*/
3021QTextTableCellFormat::QTextTableCellFormat()
3022 : QTextCharFormat()
3023{
3024 setObjectType(TableCellObject);
3025}
3026
3027/*!
3028 \internal
3029 \fn QTextTableCellFormat::QTextTableCellFormat(const QTextFormat &other)
3030
3031 Creates a new table cell format with the same attributes as the \a given
3032 text format.
3033*/
3034QTextTableCellFormat::QTextTableCellFormat(const QTextFormat &fmt)
3035 : QTextCharFormat(fmt)
3036{
3037}
3038
3039/*!
3040 \class QTextTableCellFormat
3041 \reentrant
3042 \since 4.4
3043
3044 \brief The QTextTableCellFormat class provides formatting information for
3045 table cells in a QTextDocument.
3046
3047 \ingroup richtext-processing
3048
3049 The table cell format of a table cell in a document specifies the visual
3050 properties of the table cell.
3051
3052 The padding properties of a table cell are controlled by setLeftPadding(),
3053 setRightPadding(), setTopPadding(), and setBottomPadding(). All the paddings
3054 can be set at once using setPadding().
3055
3056 \sa QTextFormat QTextBlockFormat QTextTableFormat QTextCharFormat
3057*/
3058
3059// ------------------------------------------------------
3060
3061
3062QTextFormatCollection::QTextFormatCollection(const QTextFormatCollection &rhs)
3063{
3064 formats = rhs.formats;
3065 objFormats = rhs.objFormats;
3066}
3067
3068QTextFormatCollection &QTextFormatCollection::operator=(const QTextFormatCollection &rhs)
3069{
3070 formats = rhs.formats;
3071 objFormats = rhs.objFormats;
3072 return *this;
3073}
3074
3075QTextFormatCollection::~QTextFormatCollection()
3076{
3077}
3078
3079int QTextFormatCollection::indexForFormat(const QTextFormat &format)
3080{
3081 uint hash = getHash(format.d, format.format_type);
3082 QMultiHash<uint, int>::const_iterator i = hashes.find(hash);
3083 while (i != hashes.end() && i.key() == hash) {
3084 if (formats.value(i.value()) == format) {
3085 return i.value();
3086 }
3087 ++i;
3088 }
3089
3090 int idx = formats.size();
3091 formats.append(format);
3092
3093 QT_TRY{
3094 QTextFormat &f = formats.last();
3095 if (!f.d)
3096 f.d = new QTextFormatPrivate;
3097 f.d->resolveFont(defaultFnt);
3098
3099 if (!hashes.contains(hash, idx))
3100 hashes.insert(hash, idx);
3101
3102 } QT_CATCH(...) {
3103 formats.pop_back();
3104 QT_RETHROW;
3105 }
3106 return idx;
3107}
3108
3109bool QTextFormatCollection::hasFormatCached(const QTextFormat &format) const
3110{
3111 uint hash = getHash(format.d, format.format_type);
3112 QMultiHash<uint, int>::const_iterator i = hashes.find(hash);
3113 while (i != hashes.end() && i.key() == hash) {
3114 if (formats.value(i.value()) == format) {
3115 return true;
3116 }
3117 ++i;
3118 }
3119 return false;
3120}
3121
3122QTextFormat QTextFormatCollection::objectFormat(int objectIndex) const
3123{
3124 if (objectIndex == -1)
3125 return QTextFormat();
3126 return format(objFormats.at(objectIndex));
3127}
3128
3129void QTextFormatCollection::setObjectFormat(int objectIndex, const QTextFormat &f)
3130{
3131 const int formatIndex = indexForFormat(f);
3132 objFormats[objectIndex] = formatIndex;
3133}
3134
3135int QTextFormatCollection::objectFormatIndex(int objectIndex) const
3136{
3137 if (objectIndex == -1)
3138 return -1;
3139 return objFormats.at(objectIndex);
3140}
3141
3142void QTextFormatCollection::setObjectFormatIndex(int objectIndex, int formatIndex)
3143{
3144 objFormats[objectIndex] = formatIndex;
3145}
3146
3147int QTextFormatCollection::createObjectIndex(const QTextFormat &f)
3148{
3149 const int objectIndex = objFormats.size();
3150 objFormats.append(indexForFormat(f));
3151 return objectIndex;
3152}
3153
3154QTextFormat QTextFormatCollection::format(int idx) const
3155{
3156 if (idx < 0 || idx >= formats.count())
3157 return QTextFormat();
3158
3159 return formats.at(idx);
3160}
3161
3162void QTextFormatCollection::setDefaultFont(const QFont &f)
3163{
3164 defaultFnt = f;
3165 for (int i = 0; i < formats.count(); ++i)
3166 if (formats[i].d)
3167 formats[i].d->resolveFont(defaultFnt);
3168}
3169
3170QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.