From 00c95be2102200e3ea0487ab19893b2c2b4fd770 Mon Sep 17 00:00:00 2001 From: Lucas Murray Date: Tue, 5 Aug 2008 12:26:16 +0000 Subject: [PATCH] Added text alignment support to paintText() functions svn path=/trunk/KDE/kdebase/workspace/; revision=842497 --- effects/presentwindows.cpp | 9 ++++--- lib/kwineffects.cpp | 48 ++++++++++++++++++++++++++++---------- lib/kwineffects.h | 28 +++++++++++++++++++++- 3 files changed, 67 insertions(+), 18 deletions(-) diff --git a/effects/presentwindows.cpp b/effects/presentwindows.cpp index fb81a767fc..ce24dcc2c4 100644 --- a/effects/presentwindows.cpp +++ b/effects/presentwindows.cpp @@ -205,17 +205,16 @@ void PresentWindowsEffect::paintWindow( EffectWindow* w, int mask, QRegion regio if (drawWindowCaptions) { QString text = w->caption(); - double centerx = w->x() + data.xTranslate + w->width() * data.xScale * 0.5f; - double centery = w->y() + data.yTranslate + w->height() * data.yScale * 0.5f; - int maxwidth = (int)(w->width() * data.xScale - 20); + QRect textArea( w->x() + data.xTranslate, w->y() + data.yTranslate, + w->width() * data.xScale, w->height() * data.yScale ); + textArea.adjust( 10, 10, -10, -10 ); double opacity = (0.7 + 0.2*windata.highlight) * data.opacity * mActiveness; QColor textcolor( 255, 255, 255, (int)(255*opacity) ); QColor bgcolor( 0, 0, 0, (int)(255*opacity) ); QFont f; f.setBold( true ); f.setPointSize( 12 ); - effects->paintTextWithBackground( text, QPoint( (int)centerx, (int)centery ), maxwidth, - textcolor, bgcolor, f); + effects->paintTextWithBackground( text, textArea, textcolor, bgcolor, f ); } } } diff --git a/lib/kwineffects.cpp b/lib/kwineffects.cpp index 702bf93209..51f3999fd8 100644 --- a/lib/kwineffects.cpp +++ b/lib/kwineffects.cpp @@ -333,13 +333,13 @@ KConfigGroup EffectsHandler::effectConfig( const QString& effectname ) return kwinconfig->group( "Effect-" + effectname ); } -bool EffectsHandler::paintText( const QString& text, const QPoint& center, int maxwidth, - const QColor& color, const QFont& font ) +bool EffectsHandler::paintText( const QString& text, const QRect& rect, const QColor& color, + const QFont& font, const Qt::Alignment& alignment ) { QPainter p; // Calculate size of the text 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 ); // 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(); // Area covered by text - QRect area( center.x() - textrect.width() / 2, center.y() - textrect.height() / 2, - textrect.width(), textrect.height() ); + int rectX, rectY; + 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 if( effects->compositingType() == OpenGLCompositing ) @@ -399,17 +411,29 @@ bool EffectsHandler::paintText( const QString& text, const QPoint& center, int m return false; } -bool EffectsHandler::paintTextWithBackground( const QString& text, const QPoint& center, int maxwidth, - const QColor& color, const QColor& bgcolor, const QFont& font ) +bool EffectsHandler::paintTextWithBackground( const QString& text, const QRect& rect, const QColor& color, + const QColor& bgcolor, const QFont& font, const Qt::Alignment& alignment ) { // Calculate size of the text 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 ); // Area covered by text - QRect area( center.x() - textrect.width() / 2, center.y() - textrect.height() / 2, - textrect.width(), textrect.height() ); + int rectX, rectY; + 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 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() ); renderRoundBox( area.adjusted( -8, -3, 8, 3 ), 5 ); - return paintText( text, center, maxwidth, color, font ); + return paintText( text, rect, color, font, alignment ); } #endif #ifdef KWIN_HAVE_XRENDER_COMPOSITING if( effects->compositingType() == XRenderCompositing ) { 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 return false; diff --git a/lib/kwineffects.h b/lib/kwineffects.h index e0b971c8d7..63253b8caa 100644 --- a/lib/kwineffects.h +++ b/lib/kwineffects.h @@ -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_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( \ 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, 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, const QColor& color, const QColor& bgcolor, 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; +/*************************************************************** + 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 ***************************************************************/