Classes | Public Types | Public Member Functions | Static Public Member Functions

QwtText Class Reference

A class representing a text. More...

#include <qwt_text.h>

Collaboration diagram for QwtText:
Collaboration graph
[legend]

List of all members.

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.
QwtTextoperator= (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 QwtTextEnginetextEngine (const QString &text, QwtText::TextFormat=AutoText)
static const QwtTextEnginetextEngine (QwtText::TextFormat)
 Find the text engine for a text format.
static void setTextEngine (QwtText::TextFormat, QwtTextEngine *)

Detailed Description

A class representing a text.

A QwtText is a text including a set of attributes how to render it.

See also:
QwtTextEngine, QwtTextLabel

Definition at line 51 of file qwt_text.h.


Member Enumeration Documentation

Layout Attributes.

The layout attributes affects some aspects of the layout of the text.

  • MinimumLayout
    Layout the text without its margins. This mode is useful if a text needs to be aligned accurately, like the tick labels of a scale. If QwtTextEngine::textMargins is not implemented for the format of the text, MinimumLayout has no effect.
Enumerator:
MinimumLayout 

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.

  • PaintUsingTextFont
    The text has an individual font.
  • PaintUsingTextColor
    The text has an individual color.
  • PaintBackground
    The text has an individual background.
Enumerator:
PaintUsingTextFont 
PaintUsingTextColor 
PaintBackground 

Definition at line 111 of file qwt_text.h.

Text format.

The text format defines the QwtTextEngine, that is used to render the text.

  • AutoText
    The text format is determined using QwtTextEngine::mightRender for all available text engines in increasing order > PlainText. If none of the text engines can render the text is rendered like PlainText.
  • PlainText
    Draw the text as it is, using a QwtPlainTextEngine.
  • RichText
    Use the Scribe framework (Qt Rich Text) to render the text.
  • MathMLText
    Use a MathML (http://en.wikipedia.org/wiki/MathML) render engine to display the text. The Qwt MathML extension offers such an engine based on the MathML renderer of the Qt solutions package. Unfortunately it is only available for owners of a commercial Qt license.
  • TeXText
    Use a TeX (http://en.wikipedia.org/wiki/TeX) render engine to display the text.
  • OtherFormat
    The number of text formats can be extended using setTextEngine. Formats >= OtherFormat are not used by Qwt.
See also:
QwtTextEngine, setTextEngine()
Enumerator:
AutoText 
PlainText 
RichText 
MathMLText 
TeXText 
OtherFormat 

Definition at line 85 of file qwt_text.h.


Constructor & Destructor Documentation

QwtText::QwtText ( const QString &  text = QString::null,
QwtText::TextFormat  textFormat = AutoText 
)

Constructor

Parameters:
textText content
textFormatText 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 (  )

Destructor.

Definition at line 200 of file qwt_text.cpp.

{
    delete d_data;
    delete d_layoutCache;
}

Member Function Documentation

QBrush QwtText::backgroundBrush (  ) const
Returns:
Background brush
See also:
setBackgroundBrush(), backgroundPen()

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
Returns:
Background pen
See also:
setBackgroundPen(), backgroundBrush()

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

Parameters:
painterPainter
rectRectangle

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

Parameters:
defaultFontFont, used for the calculation if the text has no font
widthWidth
Returns:
Calculated height

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]
bool QwtText::isNull (  ) const [inline]
Returns:
text().isNull()

Definition at line 149 of file qwt_text.h.

{ return text().isNull(); }
int QwtText::operator!= ( const QwtText other ) const

Relational operator.

Definition at line 228 of file qwt_text.cpp.

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

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
void QwtText::setBackgroundBrush ( const QBrush &  brush )

Set the background brush

Parameters:
brushBackground brush
See also:
backgroundBrush(), setBackgroundPen()

Definition at line 381 of file qwt_text.cpp.

References QwtText::PrivateData::backgroundBrush, PaintBackground, and setPaintAttribute().

Referenced by QmitkHistogramWidget::OnSelect().

void QwtText::setBackgroundPen ( const QPen &  pen )

Set the background pen

Parameters:
penBackground pen
See also:
backgroundPen(), setBackgroundBrush()

Definition at line 360 of file qwt_text.cpp.

References QwtText::PrivateData::backgroundPen, PaintBackground, and setPaintAttribute().

void QwtText::setColor ( const QColor &  color )

Set the pen color used for painting the text.

Parameters:
colorColor
Note:
Setting the color might have no effect, when the text contains control sequences for setting colors.

Definition at line 327 of file qwt_text.cpp.

References color(), QwtText::PrivateData::color, PaintUsingTextColor, and setPaintAttribute().

Referenced by QwtPlotPrintFilter::apply(), and QwtPlotPrintFilter::reset().

void QwtText::setFont ( const QFont &  font )

Set the font.

Parameters:
fontFont
Note:
Setting the font might have no effect, when the text contains control sequences for setting fonts.

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().

void QwtText::setLayoutAttribute ( LayoutAttribute  attribute,
bool  on = true 
)

Change a layout attribute

Parameters:
attributeLayout attribute
onOn/Off
See also:
testLayoutAttribute()

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

Parameters:
attributePaint attribute
onOn/Off
Note:
Used by setFont(), setColor(), setBackgroundPen() and setBackgroundBrush()
See also:
testPaintAttribute()

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

Parameters:
renderFlagsBitwise OR of the flags used like in QPainter::drawText
See also:
renderFlags(), QwtTextEngine::draw()
Note:
Some renderFlags might have no effect, depending on the text format.

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

Parameters:
textText content
textFormatText format
See also:
text()

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.

Parameters:
formatText format
engineText engine
See also:
QwtMathMLTextEngine
Warning:
Using QwtText::AutoText does nothing.

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

Parameters:
attributeLayout attribute
Returns:
true, if attribute is enabled
See also:
setLayoutAttribute()

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

Parameters:
attributePaint attribute
Returns:
true, if attribute is enabled
See also:
setPaintAttribute()

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.

See also:
setText()

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.

Parameters:
formatText format
Returns:
The text engine, or NULL if no engine is available.

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.

Parameters:
textText, needed in case of AutoText
formatText 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

Parameters:
defaultFontFont, used for the calculation if the text has no font
Returns:
Calculated height

Returns the size, that is needed to render text

Parameters:
defaultFontFont of the text
Returns:
Caluclated size

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.

Parameters:
defaultColorDefault color
See also:
setColor(), color(), PaintAttributes

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.

Parameters:
defaultFontDefault font
See also:
setFont(), font(), PaintAttributes

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

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines