From 6d6c7a98105dd9df5d5f517ebcf4511a995d679d Mon Sep 17 00:00:00 2001 From: Hugo Pereira Da Costa Date: Tue, 11 May 2010 16:56:00 +0000 Subject: [PATCH] Nicer implementation of shadow functions. Should allow easier tweaking to truncate tails if needed. svn path=/trunk/KDE/kdebase/workspace/; revision=1125530 --- clients/oxygen/oxygenshadowcache.cpp | 55 +++++++++------------------ clients/oxygen/oxygenshadowcache.h | 57 ++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 37 deletions(-) diff --git a/clients/oxygen/oxygenshadowcache.cpp b/clients/oxygen/oxygenshadowcache.cpp index 487efa4fbd..46020d42a6 100644 --- a/clients/oxygen/oxygenshadowcache.cpp +++ b/clients/oxygen/oxygenshadowcache.cpp @@ -183,21 +183,17 @@ namespace Oxygen const qreal hoffset = shadowConfiguration.horizontalOffset()*gradientSize/fixedSize; const qreal voffset = shadowConfiguration.verticalOffset()*gradientSize/fixedSize; - // gaussian shadow is used - int nPoints( (10*gradientSize)/fixedSize ); - const qreal magnitude( 0.85 ); - const qreal width( 0.17 ); - QRadialGradient rg = QRadialGradient( size+12.0*hoffset, size+12.0*voffset, gradientSize ); rg.setColorAt(1, Qt::transparent ); + // gaussian shadow is used + int nPoints( (10*gradientSize)/fixedSize ); + Gaussian f( 0.85, 0.17 ); QColor c = shadowConfiguration.innerColor(); for( int i = 0; i < nPoints; i++ ) { qreal x = qreal(i)/nPoints; - qreal value = magnitude*std::exp( -sqr(x/width) ); - - c.setAlphaF( value ); + c.setAlphaF( f(x) ); rg.setColorAt( x, c ); } @@ -214,21 +210,17 @@ namespace Oxygen const qreal hoffset = shadowConfiguration.horizontalOffset()*gradientSize/fixedSize; const qreal voffset = shadowConfiguration.verticalOffset()*gradientSize/fixedSize; - // gaussian shadow is used - int nPoints( (10*gradientSize)/fixedSize ); - const qreal magnitude( 0.46 ); - const qreal width( 0.34 ); - QRadialGradient rg = QRadialGradient( size+12.0*hoffset, size+12.0*voffset, gradientSize ); rg.setColorAt(1, Qt::transparent ); + // gaussian shadow is used + int nPoints( (10*gradientSize)/fixedSize ); + Gaussian f( 0.46, 0.34 ); QColor c = shadowConfiguration.outerColor(); for( int i = 0; i < nPoints; i++ ) { qreal x = qreal(i)/nPoints; - qreal value = magnitude*std::exp( -sqr(x/width) ); - - c.setAlphaF( value ); + c.setAlphaF( f(x) ); rg.setColorAt( x, c ); } @@ -246,21 +238,17 @@ namespace Oxygen const qreal hoffset = shadowConfiguration.horizontalOffset()*gradientSize/fixedSize; const qreal voffset = shadowConfiguration.verticalOffset()*gradientSize/fixedSize; - // parabolic shadow is used - int nPoints( (10*gradientSize)/fixedSize ); - const qreal magnitude( 1.0 ); - const qreal width( 21 ); - QRadialGradient rg = QRadialGradient( size+hoffset, size+voffset, gradientSize ); rg.setColorAt(1, Qt::transparent ); + // parabolic shadow is used + int nPoints( (10*gradientSize)/fixedSize ); + Parabolic f( 1.0, 0.22 ); QColor c = shadowConfiguration.outerColor(); for( int i = 0; i < nPoints; i++ ) { qreal x = qreal(i)/nPoints; - qreal value = qMax( 0.0, magnitude*(1.0 -width*sqr(x) ) ); - - c.setAlphaF( value ); + c.setAlphaF( f(x) ); rg.setColorAt( x, c ); } @@ -279,20 +267,16 @@ namespace Oxygen const qreal voffset = shadowConfiguration.verticalOffset()*gradientSize/fixedSize; // gaussian shadow is used - int nPoints( (10*gradientSize)/fixedSize ); - const qreal magnitude( 0.54 ); - const qreal width(0.21); - QRadialGradient rg = QRadialGradient( size+8.0*hoffset, size+8.0*voffset, gradientSize ); rg.setColorAt(1, Qt::transparent ); + int nPoints( (10*gradientSize)/fixedSize ); + Gaussian f( 0.54, 0.21); QColor c = shadowConfiguration.outerColor(); for( int i = 0; i < nPoints; i++ ) { qreal x = qreal(i)/nPoints; - qreal value = magnitude*std::exp( -sqr(x/width) ); - - c.setAlphaF( value ); + c.setAlphaF( f(x) ); rg.setColorAt( x, c ); } @@ -310,19 +294,16 @@ namespace Oxygen const qreal voffset = shadowConfiguration.verticalOffset()*gradientSize/fixedSize; // gaussian shadow is used - int nPoints( (10*gradientSize)/fixedSize ); - const qreal magnitude( 0.155 ); - const qreal width(0.445); - QRadialGradient rg = QRadialGradient( size+20.0*hoffset, size+20.0*voffset, gradientSize ); rg.setColorAt(1, Qt::transparent ); + int nPoints( (10*gradientSize)/fixedSize ); + Gaussian f( 0.155, 0.445); QColor c = shadowConfiguration.outerColor(); for( int i = 0; i < nPoints; i++ ) { qreal x = qreal(i)/nPoints; - qreal value = magnitude*std::exp( -sqr(x/width) ); - c.setAlphaF( value ); + c.setAlphaF( f(x) ); rg.setColorAt( x, c ); } diff --git a/clients/oxygen/oxygenshadowcache.h b/clients/oxygen/oxygenshadowcache.h index afe8702f18..a71bc313b0 100644 --- a/clients/oxygen/oxygenshadowcache.h +++ b/clients/oxygen/oxygenshadowcache.h @@ -30,6 +30,7 @@ #include "oxygendecohelper.h" #include "oxygenshadowconfiguration.h" +#include #include #include @@ -194,6 +195,62 @@ namespace Oxygen DecoHelper& helper( void ) const { return helper_; } + //! square utility function + static qreal square( qreal x ) + { return x*x; } + + //! functions used to draw shadows + class Parabolic + { + public: + + //! constructor + Parabolic( qreal amplitude, qreal width ): + amplitude_( amplitude ), + width_( width ) + {} + + //! destructor + virtual ~Parabolic( void ) + {} + + //! value + virtual qreal operator() ( qreal x ) const + { return qMax( 0.0, amplitude_*(1.0 - square(x/width_) ) ); } + + private: + + qreal amplitude_; + qreal width_; + + }; + + //! functions used to draw shadows + class Gaussian + { + public: + + //! constructor + Gaussian( qreal amplitude, qreal width ): + amplitude_( amplitude ), + width_( width ) + {} + + //! destructor + virtual ~Gaussian( void ) + {} + + //! value + virtual qreal operator() ( qreal x ) const + { return amplitude_*std::exp( -square(x/width_) ); } + + private: + + qreal amplitude_; + qreal width_; + + }; + private: //! draw gradient into rect