diff --git a/clients/oxygen/oxygenbutton.cpp b/clients/oxygen/oxygenbutton.cpp index 18d9d9a5ad..4c0a4bb324 100644 --- a/clients/oxygen/oxygenbutton.cpp +++ b/clients/oxygen/oxygenbutton.cpp @@ -81,7 +81,7 @@ namespace Oxygen {} //_______________________________________________ - QColor Button::buttonDetailColor(const QPalette &palette) + QColor Button::buttonDetailColor(const QPalette &palette) const { if( client_.glowIsAnimated() && !forceInactive_ && !client_.isForcedActive()) return KColorUtils::mix( buttonDetailColor( palette, false ), @@ -90,24 +90,6 @@ namespace Oxygen else return buttonDetailColor( palette, isActive() || client_.isForcedActive() ); } - //_______________________________________________ - QColor Button::buttonDetailColor(const QPalette &palette, bool active) - { - - if( active ) return palette.color(QPalette::Active, QPalette::WindowText); - else { - - // todo: re-implement caching - QColor ab = palette.color(QPalette::Active, QPalette::Window); - QColor af = palette.color(QPalette::Active, QPalette::WindowText); - QColor nb = palette.color(QPalette::Inactive, QPalette::Window); - QColor nf = palette.color(QPalette::Inactive, QPalette::WindowText); - return reduceContrast(nb, nf, qMax(qreal(2.5), KColorUtils::contrastRatio(ab, KColorUtils::mix(ab, af, 0.4)))); - - } - - } - //___________________________________________________ bool Button::isActive( void ) const { return (!forceInactive_) && client_.isActive(); } diff --git a/clients/oxygen/oxygenbutton.h b/clients/oxygen/oxygenbutton.h index 17683010a1..236e692c89 100644 --- a/clients/oxygen/oxygenbutton.h +++ b/clients/oxygen/oxygenbutton.h @@ -118,10 +118,15 @@ namespace Oxygen void drawIcon(QPainter*); //! color - QColor buttonDetailColor(const QPalette& ); + QColor buttonDetailColor(const QPalette& ) const; //! color - QColor buttonDetailColor(const QPalette&, bool active ); + QColor buttonDetailColor(const QPalette& palette, bool active ) const + { + return active ? + palette.color(QPalette::Active, QPalette::WindowText): + helper_.inactiveTitleBarTextColor( palette ); + } //! true if animation is in progress bool isAnimated( void ) const diff --git a/clients/oxygen/oxygenclient.cpp b/clients/oxygen/oxygenclient.cpp index 5fc1f700a4..1298a8798c 100644 --- a/clients/oxygen/oxygenclient.cpp +++ b/clients/oxygen/oxygenclient.cpp @@ -47,31 +47,6 @@ namespace Oxygen { - //_________________________________________________________ - QColor reduceContrast(const QColor &c0, const QColor &c1, double t) - { - double s = KColorUtils::contrastRatio(c0, c1); - if (s < t) - return c1; - - double l = 0.0, h = 1.0; - double x = s, a; - QColor r = c1; - for (int maxiter = 16; maxiter; --maxiter) - { - - a = 0.5 * (l + h); - r = KColorUtils::mix(c0, c1, a); - x = KColorUtils::contrastRatio(c0, r); - - if (fabs(x - t) < 0.01) break; - if (x > t) h = a; - else l = a; - } - - return r; - } - //___________________________________________ Client::Client(KDecorationBridge *b, Factory *f): KCommonDecorationUnstable(b, f), @@ -399,9 +374,8 @@ namespace Oxygen //_________________________________________________________ QRect Client::defaultTitleRect( bool active ) const { - QRect titleRect( this->titleRect() ); - titleRect.adjust( 0, -layoutMetric( LM_TitleEdgeTop ), 0, 0 ); + QRect titleRect( this->titleRect().adjusted( 0, -layoutMetric( LM_TitleEdgeTop ), 0, 0 ) ); if( active && configuration().drawTitleOutline() && isActive() ) { @@ -425,7 +399,7 @@ namespace Oxygen { // get title bounding rect - QRect boundingRect = QFontMetrics( font ).boundingRect( rect, configuration().titleAlignment() | Qt::AlignVCenter, caption ); + QRect boundingRect( QFontMetrics( font ).boundingRect( rect, configuration().titleAlignment() | Qt::AlignVCenter, caption ) ); // adjust to make sure bounding rect // 1/ has same vertical alignment as original titleRect @@ -535,7 +509,7 @@ namespace Oxygen } //_________________________________________________________ - QColor Client::titlebarTextColor(const QPalette &palette) + QColor Client::titlebarTextColor(const QPalette &palette) const { if( glowIsAnimated() ) return KColorUtils::mix( titlebarTextColor( palette, false ), @@ -544,27 +518,6 @@ namespace Oxygen else return titlebarTextColor( palette, isActive() ); } - //_________________________________________________________ - QColor Client::titlebarTextColor(const QPalette &palette, bool active) - { - - if( active ){ - - return palette.color(QPalette::Active, QPalette::WindowText); - - } else { - - // todo: reimplement cache - const QColor ab = palette.color(QPalette::Active, QPalette::Window); - const QColor af = palette.color(QPalette::Active, QPalette::WindowText); - const QColor nb = palette.color(QPalette::Inactive, QPalette::Window); - const QColor nf = palette.color(QPalette::Inactive, QPalette::WindowText); - return reduceContrast(nb, nf, qMax(qreal(2.5), KColorUtils::contrastRatio(ab, KColorUtils::mix(ab, af, 0.4)))); - - } - - } - //_________________________________________________________ void Client::renderWindowBackground( QPainter* painter, const QRect& rect, const QWidget* widget, const QPalette& palette ) const { @@ -572,7 +525,7 @@ namespace Oxygen if( configuration().blendColor() == Configuration::NoBlending ) { - painter->fillRect( rect, palette.color( widget->window()->backgroundRole() ) ); + painter->fillRect( rect, palette.color( QPalette::Window ) ); } else { @@ -778,9 +731,7 @@ namespace Oxygen // center (for active windows only) { painter->save(); - const int offset = 1; - const int voffset = 1; - QRect adjustedRect( rect.adjusted( offset, voffset, -offset, 1 ) ); + QRect adjustedRect( rect.adjusted( 1, 1, -1, 1 ) ); // prepare painter mask QRegion mask( adjustedRect.adjusted( 1, 0, -1, 0 ) ); @@ -793,9 +744,9 @@ namespace Oxygen } // shadow - const int shadowSize = 7; - const int offset = -3; - const int voffset = 5-shadowSize; + const int shadowSize( 7 ); + const int offset( -3 ); + const int voffset( 5-shadowSize ); const QRect adjustedRect( rect.adjusted(offset, voffset, -offset, shadowSize) ); helper().slab( palette.color( widget()->backgroundRole() ), 0, shadowSize )->render( adjustedRect, painter, TileSet::Tiles(TileSet::Top|TileSet::Left|TileSet::Right) ); @@ -1203,13 +1154,13 @@ namespace Oxygen const QColor inactiveColor( backgroundColor( widget, palette, false ) ); const QColor activeColor( backgroundColor( widget, palette, true ) ); const QColor mixed( KColorUtils::mix( inactiveColor, activeColor, glowIntensity() ) ); - palette.setColor( widget->window()->backgroundRole(), mixed ); + palette.setColor( QPalette::Window, mixed ); palette.setColor( QPalette::Button, mixed ); } else if( isActive() || isForcedActive() ) { const QColor color = options()->color( KDecorationDefines::ColorTitleBar, true ); - palette.setColor( widget->window()->backgroundRole(), color ); + palette.setColor( QPalette::Window, color ); palette.setColor( QPalette::Button, color ); } @@ -1221,12 +1172,12 @@ namespace Oxygen } //_________________________________________________________ - QColor Client::backgroundColor( const QWidget* widget, QPalette palette, bool active ) const + QColor Client::backgroundColor( const QWidget*, QPalette palette, bool active ) const { return ( configuration().drawTitleOutline() && active ) ? options()->color( KDecorationDefines::ColorTitleBar, true ): - palette.color( widget->window()->backgroundRole() ); + palette.color( QPalette::Window ); } diff --git a/clients/oxygen/oxygenclient.h b/clients/oxygen/oxygenclient.h index 1005eef9ad..4289754d8a 100644 --- a/clients/oxygen/oxygenclient.h +++ b/clients/oxygen/oxygenclient.h @@ -344,10 +344,15 @@ namespace Oxygen QRegion calcMask( void ) const; //! text color - QColor titlebarTextColor(const QPalette&); + QColor titlebarTextColor(const QPalette&) const; //! text color - QColor titlebarTextColor(const QPalette&, bool active); + QColor titlebarTextColor(const QPalette& palette, bool active) const + { + return active ? + palette.color(QPalette::Active, QPalette::WindowText): + helper().inactiveTitleBarTextColor( palette ); + } //! text color QColor titlebarContrastColor(const QPalette& palette ) const @@ -436,17 +441,6 @@ namespace Oxygen }; - //!@name utility functions - //@{ - - // dot - void renderDot(QPainter*, const QPointF&, qreal ); - - // contrast - QColor reduceContrast(const QColor&, const QColor&, double t); - - //@} - } // namespace Oxygen #endif // EXAMPLECLIENT_H diff --git a/clients/oxygen/oxygendecohelper.cpp b/clients/oxygen/oxygendecohelper.cpp index 535e7bcca5..6120b1975a 100644 --- a/clients/oxygen/oxygendecohelper.cpp +++ b/clients/oxygen/oxygendecohelper.cpp @@ -21,13 +21,27 @@ #include "oxygendecohelper.h" #include +#include +#include namespace Oxygen { + //______________________________________________________________________________ DecoHelper::DecoHelper(const QByteArray &componentName): Helper(componentName) - {} + {} + + //______________________________________________________________________________ + void DecoHelper::invalidateCaches( void ) + { + // base class call + Helper::invalidateCaches(); + + // local caches + m_titleBarTextColorCache.clear(); + + } //______________________________________________________________________________ QPixmap DecoHelper::windecoButton(const QColor &color, bool pressed, int size) @@ -158,4 +172,52 @@ namespace Oxygen return mask; } + + //______________________________________________________________________________ + const QColor& DecoHelper::inactiveTitleBarTextColor( const QPalette& palette ) + { + + const quint32 key( palette.color(QPalette::Active, QPalette::Window).rgba() ); + QColor* out( m_titleBarTextColorCache.object( key ) ); + if( !out ) + { + + // todo: reimplement cache + const QColor ab = palette.color(QPalette::Active, QPalette::Window); + const QColor af = palette.color(QPalette::Active, QPalette::WindowText); + const QColor nb = palette.color(QPalette::Inactive, QPalette::Window); + const QColor nf = palette.color(QPalette::Inactive, QPalette::WindowText); + out = new QColor( reduceContrast(nb, nf, qMax(qreal(2.5), KColorUtils::contrastRatio(ab, KColorUtils::mix(ab, af, 0.4)))) ); + m_titleBarTextColorCache.insert( key, out ); + } + + return *out; + } + + //_________________________________________________________ + QColor DecoHelper::reduceContrast(const QColor &c0, const QColor &c1, double t) const + { + const double s( KColorUtils::contrastRatio(c0, c1) ); + if (s < t) return c1; + + double l(0); + double h(1.0); + double x(s); + double a; + QColor r( c1 ); + for (int maxiter = 16; maxiter; --maxiter) + { + + a = 0.5 * (l + h); + r = KColorUtils::mix(c0, c1, a); + x = KColorUtils::contrastRatio(c0, r); + + if (fabs(x - t) < 0.01) break; + if (x > t) h = a; + else l = a; + } + + return r; + } + } diff --git a/clients/oxygen/oxygendecohelper.h b/clients/oxygen/oxygendecohelper.h index 260fa5e74e..5c95e8cffc 100644 --- a/clients/oxygen/oxygendecohelper.h +++ b/clients/oxygen/oxygendecohelper.h @@ -32,8 +32,15 @@ namespace Oxygen public: - explicit DecoHelper(const QByteArray &componentName); - virtual ~DecoHelper() {} + //! constructor + DecoHelper(const QByteArray &componentName); + + //! destructor + virtual ~DecoHelper() + {} + + //! reset all caches + virtual void invalidateCaches(); //!@name decoration specific helper functions //! @@ -42,8 +49,23 @@ namespace Oxygen virtual QPixmap windecoButtonGlow(const QColor &color, int size = 21); //@} + // virtual QRegion decoRoundedMask( const QRect&, int left = 1, int right = 1, int top = 1, int bottom = 1 ) const; + //! title bar text color + const QColor& inactiveTitleBarTextColor( const QPalette& ); + + protected: + + //! reduce contrast between two colors + QColor reduceContrast(const QColor&, const QColor&, double) const; + + private: + + //! titleBar text color cache + ColorCache m_titleBarTextColorCache; + + }; }