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

« back to all changes in this revision

Viewing changes to kstyles/oxygen/oxygenstylehelper.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_style_helper_h
 
2
#define oxygen_style_helper_h
 
3
 
 
4
/*
 
5
 * Copyright 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
 *
 
10
 * This library is free software; you can redistribute it and/or
 
11
 * modify it under the terms of the GNU Library General Public
 
12
 * License version 2 as published by the Free Software Foundation.
 
13
 *
 
14
 * This library is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * Library General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU Library General Public License
 
20
 * along with this library; see the file COPYING.LIB.  If not, write to
 
21
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
22
 * Boston, MA 02110-1301, USA.
 
23
 */
 
24
 
 
25
#include "oxygenhelper.h"
 
26
#include "oxygenanimationmodes.h"
 
27
 
 
28
#include <KWindowSystem>
 
29
#include <KDebug>
 
30
 
 
31
#ifdef Q_WS_X11
 
32
#include <QtGui/QX11Info>
 
33
#endif
 
34
 
 
35
//! helper class
 
36
/*! contains utility functions used at multiple places in oxygen style */
 
37
namespace Oxygen
 
38
{
 
39
 
 
40
    enum HoleOption
 
41
    {
 
42
        HoleFocus = 0x1,
 
43
        HoleHover = 0x2,
 
44
        HoleOutline = 0x4,
 
45
        HoleContrast = 0x8
 
46
    };
 
47
 
 
48
    Q_DECLARE_FLAGS(HoleOptions, HoleOption)
 
49
 
 
50
    class StyleHelper : public Helper
 
51
    {
 
52
        public:
 
53
 
 
54
        //! constructor
 
55
        explicit StyleHelper( const QByteArray &componentName );
 
56
 
 
57
        //! destructor
 
58
        virtual ~StyleHelper() {}
 
59
 
 
60
        //! dynamically allocated debug area
 
61
        int debugArea( void ) const
 
62
        { return _debugArea; }
 
63
 
 
64
        //! clear cache
 
65
        virtual void invalidateCaches();
 
66
 
 
67
        //! update maximum cache size
 
68
        virtual void setMaxCacheSize( int );
 
69
 
 
70
        //! render window background using a given color as a reference
 
71
        /*!
 
72
        For the widget style, both the gradient and the background pixmap are rendered in the same method.
 
73
        All the actual rendering is performed by the base class
 
74
        */
 
75
        using Helper::renderWindowBackground;
 
76
        virtual void renderWindowBackground( QPainter* p, const QRect& clipRect, const QWidget* widget, const QColor& color, int y_shift=-23, int gradientHeight = 64 )
 
77
        {
 
78
            Helper::renderWindowBackground( p, clipRect, widget, widget->window(), color, y_shift, gradientHeight );
 
79
            Helper::renderBackgroundPixmap( p, clipRect, widget, widget->window(), y_shift, gradientHeight );
 
80
        }
 
81
 
 
82
        // render menu background
 
83
        void renderMenuBackground( QPainter* p, const QRect& clipRect, const QWidget* widget, const QPalette& pal )
 
84
        { renderMenuBackground( p, clipRect, widget, pal.color( widget->window()->backgroundRole() ) ); }
 
85
 
 
86
        // render menu background
 
87
        void renderMenuBackground( QPainter*, const QRect&, const QWidget*, const QColor& );
 
88
 
 
89
        //! returns menu background color matching position in a given menu widget
 
90
        virtual const QColor& menuBackgroundColor( const QColor& color, const QWidget* w, const QPoint& point )
 
91
        {
 
92
            if( !( w && w->window() ) || checkAutoFillBackground( w ) ) return color;
 
93
            else return menuBackgroundColor( color, w->window()->height(), w->mapTo( w->window(), point ).y() );
 
94
        }
 
95
 
 
96
        //! returns menu background color matching position in a menu widget of given height
 
97
        virtual const QColor& menuBackgroundColor( const QColor& color, int height, int y )
 
98
        { return backgroundColor( color, qMin( qreal( 1.0 ), qreal( y )/qMin( 200, 3*height/4 ) ) ); }
 
99
 
 
100
        //! color
 
101
        inline const QColor& calcMidColor( const QColor& color );
 
102
 
 
103
        //! merge active and inactive palettes based on ratio, for smooth enable state change transition
 
104
        QPalette mergePalettes( const QPalette&, qreal ratio ) const;
 
105
 
 
106
        //! overloaded window decoration buttons for MDI windows
 
107
        virtual QPixmap windecoButton( const QColor& color, bool pressed, int size = 21 );
 
108
 
 
109
        //! round corners( used for Menus, combobox drop-down, detached toolbars and dockwidgets
 
110
        TileSet *roundCorner( const QColor&, int size = 5 );
 
111
 
 
112
        //! groupbox background
 
113
        TileSet *slope( const QColor&, qreal shade, int size = 7 );
 
114
 
 
115
        //!@name slabs
 
116
        //@{
 
117
 
 
118
        //! progressbar
 
119
        QPixmap progressBarIndicator( const QPalette&, const QRect& );
 
120
 
 
121
        //! dial
 
122
        QPixmap dialSlab( const QColor& color, qreal shade, int size = 7 )
 
123
        { return dialSlab( color, QColor(), shade, size ); }
 
124
 
 
125
        //! dial
 
126
        QPixmap dialSlab( const QColor&, const QColor&, qreal shade, int size = 7 );
 
127
 
 
128
        // round slabs
 
129
        QPixmap roundSlab( const QColor& color, qreal shade, int size = 7 )
 
130
        { return roundSlab( color, QColor(), shade, size ); }
 
131
 
 
132
        // round slab
 
133
        QPixmap roundSlab( const QColor&, const QColor& glow, qreal shade, int size = 7 );
 
134
 
 
135
        //! slider slab
 
136
        QPixmap sliderSlab( const QColor&, const QColor& glow, bool sunken, qreal shade, int size = 7 );
 
137
 
 
138
        //@}
 
139
 
 
140
        //!@name holes
 
141
        //@{
 
142
 
 
143
        void fillHole( QPainter&, const QRect&, int size = 7 ) const;
 
144
 
 
145
        //! generic hole
 
146
        void renderHole( QPainter *p, const QColor& color, const QRect &r,
 
147
            HoleOptions options = 0,
 
148
            TileSet::Tiles tiles = TileSet::Ring )
 
149
        { renderHole( p, color, r, options, -1, Oxygen::AnimationNone, tiles ); }
 
150
 
 
151
        //! generic hole (with animated glow)
 
152
        void renderHole(
 
153
            QPainter*, const QColor&, const QRect &r,
 
154
            HoleOptions,
 
155
            qreal opacity, Oxygen::AnimationMode animationMode,
 
156
            TileSet::Tiles = TileSet::Ring );
 
157
 
 
158
        TileSet *holeFlat( const QColor&, qreal shade, bool fill = true, int size = 7 );
 
159
 
 
160
        //! scrollbar hole
 
161
        TileSet *scrollHole( const QColor&, Qt::Orientation orientation, bool smallShadow = false );
 
162
 
 
163
        //! scrollbar handle
 
164
        TileSet *scrollHandle( const QColor&, const QColor&, int size = 7 );
 
165
 
 
166
        //@}
 
167
 
 
168
        //! scrollbar groove
 
169
        TileSet *groove( const QColor&, int size = 7 );
 
170
 
 
171
        //! focus rect for flat toolbuttons
 
172
        TileSet *slitFocused( const QColor& );
 
173
 
 
174
        //! dock frame
 
175
        TileSet *dockFrame( const QColor&, const QColor& );
 
176
 
 
177
        //! selection
 
178
        TileSet *selection( const QColor&, int height, bool custom );
 
179
 
 
180
        //! inverse glow
 
181
        /*! this method must be public because it is used directly by OxygenStyle to draw dials */
 
182
        void drawInverseGlow( QPainter&, const QColor&, int pad, int size, int rsize ) const;
 
183
 
 
184
        //!@name utility functions
 
185
 
 
186
        //! returns true if compositing is active
 
187
        bool compositingActive( void ) const
 
188
        { return KWindowSystem::compositingActive(); }
 
189
 
 
190
        //! returns true if a given widget supports alpha channel
 
191
        inline bool hasAlphaChannel( const QWidget* ) const;
 
192
 
 
193
        //! returns true if given widget will get a decoration
 
194
        bool hasDecoration( const QWidget* ) const;
 
195
 
 
196
        //@}
 
197
 
 
198
        protected:
 
199
 
 
200
        //!@name holes
 
201
        //@{
 
202
 
 
203
        //! holes
 
204
        TileSet *hole( const QColor& color, int size = 7, HoleOptions options = 0 )
 
205
        { return hole( color, QColor(), size, options ); }
 
206
 
 
207
        //! holes
 
208
        TileSet *hole( const QColor&, const QColor& glow, int size = 7, HoleOptions = 0 );
 
209
 
 
210
        //@}
 
211
 
 
212
        // round slabs
 
213
        void drawRoundSlab( QPainter&, const QColor&, qreal );
 
214
 
 
215
        // slider slabs
 
216
        void drawSliderSlab( QPainter&, const QColor&, bool sunken, qreal );
 
217
 
 
218
        private:
 
219
 
 
220
        //! dynamically allocated debug area
 
221
        int _debugArea;
 
222
 
 
223
        Cache<QPixmap> _dialSlabCache;
 
224
        Cache<QPixmap> _roundSlabCache;
 
225
        Cache<QPixmap> _sliderSlabCache;
 
226
        Cache<TileSet> _holeCache;
 
227
        Cache<TileSet> _scrollHandleCache;
 
228
 
 
229
        //! mid color cache
 
230
        ColorCache _midColorCache;
 
231
 
 
232
        //! progressbar cache
 
233
        PixmapCache _progressBarCache;
 
234
 
 
235
        typedef BaseCache<TileSet> TileSetCache;
 
236
        TileSetCache _cornerCache;
 
237
        TileSetCache _holeFlatCache;
 
238
        TileSetCache _slopeCache;
 
239
        TileSetCache _grooveCache;
 
240
        TileSetCache _slitCache;
 
241
        TileSetCache _dockFrameCache;
 
242
        TileSetCache _scrollHoleCache;
 
243
        TileSetCache _selectionCache;
 
244
 
 
245
    };
 
246
 
 
247
    //____________________________________________________________________
 
248
    const QColor& StyleHelper::calcMidColor( const QColor& color )
 
249
    {
 
250
        const quint64 key( color.rgba() );
 
251
        QColor* out( _midColorCache.object( key ) );
 
252
        if( !out )
 
253
        {
 
254
            out = new QColor( KColorScheme::shade( color, KColorScheme::MidShade, _contrast - 1.0 ) );
 
255
            _midColorCache.insert( key, out );
 
256
        }
 
257
 
 
258
        return *out;
 
259
    }
 
260
 
 
261
    //____________________________________________________________________
 
262
    bool StyleHelper::hasAlphaChannel( const QWidget* widget ) const
 
263
    {
 
264
        #ifdef Q_WS_X11
 
265
        if( compositingActive() )
 
266
        {
 
267
 
 
268
            if( widget ) return widget->x11Info().depth() == 32;
 
269
            else return QX11Info().appDepth() == 32;
 
270
 
 
271
        } else return false;
 
272
 
 
273
        #else
 
274
        return compositingActive();
 
275
        #endif
 
276
 
 
277
    }
 
278
 
 
279
}
 
280
#endif