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;
}