Turn paintText() into two generic methods: one just paints the text, the other also paints

background. Move the methods into kwineffects.*

svn path=/trunk/KDE/kdebase/workspace/; revision=707699
This commit is contained in:
Rivo Laks 2007-09-02 18:20:36 +00:00
parent 106fbc23ab
commit d618ba8d23
5 changed files with 132 additions and 78 deletions

View file

@ -197,8 +197,15 @@ void PresentWindowsEffect::paintWindow( EffectWindow* w, int mask, QRegion regio
QString text = w->caption();
float centerx = w->x() + data.xTranslate + w->width() * data.xScale * 0.5f;
float centery = w->y() + data.yTranslate + w->height() * data.yScale * 0.5f;
float textopacity = (0.7 + 0.2*windata.hover) * data.opacity;
paintText( text, QPointF(centerx, centery), w->width() * data.xScale - 20, textopacity );
int maxwidth = (int)(w->width() * data.xScale - 20);
float opacity = (0.7 + 0.2*windata.hover) * data.opacity;
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);
}
}
@ -894,80 +901,5 @@ void PresentWindowsEffect::paintWindowIcon( EffectWindow* w, WindowPaintData& pa
#endif
}
void PresentWindowsEffect::paintText( const QString& text, const QPointF& center, float maxwidth, float alpha )
{
QPainter p;
// Set a bigger font
QFont f = p.font();
f.setBold( true );
f.setPointSize( 12 );
// Calculate size of the text
QFontMetricsF fm( f );
QString painttext = fm.elidedText( text, Qt::ElideRight, maxwidth );
QRectF textrect = fm.boundingRect( painttext );
// Create temporary QPixmap where the text will be drawn onto
QPixmap textPixmap( textrect.width(), textrect.height());
textPixmap.fill( Qt::transparent );
// Draw the text
p.begin( &textPixmap );
p.setFont( f );
p.setRenderHint( QPainter::TextAntialiasing );
p.setPen( Qt::white );
p.drawText( -textrect.topLeft(), painttext );
p.end();
// Area covered by text
QRect area( center.x() - textrect.width() / 2, center.y() - textrect.height() / 2,
textrect.width(), textrect.height() );
#ifdef HAVE_OPENGL
if( effects->compositingType() == OpenGLCompositing )
{
GLTexture textTexture( textPixmap, GL_TEXTURE_RECTANGLE_ARB );
glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_TEXTURE_BIT );
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glColor4f( 0.0f, 0.0f, 0.0f, alpha );
renderRoundBox( area.adjusted( -8, -3, 8, 3 ), 5 );
textTexture.bind();
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glColor4f( 1.0f, 1.0f, 1.0f, alpha );
const float verts[ 4 * 2 ] =
{
area.x(), area.y(),
area.x(), area.y() + area.height(),
area.x() + area.width(), area.y() + area.height(),
area.x() + area.width(), area.y()
};
const float texcoords[ 4 * 2 ] =
{
0, textPixmap.height(),
0, 0,
textPixmap.width(), 0,
textPixmap.width(), textPixmap.height()
};
renderGLGeometry( 4, verts, texcoords );
textTexture.unbind();
glPopAttrib();
}
#endif
#ifdef HAVE_XRENDER
if( effects->compositingType() == XRenderCompositing )
{
static XRenderPictFormat* alphaFormat = 0;
if( !alphaFormat)
alphaFormat = XRenderFindStandardFormat( display(), PictStandardARGB32 );
Picture textPicture;
textPicture = XRenderCreatePicture( display(), textPixmap.handle(), alphaFormat, 0, NULL );
XRenderComposite( display(), textPixmap.depth() == 32 ? PictOpOver : PictOpSrc,
textPicture, None, effects->xrenderBufferPicture(),
0, 0, 0, 0, area.x(), area.y(), area.width(), area.height());
XRenderFreePicture( display(), textPicture );
}
#endif
}
} // namespace
#include "presentwindows.moc"

View file

@ -72,7 +72,6 @@ class PresentWindowsEffect
void discardFilterTexture();
void paintWindowIcon( EffectWindow* w, WindowPaintData& data );
void paintText( const QString& text, const QPointF& center, float maxwidth, float alpha );
// Called once the effect is activated (and wasn't activated before)
void effectActivated();

View file

@ -44,6 +44,9 @@ if(OPENGL_FOUND)
target_link_libraries(kwineffects ${DL_LIBRARY})
endif(DL_LIBRARY)
endif(OPENGL_FOUND)
if (X11_Xrender_FOUND)
target_link_libraries(kwineffects ${X11_Xrender_LIB})
endif (X11_Xrender_FOUND)
install( FILES kwinglobals.h kwineffects.h DESTINATION ${INCLUDE_INSTALL_DIR})

