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

📄 qwt_text.cpp

📁 QWT5.01用于Qt开发的二维图形库程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2003   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

// vim: expandtab

#include <qmap.h>
#include <qfont.h>
#include <qcolor.h>
#include <qpen.h>
#include <qbrush.h>
#include <qpainter.h>
#include "qwt_painter.h"
#include "qwt_text_engine.h"
#include "qwt_text.h"
#if QT_VERSION >= 0x040000
#include <qapplication.h>
#include <qdesktopwidget.h>
#endif

class QwtTextEngineDict
{
public:
    QwtTextEngineDict();
    ~QwtTextEngineDict();

    void setTextEngine(QwtText::TextFormat, QwtTextEngine *);
    const QwtTextEngine *textEngine(QwtText::TextFormat) const;
    const QwtTextEngine *textEngine(const QString &, 
        QwtText::TextFormat) const;

private:
    typedef QMap<int, QwtTextEngine *> EngineMap;

    inline const QwtTextEngine *engine(EngineMap::const_iterator &it) const 
    {
#if QT_VERSION < 0x040000
        return it.data();
#else
        return it.value();
#endif
    }

    EngineMap d_map;
};

QwtTextEngineDict::QwtTextEngineDict()
{
    d_map.insert(QwtText::PlainText, new QwtPlainTextEngine());
#ifndef QT_NO_RICHTEXT
    d_map.insert(QwtText::RichText, new QwtRichTextEngine());
#endif
}

QwtTextEngineDict::~QwtTextEngineDict()
{
    for ( EngineMap::const_iterator it = d_map.begin(); 
        it != d_map.end(); ++it )
    {
        QwtTextEngine *textEngine = (QwtTextEngine *)engine(it);
        delete textEngine;
    }
}

const QwtTextEngine *QwtTextEngineDict::textEngine(const QString& text,
    QwtText::TextFormat format) const
{
    if ( format == QwtText::AutoText )
    {
        for ( EngineMap::const_iterator it = d_map.begin(); 
            it != d_map.end(); ++it )
        {
            if ( it.key() != QwtText::PlainText )
            {
                const QwtTextEngine *e = engine(it);
                if ( e && e->mightRender(text) )
                    return (QwtTextEngine *)e;
            }
        }
    }

    EngineMap::const_iterator it = d_map.find(format);
    if ( it != d_map.end() )
    {
        const QwtTextEngine *e = engine(it);
        if ( e )
            return e;
    }

    it = d_map.find(QwtText::PlainText);
    return engine(it);
}

void QwtTextEngineDict::setTextEngine(QwtText::TextFormat format, 
    QwtTextEngine *engine)
{
    if ( format == QwtText::AutoText )
        return;

    if ( format == QwtText::PlainText && engine == NULL )
        return;

    EngineMap::const_iterator it = d_map.find(format);
    if ( it != d_map.end() )
    {
        const QwtTextEngine *e = this->engine(it);
        if ( e )
            delete e;

        d_map.remove(format);
    }

    if ( engine != NULL )
        d_map.insert(format, engine);
}

const QwtTextEngine *QwtTextEngineDict::textEngine(
    QwtText::TextFormat format) const
{
    const QwtTextEngine *e = NULL;

    EngineMap::const_iterator it = d_map.find(format);
    if ( it != d_map.end() )
        e = engine(it);

    return e;
}

static QwtTextEngineDict *engineDict = NULL;

class QwtText::PrivateData
{
public:
    PrivateData():
        renderFlags(Qt::AlignCenter),
        backgroundPen(Qt::NoPen),
        backgroundBrush(Qt::NoBrush),
        paintAttributes(0),
        layoutAttributes(0),
        textEngine(NULL)
    {
    }

    int renderFlags;
    QString text;
    QFont font;
    QColor color;
    QPen backgroundPen;
    QBrush backgroundBrush;

    int paintAttributes;
    int layoutAttributes;

    const QwtTextEngine *textEngine;
};

class QwtText::LayoutCache
{
public:
    void invalidate()
    {
        textSize = QSize();
    }

    QFont font;
    QSize textSize;
};

