~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to libs/oxygen/oxygenshadowcache.h

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef oxygen_shadowCacheh
 
2
#define oxygen_shadowCacheh
 
3
 
 
4
//////////////////////////////////////////////////////////////////////////////
 
5
// oxygenshadowcache.h
 
6
// handles caching of TileSet objects to draw shadows
 
7
// -------------------
 
8
//
 
9
// Copyright (c) 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
 
10
//
 
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:
 
17
//
 
18
// The above copyright notice and this permission notice shall be included in
 
19
// all copies or substantial portions of the Software.
 
20
//
 
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
 
27
// IN THE SOFTWARE.
 
28
//////////////////////////////////////////////////////////////////////////////
 
29
 
 
30
#include "oxygenshadowconfiguration.h"
 
31
#include "oxygenhelper.h"
 
32
#include "oxygen_export.h"
 
33
 
 
34
#include <cmath>
 
35
#include <KConfig>
 
36
#include <QtCore/QCache>
 
37
#include <QtGui/QRadialGradient>
 
38
 
 
39
namespace Oxygen
 
40
{
 
41
 
 
42
    class OXYGEN_EXPORT ShadowCache
 
43
    {
 
44
        public:
 
45
 
 
46
        //! constructor
 
47
        ShadowCache( Helper& );
 
48
 
 
49
        //! destructor
 
50
        virtual ~ShadowCache( void )
 
51
        {}
 
52
 
 
53
        //! read configuration from KConfig
 
54
        /*! returns true if changed */
 
55
        bool readConfig( const KConfig& );
 
56
 
 
57
        //! cache size
 
58
        void setEnabled( bool enabled )
 
59
        {
 
60
            _enabled = enabled;
 
61
            if( enabled )
 
62
            {
 
63
 
 
64
                _shadowCache.setMaxCost( 1<<6 );
 
65
                _animatedShadowCache.setMaxCost( _maxIndex<<6 );
 
66
 
 
67
            } else {
 
68
 
 
69
                _shadowCache.setMaxCost( 1 );
 
70
                _animatedShadowCache.setMaxCost( 1 );
 
71
 
 
72
            }
 
73
        }
 
74
 
 
75
        //! max animation index
 
76
        int maxIndex( void ) const
 
77
        { return _maxIndex; }
 
78
 
 
79
        //! max animation index
 
80
        void setMaxIndex( int value )
 
81
        {
 
82
            _maxIndex = value;
 
83
            if( _enabled )
 
84
            {
 
85
 
 
86
                _shadowCache.setMaxCost( 1<<6 );
 
87
                _animatedShadowCache.setMaxCost( _maxIndex<<6 );
 
88
 
 
89
            }
 
90
 
 
91
        }
 
92
 
 
93
        //! invalidate caches
 
94
        void invalidateCaches( void )
 
95
        {
 
96
            _shadowCache.clear();
 
97
            _animatedShadowCache.clear();
 
98
        }
 
99
 
 
100
        //! returns true if provided shadow configuration changes with respect to stored
 
101
        /*!
 
102
        use ShadowConfiguration::colorRole() to decide whether it should be stored
 
103
        as active or inactive
 
104
        */
 
105
        bool shadowConfigurationChanged( const ShadowConfiguration& ) const;
 
106
 
 
107
        //! set shadowConfiguration
 
108
        /*!
 
109
        use ShadowConfiguration::colorRole() to decide whether it should be stored
 
110
        as active or inactive
 
111
        */
 
112
        void setShadowConfiguration( const ShadowConfiguration& );
 
113
 
 
114
        //! shadow size
 
115
        qreal shadowSize( void ) const
 
116
        {
 
117
            qreal activeSize( _activeShadowConfiguration.isEnabled() ? _activeShadowConfiguration.shadowSize():0 );
 
118
            qreal inactiveSize( _inactiveShadowConfiguration.isEnabled() ? _inactiveShadowConfiguration.shadowSize():0 );
 
119
 
 
120
            // even if shadows are disabled,
 
121
            return qMax( activeSize, inactiveSize );
 
122
        }
 
123
 
 
124
        //! Key class to be used into QCache
 
125
        /*! class is entirely inline for optimization */
 
126
        class Key
 
127
        {
 
128
 
 
129
            public:
 
130
 
 
131
            //! explicit constructor
 
132
            explicit Key( void ):
 
133
                index(0),
 
134
                active(false),
 
135
                isShade(false),
 
136
                hasBorder( true )
 
137
            {}
 
138
 
 
139
            //! constructor from int
 
140
            Key( int hash ):
 
141
                index( hash >> 3 ),
 
142
                active( ( hash >> 2 )&1 ),
 
143
                isShade( ( hash >> 1)&1 ),
 
144
                hasBorder( (hash)&1 )
 
145
            {}
 
146
 
 
147
            //! hash function
 
148
            int hash( void ) const
 
149
            {
 
150
 
 
151
                return
 
152
                    ( index << 3 ) |
 
153
                    ( active << 2 ) |
 
154
                    ( isShade<< 1 ) |
 
155
                    ( hasBorder );
 
156
 
 
157
            }
 
158
 
 
159
            int index;
 
160
            bool active;
 
161
            bool isShade;
 
162
            bool hasBorder;
 
163
 
 
164
        };
 
165
 
 
166
        //! get shadow matching client
 
167
        TileSet* tileSet( const Key& );
 
168
 
 
169
        //! get shadow matching client and opacity
 
170
        TileSet* tileSet( Key, qreal );
 
171
 
 
172
        //! simple pixmap
 
173
        QPixmap shadowPixmap( const Key& key ) const
 
174
        { return shadowPixmap( key, key.active ); }
 
175
 
 
176
        //! simple pixmap
 
177
        QPixmap shadowPixmap( const Key&, bool active ) const;
 
178
 
 
179
        protected:
 
180
 
 
181
        Helper& helper( void ) const
 
182
        { return _helper; }
 
183
 
 
184
        //! square utility function
 
185
        static qreal square( qreal x )
 
186
        { return x*x; }
 
187
 
 
188
        //! functions used to draw shadows
 
189
        class Parabolic
 
190
        {
 
191
            public:
 
192
 
 
193
            //! constructor
 
194
            Parabolic( qreal amplitude, qreal width ):
 
195
                amplitude_( amplitude ),
 
196
                width_( width )
 
197
            {}
 
198
 
 
199
            //! destructor
 
200
            virtual ~Parabolic( void )
 
201
            {}
 
202
 
 
203
            //! value
 
204
            virtual qreal operator() ( qreal x ) const
 
205
            { return qMax( 0.0, amplitude_*(1.0 - square(x/width_) ) ); }
 
206
 
 
207
            private:
 
208
 
 
209
            qreal amplitude_;
 
210
            qreal width_;
 
211
 
 
212
        };
 
213
 
 
214
        //! functions used to draw shadows
 
215
        class Gaussian
 
216
        {
 
217
            public:
 
218
 
 
219
            //! constructor
 
220
            Gaussian( qreal amplitude, qreal width ):
 
221
                amplitude_( amplitude ),
 
222
                width_( width )
 
223
            {}
 
224
 
 
225
            //! destructor
 
226
            virtual ~Gaussian( void )
 
227
            {}
 
228
 
 
229
            //! value
 
230
            virtual qreal operator() ( qreal x ) const
 
231
            { return qMax( 0.0, amplitude_*(std::exp( -square(x/width_) -0.05 ) ) ); }
 
232
 
 
233
            private:
 
234
 
 
235
            qreal amplitude_;
 
236
            qreal width_;
 
237
 
 
238
        };
 
239
 
 
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;
 
243
 
 
244
        private:
 
245
 
 
246
        //! helper
 
247
        Helper& _helper;
 
248
 
 
249
        //! defines overlap between shadows and body
 
250
        enum { overlap = 4 };
 
251
 
 
252
        //! caching enable state
 
253
        bool _enabled;
 
254
 
 
255
        //! max index
 
256
        /*! it is used to set caches max cost, and calculate animation opacity */
 
257
        int _maxIndex;
 
258
 
 
259
        //! shadow configuration
 
260
        ShadowConfiguration _activeShadowConfiguration;
 
261
 
 
262
        //! shadow configuration
 
263
        ShadowConfiguration _inactiveShadowConfiguration;
 
264
 
 
265
        //! cache
 
266
        typedef QCache<int, TileSet> TileSetCache;
 
267
 
 
268
        //! shadow cache
 
269
        TileSetCache _shadowCache;
 
270
 
 
271
        //! animated shadow cache
 
272
        TileSetCache _animatedShadowCache;
 
273
 
 
274
    };
 
275
 
 
276
}
 
277
 
 
278
#endif