Added text alignment support to paintText() functions
svn path=/trunk/KDE/kdebase/workspace/; revision=842497
This commit is contained in:
parent
c1727e6740
commit
00c95be210
3 changed files with 67 additions and 18 deletions
|
@ -205,17 +205,16 @@ void PresentWindowsEffect::paintWindow( EffectWindow* w, int mask, QRegion regio
|
||||||
if (drawWindowCaptions)
|
if (drawWindowCaptions)
|
||||||
{
|
{
|
||||||
QString text = w->caption();
|
QString text = w->caption();
|
||||||
double centerx = w->x() + data.xTranslate + w->width() * data.xScale * 0.5f;
|
QRect textArea( w->x() + data.xTranslate, w->y() + data.yTranslate,
|
||||||
double centery = w->y() + data.yTranslate + w->height() * data.yScale * 0.5f;
|
w->width() * data.xScale, w->height() * data.yScale );
|
||||||
int maxwidth = (int)(w->width() * data.xScale - 20);
|
textArea.adjust( 10, 10, -10, -10 );
|
||||||
double opacity = (0.7 + 0.2*windata.highlight) * data.opacity * mActiveness;
|
double opacity = (0.7 + 0.2*windata.highlight) * data.opacity * mActiveness;
|
||||||
QColor textcolor( 255, 255, 255, (int)(255*opacity) );
|
QColor textcolor( 255, 255, 255, (int)(255*opacity) );
|
||||||
QColor bgcolor( 0, 0, 0, (int)(255*opacity) );
|
QColor bgcolor( 0, 0, 0, (int)(255*opacity) );
|
||||||
QFont f;
|
QFont f;
|
||||||
f.setBold( true );
|
f.setBold( true );
|
||||||
f.setPointSize( 12 );
|
f.setPointSize( 12 );
|
||||||
effects->paintTextWithBackground( text, QPoint( (int)centerx, (int)centery ), maxwidth,
|
effects->paintTextWithBackground( text, textArea, textcolor, bgcolor, f );
|
||||||
textcolor, bgcolor, f);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -333,13 +333,13 @@ KConfigGroup EffectsHandler::effectConfig( const QString& effectname )
|
||||||
return kwinconfig->group( "Effect-" + effectname );
|
return kwinconfig->group( "Effect-" + effectname );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EffectsHandler::paintText( const QString& text, const QPoint& center, int maxwidth,
|
bool EffectsHandler::paintText( const QString& text, const QRect& rect, const QColor& color,
|
||||||
const QColor& color, const QFont& font )
|
const QFont& font, const Qt::Alignment& alignment )
|
||||||
{
|
{
|
||||||
QPainter p;
|
QPainter p;
|
||||||
// Calculate size of the text
|
// Calculate size of the text
|
||||||
QFontMetrics fm( font );
|
QFontMetrics fm( font );
|
||||||
QString painttext = fm.elidedText( text, Qt::ElideRight, maxwidth );
|
QString painttext = fm.elidedText( text, Qt::ElideRight, rect.width() );
|
||||||
QRect textrect = fm.boundingRect( painttext );
|
QRect textrect = fm.boundingRect( painttext );
|
||||||
|
|
||||||
// Create temporary QPixmap where the text will be drawn onto
|
// Create temporary QPixmap where the text will be drawn onto
|
||||||
|
@ -355,8 +355,20 @@ bool EffectsHandler::paintText( const QString& text, const QPoint& center, int m
|
||||||
p.end();
|
p.end();
|
||||||
|
|
||||||
// Area covered by text
|
// Area covered by text
|
||||||
QRect area( center.x() - textrect.width() / 2, center.y() - textrect.height() / 2,
|
int rectX, rectY;
|
||||||
textrect.width(), textrect.height() );
|
if( alignment & Qt::AlignLeft )
|
||||||
|
rectX = rect.x();
|
||||||
|
else if( alignment & Qt::AlignRight )
|
||||||
|
rectX = rect.right() - textrect.width();
|
||||||
|
else
|
||||||
|
rectX = rect.center().x() - textrect.width() / 2;
|
||||||
|
if( alignment & Qt::AlignTop )
|
||||||
|
rectY = rect.y();
|
||||||
|
else if( alignment & Qt::AlignBottom )
|
||||||
|
rectY = rect.bottom() - textrect.height();
|
||||||
|
else
|
||||||
|
rectY = rect.center().y() - textrect.height() / 2;
|
||||||
|
QRect area( rectX, rectY, textrect.width(), textrect.height() );
|
||||||
|
|
||||||
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
|
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
|
||||||
if( effects->compositingType() == OpenGLCompositing )
|
if( effects->compositingType() == OpenGLCompositing )
|
||||||
|
@ -399,17 +411,29 @@ bool EffectsHandler::paintText( const QString& text, const QPoint& center, int m
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EffectsHandler::paintTextWithBackground( const QString& text, const QPoint& center, int maxwidth,
|
bool EffectsHandler::paintTextWithBackground( const QString& text, const QRect& rect, const QColor& color,
|
||||||
const QColor& color, const QColor& bgcolor, const QFont& font )
|
const QColor& bgcolor, const QFont& font, const Qt::Alignment& alignment )
|
||||||
{
|
{
|
||||||
// Calculate size of the text
|
// Calculate size of the text
|
||||||
QFontMetrics fm( font );
|
QFontMetrics fm( font );
|
||||||
QString painttext = fm.elidedText( text, Qt::ElideRight, maxwidth );
|
QString painttext = fm.elidedText( text, Qt::ElideRight, rect.width() );
|
||||||
QRect textrect = fm.boundingRect( painttext );
|
QRect textrect = fm.boundingRect( painttext );
|
||||||
|
|
||||||
// Area covered by text
|
// Area covered by text
|
||||||
QRect area( center.x() - textrect.width() / 2, center.y() - textrect.height() / 2,
|
int rectX, rectY;
|
||||||
textrect.width(), textrect.height() );
|
if( alignment & Qt::AlignLeft )
|
||||||
|
rectX = rect.x();
|
||||||
|
else if( alignment & Qt::AlignRight )
|
||||||
|
rectX = rect.right() - textrect.width();
|
||||||
|
else
|
||||||
|
rectX = rect.center().x() - textrect.width() / 2;
|
||||||
|
if( alignment & Qt::AlignTop )
|
||||||
|
rectY = rect.y();
|
||||||
|
else if( alignment & Qt::AlignBottom )
|
||||||
|
rectY = rect.bottom() - textrect.height();
|
||||||
|
else
|
||||||
|
rectY = rect.center().y() - textrect.height() / 2;
|
||||||
|
QRect area( rectX, rectY, textrect.width(), textrect.height() );
|
||||||
|
|
||||||
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
|
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
|
||||||
if( effects->compositingType() == OpenGLCompositing )
|
if( effects->compositingType() == OpenGLCompositing )
|
||||||
|
@ -417,14 +441,14 @@ bool EffectsHandler::paintTextWithBackground( const QString& text, const QPoint&
|
||||||
glColor4f( bgcolor.redF(), bgcolor.greenF(), bgcolor.blueF(), bgcolor.alphaF() );
|
glColor4f( bgcolor.redF(), bgcolor.greenF(), bgcolor.blueF(), bgcolor.alphaF() );
|
||||||
renderRoundBox( area.adjusted( -8, -3, 8, 3 ), 5 );
|
renderRoundBox( area.adjusted( -8, -3, 8, 3 ), 5 );
|
||||||
|
|
||||||
return paintText( text, center, maxwidth, color, font );
|
return paintText( text, rect, color, font, alignment );
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
||||||
if( effects->compositingType() == XRenderCompositing )
|
if( effects->compositingType() == XRenderCompositing )
|
||||||
{
|
{
|
||||||
xRenderRoundBox( effects->xrenderBufferPicture(), area.adjusted( -8, -3, 8, 3 ), 5, bgcolor );
|
xRenderRoundBox( effects->xrenderBufferPicture(), area.adjusted( -8, -3, 8, 3 ), 5, bgcolor );
|
||||||
return paintText( text, center, maxwidth, color, font );
|
return paintText( text, rect, color, font, alignment );
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -163,7 +163,7 @@ X-KDE-Library=kwin4_effect_cooleffect
|
||||||
|
|
||||||
#define KWIN_EFFECT_API_MAKE_VERSION( major, minor ) (( major ) << 8 | ( minor ))
|
#define KWIN_EFFECT_API_MAKE_VERSION( major, minor ) (( major ) << 8 | ( minor ))
|
||||||
#define KWIN_EFFECT_API_VERSION_MAJOR 0
|
#define KWIN_EFFECT_API_VERSION_MAJOR 0
|
||||||
#define KWIN_EFFECT_API_VERSION_MINOR 51
|
#define KWIN_EFFECT_API_VERSION_MINOR 52
|
||||||
#define KWIN_EFFECT_API_VERSION KWIN_EFFECT_API_MAKE_VERSION( \
|
#define KWIN_EFFECT_API_VERSION KWIN_EFFECT_API_MAKE_VERSION( \
|
||||||
KWIN_EFFECT_API_VERSION_MAJOR, KWIN_EFFECT_API_VERSION_MINOR )
|
KWIN_EFFECT_API_VERSION_MAJOR, KWIN_EFFECT_API_VERSION_MINOR )
|
||||||
|
|
||||||
|
@ -543,9 +543,14 @@ class KWIN_EXPORT EffectsHandler
|
||||||
**/
|
**/
|
||||||
bool paintText( const QString& text, const QPoint& center, int maxwidth,
|
bool paintText( const QString& text, const QPoint& center, int maxwidth,
|
||||||
const QColor& color, const QFont& font = QFont() );
|
const QColor& color, const QFont& font = QFont() );
|
||||||
|
bool paintText( const QString& text, const QRect& rect, const QColor& color,
|
||||||
|
const QFont& font = QFont(), const Qt::Alignment& alignment = Qt::AlignCenter );
|
||||||
bool paintTextWithBackground( const QString& text, const QPoint& center, int maxwidth,
|
bool paintTextWithBackground( const QString& text, const QPoint& center, int maxwidth,
|
||||||
const QColor& color, const QColor& bgcolor,
|
const QColor& color, const QColor& bgcolor,
|
||||||
const QFont& font = QFont() );
|
const QFont& font = QFont() );
|
||||||
|
bool paintTextWithBackground( const QString& text, const QRect& rect, const QColor& color,
|
||||||
|
const QColor& bgcolor, const QFont& font = QFont(),
|
||||||
|
const Qt::Alignment& alignment = Qt::AlignCenter );
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1163,6 +1168,27 @@ class KWIN_EXPORT TimeLine
|
||||||
**/
|
**/
|
||||||
extern KWIN_EXPORT EffectsHandler* effects;
|
extern KWIN_EXPORT EffectsHandler* effects;
|
||||||
|
|
||||||
|
/***************************************************************
|
||||||
|
EffectsHandler
|
||||||
|
***************************************************************/
|
||||||
|
|
||||||
|
inline
|
||||||
|
bool EffectsHandler::paintText( const QString& text, const QPoint& center, int maxwidth,
|
||||||
|
const QColor& color, const QFont& font )
|
||||||
|
{
|
||||||
|
return paintText( text, QRect( center.x() - maxwidth / 2, center.y() - 5000, maxwidth, 10000 ),
|
||||||
|
color, font, Qt::AlignCenter );
|
||||||
|
}
|
||||||
|
|
||||||
|
inline
|
||||||
|
bool EffectsHandler::paintTextWithBackground( const QString& text, const QPoint& center, int maxwidth,
|
||||||
|
const QColor& color, const QColor& bgcolor, const QFont& font )
|
||||||
|
{
|
||||||
|
return paintTextWithBackground( text,
|
||||||
|
QRect( center.x() - maxwidth / 2, center.y() - 5000, maxwidth, 10000 ),
|
||||||
|
color, bgcolor, font, Qt::AlignCenter );
|
||||||
|
}
|
||||||
|
|
||||||
/***************************************************************
|
/***************************************************************
|
||||||
WindowVertex
|
WindowVertex
|
||||||
***************************************************************/
|
***************************************************************/
|
||||||
|
|
Loading…
Reference in a new issue