Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
class HTMLLayer : public  QCPLayerable
{
    Q_OBJECT
public:
    HTMLLayer(QCustomPlot
* plot);
    
~HTMLLayer();
public:
    QString layerName()
const;
    
void setVisible(bool visible);

    
void setHTML(const QString& html);
    
void setPen(const QPen& pen);
    
void setFont(const QFont& font);
protected:
    
virtual void applyDefaultAntialiasingHint(QCPPainter* painter)const;
    
virtual void draw(QCPPainter* painter);
private:
    QPen mPen;
    QFont mFont;
    QTextDocument mDocument;
};



HTMLLayer::HTMLLayer(QCustomPlot* plot):
QCPLayerable(plot)
{
}

void HTMLLayer::setHTML(const QString& html)
{
    mDocument.setHtml(html);
}

HTMLLayer::~HTMLLayer()
{
}

QString HTMLLayer::layerName()const
{
    return "HtmlLayer";
}

void HTMLLayer::setVisible(bool visible)
{
    QCPLayer* layer = mParentPlot->layer(layerName());
    if(layer)
    {
        layer->setVisible(visible);
    }    
}

void HTMLLayer::setPen(const QPen& pen)
{
    mPen = pen;
}

void HTMLLayer::setFont(const QFont& font)
{
    mFont = font;
}

void HTMLLayer::applyDefaultAntialiasingHint(QCPPainter* painter)const
{
}

void HTMLLayer::draw(QCPPainter* painter)
{
    painter->save();
    QRectF rect = mParentPlot->rect();
    painter->setPen(mPen);
    painter->setFont(mFont);
    painter->setAntialiasing(true);
    QwtPainter::drawSimpleRichText(painter,rect,Qt::AlignLeft | Qt::AlignTop,mDocument);
    painter->restore();
}

void QwtPainter::drawSimpleRichText( QPainter *painter, const QRectF &rect,
    int flags, const QTextDocument &text )
{
    QTextDocument *txt = text.clone();

    painter->save();

    painter->setFont( txt->defaultFont() );
    qwtUnscaleFont( painter );

    txt->setDefaultFont( painter->font() );
    txt->setPageSize( QSizeF( rect.width(), QWIDGETSIZE_MAX ) );

    QAbstractTextDocumentLayout* layout = txt->documentLayout();

    const double height = layout->documentSize().height();
    double y = rect.y();
    if ( flags & Qt::AlignBottom )
        y += ( rect.height() - height );
    else if ( flags & Qt::AlignVCenter )
        y += ( rect.height() - height ) / 2;

    QAbstractTextDocumentLayout::PaintContext context;
    context.palette.setColor( QPalette::Text, painter->pen().color() );

    painter->translate( rect.x(), y );
    layout->draw( painter, context );

    painter->restore();
    delete txt;
}

posted on 2017-05-06 11:11 ccsdu2009 阅读(1617) 评论(0)  编辑 收藏 引用 所属分类: QT编程

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理