A class representing a text. More...
#include <qwt_text.h>
Classes | |
class | LayoutCache |
class | PrivateData |
Public Types | |
enum | TextFormat { AutoText = 0, PlainText, RichText, MathMLText, TeXText, OtherFormat = 100 } |
Text format. More... | |
enum | PaintAttribute { PaintUsingTextFont = 1, PaintUsingTextColor = 2, PaintBackground = 4 } |
Paint Attributes. More... | |
enum | LayoutAttribute { MinimumLayout = 1 } |
Layout Attributes. More... | |
Public Member Functions | |
QwtText (const QString &=QString::null, TextFormat textFormat=AutoText) | |
QwtText (const QwtText &) | |
Copy constructor. | |
~QwtText () | |
Destructor. | |
QwtText & | operator= (const QwtText &) |
Assignment operator. | |
int | operator== (const QwtText &) const |
Relational operator. | |
int | operator!= (const QwtText &) const |
Relational operator. | |
void | setText (const QString &, QwtText::TextFormat textFormat=AutoText) |
QString | text () const |
bool | isNull () const |
bool | isEmpty () const |
void | setFont (const QFont &) |
QFont | font () const |
Return the font. | |
QFont | usedFont (const QFont &) const |
void | setRenderFlags (int flags) |
Change the render flags. | |
int | renderFlags () const |
void | setColor (const QColor &) |
QColor | color () const |
Return the pen color, used for painting the text. | |
QColor | usedColor (const QColor &) const |
void | setBackgroundPen (const QPen &) |
QPen | backgroundPen () const |
void | setBackgroundBrush (const QBrush &) |
QBrush | backgroundBrush () const |
void | setPaintAttribute (PaintAttribute, bool on=true) |
bool | testPaintAttribute (PaintAttribute) const |
void | setLayoutAttribute (LayoutAttribute, bool on=true) |
bool | testLayoutAttribute (LayoutAttribute) const |
int | heightForWidth (int width, const QFont &=QFont()) const |
QSize | textSize (const QFont &=QFont()) const |
void | draw (QPainter *painter, const QRect &rect) const |
Static Public Member Functions | |
static const QwtTextEngine * | textEngine (const QString &text, QwtText::TextFormat=AutoText) |
static const QwtTextEngine * | textEngine (QwtText::TextFormat) |
Find the text engine for a text format. | |
static void | setTextEngine (QwtText::TextFormat, QwtTextEngine *) |
A class representing a text.
A QwtText is a text including a set of attributes how to render it.
Definition at line 51 of file qwt_text.h.
Layout Attributes.
The layout attributes affects some aspects of the layout of the text.
Definition at line 129 of file qwt_text.h.
{ MinimumLayout = 1 };
Paint Attributes.
Font and color and background are optional attributes of a QwtText. The paint attributes hold the information, if they are set.
Definition at line 111 of file qwt_text.h.
{ PaintUsingTextFont = 1, PaintUsingTextColor = 2, PaintBackground = 4 };
enum QwtText::TextFormat |
Text format.
The text format defines the QwtTextEngine, that is used to render the text.
Definition at line 85 of file qwt_text.h.
{ AutoText = 0, PlainText, RichText, MathMLText, TeXText, OtherFormat = 100 };
QwtText::QwtText | ( | const QString & | text = QString::null , |
QwtText::TextFormat | textFormat = AutoText |
||
) |
Constructor
text | Text content |
textFormat | Text format |
Definition at line 180 of file qwt_text.cpp.
References text(), QwtText::PrivateData::text, textEngine(), and QwtText::PrivateData::textEngine.
{ d_data = new PrivateData; d_data->text = text; d_data->textEngine = textEngine(text, textFormat); d_layoutCache = new LayoutCache; }
QwtText::QwtText | ( | const QwtText & | other ) |
Copy constructor.
Definition at line 190 of file qwt_text.cpp.
{ d_data = new PrivateData; *d_data = *other.d_data; d_layoutCache = new LayoutCache; *d_layoutCache = *other.d_layoutCache; }
QwtText::~QwtText | ( | ) |
QBrush QwtText::backgroundBrush | ( | ) | const |
Definition at line 391 of file qwt_text.cpp.
References QwtText::PrivateData::backgroundBrush.
Referenced by QwtPicker::PickerWidget::updateMask().
{ return d_data->backgroundBrush; }
QPen QwtText::backgroundPen | ( | ) | const |
Definition at line 370 of file qwt_text.cpp.
References QwtText::PrivateData::backgroundPen.
{ return d_data->backgroundPen; }
QColor QwtText::color | ( | ) | const |
Return the pen color, used for painting the text.
Definition at line 334 of file qwt_text.cpp.
References QwtText::PrivateData::color.
Referenced by QwtPlotPrintFilter::apply(), and setColor().
{ return d_data->color; }
void QwtText::draw | ( | QPainter * | painter, |
const QRect & | rect | ||
) | const |
Draw a text into a rectangle
painter | Painter |
rect | Rectangle |
Definition at line 570 of file qwt_text.cpp.
References QwtText::PrivateData::backgroundBrush, QwtText::PrivateData::backgroundPen, QwtText::PrivateData::color, QwtTextEngine::draw(), QwtPainter::drawRect(), font(), QwtText::PrivateData::font, QwtText::PrivateData::layoutAttributes, QwtPainter::metricsMap(), MinimumLayout, QwtText::PrivateData::paintAttributes, PaintBackground, PaintUsingTextColor, PaintUsingTextFont, QwtText::PrivateData::renderFlags, QwtPainter::scaledPen(), QwtMetricsMap::screenToLayoutX(), QwtMetricsMap::screenToLayoutY(), QwtText::PrivateData::text, QwtText::PrivateData::textEngine, and QwtTextEngine::textMargins().
Referenced by QwtLegendItem::drawItem(), QwtScaleDraw::drawLabel(), QwtRoundScaleDraw::drawLabel(), QwtTextLabel::drawText(), QwtScaleWidget::drawTitle(), QwtPicker::drawTracker(), and QwtPlot::printTitle().
{ if ( d_data->paintAttributes & PaintBackground ) { if ( d_data->backgroundPen != Qt::NoPen || d_data->backgroundBrush != Qt::NoBrush ) { painter->save(); painter->setPen(QwtPainter::scaledPen(d_data->backgroundPen)); painter->setBrush(d_data->backgroundBrush); #if QT_VERSION < 0x040000 QwtPainter::drawRect(painter, rect); #else const QRect r(rect.x(), rect.y(), rect.width() - 1, rect.height() - 1); QwtPainter::drawRect(painter, r); #endif painter->restore(); } } painter->save(); if ( d_data->paintAttributes & PaintUsingTextFont ) { painter->setFont(d_data->font); } if ( d_data->paintAttributes & PaintUsingTextColor ) { if ( d_data->color.isValid() ) painter->setPen(d_data->color); } QRect expandedRect = rect; if ( d_data->layoutAttributes & MinimumLayout ) { #if QT_VERSION < 0x040000 const QFont font(painter->font()); #else // We want to calculate in screen metrics. So // we need a font that uses screen metrics const QFont font(painter->font(), QApplication::desktop()); #endif int left, right, top, bottom; d_data->textEngine->textMargins( font, d_data->text, left, right, top, bottom); const QwtMetricsMap map = QwtPainter::metricsMap(); left = map.screenToLayoutX(left); right = map.screenToLayoutX(right); top = map.screenToLayoutY(top); bottom = map.screenToLayoutY(bottom); expandedRect.setTop(rect.top() - top); expandedRect.setBottom(rect.bottom() + bottom); expandedRect.setLeft(rect.left() - left); expandedRect.setRight(rect.right() + right); } d_data->textEngine->draw(painter, expandedRect, d_data->renderFlags, d_data->text); painter->restore(); }
QFont QwtText::font | ( | ) | const |
Return the font.
Definition at line 300 of file qwt_text.cpp.
References QwtText::PrivateData::font.
Referenced by QwtPlotPrintFilter::apply(), draw(), heightForWidth(), setFont(), and textSize().
{ return d_data->font; }
int QwtText::heightForWidth | ( | int | width, |
const QFont & | defaultFont = QFont() |
||
) | const |
Find the height for a given width
defaultFont | Font, used for the calculation if the text has no font |
width | Width |
Definition at line 463 of file qwt_text.cpp.
References font(), QwtTextEngine::heightForWidth(), QwtText::PrivateData::layoutAttributes, QwtMetricsMap::layoutToScreenX(), QwtPainter::metricsMap(), MinimumLayout, QwtText::PrivateData::renderFlags, QwtMetricsMap::screenToLayoutY(), QwtText::PrivateData::text, QwtText::PrivateData::textEngine, QwtTextEngine::textMargins(), and usedFont().
Referenced by QwtPlotLayout::expandLineBreaks(), QwtTextLabel::heightForWidth(), and QwtScaleWidget::titleHeightForWidth().
{ const QwtMetricsMap map = QwtPainter::metricsMap(); width = map.layoutToScreenX(width); #if QT_VERSION < 0x040000 const QFont font = usedFont(defaultFont); #else // We want to calculate in screen metrics. So // we need a font that uses screen metrics const QFont font(usedFont(defaultFont), QApplication::desktop()); #endif int h = 0; if ( d_data->layoutAttributes & MinimumLayout ) { int left, right, top, bottom; d_data->textEngine->textMargins(font, d_data->text, left, right, top, bottom); h = d_data->textEngine->heightForWidth( font, d_data->renderFlags, d_data->text, width + left + right); h -= top + bottom; } else { h = d_data->textEngine->heightForWidth( font, d_data->renderFlags, d_data->text, width); } h = map.screenToLayoutY(h); return h; }
bool QwtText::isEmpty | ( | ) | const [inline] |
Definition at line 152 of file qwt_text.h.
Referenced by QwtScaleDraw::boundingLabelRect(), QwtScaleWidget::dimForLength(), QwtScaleWidget::draw(), QwtScaleDraw::drawLabel(), QwtRoundScaleDraw::drawLabel(), QwtPicker::drawTracker(), QwtPlotLayout::expandLineBreaks(), QwtRoundScaleDraw::extent(), QwtPlotLayout::LayoutData::init(), QwtScaleDraw::labelRect(), QwtPlot::print(), and QwtPicker::trackerRect().
{ return text().isEmpty(); }
bool QwtText::isNull | ( | ) | const [inline] |
int QwtText::operator!= | ( | const QwtText & | other ) | const |
Assignment operator.
Definition at line 207 of file qwt_text.cpp.
{ *d_data = *other.d_data; *d_layoutCache = *other.d_layoutCache; return *this; }
int QwtText::operator== | ( | const QwtText & | other ) | const |
Relational operator.
Definition at line 215 of file qwt_text.cpp.
References QwtText::PrivateData::backgroundBrush, QwtText::PrivateData::backgroundPen, QwtText::PrivateData::color, QwtText::PrivateData::font, QwtText::PrivateData::paintAttributes, QwtText::PrivateData::renderFlags, QwtText::PrivateData::text, and QwtText::PrivateData::textEngine.
{ 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::renderFlags | ( | ) | const |
Definition at line 281 of file qwt_text.cpp.
References QwtText::PrivateData::renderFlags.
Referenced by QwtScaleWidget::drawTitle(), QwtTextLabel::heightForWidth(), QwtTextLabel::minimumSizeHint(), setRenderFlags(), QwtScaleWidget::setTitle(), and QwtTextLabel::textRect().
{ return d_data->renderFlags; }
void QwtText::setBackgroundBrush | ( | const QBrush & | brush ) |
Set the background brush
brush | Background brush |
Definition at line 381 of file qwt_text.cpp.
References QwtText::PrivateData::backgroundBrush, PaintBackground, and setPaintAttribute().
Referenced by QmitkHistogramWidget::OnSelect().
{ d_data->backgroundBrush = brush; setPaintAttribute(PaintBackground); }
void QwtText::setBackgroundPen | ( | const QPen & | pen ) |
Set the background pen
pen | Background pen |
Definition at line 360 of file qwt_text.cpp.
References QwtText::PrivateData::backgroundPen, PaintBackground, and setPaintAttribute().
{ d_data->backgroundPen = pen; setPaintAttribute(PaintBackground); }
void QwtText::setColor | ( | const QColor & | color ) |
Set the pen color used for painting the text.
color | Color |
Definition at line 327 of file qwt_text.cpp.
References color(), QwtText::PrivateData::color, PaintUsingTextColor, and setPaintAttribute().
Referenced by QwtPlotPrintFilter::apply(), and QwtPlotPrintFilter::reset().
{ d_data->color = color; setPaintAttribute(PaintUsingTextColor); }
void QwtText::setFont | ( | const QFont & | font ) |
Set the font.
font | Font |
Definition at line 293 of file qwt_text.cpp.
References font(), QwtText::PrivateData::font, PaintUsingTextFont, and setPaintAttribute().
Referenced by QwtPlotPrintFilter::apply(), QwtPicker::drawTracker(), QwtPlotLayout::LayoutData::init(), QmitkHistogramWidget::OnSelect(), QmitkHistogramWidget::QmitkHistogramWidget(), and QwtPlotPrintFilter::reset().
{ d_data->font = font; setPaintAttribute(PaintUsingTextFont); }
void QwtText::setLayoutAttribute | ( | LayoutAttribute | attribute, |
bool | on = true |
||
) |
Change a layout attribute
attribute | Layout attribute |
on | On/Off |
Definition at line 434 of file qwt_text.cpp.
References QwtText::PrivateData::layoutAttributes.
Referenced by QwtAbstractScaleDraw::tickLabel().
{ if ( on ) d_data->layoutAttributes |= attribute; else d_data->layoutAttributes &= ~attribute; }
void QwtText::setPaintAttribute | ( | PaintAttribute | attribute, |
bool | on = true |
||
) |
Change a paint attribute
attribute | Paint attribute |
on | On/Off |
Definition at line 406 of file qwt_text.cpp.
References QwtText::PrivateData::paintAttributes.
Referenced by setBackgroundBrush(), setBackgroundPen(), setColor(), and setFont().
{ if ( on ) d_data->paintAttributes |= attribute; else d_data->paintAttributes &= ~attribute; }
void QwtText::setRenderFlags | ( | int | renderFlags ) |
Change the render flags.
The default setting is Qt::AlignCenter
renderFlags | Bitwise OR of the flags used like in QPainter::drawText |
Definition at line 268 of file qwt_text.cpp.
References QwtText::LayoutCache::invalidate(), renderFlags(), and QwtText::PrivateData::renderFlags.
Referenced by QwtScaleWidget::drawTitle(), QwtLegendItem::setText(), QwtScaleWidget::setTitle(), and QwtAbstractScaleDraw::tickLabel().
{ if ( renderFlags != d_data->renderFlags ) { d_data->renderFlags = renderFlags; d_layoutCache->invalidate(); } }
void QwtText::setText | ( | const QString & | text, |
QwtText::TextFormat | textFormat = AutoText |
||
) |
Assign a new text content
text | Text content |
textFormat | Text format |
Definition at line 241 of file qwt_text.cpp.
References QwtText::LayoutCache::invalidate(), text(), QwtText::PrivateData::text, textEngine(), and QwtText::PrivateData::textEngine.
Referenced by QwtTextLabel::setText(), and QwtScaleWidget::setTitle().
{ d_data->text = text; d_data->textEngine = textEngine(text, textFormat); d_layoutCache->invalidate(); }
void QwtText::setTextEngine | ( | QwtText::TextFormat | format, |
QwtTextEngine * | engine | ||
) | [static] |
Assign/Replace a text engine for a text format
With setTextEngine it is possible to extend Qwt with other types of text formats.
Owner of a commercial Qt license can build the qwtmathml library, that is based on the MathML renderer, that is included in MML Widget component of the Qt solutions package.
For QwtText::PlainText it is not allowed to assign a engine == NULL.
format | Text format |
engine | Text engine |
Definition at line 687 of file qwt_text.cpp.
References QwtTextEngineDict::setTextEngine().
{ if ( engineDict == NULL ) engineDict = new QwtTextEngineDict(); engineDict->setTextEngine(format, engine); }
bool QwtText::testLayoutAttribute | ( | LayoutAttribute | attribute ) | const |
Test a layout attribute
attribute | Layout attribute |
Definition at line 450 of file qwt_text.cpp.
References QwtText::PrivateData::layoutAttributes.
{ return d_data->layoutAttributes | attribute; }
bool QwtText::testPaintAttribute | ( | PaintAttribute | attribute ) | const |
Test a paint attribute
attribute | Paint attribute |
Definition at line 422 of file qwt_text.cpp.
References QwtText::PrivateData::paintAttributes.
Referenced by QwtPlotPrintFilter::apply(), QwtPlotLayout::LayoutData::init(), QwtPlotPrintFilter::reset(), and QwtPicker::PickerWidget::updateMask().
{ return d_data->paintAttributes & attribute; }
QString QwtText::text | ( | ) | const |
Return the text.
Definition at line 253 of file qwt_text.cpp.
References QwtText::PrivateData::text.
Referenced by QwtPlot::grabProperties(), QwtText(), setText(), and QwtScaleWidget::setTitle().
{ return d_data->text; }
const QwtTextEngine * QwtText::textEngine | ( | QwtText::TextFormat | format ) | [static] |
Find the text engine for a text format.
textEngine can be used to find out if a text format is supported. F.e, if one wants to use MathML labels, the MathML renderer from the commercial Qt solutions package might be required, that is not available in Qt Open Source Edition environments.
format | Text format |
Definition at line 707 of file qwt_text.cpp.
References QwtTextEngineDict::textEngine().
{ if ( engineDict == NULL ) engineDict = new QwtTextEngineDict(); return engineDict->textEngine(format); }
const QwtTextEngine * QwtText::textEngine | ( | const QString & | text, |
QwtText::TextFormat | format = AutoText |
||
) | [static] |
Find the text engine for a text format
In case of QwtText::AutoText the first text engine (beside QwtPlainTextEngine) is returned, where QwtTextEngine::mightRender returns true. If there is none QwtPlainTextEngine is returnd.
If no text engine is registered for the format QwtPlainTextEngine is returnd.
text | Text, needed in case of AutoText |
format | Text format |
Definition at line 652 of file qwt_text.cpp.
References QwtTextEngineDict::textEngine().
Referenced by QwtText(), and setText().
{ if ( engineDict == NULL ) { /* Note: engineDict is allocated, the first time it is used, but never deleted, because there is no known last access time. So don't be irritated, if it is reported as a memory leak from your memory profiler. */ engineDict = new QwtTextEngineDict(); } return engineDict->textEngine(text, format); }
QSize QwtText::textSize | ( | const QFont & | defaultFont = QFont() ) |
const |
Find the height for a given width
defaultFont | Font, used for the calculation if the text has no font |
Returns the size, that is needed to render text
defaultFont | Font of the text |
Definition at line 515 of file qwt_text.cpp.
References QwtText::LayoutCache::font, font(), QwtMetricsMap::isIdentity(), QwtText::PrivateData::layoutAttributes, QwtPainter::metricsMap(), MinimumLayout, QwtText::PrivateData::renderFlags, QwtMetricsMap::screenToLayout(), QwtText::PrivateData::text, QwtText::PrivateData::textEngine, QwtTextEngine::textMargins(), QwtTextEngine::textSize(), QwtText::LayoutCache::textSize, and usedFont().
Referenced by QwtScaleDraw::boundingLabelRect(), QwtScaleDraw::drawLabel(), QwtRoundScaleDraw::drawLabel(), QwtRoundScaleDraw::extent(), QwtScaleDraw::labelRect(), QwtTextLabel::minimumSizeHint(), QwtAbstractScaleDraw::tickLabel(), and QwtPicker::trackerRect().
{ #if QT_VERSION < 0x040000 const QFont font(usedFont(defaultFont)); #else // We want to calculate in screen metrics. So // we need a font that uses screen metrics const QFont font(usedFont(defaultFont), QApplication::desktop()); #endif if ( !d_layoutCache->textSize.isValid() || d_layoutCache->font != font ) { d_layoutCache->textSize = d_data->textEngine->textSize( font, d_data->renderFlags, d_data->text); d_layoutCache->font = font; } QSize sz = d_layoutCache->textSize; const QwtMetricsMap map = QwtPainter::metricsMap(); if ( d_data->layoutAttributes & MinimumLayout ) { int left, right, top, bottom; d_data->textEngine->textMargins(font, d_data->text, left, right, top, bottom); sz -= QSize(left + right, top + bottom); #if QT_VERSION >= 0x040000 if ( !map.isIdentity() ) { #ifdef __GNUC__ #endif /* When printing in high resolution, the tick labels of are cut of. We need to find out why, but for the moment we add a couple of pixels instead. */ sz += QSize(3, 2); } #endif } sz = map.screenToLayout(sz); return sz; }
QColor QwtText::usedColor | ( | const QColor & | defaultColor ) | const |
Return the color of the text, if it has one. Otherwise return defaultColor.
defaultColor | Default color |
Definition at line 346 of file qwt_text.cpp.
References QwtText::PrivateData::color, QwtText::PrivateData::paintAttributes, and PaintUsingTextColor.
{ if ( d_data->paintAttributes & PaintUsingTextColor ) return d_data->color; return defaultColor; }
QFont QwtText::usedFont | ( | const QFont & | defaultFont ) | const |
Return the font of the text, if it has one. Otherwise return defaultFont.
defaultFont | Default font |
Definition at line 312 of file qwt_text.cpp.
References QwtText::PrivateData::font, QwtText::PrivateData::paintAttributes, and PaintUsingTextFont.
Referenced by QwtPicker::drawTracker(), heightForWidth(), and textSize().
{ if ( d_data->paintAttributes & PaintUsingTextFont ) return d_data->font; return defaultFont; }