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

📄 qtextformat.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 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://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** 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    \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 length.    If the length is QTextLength::PercentageLength 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) << double(length.fixedValueOrPercentage);}QDataStream &operator>>(QDataStream &stream, QTextLength &length){    qint32 type;    double fixedValueOrPercentage;    stream >> type >> fixedValueOrPercentage;    length.fixedValueOrPercentage = fixedValueOrPercentage;    length.lengthType = QTextLength::Type(type);    return stream;}class QTextFormatPrivate : public QSharedData{public:    QTextFormatPrivate() : hashDirty(true), fontDirty(true), hashValue(0) {}    struct Property    {        inline Property(qint32 k, const QVariant &v) : key(k), value(v) {}        inline Property() {}        qint32 key;        QVariant value;        inline bool operator==(const Property &other) const        { return key == other.key && value == other.value; }        inline bool operator!=(const Property &other) const        { return key != other.key || value != other.value; }    };    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;        for (int i = 0; i < props.count(); ++i)            if (props.at(i).key == key) {                props[i].value = value;                return;            }        props.append(Property(key, value));    }    inline void clearProperty(qint32 key)    {        hashDirty = true;        fontDirty = true;        for (int i = 0; i < props.count(); ++i)            if (props.at(i).key == key) {                props.remove(i);                return;            }    }    inline int propertyIndex(qint32 key) const    {        for (int i = 0; i < props.count(); ++i)            if (props.at(i).key == key)                return i;        return -1;    }    inline QVariant property(qint32 key) const    {        const int idx = propertyIndex(key);        if (idx < 0)            return QVariant();        return props.at(idx).value;    }    inline bool hasProperty(qint32 key) const    { return propertyIndex(key) != -1; }    void resolveFont(const QFont &defaultFont);    inline const QFont &font() const {        if (fontDirty)            recalcFont();        return fnt;    }    QVector<Property> props;private:    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 (QVector<Property>::ConstIterator it = props.constBegin(); it != props.constEnd(); ++it)        hashValue += (it->key << 16) + variantHash(it->value);    hashDirty = false;    return hashValue;}void QTextFormatPrivate::resolveFont(const QFont &defaultFont){    recalcFont();    const uint oldMask = fnt.resolve();    fnt = fnt.resolve(defaultFont);    if (hasProperty(QTextFormat::FontSizeAdjustment)) {        const qreal scaleFactors[7] = {0.7, 0.8, 1.0, 1.2, 1.5, 2, 2.4};        const int htmlFontSize = qBound(0, property(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;    for (int i = 0; i < props.count(); ++i) {        switch (props.at(i).key) {            case QTextFormat::FontFamily:                f.setFamily(props.at(i).value.toString());                break;            case QTextFormat::FontPointSize:                f.setPointSizeF(props.at(i).value.toDouble());                break;            case  QTextFormat::FontPixelSize:                f.setPixelSize(props.at(i).value.toInt());                break;            case QTextFormat::FontWeight: {                int weight = props.at(i).value.toInt();                if (weight == 0) weight = QFont::Normal;                f.setWeight(weight);                break; }            case QTextFormat::FontItalic:                f.setItalic(props.at(i).value.toBool());                break;            case QTextFormat::FontUnderline:                if (! hasProperty(QTextFormat::TextUnderlineStyle)) // don't use the old one if the new one is there.                    f.setUnderline(props.at(i).value.toBool());                break;            case QTextFormat::TextUnderlineStyle:                f.setUnderline(static_cast<QTextCharFormat::UnderlineStyle>(props.at(i).value.toInt()) == QTextCharFormat::SingleUnderline);                break;            case QTextFormat::FontOverline:                f.setOverline(props.at(i).value.toBool());                break;            case QTextFormat::FontStrikeOut:                f.setStrikeOut(props.at(i).value.toBool());                break;            case QTextFormat::FontFixedPitch:                f.setFixedPitch(props.at(i).value.toBool());                break;            default:                break;            }    }    fnt = f;    fontDirty = false;}Q_GUI_EXPORT QDataStream &operator<<(QDataStream &stream, const QTextFormat &fmt){    stream << fmt.format_type << fmt.properties();    return stream;}Q_GUI_EXPORT QDataStream &operator>>(QDataStream &stream, QTextFormat &fmt){    QMap<qint32, QVariant> properties;    stream >> fmt.format_type >> properties;    // QTextFormat's default constructor doesn't allocate the private structure, so    // we have to do this, in case fmt is a default constructed value.    if(!fmt.d)        fmt.d = new QTextFormatPrivate();    for (QMap<qint32, QVariant>::ConstIterator it = properties.constBegin();         it != properties.constEnd(); ++it)        fmt.d->insertProperty(it.key(), it.value());    return stream;}/*!    \class QTextFormat    \brief The QTextFormat class provides formatting information for a    QTextDocument.    \ingroup text    \ingroup shared    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 Processing 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 \e{This property has been deprecated.} Use QTextFormat::TextUnderlineStyle instead.    \value FontOverline     \value FontStrikeOut    \value FontFixedPitch    \value FontPixelSize    \value TextUnderlineColor    \value TextVerticalAlignment    \value TextOutline    \value TextUnderlineStyle    \value TextToolTip Specifies the (optional) tool tip to be displayed for a fragment of text.    \value IsAnchor    \value AnchorHref    \value AnchorName    \value ObjectType    List properties    \value ListStyle    \value ListIndent    Table and frame properties    \value FrameBorder    \value FrameBorderBrush    \value FrameBorderStyle    \value FrameBottomMargin    \value FrameHeight    \value FrameLeftMargin     \value FrameMargin    \value FramePadding    \value FrameRightMargin    \value FrameTopMargin    \value FrameWidth    \value TableCellSpacing    \value TableCellPadding    \value TableColumns    \value TableColumnWidthConstraints    \value TableHeaderRowCount    Table cell properties    \value TableCellRowSpan    \value TableCellColumnSpan    Image properties    \value ImageName    \value ImageWidth    \value ImageHeight    Selection properties    \value FullWidthSelection    Page break properties    \value PageBreakPolicy

⌨️ 快捷键说明

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