diff --git a/kstyle/breezehelper.cpp b/kstyle/breezehelper.cpp index 0d90ff29..d3ae8f31 100644 --- a/kstyle/breezehelper.cpp +++ b/kstyle/breezehelper.cpp @@ -22,7 +22,98 @@ #include "breezehelper.h" +#include "breezemetrics.h" + +#include + namespace Breeze { + //____________________________________________________________________ + Helper::Helper( KSharedConfig::Ptr config ): + _config( config ) + {} + + //____________________________________________________________________ + KSharedConfigPtr Helper::config() const + { return _config; } + + //____________________________________________________________________ + void Helper::loadConfig() + { + _viewFocusBrush = KStatefulBrush( KColorScheme::View, KColorScheme::FocusColor, _config ); + _viewHoverBrush = KStatefulBrush( KColorScheme::View, KColorScheme::HoverColor, _config ); + _viewNegativeTextBrush = KStatefulBrush( KColorScheme::View, KColorScheme::NegativeText, _config ); + } + + //______________________________________________________________________________ + void Helper::invalidateCaches( void ) + { + _scrollBarHandleCache.clear(); + } + + //____________________________________________________________________ + void Helper::setMaxCacheSize( int value ) + { + _scrollBarHandleCache.setMaxCost( value ); + } + + //____________________________________________________________________ + QColor Helper::alphaColor( QColor color, qreal alpha ) + { + if( alpha >= 0 && alpha < 1.0 ) + { color.setAlphaF( alpha*color.alphaF() ); } + return color; + } + + //________________________________________________________________________________________________________ + TileSet *Helper::scrollBarHandle( const QColor& color, const QColor& glow) + { + + const quint64 key( ( colorKey(color) << 32 ) | colorKey(glow) ); + TileSet *tileSet = _scrollBarHandleCache.object( key ); + + if ( !tileSet ) + { + + QPixmap pm( Metrics::ScrollBar_SliderWidth, Metrics::ScrollBar_SliderWidth ); + pm.fill( Qt::transparent ); + + QPainter p( &pm ); + p.setRenderHints( QPainter::Antialiasing ); + + // content + if( color.isValid() ) + { + p.setPen( Qt::NoPen ); + p.setBrush( color ); + // p.drawEllipse( QRectF( 0.5, 0.5, Metrics::ScrollBar_SliderWidth-1, Metrics::ScrollBar_SliderWidth-1 ) ); + p.drawEllipse( QRectF( 0, 0, Metrics::ScrollBar_SliderWidth, Metrics::ScrollBar_SliderWidth ) ); + } + + // border + if( glow.isValid() ) + { + p.setPen( QPen( glow, 2 ) ); + p.setBrush( Qt::NoBrush ); + p.drawEllipse( QRectF( 1, 1, Metrics::ScrollBar_SliderWidth-2, Metrics::ScrollBar_SliderWidth-2 ) ); + } + + p.end(); + + // create tileset and return + tileSet = new TileSet( pm, Metrics::ScrollBar_SliderWidth/2, Metrics::ScrollBar_SliderWidth/2, 1, 1 ); + _scrollBarHandleCache.insert( key, tileSet ); + + } + + return tileSet; + } + + //________________________________________________________________________________________________________ + TileSet *Helper::scrollBarHole( const QColor& color ) + { + // in the current implementation, holes and handles are rendered with the same code + return scrollBarHandle( color, QColor() ); + } } diff --git a/kstyle/breezehelper.h b/kstyle/breezehelper.h index aba48699..9d9bad3c 100644 --- a/kstyle/breezehelper.h +++ b/kstyle/breezehelper.h @@ -23,6 +23,12 @@ * Boston, MA 02110-1301, USA. */ +#include "breezetileset.h" + +#include +#include + +#include #include #if HAVE_X11 @@ -32,35 +38,146 @@ namespace Breeze { + template class BaseCache: public QCache + { + + public: + + //! constructor + BaseCache( int maxCost ): + QCache( maxCost ), + _enabled( true ) + {} + + //! constructor + explicit BaseCache( void ): + _enabled( true ) + {} + + //! destructor + ~BaseCache( void ) + {} + + //! enable + void setEnabled( bool value ) + { _enabled = value; } + + //! enable state + bool enabled( void ) const + { return _enabled; } + + //! access + T* object( const quint64& key ) + { return _enabled ? QCache::object( key ) : 0; } + + //! max cost + void setMaxCost( int cost ) + { + if( cost <= 0 ) { + + QCache::clear(); + QCache::setMaxCost( 1 ); + setEnabled( false ); + + } else { + + setEnabled( true ); + QCache::setMaxCost( cost ); + + } + } + + private: + + //! enable flag + bool _enabled; + + }; + //! breeze style helper class. /*! contains utility functions used at multiple places in both breeze style and breeze window decoration */ class Helper { public: + //! scoped pointer convenience typedef + template using ScopedPointer = QScopedPointer; + //! constructor - explicit Helper() - {} + explicit Helper( KSharedConfig::Ptr ); //! destructor virtual ~Helper() {} - template class ScopedPointer: public QScopedPointer - { - public: + //! load configuration + virtual void loadConfig(); - //! constructor - ScopedPointer( T* t ): - QScopedPointer( t ) - {} + //! pointer to shared config + KSharedConfigPtr config() const; - //! destructor - virtual ~ScopedPointer( void ) - {} + //! reset all caches + virtual void invalidateCaches(); + + //! update maximum cache size + virtual void setMaxCacheSize( int ); + + //! add alpha channel multiplier to color + static QColor alphaColor( QColor color, qreal alpha ); + + //!@name brushes + //@{ + + //! focus brush + const KStatefulBrush& viewFocusBrush( void ) const + { return _viewFocusBrush; } + + //! hover brush + const KStatefulBrush& viewHoverBrush( void ) const + { return _viewHoverBrush; } + + //! negative text brush ( used for close button hover ) + const KStatefulBrush& viewNegativeTextBrush( void ) const + { return _viewNegativeTextBrush; } + + //@} + + //!@name tilesets + //@{ + + //! scrollbar hole + TileSet *scrollBarHole( const QColor& ); + + //! scrollbar handle + TileSet *scrollBarHandle( const QColor&, const QColor& ); + + //@} + + protected: + + //! return color key for a given color, properly accounting for invalid colors + quint64 colorKey( const QColor& color ) const + { return color.isValid() ? color.rgba():0; } + + private: + + //! configuration + KSharedConfigPtr _config; + + //!@name brushes + //@{ + KStatefulBrush _viewFocusBrush; + KStatefulBrush _viewHoverBrush; + KStatefulBrush _viewNegativeTextBrush; + //@} + + //!@name tileset caches + //@{ - }; + typedef BaseCache TileSetCache; + TileSetCache _scrollBarHandleCache; + //@} }; }