1
#ifndef oxygen_shadowCacheh
2
#define oxygen_shadowCacheh
4
//////////////////////////////////////////////////////////////////////////////
6
// handles caching of TileSet objects to draw shadows
9
// Copyright (c) 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
11
// Permission is hereby granted, free of charge, to any person obtaining a copy
12
// of this software and associated documentation files (the "Software"), to
13
// deal in the Software without restriction, including without limitation the
14
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
15
// sell copies of the Software, and to permit persons to whom the Software is
16
// furnished to do so, subject to the following conditions:
18
// The above copyright notice and this permission notice shall be included in
19
// all copies or substantial portions of the Software.
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28
//////////////////////////////////////////////////////////////////////////////
30
#include "oxygenshadowconfiguration.h"
31
#include "oxygenhelper.h"
32
#include "oxygen_export.h"
36
#include <QtCore/QCache>
37
#include <QtGui/QRadialGradient>
42
class OXYGEN_EXPORT ShadowCache
47
ShadowCache( Helper& );
50
virtual ~ShadowCache( void )
53
//! read configuration from KConfig
54
/*! returns true if changed */
55
bool readConfig( const KConfig& );
58
void setEnabled( bool enabled )
64
_shadowCache.setMaxCost( 1<<6 );
65
_animatedShadowCache.setMaxCost( _maxIndex<<6 );
69
_shadowCache.setMaxCost( 1 );
70
_animatedShadowCache.setMaxCost( 1 );
75
//! max animation index
76
int maxIndex( void ) const
79
//! max animation index
80
void setMaxIndex( int value )
86
_shadowCache.setMaxCost( 1<<6 );
87
_animatedShadowCache.setMaxCost( _maxIndex<<6 );
94
void invalidateCaches( void )
97
_animatedShadowCache.clear();
100
//! returns true if provided shadow configuration changes with respect to stored
102
use ShadowConfiguration::colorRole() to decide whether it should be stored
103
as active or inactive
105
bool shadowConfigurationChanged( const ShadowConfiguration& ) const;
107
//! set shadowConfiguration
109
use ShadowConfiguration::colorRole() to decide whether it should be stored
110
as active or inactive
112
void setShadowConfiguration( const ShadowConfiguration& );
115
qreal shadowSize( void ) const
117
qreal activeSize( _activeShadowConfiguration.isEnabled() ? _activeShadowConfiguration.shadowSize():0 );
118
qreal inactiveSize( _inactiveShadowConfiguration.isEnabled() ? _inactiveShadowConfiguration.shadowSize():0 );
120
// even if shadows are disabled,
121
return qMax( activeSize, inactiveSize );
124
//! Key class to be used into QCache
125
/*! class is entirely inline for optimization */
131
//! explicit constructor
132
explicit Key( void ):
139
//! constructor from int
142
active( ( hash >> 2 )&1 ),
143
isShade( ( hash >> 1)&1 ),
144
hasBorder( (hash)&1 )
148
int hash( void ) const
166
//! get shadow matching client
167
TileSet* tileSet( const Key& );
169
//! get shadow matching client and opacity
170
TileSet* tileSet( Key, qreal );
173
QPixmap shadowPixmap( const Key& key ) const
174
{ return shadowPixmap( key, key.active ); }
177
QPixmap shadowPixmap( const Key&, bool active ) const;
181
Helper& helper( void ) const
184
//! square utility function
185
static qreal square( qreal x )
188
//! functions used to draw shadows
194
Parabolic( qreal amplitude, qreal width ):
195
amplitude_( amplitude ),
200
virtual ~Parabolic( void )
204
virtual qreal operator() ( qreal x ) const
205
{ return qMax( 0.0, amplitude_*(1.0 - square(x/width_) ) ); }
214
//! functions used to draw shadows
220
Gaussian( qreal amplitude, qreal width ):
221
amplitude_( amplitude ),
226
virtual ~Gaussian( void )
230
virtual qreal operator() ( qreal x ) const
231
{ return qMax( 0.0, amplitude_*(std::exp( -square(x/width_) -0.05 ) ) ); }
240
//! draw gradient into rect
241
/*! a separate method is used in order to properly account for corners */
242
void renderGradient( QPainter&, const QRectF&, const QRadialGradient&, bool hasBorder = true ) const;
249
//! defines overlap between shadows and body
250
enum { overlap = 4 };
252
//! caching enable state
256
/*! it is used to set caches max cost, and calculate animation opacity */
259
//! shadow configuration
260
ShadowConfiguration _activeShadowConfiguration;
262
//! shadow configuration
263
ShadowConfiguration _inactiveShadowConfiguration;
266
typedef QCache<int, TileSet> TileSetCache;
269
TileSetCache _shadowCache;
271
//! animated shadow cache
272
TileSetCache _animatedShadowCache;