Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/gui/text/qtextformat.cpp

    r2 r561  
    22**
    33** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    4 ** Contact: Qt Software Information (qt-info@nokia.com)
     4** All rights reserved.
     5** Contact: Nokia Corporation (qt-info@nokia.com)
    56**
    67** This file is part of the QtGui module of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    23 ** In addition, as a special exception, Nokia gives you certain
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
    26 ** package.
     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.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you have questions regarding the use of this file, please contact
     37** Nokia at qt-info@nokia.com.
    3838** $QT_END_LICENSE$
    3939**
     
    5858    used in a QTextDocument.
    5959
    60     \ingroup text
     60    \ingroup richtext-processing
    6161
    6262    When we specify a value for the length of an element in a text document,
     
    9090    \fn Type QTextLength::type() const
    9191
    92     Returns the type of length.
     92    Returns the type of this length object.
    9393
    9494    \sa QTextLength::Type
     
    130130    \enum QTextLength::Type
    131131
    132     \value VariableLength
    133     \value FixedLength
    134     \value PercentageLength
     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()
    135141*/
    136142
     
    143149}
    144150
     151#ifndef QT_NO_DATASTREAM
    145152QDataStream &operator<<(QDataStream &stream, const QTextLength &length)
    146153{
     
    157164    return stream;
    158165}
     166#endif // QT_NO_DATASTREAM
    159167
    160168class QTextFormatPrivate : public QSharedData
     
    258266};
    259267
    260 static uint variantHash(const QVariant &variant)
    261 {
    262     switch (variant.type()) {
     268// this is only safe if sizeof(int) == sizeof(float)
     269static inline uint hash(float d)
     270{
     271    return reinterpret_cast<uint&>(d);
     272}
     273
     274static inline uint hash(const QColor &color)
     275{
     276    return (color.isValid()) ? color.rgba() : 0x234109;
     277}
     278
     279static inline uint hash(const QPen &pen)
     280{
     281    return hash(pen.color()) + hash(pen.widthF());
     282}
     283
     284static inline uint hash(const QBrush &brush)
     285{
     286    return hash(brush.color()) + (brush.style() << 3);
     287}
     288
     289static inline uint variantHash(const QVariant &variant)
     290{
     291    // simple and fast hash functions to differentiate between type and value
     292    switch (variant.userType()) { // sorted by occurrence frequency
     293    case QVariant::String: return qHash(variant.toString());
     294    case QVariant::Double: return hash(variant.toDouble());
     295    case QVariant::Int: return 0x811890 + variant.toInt();
     296    case QVariant::Brush:
     297        return 0x01010101 + hash(qvariant_cast<QBrush>(variant));
     298    case QVariant::Bool: return 0x371818 + variant.toBool();
     299    case QVariant::Pen: return 0x02020202 + hash(qvariant_cast<QPen>(variant));
     300    case QVariant::List:
     301        return 0x8377 + qvariant_cast<QVariantList>(variant).count();
     302    case QVariant::Color: return hash(qvariant_cast<QColor>(variant));
     303      case QVariant::TextLength:
     304        return 0x377 + hash(qvariant_cast<QTextLength>(variant).rawValue());
     305    case QMetaType::Float: return hash(variant.toFloat());
    263306    case QVariant::Invalid: return 0;
    264     case QVariant::Bool: return variant.toBool();
    265     case QVariant::Int: return variant.toInt();
    266     case QVariant::Double: return static_cast<int>(variant.toDouble());
    267     case QVariant::String: return qHash(variant.toString());
    268     case QVariant::Color: return qHash(qvariant_cast<QColor>(variant).rgb());
    269307    default: break;
    270308    }
    271309    return qHash(variant.typeName());
     310}
     311
     312static inline int getHash(const QTextFormatPrivate *d, int format)
     313{
     314    return (d ? d->hash() : 0) + format;
    272315}
    273316
     
    318361                break;
    319362            case QTextFormat::FontPointSize:
    320                 f.setPointSizeF(props.at(i).value.toDouble());
     363                f.setPointSizeF(props.at(i).value.toReal());
    321364                break;
    322365            case  QTextFormat::FontPixelSize:
     
    345388                break;
    346389            case QTextFormat::FontLetterSpacing:
    347                 f.setLetterSpacing(QFont::PercentageSpacing, props.at(i).value.toDouble());
     390                f.setLetterSpacing(QFont::PercentageSpacing, props.at(i).value.toReal());
    348391                break;
    349392            case QTextFormat::FontWordSpacing:
    350                 f.setWordSpacing(props.at(i).value.toDouble());
     393                f.setWordSpacing(props.at(i).value.toReal());
    351394                break;
    352395            case QTextFormat::FontCapitalization:
     
    375418}
    376419
     420#ifndef QT_NO_DATASTREAM
    377421Q_GUI_EXPORT QDataStream &operator<<(QDataStream &stream, const QTextFormat &fmt)
    378422{
     
    397441    return stream;
    398442}
     443#endif // QT_NO_DATASTREAM
    399444
    400445/*!
     
    405450    QTextDocument.
    406451
    407     \ingroup text
     452    \ingroup richtext-processing
    408453    \ingroup shared
    409454
     
    414459    specific parts of the document.
    415460
    416     A format has a \c FormatType which specifies the kinds of thing it
     461    A format has a \c FormatType which specifies the kinds of text item it
    417462    can format; e.g. a block of text, a list, a table, etc. A format
    418463    also has various properties (some specific to particular format
     
    438483    lists, frames, and tables inside the document.
    439484
    440     \sa {Text Processing Classes}
     485    \sa {Rich Text Processing}
    441486*/
    442487
     
    444489    \enum QTextFormat::FormatType
    445490
    446     \value InvalidFormat
    447     \value BlockFormat
    448     \value CharFormat
    449     \value ListFormat
    450     \value TableFormat
    451     \value FrameFormat
     491    This enum describes the text item a QTextFormat object is formatting.
     492
     493    \value InvalidFormat An invalid format as created by the default
     494                         constructor
     495    \value BlockFormat The object formats a text block
     496    \value CharFormat The object formats a single character
     497    \value ListFormat The object formats a list
     498    \value TableFormat The object formats a table
     499    \value FrameFormat The object formats a frame
    452500
    453501    \value UserFormat
     502
     503    \sa QTextCharFormat, QTextBlockFormat, QTextListFormat,
     504    QTextTableFormat, type()
    454505*/
    455506
     
    457508    \enum QTextFormat::Property
    458509
    459     \value ObjectIndex
     510    This enum describes the different properties a format can have.
     511
     512    \value ObjectIndex The index of the formatted object. See objectIndex().
    460513
    461514    Paragraph and character properties
    462515
    463     \value CssFloat
     516    \value CssFloat How a frame is located relative to the surrounding text
    464517    \value LayoutDirection  The layout direction of the text in the document
    465518                            (Qt::LayoutDirection).
     
    479532    \value TextIndent
    480533    \value TabPositions     Specifies the tab positions.  The tab positions are structs of QTextOption::Tab which are stored in
    481                                           a QList (internally, in a QList<QVariant>).
     534                            a QList (internally, in a QList<QVariant>).
    482535    \value BlockIndent
    483536    \value BlockNonBreakableLines
    484     \value BlockTrailingHorizontalRulerWidth
     537    \value BlockTrailingHorizontalRulerWidth The width of a horizontal ruler element.
    485538
    486539    Character properties
     
    488541    \value FontFamily
    489542    \value FontPointSize
     543    \value FontPixelSize
    490544    \value FontSizeAdjustment       Specifies the change in size given to the fontsize already set using
    491545                                    FontPointSize or FontPixelSize.
     546    \value FontFixedPitch
    492547    \omitvalue FontSizeIncrement
    493548    \value FontWeight
     
    496551    \value FontOverline
    497552    \value FontStrikeOut
    498     \value FontFixedPitch
    499     \value FontPixelSize
    500553    \value FontCapitalization Specifies the capitalization type that is to be applied to the text.
    501554    \value FontLetterSpacing Changes the default spacing between individual letters in the font. The value is
     
    509562    \omitvalue FirstFontProperty
    510563    \omitvalue LastFontProperty
    511    
     564
    512565    \value TextUnderlineColor
    513566    \value TextVerticalAlignment
     
    530583    \value FrameBorder
    531584    \value FrameBorderBrush
    532     \value FrameBorderStyle
     585    \value FrameBorderStyle See the \l{QTextFrameFormat::BorderStyle}{BorderStyle} enum.
    533586    \value FrameBottomMargin
    534587    \value FrameHeight
     
    562615    Selection properties
    563616
    564     \value FullWidthSelection When set on the characterFormat of a selection, the whole width of the text will be shown selected
     617    \value FullWidthSelection When set on the characterFormat of a selection,
     618                              the whole width of the text will be shown selected.
    565619
    566620    Page break properties
    567621
    568     \value PageBreakPolicy
     622    \value PageBreakPolicy Specifies how pages are broken. See the PageBreakFlag enum.
    569623
    570624    \value UserProperty
     625
     626    \sa property(), setProperty()
    571627*/
    572628
    573629/*!
    574630    \enum QTextFormat::ObjectTypes
     631
     632    This enum describes what kind of QTextObject this format is associated with.
    575633
    576634    \value NoObject
     
    579637    \value TableCellObject
    580638    \value UserObject The first object that can be used for application-specific purposes.
     639
     640    \sa QTextObject, QTextTable, QTextObject::format()
    581641*/
    582642
     
    584644    \enum QTextFormat::PageBreakFlag
    585645    \since 4.2
     646
     647    This enum describes how page breaking is performed when printing. It maps to the
     648    corresponding css properties.
    586649
    587650    \value PageBreak_Auto The page break is determined automatically depending on the
     
    589652    \value PageBreak_AlwaysBefore The page is always broken before the paragraph/table
    590653    \value PageBreak_AlwaysAfter  A new page is always started after the paragraph/table
     654
     655    \sa QTextBlockFormat::pageBreakPolicy(), QTextFrameFormat::pageBreakPolicy(),
     656    PageBreakPolicy
    591657*/
    592658
     
    822888        return false;
    823889    const QVariant prop = d->property(propertyId);
    824     if (prop.type() != QVariant::Bool)
     890    if (prop.userType() != QVariant::Bool)
    825891        return false;
    826892    return prop.toBool();
     
    838904        return 0;
    839905    const QVariant prop = d->property(propertyId);
    840     if (prop.type() != QVariant::Int)
     906    if (prop.userType() != QVariant::Int)
    841907        return 0;
    842908    return prop.toInt();
     
    845911/*!
    846912    Returns the value of the property specified by \a propertyId. If the
    847     property isn't of QVariant::Double type, 0 is returned instead.
     913    property isn't of QVariant::Double or QMetaType::Float type, 0 is
     914    returned instead.
    848915
    849916    \sa setProperty() boolProperty() intProperty() stringProperty() colorProperty() lengthProperty() lengthVectorProperty() Property
     
    854921        return 0.;
    855922    const QVariant prop = d->property(propertyId);
    856     if (prop.type() != QVariant::Double)
     923    if (prop.userType() != QVariant::Double && prop.userType() != QMetaType::Float)
    857924        return 0.;
    858     return prop.toDouble(); // ####
     925    return qVariantValue<qreal>(prop);
    859926}
    860927
     
    871938        return QString();
    872939    const QVariant prop = d->property(propertyId);
    873     if (prop.type() != QVariant::String)
     940    if (prop.userType() != QVariant::String)
    874941        return QString();
    875942    return prop.toString();
     
    889956        return QColor();
    890957    const QVariant prop = d->property(propertyId);
    891     if (prop.type() != QVariant::Color)
     958    if (prop.userType() != QVariant::Color)
    892959        return QColor();
    893960    return qvariant_cast<QColor>(prop);
     
    906973        return QPen(Qt::NoPen);
    907974    const QVariant prop = d->property(propertyId);
    908     if (prop.type() != QVariant::Pen)
     975    if (prop.userType() != QVariant::Pen)
    909976        return QPen(Qt::NoPen);
    910977    return qvariant_cast<QPen>(prop);
     
    923990        return QBrush(Qt::NoBrush);
    924991    const QVariant prop = d->property(propertyId);
    925     if (prop.type() != QVariant::Brush)
     992    if (prop.userType() != QVariant::Brush)
    926993        return QBrush(Qt::NoBrush);
    927994    return qvariant_cast<QBrush>(prop);
     
    9531020        return vector;
    9541021    const QVariant prop = d->property(propertyId);
    955     if (prop.type() != QVariant::List)
     1022    if (prop.userType() != QVariant::List)
    9561023        return vector;
    9571024
     
    9591026    for (int i=0; i<propertyList.size(); ++i) {
    9601027        QVariant var = propertyList.at(i);
    961         if (var.type() == QVariant::TextLength)
     1028        if (var.userType() == QVariant::TextLength)
    9621029            vector.append(qvariant_cast<QTextLength>(var));
    9631030    }
     
    9681035/*!
    9691036    Returns the property specified by the given \a propertyId.
     1037
     1038    \sa Property
    9701039*/
    9711040QVariant QTextFormat::property(int propertyId) const
     
    9761045/*!
    9771046    Sets the property specified by the \a propertyId to the given \a value.
     1047
     1048    \sa Property
    9781049*/
    9791050void QTextFormat::setProperty(int propertyId, const QVariant &value)
     
    10031074
    10041075/*!
    1005   Clears the value of the property given by \a propertyId
    1006  */
     1076    Clears the value of the property given by \a propertyId
     1077
     1078    \sa Property
     1079*/
    10071080void QTextFormat::clearProperty(int propertyId)
    10081081{
     
    10161089    \fn void QTextFormat::setObjectType(int type)
    10171090
    1018     Sets the text format's object \a type. See \c{ObjectTypes}.
     1091    Sets the text format's object type to \a type.
     1092
     1093    \sa ObjectTypes, objectType()
    10191094*/
    10201095
     
    10231098    \fn int QTextFormat::objectType() const
    10241099
    1025     Returns the text format's object type. See \c{ObjectTypes}.
     1100    Returns the text format's object type.
     1101
     1102    \sa ObjectTypes, setObjectType()
    10261103*/
    10271104
     
    10371114        return -1;
    10381115    const QVariant prop = d->property(ObjectIndex);
    1039     if (prop.type() != QVariant::Int) // ####
     1116    if (prop.userType() != QVariant::Int) // ####
    10401117        return -1;
    10411118    return prop.toInt();
     
    11421219    characters in a QTextDocument.
    11431220
    1144     \ingroup text
     1221    \ingroup richtext-processing
    11451222
    11461223    The character format of text in a document specifies the visual properties
     
    14501527    \since 4.5
    14511528    \fn  bool QTextCharFormat::fontKerning() const
    1452     Returns true if the the font kerning is enabled.
     1529    Returns true if the font kerning is enabled.
    14531530
    14541531    \sa setFontKerning()
     
    15631640
    15641641    Sets the hypertext link for the text format to the given \a value.
    1565     This is typically a URL like "http://qtsoftware.com/index.html".
     1642    This is typically a URL like "http://example.com/index.html".
    15661643
    15671644    The anchor will be displayed with the \a value as its display text;
     
    16131690{
    16141691    QVariant prop = property(AnchorName);
    1615     if (prop.type() == QVariant::StringList)
     1692    if (prop.userType() == QVariant::StringList)
    16161693        return prop.toStringList().value(0);
    1617     else if (prop.type() != QVariant::String)
     1694    else if (prop.userType() != QVariant::String)
    16181695        return QString();
    16191696    return prop.toString();
     
    16311708{
    16321709    QVariant prop = property(AnchorName);
    1633     if (prop.type() == QVariant::StringList)
     1710    if (prop.userType() == QVariant::StringList)
    16341711        return prop.toStringList();
    1635     else if (prop.type() != QVariant::String)
     1712    else if (prop.userType() != QVariant::String)
    16361713        return QStringList();
    16371714    return QStringList(prop.toString());
     
    17551832    blocks of text in a QTextDocument.
    17561833
    1757     \ingroup text
     1834    \ingroup richtext-processing
    17581835
    17591836    A document is composed of a list of blocks, represented by QTextBlock
     
    20462123    lists in a QTextDocument.
    20472124
    2048     \ingroup text
     2125    \ingroup richtext-processing
    20492126
    20502127    A list is composed of one or more items, represented as text blocks.
     
    20752152    \value ListLowerAlpha  lower case Latin characters in alphabetical order
    20762153    \value ListUpperAlpha  upper case Latin characters in alphabetical order
     2154    \value ListLowerRoman  lower case roman numerals (supports up to 4999 items only)
     2155    \value ListUpperRoman  upper case roman numerals (supports up to 4999 items only)
    20772156    \omitvalue ListStyleUndefined
    20782157*/
     
    21112190    \fn void QTextListFormat::setStyle(Style style)
    21122191
    2113     Sets the list format's \a style. See \c{Style} for the available styles.
    2114 
    2115     \sa style()
     2192    Sets the list format's \a style.
     2193
     2194    \sa style() Style
    21162195*/
    21172196
     
    21192198    \fn Style QTextListFormat::style() const
    21202199
    2121     Returns the list format's style. See \c{Style}.
    2122 
    2123     \sa setStyle()
     2200    Returns the list format's style.
     2201
     2202    \sa setStyle() Style
    21242203*/
    21252204
     
    21542233    frames in a QTextDocument.
    21552234
    2156     \ingroup text
     2235    \ingroup richtext-processing
    21572236
    21582237    A text frame groups together one or more blocks of text, providing a layer
     
    21832262    \enum QTextFrameFormat::Position
    21842263
     2264    This enum describes how a frame is located relative to the surrounding text.
     2265
    21852266    \value InFlow
    21862267    \value FloatLeft
    21872268    \value FloatRight
    21882269
     2270    \sa position() CssFloat
    21892271*/
    21902272
     
    21922274    \enum QTextFrameFormat::BorderStyle
    21932275    \since 4.3
     2276
     2277    This enum describes different border styles for the text frame.
    21942278
    21952279    \value BorderStyle_None
     
    22052289    \value BorderStyle_Outset
    22062290
     2291    \sa borderStyle() FrameBorderStyle
    22072292*/
    22082293
     
    24742559    tables in a QTextDocument.
    24752560
    2476     \ingroup text
     2561    \ingroup richtext-processing
    24772562
    24782563    A table is a group of cells ordered into rows and columns. Each table
     
    26802765    images in a QTextDocument.
    26812766
    2682     \ingroup text
     2767    \ingroup richtext-processing
    26832768
    26842769    Inline images are represented by an object replacement character
     
    29493034    table cells in a QTextDocument.
    29503035
    2951     \ingroup text
     3036    \ingroup richtext-processing
    29523037
    29533038    The table cell format of a table cell in a document specifies the visual
     
    29833068int QTextFormatCollection::indexForFormat(const QTextFormat &format)
    29843069{
    2985     uint hash = format.d ? format.d->hash() : 0;
    2986     if (hashes.contains(hash)) {
    2987         for (int i = 0; i < formats.size(); ++i) {
    2988             if (formats.at(i) == format)
    2989                 return i;
     3070    uint hash = getHash(format.d, format.format_type);
     3071    QMultiHash<uint, int>::const_iterator i = hashes.find(hash);
     3072    while (i != hashes.end() && i.key() == hash) {
     3073        if (formats.value(i.value()) == format) {
     3074            return i.value();
    29903075        }
     3076        ++i;
    29913077    }
     3078
    29923079    int idx = formats.size();
    29933080    formats.append(format);
    29943081
    2995     QTextFormat &f = formats.last();
    2996     if (!f.d)
    2997         f.d = new QTextFormatPrivate;
    2998     f.d->resolveFont(defaultFnt);
    2999 
    3000     hashes.insert(hash);
     3082    QT_TRY{
     3083        QTextFormat &f = formats.last();
     3084        if (!f.d)
     3085            f.d = new QTextFormatPrivate;
     3086        f.d->resolveFont(defaultFnt);
     3087
     3088        hashes.insert(hash, idx);
     3089
     3090    } QT_CATCH(...) {
     3091        formats.pop_back();
     3092        QT_RETHROW;
     3093    }
    30013094    return idx;
    30023095}
     
    30043097bool QTextFormatCollection::hasFormatCached(const QTextFormat &format) const
    30053098{
    3006     uint hash = format.d ? format.d->hash() : 0;
    3007     if (hashes.contains(hash)) {
    3008         for (int i = 0; i < formats.size(); ++i)
    3009             if (formats.at(i) == format)
    3010                 return true;
     3099    uint hash = getHash(format.d, format.format_type);
     3100    QMultiHash<uint, int>::const_iterator i = hashes.find(hash);
     3101    while (i != hashes.end() && i.key() == hash) {
     3102        if (formats.value(i.value()) == format) {
     3103            return true;
     3104        }
     3105        ++i;
    30113106    }
    30123107    return false;
Note: See TracChangeset for help on using the changeset viewer.