⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtextformat.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qtextformat.h"#include "qtextformat_p.h"#include <qvariant.h>#include <qdatastream.h>#include <qdebug.h>#include <qmap.h>#include <qhash.h>/*!    \class QTextLength qtextformat.h    \brief The QTextLength class encapsulates the different types of length    used in a QTextDocument.    \ingroup text    When we specify a value for the length of an element in a text document,    we often need to provide some other information so that the length is    used in the way we expect. For example, when we specify a table width,    the value can represent a fixed number of pixels, or it can be a percentage    value. This information changes both the meaning of the value and the way    it is used.    Generally, this class is used to specify table widths. These can be    specified either as a fixed amount of pixels, as a percentage of the    containing frame's width, or by a variable width that allows it to take    up just the space it requires.    \sa QTextTable*//*!    \fn explicit QTextLength::QTextLength()    Constructs a new length object which represents a variable size.*//*!    \fn QTextLength::QTextLength(Type type, qreal value)    Constructs a new length object of the given \a type and \a value.*//*!    \fn Type QTextLength::type() const    Returns the type of length.    \sa QTextLength::Type*//*!    \fn qreal QTextLength::value(qreal maximumLength) const    Returns the effective length, constrained by the type of the length object    and the specified \a maximumLength.    \sa type()*//*!    \fn qreal QTextLength::rawValue() const    Returns the constraint value that is specific for the type of the lenght.    If the length is QTextLength::PercentageLengt then the raw value is in    percent, in the range of 0 to 100. If the length is QTextLength::FixedLength    then that fixed amount is returned. For variable lengths zero is returned.*//*!    \fn bool QTextLength::operator==(const QTextLength &other) const    Returns true if this text length is the same as the \a other text    length.*//*!    \fn bool QTextLength::operator!=(const QTextLength &other) const    Returns true if this text length is different from the \a other text    length.*//*!    \enum QTextLength::Type    \value VariableLength    \value FixedLength    \value PercentageLength*//*!   Returns the text length as a QVariant*/QTextLength::operator QVariant() const{    return QVariant(QVariant::TextLength, this);}QDataStream &operator<<(QDataStream &stream, const QTextLength &length){    return stream << qint32(length.lengthType) << length.fixedValueOrPercentage;}QDataStream &operator>>(QDataStream &stream, QTextLength &length){    qint32 type;    stream >> type >> length.fixedValueOrPercentage;    length.lengthType = QTextLength::Type(type);    return stream;}class QTextFormatPrivate : public QSharedData{public:    QTextFormatPrivate() : hashDirty(true), fontDirty(true), hashValue(0) {}    // keep qint* types here, so we can safely stream to a datastream    typedef QMap<qint32, QVariant> PropertyMap;    inline uint hash() const    {        if (!hashDirty)            return hashValue;        return recalcHash();    }    inline bool operator==(const QTextFormatPrivate &rhs) const {        if (hash() != rhs.hash())            return false;        return props == rhs.props;    }    inline void insertProperty(qint32 key, const QVariant &value)    {        hashDirty = true;        fontDirty = true;        props.insert(key, value);    }    inline void clearProperty(qint32 key)    {        hashDirty = true;        fontDirty = true;        props.remove(key);    }    void resolveFont(const QFont &defaultFont);    const PropertyMap &properties() const { return props; }    inline const QFont &font() const {        if (fontDirty)            recalcFont();        return fnt;    }private:    PropertyMap props;    uint recalcHash() const;    void recalcFont() const;    mutable bool hashDirty;    mutable bool fontDirty;    mutable uint hashValue;    mutable QFont fnt;    friend QDataStream &operator<<(QDataStream &, const QTextFormat &);    friend QDataStream &operator>>(QDataStream &, QTextFormat &);};static uint variantHash(const QVariant &variant){    switch (variant.type()) {    case QVariant::Invalid: return 0;    case QVariant::Bool: return variant.toBool();    case QVariant::Int: return variant.toInt();    case QVariant::Double: return static_cast<int>(variant.toDouble());    case QVariant::String: return qHash(variant.toString());    case QVariant::Color: return qHash(qvariant_cast<QColor>(variant).rgb());    default: break;    }    return qHash(variant.typeName());}uint QTextFormatPrivate::recalcHash() const{    hashValue = 0;    for (PropertyMap::ConstIterator it = props.begin(); it != props.end(); ++it)        hashValue += (it.key() << 16) + variantHash(*it);    hashDirty = false;    return hashValue;}void QTextFormatPrivate::resolveFont(const QFont &defaultFont){    recalcFont();    const uint oldMask = fnt.resolve();    fnt = fnt.resolve(defaultFont);    if (props.contains(QTextFormat::FontSizeAdjustment)) {        const qreal scaleFactors[7] = {0.7, 0.8, 1.0, 1.2, 1.5, 2, 2.4};        const int htmlFontSize = qBound(0, props.value(QTextFormat::FontSizeAdjustment).toInt() + 3 - 1, 6);        if (defaultFont.pointSize() <= 0) {            qreal pixelSize = scaleFactors[htmlFontSize] * defaultFont.pixelSize();            fnt.setPixelSize(qRound(pixelSize));        } else {            qreal pointSize = scaleFactors[htmlFontSize] * defaultFont.pointSizeF();            fnt.setPointSizeF(pointSize);        }    }    fnt.resolve(oldMask);}void QTextFormatPrivate::recalcFont() const{    // update cached font as well    QFont f;    if (props.contains(QTextFormat::FontFamily))        f.setFamily(props.value(QTextFormat::FontFamily).toString());    if (props.contains(QTextFormat::FontPointSize))        f.setPointSizeF(props.value(QTextFormat::FontPointSize).toDouble());    else if (props.contains(QTextFormat::FontPixelSize))        f.setPixelSize(props.value(QTextFormat::FontPixelSize).toInt());    if (props.contains(QTextFormat::FontWeight))        f.setWeight(props.value(QTextFormat::FontWeight).toInt());    if (props.contains(QTextFormat::FontItalic))        f.setItalic(props.value(QTextFormat::FontItalic).toBool());    if (props.contains(QTextFormat::FontUnderline))        f.setUnderline(props.value(QTextFormat::FontUnderline).toBool());    if (props.contains(QTextFormat::FontOverline))        f.setOverline(props.value(QTextFormat::FontOverline).toBool());    if (props.contains(QTextFormat::FontStrikeOut))        f.setStrikeOut(props.value(QTextFormat::FontStrikeOut).toBool());    if (props.contains(QTextFormat::FontFixedPitch))        f.setFixedPitch(props.value(QTextFormat::FontFixedPitch).toBool());    fnt = f;    fontDirty = false;}Q_GUI_EXPORT QDataStream &operator<<(QDataStream &stream, const QTextFormat &fmt){    stream << fmt.format_type << fmt.d->props;    return stream;}Q_GUI_EXPORT QDataStream &operator>>(QDataStream &stream, QTextFormat &fmt){    stream >> fmt.format_type >> fmt.d->props;    return stream;}/*!    \class QTextFormat qtextformat.h    \brief The QTextFormat class provides formatting information for a    QTextDocument.    \ingroup text    A QTextFormat is a generic class used for describing the format of    parts of a QTextDocument. The derived classes QTextCharFormat,    QTextBlockFormat, QTextListFormat, and QTextTableFormat are usually    more useful, and describe the formatting that is applied to    specific parts of the document.    A format has a \c FormatType which specifies the kinds of thing it    can format; e.g. a block of text, a list, a table, etc. A format    also has various properties (some specific to particular format    types), as described by the Property enum. Every property has a    corresponding Property.    The format type is given by type(), and the format can be tested    with isCharFormat(), isBlockFormat(), isListFormat(),    isTableFormat(), isFrameFormat(), and isImageFormat(). If the    type is determined, it can be retrieved with toCharFormat(),    toBlockFormat(), toListFormat(), toTableFormat(), toFrameFormat(),    and toImageFormat().    A format's properties can be set with the setProperty() functions,    and retrieved with boolProperty(), intProperty(), doubleProperty(),    and stringProperty() as appropriate. All the property IDs used in    the format can be retrieved with allPropertyIds(). One format can    be merged into another using merge().    A format's object index can be set with setObjectIndex(), and    retrieved with objectIndex(). These methods can be used to    associate the format with a QTextObject. It is used to represent    lists, frames, and tables inside the document.    \sa {Text Related Classes}*//*!    \enum QTextFormat::FormatType    \value InvalidFormat    \value BlockFormat    \value CharFormat    \value ListFormat    \value TableFormat    \value FrameFormat    \value UserFormat*//*!    \enum QTextFormat::Property    \value ObjectIndex    Paragraph and character properties    \value CssFloat    \value LayoutDirection  The layout direction of the text in the document                            (Qt::LayoutDirection).    \value OutlinePen    \value ForegroundBrush    \value BackgroundBrush    Paragraph properties    \value BlockAlignment    \value BlockTopMargin    \value BlockBottomMargin    \value BlockLeftMargin    \value BlockRightMargin    \value TextIndent    \value BlockIndent    \value BlockNonBreakableLines    \value BlockTrailingHorizontalRulerWidth    Character properties    \value FontFamily    \value FontPointSize    \omitvalue FontSizeAdjustment    \value FontSizeIncrement    \value FontWeight    \value FontItalic    \value FontUnderline    \value FontOverline    \value FontStrikeOut    \value FontFixedPitch    \value FontPixelSize    \value TextUnderlineColor    \value TextVerticalAlignment    \value TextOutline    \value IsAnchor    \value AnchorHref    \value AnchorName    \value ObjectType    List properties    \value ListStyle    \value ListIndent    Table and frame properties    \value TableColumns    \value FrameBorder    \value FrameMargin    \value FramePadding    \value FrameWidth    \value FrameHeight    \value TableColumnWidthConstraints    \value TableCellSpacing    \value TableCellPadding    Table cell properties    \value TableCellRowSpan    \value TableCellColumnSpan    Image properties    \value ImageName    \value ImageWidth    \value ImageHeight    \value UserProperty*//*!    \enum QTextFormat::ObjectTypes    \value NoObject    \value ImageObject    \value TableObject    \value UserObject The first object that can be used for application-specific purposes.*//*!    \fn bool QTextFormat::isValid() const    Returns true if the format is valid (i.e. is not    InvalidFormat); otherwise returns false.*//*!    \fn bool QTextFormat::isCharFormat() const    Returns true if this text format is a \c CharFormat; otherwise    returns false.*//*!    \fn bool QTextFormat::isBlockFormat() const    Returns true if this text format is a \c BlockFormat; otherwise    returns false.*//*!    \fn bool QTextFormat::isListFormat() const    Returns true if this text format is a \c ListFormat; otherwise    returns false.*//*!    \fn bool QTextFormat::isTableFormat() const    Returns true if this text format is a \c TableFormat; otherwise    returns false.*//*!    \fn bool QTextFormat::isFrameFormat() const    Returns true if this text format is a \c FrameFormat; otherwise    returns false.*//*!    \fn bool QTextFormat::isImageFormat() const    Returns true if this text format is an image format; otherwise    returns false.*//*!    Creates a new text format with an \c InvalidFormat.    \sa FormatType*/QTextFormat::QTextFormat()    : format_type(InvalidFormat){}/*!    Creates a new text format of the given \a type.    \sa FormatType*/QTextFormat::QTextFormat(int type)    : format_type(type){}/*!    \fn QTextFormat::QTextFormat(const QTextFormat &other)    Creates a new text format with the same attributes at the \a other    text format.*/QTextFormat::QTextFormat(const QTextFormat &rhs)    : d(rhs.d), format_type(rhs.format_type){}/*!    \fn QTextFormat &QTextFormat::operator=(const QTextFormat &other)    Assigns the \a other text format to this text format, and returns a    reference to this text format.*/QTextFormat &QTextFormat::operator=(const QTextFormat &rhs){    d = rhs.d;    format_type = rhs.format_type;    return *this;}/*!    Destroys this text format.*/QTextFormat::~QTextFormat(){}/*!   Returns the text format as a QVariant*/QTextFormat::operator QVariant() const{    return QVariant(QVariant::TextFormat, this);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -