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

« back to all changes in this revision

Viewing changes to libs/oxygen/oxygenhelper.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_helper_h
 
2
#define oxygen_helper_h
 
3
 
 
4
/*
 
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>
 
10
 *
 
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.
 
14
 *
 
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.
 
19
 *
 
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.
 
24
 */
 
25
 
 
26
#include "oxygentileset.h"
 
27
 
 
28
#include <KSharedConfig>
 
29
#include <KComponentData>
 
30
#include <KColorScheme>
 
31
 
 
32
#include <QtGui/QColor>
 
33
#include <QtGui/QPixmap>
 
34
#include <QtGui/QWidget>
 
35
#include <QtGui/QLinearGradient>
 
36
#include <QtCore/QCache>
 
37
 
 
38
#ifdef Q_WS_X11
 
39
#include <X11/Xdefs.h>
 
40
#endif
 
41
 
 
42
namespace Oxygen
 
43
{
 
44
 
 
45
    template<typename T> class BaseCache: public QCache<quint64, T>
 
46
    {
 
47
 
 
48
        public:
 
49
 
 
50
        //! constructor
 
51
        BaseCache( int maxCost ):
 
52
            QCache<quint64, T>( maxCost ),
 
53
            _enabled( true )
 
54
        {}
 
55
 
 
56
        //! constructor
 
57
        explicit BaseCache( void ):
 
58
            _enabled( true )
 
59
            {}
 
60
 
 
61
        //! destructor
 
62
        ~BaseCache( void )
 
63
        {}
 
64
 
 
65
        //! enable
 
66
        void setEnabled( bool value )
 
67
        { _enabled = value; }
 
68
 
 
69
        //! enable state
 
70
        bool enabled( void ) const
 
71
        { return _enabled; }
 
72
 
 
73
        //! access
 
74
        T* object( const quint64& key )
 
75
        { return _enabled ? QCache<quint64, T>::object( key ) : 0; }
 
76
 
 
77
        //! max cost
 
78
        void setMaxCost( int cost )
 
79
        {
 
80
            if( cost <= 0 ) {
 
81
 
 
82
                QCache<quint64, T>::clear();
 
83
                QCache<quint64, T>::setMaxCost( 1 );
 
84
                setEnabled( false );
 
85
 
 
86
            } else {
 
87
 
 
88
                setEnabled( true );
 
89
                QCache<quint64, T>::setMaxCost( cost );
 
90
 
 
91
            }
 
92
        }
 
93
 
 
94
        private:
 
95
 
 
96
        //! enable flag
 
97
        bool _enabled;
 
98
 
 
99
    };
 
100
 
 
101
    template<typename T> class Cache
 
102
    {
 
103
 
 
104
        public:
 
105
 
 
106
        //! constructor
 
107
        Cache()
 
108
        {}
 
109
 
 
110
        //! destructor
 
111
        ~Cache()
 
112
        {}
 
113
 
 
114
        //! return cache matching a given key
 
115
        //typedef QCache<quint64, T> Value;
 
116
        typedef BaseCache<T> Value;
 
117
        Value* get( const QColor& color )
 
118
        {
 
119
            quint64 key = ( quint64( color.rgba() ) << 32 );
 
120
            Value* cache = data_.object( key );
 
121
 
 
122
            if ( !cache )
 
123
            {
 
124
                cache = new Value( data_.maxCost() );
 
125
                data_.insert( key, cache );
 
126
            }
 
127
 
 
128
            return cache;
 
129
        }
 
130
 
 
131
        //! clear
 
132
        void clear( void )
 
133
        { data_.clear(); }
 
134
 
 
135
        //! max cache size
 
136
        void setMaxCacheSize( int value )
 
137
        {
 
138
            data_.setMaxCost( value );
 
139
            foreach( quint64 key, data_.keys() )
 
140
            { data_.object( key )->setMaxCost( value ); }
 
141
        }
 
142
 
 
143
        private:
 
144
 
 
145
        //! data
 
146
        BaseCache<Value> data_;
 
147
 
 
148
    };
 
149
 
 
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
 
153
    {
 
154
        public:
 
155
 
 
156
        //! constructor
 
157
        Helper( const QByteArray& componentName );
 
158
 
 
159
        //! destructor
 
160
        virtual ~Helper()
 
161
        {}
 
162
 
 
163
        //! reload configuration
 
164
        virtual void reloadConfig();
 
165
 
 
166
        //! pointer to shared config
 
167
        KSharedConfigPtr config() const;
 
168
 
 
169
        //! reset all caches
 
170
        virtual void invalidateCaches();
 
171
 
 
172
        //! update maximum cache size
 
173
        virtual void setMaxCacheSize( int );
 
174
 
 
175
        //!@name window background gradients
 
176
        //@{
 
177
        /*!
 
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
 
181
        */
 
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 ); }
 
184
 
 
185
        /*!
 
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
 
189
        */
 
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 ); }
 
192
 
 
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 ); }
 
196
 
 
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 );
 
199
 
 
200
        //! background pixmap
 
201
        bool hasBackgroundPixmap( void ) const
 
202
        { return !_backgroundPixmap.isNull(); }
 
203
 
 
204
        //! background pixmap
 
205
        void setBackgroundPixmap( const QPixmap& pixmap )
 
206
        { _backgroundPixmap = pixmap; }
 
207
 
 
208
        //! offset
 
209
        void setBackgroundPixmapOffset( const QPoint& offset )
 
210
        { _backgroundPixmapOffset = offset; }
 
211
 
 
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 );
 
214
 
 
215
        //@}
 
216
 
 
217
        //! dots
 
218
        void renderDot( QPainter*, const QPoint&, const QColor& );
 
219
 
 
220
        //! returns true for too 'dark' colors
 
221
        bool lowThreshold( const QColor& color );
 
222
 
 
223
        //! returns true for too 'light' colors
 
224
        bool highThreshold( const QColor& color );
 
225
 
 
226
        //! add alpha channel multiplier to color
 
227
        static QColor alphaColor( QColor color, qreal alpha );
 
228
 
 
229
        //! calculated light color from argument
 
230
        virtual const QColor& calcLightColor( const QColor& color );
 
231
 
 
232
        //! calculated dark color from argument
 
233
        virtual const QColor& calcDarkColor( const QColor& color );
 
234
 
 
235
        //! calculated shadow color from argument
 
236
        virtual const QColor& calcShadowColor( const QColor& color );
 
237
 
 
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 )
 
240
        {
 
241
            if( !( w && w->window() ) || checkAutoFillBackground( w ) ) return color;
 
242
            else return backgroundColor( color, w->window()->height(), w->mapTo( w->window(), point ).y() );
 
243
        }
 
244
 
 
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 ) ) ); }
 
248
 
 
249
        //! color used for background radial gradient
 
250
        virtual const QColor& backgroundRadialColor( const QColor& color );
 
251
 
 
252
        //! color used at the top of window background
 
253
        virtual const QColor& backgroundTopColor( const QColor& color );
 
254
 
 
255
        //! color used at the bottom of window background
 
256
        virtual const QColor& backgroundBottomColor( const QColor& color );
 
257
 
 
258
        //! vertical gradient for window background
 
259
        virtual QPixmap verticalGradient( const QColor& color, int height, int offset = 0 );
 
260
 
 
261
        //! radial gradient for window background
 
262
        virtual QPixmap radialGradient( const QColor& color, int width, int height = 64 );
 
263
 
 
264
        //! merge background and front color for check marks, arrows, etc. using _contrast
 
265
        virtual const QColor& decoColor( const QColor& background, const QColor& color );
 
266
 
 
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;
 
270
 
 
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
 
278
            );
 
279
 
 
280
        //! draw dividing line
 
281
        virtual void drawSeparator( QPainter*, const QRect&, const QColor&, Qt::Orientation );
 
282
 
 
283
        //! default slab
 
284
        virtual TileSet* slab( const QColor& color, qreal shade, int size = 7 )
 
285
        { return slab( color, QColor(), shade, size );  }
 
286
 
 
287
        //! default slab (with glow)
 
288
        virtual TileSet* slab( const QColor&, const QColor& glow, qreal shade, int size = 7 );
 
289
 
 
290
        //! sunken slab
 
291
        virtual TileSet *slabSunken( const QColor&, int size = 7 );
 
292
 
 
293
        //! fill a slab of given size with brush set on painter
 
294
        void fillSlab( QPainter&, const QRect&, int size = 7 ) const;
 
295
 
 
296
        //! linear gradient used to fill buttons
 
297
        virtual void fillButtonSlab( QPainter&, const QRect&, const QColor&, bool sunken );
 
298
 
 
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;
 
302
 
 
303
        //! focus brush
 
304
        const KStatefulBrush& viewFocusBrush( void ) const
 
305
        { return _viewFocusBrush; }
 
306
 
 
307
        //! hover brush
 
308
        const KStatefulBrush& viewHoverBrush( void ) const
 
309
        { return _viewHoverBrush; }
 
310
 
 
311
        //! negative text brush ( used for close button hover )
 
312
        const KStatefulBrush& viewNegativeTextBrush( void ) const
 
313
        { return _viewNegativeTextBrush; }
 
314
 
 
315
        /*!
 
316
        returns first widget in parent chain that sets autoFillBackground to true,
 
317
        or NULL if none
 
318
        */
 
319
        const QWidget* checkAutoFillBackground( const QWidget* ) const;
 
320
 
 
321
        //!@name background gradient XProperty
 
322
        //@{
 
323
 
 
324
        //! set background gradient hint to widget
 
325
        virtual void setHasBackgroundGradient( WId, bool ) const;
 
326
 
 
327
        //! true if background gradient hint is set
 
328
        virtual bool hasBackgroundGradient( WId ) const;
 
329
 
 
330
        //! set background pixmap hint to widget
 
331
        virtual void setHasBackgroundPixmap( WId, bool ) const;
 
332
 
 
333
        //! true if background pixmap hint is set
 
334
        virtual bool hasBackgroundPixmap( WId ) const;
 
335
 
 
336
        //@}
 
337
 
 
338
        protected:
 
339
 
 
340
        //! generic slab painting (to be stored in tilesets)
 
341
        virtual void drawSlab( QPainter&, const QColor&, qreal shade );
 
342
 
 
343
        //! generic outer shadow (to be stored in tilesets)
 
344
        virtual void drawShadow( QPainter&, const QColor&, int size );
 
345
 
 
346
        //! generic outer glow (to be stored in tilesets)
 
347
        virtual void drawOuterGlow( QPainter&, const QColor&, int size );
 
348
 
 
349
        //! return background adjusted color matching relative vertical position in window
 
350
        const QColor& backgroundColor( const QColor&, qreal ratio );
 
351
 
 
352
        //!@name global configuration parameters
 
353
        //@{
 
354
 
 
355
        static const qreal _glowBias;
 
356
        static const qreal _slabThickness;
 
357
        static const qreal _shadowGain;
 
358
        qreal _contrast;
 
359
 
 
360
        //@}
 
361
 
 
362
        //!@name pixmap caches
 
363
        //@{
 
364
        typedef BaseCache<QPixmap> PixmapCache;
 
365
 
 
366
        //! window decoration buttons
 
367
        PixmapCache& windecoButtonCache( void )
 
368
        { return _windecoButtonCache; }
 
369
 
 
370
        //! window decoration button glow
 
371
        PixmapCache& windecoButtonGlowCache( void )
 
372
        { return _windecoButtonGlowCache; }
 
373
 
 
374
        //@}
 
375
 
 
376
        //! shortcut to color caches
 
377
        /*! it is made protected because it is also used in the style helper */
 
378
        typedef BaseCache<QColor> ColorCache;
 
379
 
 
380
        private:
 
381
 
 
382
        //!@name tileset caches
 
383
        //!@{
 
384
 
 
385
        //! slabs
 
386
        Oxygen::Cache<TileSet> _slabCache;
 
387
 
 
388
        //! sunken slabs
 
389
        BaseCache<TileSet> _slabSunkenCache;
 
390
 
 
391
        //@}
 
392
 
 
393
        //!@name brushes
 
394
        //@{
 
395
        KStatefulBrush _viewFocusBrush;
 
396
        KStatefulBrush _viewHoverBrush;
 
397
        KStatefulBrush _viewNegativeTextBrush;
 
398
        //@}
 
399
 
 
400
        KComponentData _componentData;
 
401
        KSharedConfigPtr _config;
 
402
        qreal _bgcontrast;
 
403
 
 
404
        //!@name color caches
 
405
        //@{
 
406
        ColorCache _decoColorCache;
 
407
        ColorCache _lightColorCache;
 
408
        ColorCache _darkColorCache;
 
409
        ColorCache _shadowColorCache;
 
410
        ColorCache _backgroundTopColorCache;
 
411
        ColorCache _backgroundBottomColorCache;
 
412
        ColorCache _backgroundRadialColorCache;
 
413
        ColorCache _backgroundColorCache;
 
414
        //@}
 
415
 
 
416
        PixmapCache _windecoButtonCache;
 
417
        PixmapCache _windecoButtonGlowCache;
 
418
        PixmapCache _backgroundCache;
 
419
        PixmapCache _dotCache;
 
420
 
 
421
        //! high threshold colors
 
422
        typedef QMap<quint32, bool> ColorMap;
 
423
        ColorMap _highThreshold;
 
424
        ColorMap _lowThreshold;
 
425
 
 
426
        //! background pixmap
 
427
        QPixmap _backgroundPixmap;
 
428
 
 
429
        //! background pixmap offsets
 
430
        QPoint _backgroundPixmapOffset;
 
431
 
 
432
        #ifdef Q_WS_X11
 
433
 
 
434
        //! set value for given hint
 
435
        void setHasHint( WId, Atom, bool ) const;
 
436
 
 
437
        //! value for given hint
 
438
        bool hasHint( WId, Atom ) const;
 
439
 
 
440
        //! background gradient hint atom
 
441
        Atom _backgroundGradientAtom;
 
442
 
 
443
        //! background gradient hint atom
 
444
        Atom _backgroundPixmapAtom;
 
445
 
 
446
        #endif
 
447
     };
 
448
 
 
449
}
 
450
 
 
451
#endif