1
#ifndef oxygen_helper_h
2
#define oxygen_helper_h
5
* Copyright 2009-2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
6
* Copyright 2008 Long Huynh Huu <long.upcase@googlemail.com>
7
* Copyright 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
8
* Copyright 2007 Casper Boemann <cbr@boemann.dk>
9
* Copyright 2007 Fredrik Höglund <fredrik@kde.org>
11
* This library is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Library General Public
13
* License version 2 as published by the Free Software Foundation.
15
* This library is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* Library General Public License for more details.
20
* You should have received a copy of the GNU Library General Public License
21
* along with this library; see the file COPYING.LIB. If not, write to
22
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23
* Boston, MA 02110-1301, USA.
26
#include "oxygentileset.h"
28
#include <KSharedConfig>
29
#include <KComponentData>
30
#include <KColorScheme>
32
#include <QtGui/QColor>
33
#include <QtGui/QPixmap>
34
#include <QtGui/QWidget>
35
#include <QtGui/QLinearGradient>
36
#include <QtCore/QCache>
39
#include <X11/Xdefs.h>
45
template<typename T> class BaseCache: public QCache<quint64, T>
51
BaseCache( int maxCost ):
52
QCache<quint64, T>( maxCost ),
57
explicit BaseCache( void ):
66
void setEnabled( bool value )
70
bool enabled( void ) const
74
T* object( const quint64& key )
75
{ return _enabled ? QCache<quint64, T>::object( key ) : 0; }
78
void setMaxCost( int cost )
82
QCache<quint64, T>::clear();
83
QCache<quint64, T>::setMaxCost( 1 );
89
QCache<quint64, T>::setMaxCost( cost );
101
template<typename T> class Cache
114
//! return cache matching a given key
115
//typedef QCache<quint64, T> Value;
116
typedef BaseCache<T> Value;
117
Value* get( const QColor& color )
119
quint64 key = ( quint64( color.rgba() ) << 32 );
120
Value* cache = data_.object( key );
124
cache = new Value( data_.maxCost() );
125
data_.insert( key, cache );
136
void setMaxCacheSize( int value )
138
data_.setMaxCost( value );
139
foreach( quint64 key, data_.keys() )
140
{ data_.object( key )->setMaxCost( value ); }
146
BaseCache<Value> data_;
150
//! oxygen style helper class.
151
/*! contains utility functions used at multiple places in both oxygen style and oxygen window decoration */
152
class OXYGEN_EXPORT Helper
157
Helper( const QByteArray& componentName );
163
//! reload configuration
164
virtual void reloadConfig();
166
//! pointer to shared config
167
KSharedConfigPtr config() const;
170
virtual void invalidateCaches();
172
//! update maximum cache size
173
virtual void setMaxCacheSize( int );
175
//!@name window background gradients
178
\par y_shift: shift the background gradient upwards, to fit with the windec
179
\par gradientHeight: the height of the generated gradient.
180
for different heights, the gradient is translated so that it is always at the same position from the bottom
182
virtual void renderWindowBackground( QPainter* p, const QRect& clipRect, const QWidget* widget, const QPalette& pal, int y_shift=-23, int gradientHeight = 64 )
183
{ renderWindowBackground( p, clipRect, widget, pal.color( widget->window()->backgroundRole() ), y_shift, gradientHeight ); }
186
y_shift: shift the background gradient upwards, to fit with the windec
187
gradientHeight: the height of the generated gradient.
188
for different heights, the gradient is translated so that it is always at the same position from the bottom
190
virtual void renderWindowBackground( QPainter* p, const QRect& clipRect, const QWidget* widget, const QWidget* window, const QPalette& pal, int y_shift=-23, int gradientHeight = 64 )
191
{ renderWindowBackground( p, clipRect, widget, window, pal.color( window->backgroundRole() ), y_shift, gradientHeight ); }
193
//! render window background using a given color as a reference
194
virtual void renderWindowBackground( QPainter* p, const QRect& clipRect, const QWidget* widget, const QColor& color, int y_shift=-23, int gradientHeight = 64 )
195
{ renderWindowBackground( p, clipRect, widget, widget->window(), color, y_shift, gradientHeight ); }
197
//! render window background using a given color as a reference
198
virtual void renderWindowBackground( QPainter* p, const QRect& clipRect, const QWidget* widget, const QWidget* window, const QColor& color, int y_shift=-23, int gradientHeight = 64 );
200
//! background pixmap
201
bool hasBackgroundPixmap( void ) const
202
{ return !_backgroundPixmap.isNull(); }
204
//! background pixmap
205
void setBackgroundPixmap( const QPixmap& pixmap )
206
{ _backgroundPixmap = pixmap; }
209
void setBackgroundPixmapOffset( const QPoint& offset )
210
{ _backgroundPixmapOffset = offset; }
212
//! render window background using a given color as a reference
213
virtual void renderBackgroundPixmap( QPainter* p, const QRect& clipRect, const QWidget* widget, const QWidget* window, int y_shift=-23, int gradientHeight = 64 );
218
void renderDot( QPainter*, const QPoint&, const QColor& );
220
//! returns true for too 'dark' colors
221
bool lowThreshold( const QColor& color );
223
//! returns true for too 'light' colors
224
bool highThreshold( const QColor& color );
226
//! add alpha channel multiplier to color
227
static QColor alphaColor( QColor color, qreal alpha );
229
//! calculated light color from argument
230
virtual const QColor& calcLightColor( const QColor& color );
232
//! calculated dark color from argument
233
virtual const QColor& calcDarkColor( const QColor& color );
235
//! calculated shadow color from argument
236
virtual const QColor& calcShadowColor( const QColor& color );
238
//! returns menu background color matching position in a given top level widget
239
virtual const QColor& backgroundColor( const QColor& color, const QWidget* w, const QPoint& point )
241
if( !( w && w->window() ) || checkAutoFillBackground( w ) ) return color;
242
else return backgroundColor( color, w->window()->height(), w->mapTo( w->window(), point ).y() );
245
//! returns menu background color matching position in a top level widget of given height
246
virtual const QColor& backgroundColor( const QColor& color, int height, int y )
247
{ return backgroundColor( color, qMin( qreal( 1.0 ), qreal( y )/qMin( 300, 3*height/4 ) ) ); }
249
//! color used for background radial gradient
250
virtual const QColor& backgroundRadialColor( const QColor& color );
252
//! color used at the top of window background
253
virtual const QColor& backgroundTopColor( const QColor& color );
255
//! color used at the bottom of window background
256
virtual const QColor& backgroundBottomColor( const QColor& color );
258
//! vertical gradient for window background
259
virtual QPixmap verticalGradient( const QColor& color, int height, int offset = 0 );
261
//! radial gradient for window background
262
virtual QPixmap radialGradient( const QColor& color, int width, int height = 64 );
264
//! merge background and front color for check marks, arrows, etc. using _contrast
265
virtual const QColor& decoColor( const QColor& background, const QColor& color );
267
//! returns a region matching given rect, with rounded corners, based on the multipliers
268
/*! setting any of the multipliers to zero will result in no corners shown on the corresponding side */
269
virtual QRegion roundedMask( const QRect&, int left = 1, int right = 1, int top = 1, int bottom = 1 ) const;
271
//! draw frame that mimics some sort of shadows around a panel
272
/*! it is used for menus, detached dock panels and toolbar, as well as window decoration when compositing is disabled */
273
virtual void drawFloatFrame(
274
QPainter* p, const QRect r, const QColor& color,
275
bool drawUglyShadow=true, bool isActive=false,
276
const QColor& frameColor=QColor(),
277
TileSet::Tiles tiles = TileSet::Ring
280
//! draw dividing line
281
virtual void drawSeparator( QPainter*, const QRect&, const QColor&, Qt::Orientation );
284
virtual TileSet* slab( const QColor& color, qreal shade, int size = 7 )
285
{ return slab( color, QColor(), shade, size ); }
287
//! default slab (with glow)
288
virtual TileSet* slab( const QColor&, const QColor& glow, qreal shade, int size = 7 );
291
virtual TileSet *slabSunken( const QColor&, int size = 7 );
293
//! fill a slab of given size with brush set on painter
294
void fillSlab( QPainter&, const QRect&, int size = 7 ) const;
296
//! linear gradient used to fill buttons
297
virtual void fillButtonSlab( QPainter&, const QRect&, const QColor&, bool sunken );
299
//! inverse (inner-hole) shadow
300
/*! this method must be public because it is used directly by OxygenStyle to draw dials */
301
void drawInverseShadow( QPainter&, const QColor&, int pad, int size, qreal fuzz ) const;
304
const KStatefulBrush& viewFocusBrush( void ) const
305
{ return _viewFocusBrush; }
308
const KStatefulBrush& viewHoverBrush( void ) const
309
{ return _viewHoverBrush; }
311
//! negative text brush ( used for close button hover )
312
const KStatefulBrush& viewNegativeTextBrush( void ) const
313
{ return _viewNegativeTextBrush; }
316
returns first widget in parent chain that sets autoFillBackground to true,
319
const QWidget* checkAutoFillBackground( const QWidget* ) const;
321
//!@name background gradient XProperty
324
//! set background gradient hint to widget
325
virtual void setHasBackgroundGradient( WId, bool ) const;
327
//! true if background gradient hint is set
328
virtual bool hasBackgroundGradient( WId ) const;
330
//! set background pixmap hint to widget
331
virtual void setHasBackgroundPixmap( WId, bool ) const;
333
//! true if background pixmap hint is set
334
virtual bool hasBackgroundPixmap( WId ) const;
340
//! generic slab painting (to be stored in tilesets)
341
virtual void drawSlab( QPainter&, const QColor&, qreal shade );
343
//! generic outer shadow (to be stored in tilesets)
344
virtual void drawShadow( QPainter&, const QColor&, int size );
346
//! generic outer glow (to be stored in tilesets)
347
virtual void drawOuterGlow( QPainter&, const QColor&, int size );
349
//! return background adjusted color matching relative vertical position in window
350
const QColor& backgroundColor( const QColor&, qreal ratio );
352
//!@name global configuration parameters
355
static const qreal _glowBias;
356
static const qreal _slabThickness;
357
static const qreal _shadowGain;
362
//!@name pixmap caches
364
typedef BaseCache<QPixmap> PixmapCache;
366
//! window decoration buttons
367
PixmapCache& windecoButtonCache( void )
368
{ return _windecoButtonCache; }
370
//! window decoration button glow
371
PixmapCache& windecoButtonGlowCache( void )
372
{ return _windecoButtonGlowCache; }
376
//! shortcut to color caches
377
/*! it is made protected because it is also used in the style helper */
378
typedef BaseCache<QColor> ColorCache;
382
//!@name tileset caches
386
Oxygen::Cache<TileSet> _slabCache;
389
BaseCache<TileSet> _slabSunkenCache;
395
KStatefulBrush _viewFocusBrush;
396
KStatefulBrush _viewHoverBrush;
397
KStatefulBrush _viewNegativeTextBrush;
400
KComponentData _componentData;
401
KSharedConfigPtr _config;
404
//!@name color caches
406
ColorCache _decoColorCache;
407
ColorCache _lightColorCache;
408
ColorCache _darkColorCache;
409
ColorCache _shadowColorCache;
410
ColorCache _backgroundTopColorCache;
411
ColorCache _backgroundBottomColorCache;
412
ColorCache _backgroundRadialColorCache;
413
ColorCache _backgroundColorCache;
416
PixmapCache _windecoButtonCache;
417
PixmapCache _windecoButtonGlowCache;
418
PixmapCache _backgroundCache;
419
PixmapCache _dotCache;
421
//! high threshold colors
422
typedef QMap<quint32, bool> ColorMap;
423
ColorMap _highThreshold;
424
ColorMap _lowThreshold;
426
//! background pixmap
427
QPixmap _backgroundPixmap;
429
//! background pixmap offsets
430
QPoint _backgroundPixmapOffset;
434
//! set value for given hint
435
void setHasHint( WId, Atom, bool ) const;
437
//! value for given hint
438
bool hasHint( WId, Atom ) const;
440
//! background gradient hint atom
441
Atom _backgroundGradientAtom;
443
//! background gradient hint atom
444
Atom _backgroundPixmapAtom;