QCustomPlot和QWt各有千秋
前者偏轻,但是功能有点弱了,可以修改QCustomPlot增加Qwt的一些模块和类
如下 代码为QComstomPlot使用QwtText的例子
class QwtText : public QCPLayerable
{
public:
/*!
\brief Text format
The text format defines the QwtTextEngine, that is used to render
the text.
\sa QwtTextEngine, setTextEngine()
*/
enum TextFormat
{
/*!
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 QwtText::PlainText.
*/
AutoText = 0,
//! Draw the text as it is, using a QwtPlainTextEngine.
PlainText,
//! Use the Scribe framework (Qt Rich Text) to render the text.
RichText,
/*!
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.
To enable MathML support the following code needs to be added to the
application:
\verbatim QwtText::setTextEngine(QwtText::MathMLText, new QwtMathMLTextEngine()); \endverbatim
*/
MathMLText,
/*!
Use a TeX (http://en.wikipedia.org/wiki/TeX) render engine
to display the text ( not implemented yet ).
*/
TeXText,
/*!
The number of text formats can be extended using setTextEngine.
Formats >= QwtText::OtherFormat are not used by Qwt.
*/
OtherFormat = 100
};
/*!
\brief Paint Attributes
Font and color and background are optional attributes of a QwtText.
The paint attributes hold the information, if they are set.
*/
enum PaintAttribute
{
//! The text has an individual font.
PaintUsingTextFont = 0x01,
//! The text has an individual color.
PaintUsingTextColor = 0x02,
//! The text has an individual background.
PaintBackground = 0x04
};
//! Paint attributes
typedef QFlags<PaintAttribute> PaintAttributes;
/*!
\brief Layout Attributes
The layout attributes affects some aspects of the layout of the text.
*/
enum LayoutAttribute
{
/*!
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.
*/
MinimumLayout = 0x01
};
//! Layout attributes
typedef QFlags<LayoutAttribute> LayoutAttributes;
QwtText(QCustomPlot *plot,const QString & = QString::null,
TextFormat textFormat = AutoText );
~QwtText();
void setText( const QString &,
QwtText::TextFormat textFormat = AutoText );
QString text() const;
bool isNull() const;
bool isEmpty() const;
void setFont( const QFont & );
QFont font() const;
QFont usedFont( const QFont & ) const;
void setRenderFlags( int flags );
int renderFlags() const;
void setColor( const QColor & );
QColor color() const;
QColor usedColor( const QColor & ) const;
void setBorderRadius( double );
double borderRadius() const;
void setBorderPen( const QPen & );
QPen borderPen() 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;
double heightForWidth( double width, const QFont & = QFont() ) const;
QSizeF textSize( const QFont & = QFont() ) const;
//void draw( QPainter *painter, const QRectF &rect ) const;
virtual void applyDefaultAntialiasingHint(QCPPainter* painter)const;
virtual void draw(QCPPainter* painter);
static const QwtTextEngine *textEngine(
const QString &text, QwtText::TextFormat = AutoText );
static const QwtTextEngine *textEngine( QwtText::TextFormat );
static void setTextEngine( QwtText::TextFormat, QwtTextEngine * );
private:
class PrivateData;
PrivateData *d_data;
class LayoutCache;
LayoutCache *d_layoutCache;
};
//! \return text().isNull()
inline bool QwtText::isNull() const
{
return text().isNull();
}
//! \return text().isEmpty()
inline bool QwtText::isEmpty() const
{
return text().isEmpty();
}
Q_DECLARE_OPERATORS_FOR_FLAGS( QwtText::PaintAttributes )
Q_DECLARE_OPERATORS_FOR_FLAGS( QwtText::LayoutAttributes )
class QwtTextEngineDict
{
public:
static QwtTextEngineDict &dict();
void setTextEngine( QwtText::TextFormat, QwtTextEngine * );
const QwtTextEngine *textEngine( QwtText::TextFormat ) const;
const QwtTextEngine *textEngine( const QString &,
QwtText::TextFormat ) const;
private:
QwtTextEngineDict();
~QwtTextEngineDict();
typedef QMap<int, QwtTextEngine *> EngineMap;
inline const QwtTextEngine *engine( EngineMap::const_iterator &it ) const
{
return it.value();
}
EngineMap d_map;
};
QwtTextEngineDict &QwtTextEngineDict::dict()
{
static QwtTextEngineDict engineDict;
return engineDict;
}
QwtTextEngineDict::QwtTextEngineDict()
{
d_map.insert( QwtText::PlainText, new QwtPlainTextEngine() );
#ifndef QT_NO_RICHTEXT
d_map.insert( QwtText::RichText, new QwtRichTextEngine() );
#endif
}
QwtTextEngineDict::~QwtTextEngineDict()
{
for ( EngineMap::const_iterator it = d_map.begin();
it != d_map.end(); ++it )
{
const QwtTextEngine *textEngine = engine( it );
delete textEngine;
}
}
const QwtTextEngine *QwtTextEngineDict::textEngine( const QString& text,
QwtText::TextFormat format ) const
{
if ( format == QwtText::AutoText )
{
for ( EngineMap::const_iterator it = d_map.begin();
it != d_map.end(); ++it )
{
if ( it.key() != QwtText::PlainText )
{
const QwtTextEngine *e = engine( it );
if ( e && e->mightRender( text ) )
return e;
}
}
}
EngineMap::const_iterator it = d_map.find( format );
if ( it != d_map.end() )
{
const QwtTextEngine *e = engine( it );
if ( e )
return e;
}
it = d_map.find( QwtText::PlainText );
return engine( it );
}
void QwtTextEngineDict::setTextEngine( QwtText::TextFormat format,
QwtTextEngine *engine )
{
if ( format == QwtText::AutoText )
return;
if ( format == QwtText::PlainText && engine == NULL )
return;
EngineMap::const_iterator it = d_map.find( format );
if ( it != d_map.end() )
{
const QwtTextEngine *e = this->engine( it );
if ( e )
delete e;
d_map.remove( format );
}
if ( engine != NULL )
d_map.insert( format, engine );
}
const QwtTextEngine *QwtTextEngineDict::textEngine(
QwtText::TextFormat format ) const
{
const QwtTextEngine *e = NULL;
EngineMap::const_iterator it = d_map.find( format );
if ( it != d_map.end() )
e = engine( it );
return e;
}
class QwtText::PrivateData
{
public:
PrivateData():
renderFlags( Qt::AlignCenter ),
borderRadius( 0 ),
borderPen( Qt::NoPen ),
backgroundBrush( Qt::NoBrush ),
paintAttributes( 0 ),
layoutAttributes( 0 ),
textEngine( NULL )
{
}
int renderFlags;
QString text;
QFont font;
QColor color;
double borderRadius;
QPen borderPen;
QBrush backgroundBrush;
QwtText::PaintAttributes paintAttributes;
QwtText::LayoutAttributes layoutAttributes;
const QwtTextEngine *textEngine;
};
class QwtText::LayoutCache
{
public:
void invalidate()
{
textSize = QSizeF();
}
QFont font;
QSizeF textSize;
};
/*!
Constructor
\param text Text content
\param textFormat Text format
*/
QwtText::QwtText(QCustomPlot* plot, const QString &text, QwtText::TextFormat textFormat ):
QCPLayerable(plot)
{
d_data = new PrivateData;
d_data->text = text;
d_data->textEngine = textEngine( text, textFormat );
d_layoutCache = new LayoutCache;
}
//! Destructor
QwtText::~QwtText()
{
delete d_data;
delete d_layoutCache;
}
/*!
Assign a new text content
\param text Text content
\param textFormat Text format
\sa text()
*/
void QwtText::setText( const QString &text,
QwtText::TextFormat textFormat )
{
d_data->text = text;
d_data->textEngine = textEngine( text, textFormat );
d_layoutCache->invalidate();
}
/*!
\return Text as QString.
\sa setText()
*/
QString QwtText::text() const
{
return d_data->text;
}
/*!
\brief Change the render flags
The default setting is Qt::AlignCenter
\param renderFlags Bitwise OR of the flags used like in QPainter::drawText()
\sa renderFlags(), QwtTextEngine::draw()
\note Some renderFlags might have no effect, depending on the text format.
*/
void QwtText::setRenderFlags( int renderFlags )
{
if ( renderFlags != d_data->renderFlags )
{
d_data->renderFlags = renderFlags;
d_layoutCache->invalidate();
}
}
/*!
\return Render flags
\sa setRenderFlags()
*/
int QwtText::renderFlags() const
{
return d_data->renderFlags;
}
/*!
Set the font.
\param font Font
\note Setting the font might have no effect, when
the text contains control sequences for setting fonts.
*/
void QwtText::setFont( const QFont &font )
{
d_data->font = font;
setPaintAttribute( PaintUsingTextFont );
}
//! Return the font.
QFont QwtText::font() const
{
return d_data->font;
}
/*!
Return the font of the text, if it has one.
Otherwise return defaultFont.
\param defaultFont Default font
\return Font used for drawing the text
\sa setFont(), font(), PaintAttributes
*/
QFont QwtText::usedFont( const QFont &defaultFont ) const
{
if ( d_data->paintAttributes & PaintUsingTextFont )
return d_data->font;
return defaultFont;
}
/*!
Set the pen color used for drawing the text.
\param color Color
\note Setting the color might have no effect, when
the text contains control sequences for setting colors.
*/
void QwtText::setColor( const QColor &color )
{
d_data->color = color;
setPaintAttribute( PaintUsingTextColor );
}
//! Return the pen color, used for painting the text
QColor QwtText::color() const
{
return d_data->color;
}
/*!
Return the color of the text, if it has one.
Otherwise return defaultColor.
\param defaultColor Default color
\return Color used for drawing the text
\sa setColor(), color(), PaintAttributes
*/
QColor QwtText::usedColor( const QColor &defaultColor ) const
{
if ( d_data->paintAttributes & PaintUsingTextColor )
return d_data->color;
return defaultColor;
}
/*!
Set the radius for the corners of the border frame
\param radius Radius of a rounded corner
\sa borderRadius(), setBorderPen(), setBackgroundBrush()
*/
void QwtText::setBorderRadius( double radius )
{
d_data->borderRadius = qMax( 0.0, radius );
}
/*!
\return Radius for the corners of the border frame
\sa setBorderRadius(), borderPen(), backgroundBrush()
*/
double QwtText::borderRadius() const
{
return d_data->borderRadius;
}
/*!
Set the background pen
\param pen Background pen
\sa borderPen(), setBackgroundBrush()
*/
void QwtText::setBorderPen( const QPen &pen )
{
d_data->borderPen = pen;
setPaintAttribute( PaintBackground );
}
/*!
\return Background pen
\sa setBorderPen(), backgroundBrush()
*/
QPen QwtText::borderPen() const
{
return d_data->borderPen;
}
/*!
Set the background brush
\param brush Background brush
\sa backgroundBrush(), setBorderPen()
*/
void QwtText::setBackgroundBrush( const QBrush &brush )
{
d_data->backgroundBrush = brush;
setPaintAttribute( PaintBackground );
}
/*!
\return Background brush
\sa setBackgroundBrush(), borderPen()
*/
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(),
setBorderPen() 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
*/
double QwtText::heightForWidth( double width, const QFont &defaultFont ) const
{
// We want to calculate in screen metrics. So
// we need a font that uses screen metrics
const QFont font( usedFont( defaultFont ), QApplication::desktop() );
double h = 0;
if ( d_data->layoutAttributes & MinimumLayout )
{
double 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 );
}
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
*/
QSizeF QwtText::textSize( const QFont &defaultFont ) const
{
// We want to calculate in screen metrics. So
// we need a font that uses screen metrics
const QFont font( usedFont( defaultFont ), QApplication::desktop() );
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;
}
QSizeF sz = d_layoutCache->textSize;
if ( d_data->layoutAttributes & MinimumLayout )
{
double left, right, top, bottom;
d_data->textEngine->textMargins( font, d_data->text,
left, right, top, bottom );
sz -= QSizeF( left + right, top + bottom );
}
return sz;
}
void QwtText::applyDefaultAntialiasingHint(QCPPainter* painter)const
{
}
void QwtText::draw(QCPPainter* painter)
{
QRectF rect = mParentPlot->rect();
if ( d_data->paintAttributes & PaintBackground )
{
if ( d_data->borderPen != Qt::NoPen ||
d_data->backgroundBrush != Qt::NoBrush )
{
painter->save();
painter->setPen( d_data->borderPen );
painter->setBrush( d_data->backgroundBrush );
if ( d_data->borderRadius == 0 )
{
QwtPainter::drawRect( painter, rect );
}
else
{
painter->setRenderHint( QPainter::Antialiasing, true );
painter->drawRoundedRect( rect,
d_data->borderRadius, d_data->borderRadius );
}
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 );
}
QRectF expandedRect = rect;
if ( d_data->layoutAttributes & MinimumLayout )
{
// We want to calculate in screen metrics. So
// we need a font that uses screen metrics
const QFont font( painter->font(), QApplication::desktop() );
double left, right, top, bottom;
d_data->textEngine->textMargins(
font, d_data->text, left, right, top, 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 returned.
If no text engine is registered for the format QwtPlainTextEngine
is returnd.
\param text Text, needed in case of AutoText
\param format Text format
\return Corresponding text engine
*/
const QwtTextEngine *QwtText::textEngine( const QString &text,
QwtText::TextFormat format )
{
return QwtTextEngineDict::dict().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.
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 )
{
QwtTextEngineDict::dict().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.
\param format Text format
\return The text engine, or NULL if no engine is available.
*/
const QwtTextEngine *QwtText::textEngine( QwtText::TextFormat format )
{
return QwtTextEngineDict::dict().textEngine( format );
}
主要是修改了draw函数和去掉了几个构造函数