/*!
   Constructor

   \param text Text content
   \param textFormat Text format
*/
QwtText::QwtText(const QString &text, QwtText::TextFormat textFormat)
{
    d_data = new PrivateData;
    d_data->text = text;
    d_data->textEngine = textEngine(text, textFormat);

    d_layoutCache = new LayoutCache;
}

//! Copy constructor
QwtText::QwtText(const QwtText &other)
{
    d_data = new PrivateData;
    *d_data = *other.d_data;

    d_layoutCache = new LayoutCache;
    *d_layoutCache = *other.d_layoutCache;
}

//! Destructor
QwtText::~QwtText() 
{
    delete d_data;
    delete d_layoutCache;
}

//! Assignement operator
QwtText &QwtText::operator=(const QwtText &other)
{
    *d_data = *other.d_data;
    *d_layoutCache = *other.d_layoutCache;
    return *this;
}
    
int QwtText::operator==(const QwtText &other) const
{
    return d_data->renderFlags == other.d_data->renderFlags &&
        d_data->text == other.d_data->text &&
        d_data->font == other.d_data->font &&
        d_data->color == other.d_data->color &&
        d_data->backgroundPen == other.d_data->backgroundPen &&
        d_data->backgroundBrush == other.d_data->backgroundBrush &&
        d_data->paintAttributes == other.d_data->paintAttributes &&
        d_data->textEngine == other.d_data->textEngine;
}

int QwtText::operator!=(const QwtText &other) const // invalidate
{
   return !(other == *this);
}

/*!
   Assign a new text content

   \param text Text content
   \param textFormat Text format
*/
void QwtText::setText(const QString &text, 
    QwtText::TextFormat textFormat) 
{ 
    d_data->text = text; 
    d_data->textEngine = textEngine(text, textFormat);
    d_layoutCache->invalidate();
}

/*! 
   Return the text.
   \sa setText
*/
QString QwtText::text() const 
{ 
    return d_data->text; 
}

/*!
   \brief Change the render flags

   The default setting is Qt::AlignCenter

   \param renderFlags Bitwise OR of the flags used like in QPainter::drawText

   \sa renderFlags, QwtTextEngine::draw
   \note Some renderFlags might have no effect, depending on the text format.
*/
void QwtText::setRenderFlags(int renderFlags) 
{ 
    if ( renderFlags != d_data->renderFlags )
    {
        d_data->renderFlags = renderFlags; 
        d_layoutCache->invalidate();
    }
}

/*!
   \return Render flags
   \sa setRenderFlags
*/
int QwtText::renderFlags() const 
{ 
    return d_data->renderFlags; 
}

/*! 
   Set the font.

   \param font Font
   \note Setting the font might have no effect, when
         the text contains control sequences for setting fonts.
*/
void QwtText::setFont(const QFont &font) 
{
    d_data->font = font; 
    setPaintAttribute(PaintUsingTextFont);
}

//! Return the font.
QFont QwtText::font() const 
{ 
    return d_data->font; 
}

/*!
  Return the font of the text, if it has one. 
  Otherwise return defaultFont.

  \param defaultFont Default font
  \sa setFont, font, PaintAttributes
*/
QFont QwtText::usedFont(const QFont &defaultFont) const
{
    if ( d_data->paintAttributes & PaintUsingTextFont )
        return d_data->font;

    return defaultFont;
}

/*! 
   Set the pen color used for painting the text.

   \param color Color
   \note Setting the color might have no effect, when
         the text contains control sequences for setting colors.
*/
void QwtText::setColor(const QColor &color) 
{ 
    d_data->color = color; 
    setPaintAttribute(PaintUsingTextColor);
}

//! Return the pen color, used for painting the text
QColor QwtText::color() const 
{ 
    return d_data->color; 
}

/*!
  Return the color of the text, if it has one. 
  Otherwise return defaultColor.

  \param defaultColor Default color
  \sa setColor, color, PaintAttributes
*/
QColor QwtText::usedColor(const QColor &defaultColor) const
{
    if ( d_data->paintAttributes & PaintUsingTextColor )
        return d_data->color;

    return defaultColor;

⌨️ 快捷键说明

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