📄 qwt_text.cpp
字号:
}
/*!
Set the background pen
\param pen Background pen
\sa backgroundPen, setBackgroundBrush
*/
void QwtText::setBackgroundPen(const QPen &pen)
{
d_data->backgroundPen = pen;
setPaintAttribute(PaintBackground);
}
/*!
\return Background pen
\sa setBackgroundPen, backgroundBrush
*/
QPen QwtText::backgroundPen() const
{
return d_data->backgroundPen;
}
/*!
Set the background brush
\param brush Background brush
\sa backgroundBrush, setBackgroundPen
*/
void QwtText::setBackgroundBrush(const QBrush &brush)
{
d_data->backgroundBrush = brush;
setPaintAttribute(PaintBackground);
}
/*!
\return Background brush
\sa setBackgroundBrush, backgroundPen
*/
QBrush QwtText::backgroundBrush() const
{
return d_data->backgroundBrush;
}
/*!
Change a paint attribute
\param attribute Paint attribute
\param on On/Off
\note Used by setFont, setColor, setBackgroundPen and setBackgroundBrush
\sa testPaintAttribute
*/
void QwtText::setPaintAttribute(PaintAttribute attribute, bool on)
{
if ( on )
d_data->paintAttributes |= attribute;
else
d_data->paintAttributes &= ~attribute;
}
/*!
Test a paint attribute
\param attribute Paint attribute
\return true, if attribute is enabled
\sa setPaintAttribute
*/
bool QwtText::testPaintAttribute(PaintAttribute attribute) const
{
return d_data->paintAttributes & attribute;
}
/*!
Change a layout attribute
\param attribute Layout attribute
\param on On/Off
\sa testLayoutAttribute
*/
void QwtText::setLayoutAttribute(LayoutAttribute attribute, bool on)
{
if ( on )
d_data->layoutAttributes |= attribute;
else
d_data->layoutAttributes &= ~attribute;
}
/*!
Test a layout attribute
\param attribute Layout attribute
\return true, if attribute is enabled
\sa setLayoutAttribute
*/
bool QwtText::testLayoutAttribute(LayoutAttribute attribute) const
{
return d_data->layoutAttributes | attribute;
}
/*!
Find the height for a given width
\param defaultFont Font, used for the calculation if the text has no font
\param width Width
\return Calculated height
*/
int QwtText::heightForWidth(int width, const QFont &defaultFont) const
{
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;
}
/*!
Find the height for a given width
\param defaultFont Font, used for the calculation if the text has no font
\return Calculated height
*/
/*!
Returns the size, that is needed to render text
\param defaultFont Font of the text
\return Caluclated size
*/
QSize QwtText::textSize(const QFont &defaultFont) const
{
#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, 0);
}
#endif
}
sz = map.screenToLayout(sz);
return sz;
}
/*!
Draw a text into a rectangle
\param painter Painter
\param rect Rectangle
*/
void QwtText::draw(QPainter *painter, const QRect &rect) const
{
if ( d_data->paintAttributes & PaintBackground )
{
if ( d_data->backgroundPen != Qt::NoPen ||
d_data->backgroundBrush != Qt::NoBrush )
{
painter->save();
painter->setPen(d_data->backgroundPen);
painter->setBrush(d_data->backgroundBrush);
QwtPainter::drawRect(painter, rect);
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();
}
/*!
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.
\param text Text, needed in case of AutoText
\param format Text format
*/
const QwtTextEngine *QwtText::textEngine(const QString &text,
QwtText::TextFormat format)
{
if ( engineDict == NULL )
engineDict = new QwtTextEngineDict();
return engineDict->textEngine(text, format);
}
/*!
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.
\param format Text format
\param engine Text engine
\sa QwtMathMLTextEngine
\warning Using QwtText::AutoText does nothing.
*/
void QwtText::setTextEngine(QwtText::TextFormat format,
QwtTextEngine *engine)
{
if ( engineDict == NULL )
engineDict = new QwtTextEngineDict();
engineDict->setTextEngine(format, engine);
}
/*!
\brief 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.
\param format Text format
\return The text engine, or NULL if no engine is available.
*/
const QwtTextEngine *QwtText::textEngine(QwtText::TextFormat format)
{
if ( engineDict == NULL )
engineDict = new QwtTextEngineDict();
return engineDict->textEngine(format);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -