地面站终端 App
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

685 lines
16 KiB

15 years ago
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2003 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_text.h"
#include "qwt_painter.h"
#include "qwt_text_engine.h"
15 years ago
#include <qmap.h>
#include <qfont.h>
#include <qcolor.h>
#include <qpen.h>
#include <qbrush.h>
#include <qpainter.h>
#include <qapplication.h>
#include <qdesktopwidget.h>
#include <qmath.h>
15 years ago
class QwtTextEngineDict
{
public:
static QwtTextEngineDict &dict();
15 years ago
void setTextEngine( QwtText::TextFormat, QwtTextEngine * );
const QwtTextEngine *textEngine( QwtText::TextFormat ) const;
const QwtTextEngine *textEngine( const QString &,
QwtText::TextFormat ) const;
15 years ago
private:
QwtTextEngineDict();
~QwtTextEngineDict();
15 years ago
typedef QMap<int, QwtTextEngine *> EngineMap;
inline const QwtTextEngine *engine( EngineMap::const_iterator &it ) const
{
15 years ago
return it.value();
}
EngineMap d_map;
};
QwtTextEngineDict &QwtTextEngineDict::dict()
{
static QwtTextEngineDict engineDict;
return engineDict;
}
15 years ago
QwtTextEngineDict::QwtTextEngineDict()
{
d_map.insert( QwtText::PlainText, new QwtPlainTextEngine() );
15 years ago
#ifndef QT_NO_RICHTEXT
d_map.insert( QwtText::RichText, new QwtRichTextEngine() );
15 years ago
#endif
}
QwtTextEngineDict::~QwtTextEngineDict()
{
for ( EngineMap::const_iterator it = d_map.begin();
it != d_map.end(); ++it )
{
const QwtTextEngine *textEngine = engine( it );
15 years ago
delete textEngine;
}
}
const QwtTextEngine *QwtTextEngineDict::textEngine( const QString& text,
QwtText::TextFormat format ) const
15 years ago
{
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;
15 years ago
}
}
}
EngineMap::const_iterator it = d_map.find( format );
if ( it != d_map.end() )
{
const QwtTextEngine *e = engine( it );
15 years ago
if ( e )
return e;
}
it = d_map.find( QwtText::PlainText );
return engine( it );
15 years ago
}
void QwtTextEngineDict::setTextEngine( QwtText::TextFormat format,
QwtTextEngine *engine )
15 years ago
{
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 );
15 years ago
if ( e )
delete e;
d_map.remove( format );
15 years ago
}
if ( engine != NULL )
d_map.insert( format, engine );
15 years ago
}
const QwtTextEngine *QwtTextEngineDict::textEngine(
QwtText::TextFormat format ) const
15 years ago
{
const QwtTextEngine *e = NULL;
EngineMap::const_iterator it = d_map.find( format );
15 years ago
if ( it != d_map.end() )
e = engine( it );
15 years ago
return e;
}
class QwtText::PrivateData
{
public:
PrivateData():
renderFlags( Qt::AlignCenter ),
borderRadius( 0 ),
borderPen( Qt::NoPen ),
backgroundBrush( Qt::NoBrush ),
paintAttributes( 0 ),
layoutAttributes( 0 ),
textEngine( NULL )
{
15 years ago
}
int renderFlags;
QString text;
QFont font;
QColor color;
double borderRadius;
QPen borderPen;
15 years ago
QBrush backgroundBrush;
QwtText::PaintAttributes paintAttributes;
QwtText::LayoutAttributes layoutAttributes;
15 years ago
const QwtTextEngine *textEngine;
};
class QwtText::LayoutCache
{
public:
void invalidate()
{
textSize = QSizeF();
15 years ago
}
QFont font;
QSizeF textSize;
15 years ago
};
/*!
Constructor
\param text Text content
\param textFormat Text format
*/
QwtText::QwtText( const QString &text, QwtText::TextFormat textFormat )
15 years ago
{
d_data = new PrivateData;
d_data->text = text;
d_data->textEngine = textEngine( text, textFormat );
15 years ago
d_layoutCache = new LayoutCache;
}
//! Copy constructor
QwtText::QwtText( const QwtText &other )
15 years ago
{
d_data = new PrivateData;
*d_data = *other.d_data;
d_layoutCache = new LayoutCache;
*d_layoutCache = *other.d_layoutCache;
}
//! Destructor
QwtText::~QwtText()
15 years ago
{
delete d_data;
delete d_layoutCache;
}
//! Assignment operator
QwtText &QwtText::operator=( const QwtText & other )
15 years ago
{
*d_data = *other.d_data;
*d_layoutCache = *other.d_layoutCache;
return *this;
}
//! Relational operator
bool QwtText::operator==( const QwtText &other ) const
15 years ago
{
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->borderRadius == other.d_data->borderRadius &&
d_data->borderPen == other.d_data->borderPen &&
d_data->backgroundBrush == other.d_data->backgroundBrush &&
d_data->paintAttributes == other.d_data->paintAttributes &&
d_data->textEngine == other.d_data->textEngine;
15 years ago
}
//! Relational operator
bool QwtText::operator!=( const QwtText &other ) const // invalidate
15 years ago
{
return !( other == *this );
15 years ago
}
/*!
Assign a new text content
\param text Text content
\param textFormat Text format
\sa text()
15 years ago
*/
void QwtText::setText( const QString &text,
QwtText::TextFormat textFormat )
{
d_data->text = text;
d_data->textEngine = textEngine( text, textFormat );
15 years ago
d_layoutCache->invalidate();
}
/*!
\return Text as QString.
\sa setText()
15 years ago
*/
QString QwtText::text() const
{
return d_data->text;
15 years ago
}
/*!
\brief Change the render flags
The default setting is Qt::AlignCenter
\param renderFlags Bitwise OR of the flags used like in QPainter::drawText()
15 years ago
\sa renderFlags(), QwtTextEngine::draw()
15 years ago
\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;
15 years ago
d_layoutCache->invalidate();
}
}
/*!
\return Render flags
\sa setRenderFlags()
15 years ago
*/
int QwtText::renderFlags() const
{
return d_data->renderFlags;
15 years ago
}
/*!
15 years ago
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 )
15 years ago
{
d_data->font = font;
setPaintAttribute( PaintUsingTextFont );
15 years ago
}
//! Return the font.
QFont QwtText::font() const
{
return d_data->font;
15 years ago
}
/*!
Return the font of the text, if it has one.
Otherwise return defaultFont.
\param defaultFont Default font
\return Font used for drawing the text
15 years ago
\sa setFont(), font(), PaintAttributes
15 years ago
*/
QFont QwtText::usedFont( const QFont &defaultFont ) const
15 years ago
{
if ( d_data->paintAttributes & PaintUsingTextFont )
return d_data->font;
return defaultFont;
}
/*!
Set the pen color used for drawing the text.
15 years ago
\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 );
15 years ago
}
//! Return the pen color, used for painting the text
QColor QwtText::color() const
{
return d_data->color;
15 years ago
}
/*!
Return the color of the text, if it has one.
15 years ago
Otherwise return defaultColor.
\param defaultColor Default color
\return Color used for drawing the text
\sa setColor(), color(), PaintAttributes
15 years ago
*/
QColor QwtText::usedColor( const QColor &defaultColor ) const
15 years ago
{
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;
}
/*!
15 years ago
Set the background pen
\param pen Background pen
\sa borderPen(), setBackgroundBrush()
15 years ago
*/
void QwtText::setBorderPen( const QPen &pen )
{
d_data->borderPen = pen;
setPaintAttribute( PaintBackground );
15 years ago
}
/*!
15 years ago
\return Background pen
\sa setBorderPen(), backgroundBrush()
15 years ago
*/
QPen QwtText::borderPen() const
{
return d_data->borderPen;
15 years ago
}
/*!
Set the background brush
\param brush Background brush
\sa backgroundBrush(), setBorderPen()
15 years ago
*/
void QwtText::setBackgroundBrush( const QBrush &brush )
{
d_data->backgroundBrush = brush;
setPaintAttribute( PaintBackground );
15 years ago
}
/*!
15 years ago
\return Background brush
\sa setBackgroundBrush(), borderPen()
15 years ago
*/
QBrush QwtText::backgroundBrush() const
{
return d_data->backgroundBrush;
15 years ago
}
/*!
Change a paint attribute
\param attribute Paint attribute
\param on On/Off
\note Used by setFont(), setColor(),
setBorderPen() and setBackgroundBrush()
\sa testPaintAttribute()
15 years ago
*/
void QwtText::setPaintAttribute( PaintAttribute attribute, bool on )
15 years ago
{
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()
15 years ago
*/
bool QwtText::testPaintAttribute( PaintAttribute attribute ) const
15 years ago
{
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 )
15 years ago
{
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()
15 years ago
*/
bool QwtText::testLayoutAttribute( LayoutAttribute attribute ) const
15 years ago
{
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
15 years ago
{
// We want to calculate in screen metrics. So
// we need a font that uses screen metrics
const QFont font( usedFont( defaultFont ), QApplication::desktop() );
15 years ago
double h = 0;
15 years ago
if ( d_data->layoutAttributes & MinimumLayout )
{
double left, right, top, bottom;
d_data->textEngine->textMargins( font, d_data->text,
left, right, top, bottom );
15 years ago
h = d_data->textEngine->heightForWidth(
font, d_data->renderFlags, d_data->text,
width + left + right );
15 years ago
h -= top + bottom;
}
else
{
15 years ago
h = d_data->textEngine->heightForWidth(
font, d_data->renderFlags, d_data->text, width );
15 years ago
}
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
15 years ago
{
// We want to calculate in screen metrics. So
// we need a font that uses screen metrics
const QFont font( usedFont( defaultFont ), QApplication::desktop() );
15 years ago
if ( !d_layoutCache->textSize.isValid()
|| d_layoutCache->font != font )
{
15 years ago
d_layoutCache->textSize = d_data->textEngine->textSize(
font, d_data->renderFlags, d_data->text );
15 years ago
d_layoutCache->font = font;
}
QSizeF sz = d_layoutCache->textSize;
15 years ago
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 );
15 years ago
}
return sz;
}
/*!
Draw a text into a rectangle
\param painter Painter
\param rect Rectangle
*/
void QwtText::draw( QPainter *painter, const QRectF &rect ) const
15 years ago
{
if ( d_data->paintAttributes & PaintBackground )
{
if ( d_data->borderPen != Qt::NoPen ||
d_data->backgroundBrush != Qt::NoBrush )
{
15 years ago
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 );
}
15 years ago
painter->restore();
}
}
painter->save();
if ( d_data->paintAttributes & PaintUsingTextFont )
{
painter->setFont( d_data->font );
15 years ago
}
if ( d_data->paintAttributes & PaintUsingTextColor )
{
15 years ago
if ( d_data->color.isValid() )
painter->setPen( d_data->color );
15 years ago
}
QRectF expandedRect = rect;
if ( d_data->layoutAttributes & MinimumLayout )
{
15 years ago
// We want to calculate in screen metrics. So
// we need a font that uses screen metrics
const QFont font( painter->font(), QApplication::desktop() );
15 years ago
double left, right, top, bottom;
15 years ago
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 );
15 years ago
}
d_data->textEngine->draw( painter, expandedRect,
d_data->renderFlags, d_data->text );
15 years ago
painter->restore();
}
/*!
Find the text engine for a text format
In case of QwtText::AutoText the first text engine
15 years ago
(beside QwtPlainTextEngine) is returned, where QwtTextEngine::mightRender
returns true. If there is none QwtPlainTextEngine is returned.
15 years ago
If no text engine is registered for the format QwtPlainTextEngine
15 years ago
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 );
15 years ago
}
/*!
Assign/Replace a text engine for a text format
With setTextEngine it is possible to extend Qwt with
other types of text formats.
15 years ago
For QwtText::PlainText it is not allowed to assign a engine == NULL.
15 years ago
\param format Text format
\param engine Text engine
\sa QwtMathMLTextEngine
\warning Using QwtText::AutoText does nothing.
*/
void QwtText::setTextEngine( QwtText::TextFormat format,
QwtTextEngine *engine )
15 years ago
{
QwtTextEngineDict::dict().setTextEngine( format, engine );
15 years ago
}
/*!
\brief Find the text engine for a text format
textEngine can be used to find out if a text format is supported.
15 years ago
\param format Text format
\return The text engine, or NULL if no engine is available.
*/
const QwtTextEngine *QwtText::textEngine( QwtText::TextFormat format )
15 years ago
{
return QwtTextEngineDict::dict().textEngine( format );
15 years ago
}