View file

@ -10,15 +10,25 @@ License. See the file "COPYING" for the exact licensing terms.
#include "kwineffects.h"
#include "kwinglutils.h"
#include <QtDBus/QtDBus>
#include <QVariant>
#include <QList>
#include <QtGui/QFontMetrics>
#include <QtGui/QPainter>
#include <QtGui/QPixmap>
#include <kdebug.h>
#include <ksharedconfig.h>
#include <assert.h>
#ifdef HAVE_XRENDER
#include <X11/extensions/Xrender.h>
#endif
namespace KWin
{
@ -275,6 +285,101 @@ 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 )
{
QPainter p;
// Calculate size of the text
QFontMetrics fm( font );
QString painttext = fm.elidedText( text, Qt::ElideRight, maxwidth );
QRect textrect = fm.boundingRect( painttext );
// Create temporary QPixmap where the text will be drawn onto
QPixmap textPixmap( textrect.width(), textrect.height());
textPixmap.fill( Qt::transparent );
// Draw the text
p.begin( &textPixmap );
p.setFont( font );
p.setRenderHint( QPainter::TextAntialiasing );
p.setPen( color );
p.drawText( -textrect.topLeft(), painttext );
p.end();
// Area covered by text
QRect area( center.x() - textrect.width() / 2, center.y() - textrect.height() / 2,
textrect.width(), textrect.height() );
#ifdef HAVE_OPENGL
if( effects->compositingType() == OpenGLCompositing )
{
GLTexture textTexture( textPixmap, GL_TEXTURE_RECTANGLE_ARB );
glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_TEXTURE_BIT );
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
textTexture.bind();
const float verts[ 4 * 2 ] =
{
area.x(), area.y(),
area.x(), area.y() + area.height(),
area.x() + area.width(), area.y() + area.height(),
area.x() + area.width(), area.y()
};
const float texcoords[ 4 * 2 ] =
{
0, textPixmap.height(),
0, 0,
textPixmap.width(), 0,
textPixmap.width(), textPixmap.height()
};
renderGLGeometry( 4, verts, texcoords );
textTexture.unbind();
glPopAttrib();
return true;
}
#endif
#ifdef HAVE_XRENDER
if( effects->compositingType() == XRenderCompositing )
{
static XRenderPictFormat* alphaFormat = 0;
if( !alphaFormat)
alphaFormat = XRenderFindStandardFormat( display(), PictStandardARGB32 );
Picture textPicture;
textPicture = XRenderCreatePicture( display(), textPixmap.handle(), alphaFormat, 0, NULL );
XRenderComposite( display(), textPixmap.depth() == 32 ? PictOpOver : PictOpSrc,
textPicture, None, effects->xrenderBufferPicture(),
0, 0, 0, 0, area.x(), area.y(), area.width(), area.height());
XRenderFreePicture( display(), textPicture );
return true;
}
#endif
return false;
}
bool EffectsHandler::paintTextWithBackground( const QString& text, const QPoint& center, int maxwidth,
const QColor& color, const QColor& bgcolor, const QFont& font )
{
// Calculate size of the text
QFontMetrics fm( font );
QString painttext = fm.elidedText( text, Qt::ElideRight, maxwidth );
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() );
#ifdef HAVE_OPENGL
if( effects->compositingType() == OpenGLCompositing )
{
glColor4f( bgcolor.redF(), bgcolor.greenF(), bgcolor.blueF(), bgcolor.alphaF() );
renderRoundBox( area.adjusted( -8, -3, 8, 3 ), 5 );
return paintText( text, center, maxwidth, color, font );
}
#endif
// TODO: render at least a simple background rect in XRender mode
return false;
}
EffectsHandler* effects = 0;

View file

@ -18,6 +18,7 @@ License. See the file "COPYING" for the exact licensing terms.
#include <QtCore/QPair>
#include <QtCore/QRect>
#include <QtGui/QRegion>
#include <QtGui/QFont>
#include <QtCore/QVector>
#include <QtCore/QList>
@ -234,6 +235,20 @@ class KWIN_EXPORT EffectsHandler
virtual unsigned long xrenderBufferPicture() = 0;
virtual void reconfigure() = 0;
/**
* Paints given text onto screen, possibly in elided form
* @param text
* @param center center point of the painted text
* @param maxwidth if text is longer than this, is will be elided
* @param color color of the text, may contain alpha
* @param font font for the text
**/
bool paintText( const QString& text, const QPoint& center, int maxwidth,
const QColor& color, const QFont& font = QFont() );
bool paintTextWithBackground( const QString& text, const QPoint& center, int maxwidth,
const QColor& color, const QColor& bgcolor,
const QFont& font = QFont() );
/**
* Sends message over DCOP to reload given effect.