File: oxygenstylehelper.h

package info (click to toggle)
oxygen 4%3A5.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 25,980 kB
  • sloc: cpp: 22,277; python: 39; makefile: 24; sh: 13
file content (309 lines) | stat: -rw-r--r-- 11,570 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#ifndef oxygen_style_helper_h
#define oxygen_style_helper_h

/*
 * Copyright 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr>
 * Copyright 2008 Long Huynh Huu <long.upcase@googlemail.com>
 * Copyright 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
 * Copyright 2007 Casper Boemann <cbr@boemann.dk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License version 2 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "oxygenhelper.h"
#include "oxygen.h"

#include <KWindowSystem>

#if OXYGEN_HAVE_X11
#include <xcb/xcb.h>
#endif

//* helper class
/** contains utility functions used at multiple places in oxygen style */
namespace Oxygen
{

    class StyleHelper : public Helper
    {
        public:

        //* constructor
        explicit StyleHelper( KSharedConfigPtr config );

        #if OXYGEN_USE_KDE4
        //* constructor
        explicit StyleHelper( const QByteArray& );
        #endif

        //* destructor
        virtual ~StyleHelper() {}

        //* clear cache
        void invalidateCaches() ;

        //* update maximum cache size
        void setMaxCacheSize( int ) ;

        //* background gradient
        virtual void setUseBackgroundGradient( bool value )
        { _useBackgroundGradient = value; }

        //* render window background using a given color as a reference
        /**
        For the widget style, both the gradient and the background pixmap are rendered in the same method.
        All the actual rendering is performed by the base class
        */
        using Helper::renderWindowBackground;
        void renderWindowBackground( QPainter*, const QRect&, const QWidget*, const QColor&, int y_shift=-23 ) ;

        //* set background gradient hint to widget
        void setHasBackgroundGradient( WId, bool ) const ;

        // render menu background
        void renderMenuBackground( QPainter* p, const QRect& clipRect, const QWidget* widget, const QPalette& pal )
        { renderMenuBackground( p, clipRect, widget, pal.color( widget->window()->backgroundRole() ) ); }

        // render menu background
        void renderMenuBackground( QPainter*, const QRect&, const QWidget*, const QColor& );

        //*@name color utilities
        //@{

        //* glow color for buttons (mouse-over takes precedence over focus)
        QColor buttonGlowColor( const QPalette& palette, StyleOptions options, qreal opacity, AnimationMode mode ) const
        { return buttonGlowColor( palette.currentColorGroup(), options, opacity, mode ); }

        //* glow color for frames (focus takes precedence over mouse-over)
        QColor frameGlowColor( const QPalette& palette, StyleOptions options, qreal opacity, AnimationMode mode ) const
        { return frameGlowColor( palette.currentColorGroup(), options, opacity, mode ); }

        //* glow color for arrows (mouse-over takes precedence over focus)
        QColor arrowColor( const QPalette& palette, StyleOptions options, qreal opacity, AnimationMode mode ) const;

        //* glow color for buttons (mouse-over takes precedence over focus)
        QColor buttonGlowColor( QPalette::ColorGroup, StyleOptions, qreal, AnimationMode ) const;

        //* glow color for frames (focus takes precedence over mouse-over)
        QColor frameGlowColor( QPalette::ColorGroup, StyleOptions, qreal, AnimationMode ) const;

        //* returns menu background color matching position in a given menu widget
        virtual QColor menuBackgroundColor( const QColor& color, const QWidget* w, const QPoint& point )
        {
            if( !( w && w->window() ) || checkAutoFillBackground( w ) ) return color;
            else return menuBackgroundColor( color, w->window()->height(), w->mapTo( w->window(), point ).y() );
        }

        //* returns menu background color matching position in a menu widget of given height
        virtual QColor menuBackgroundColor( const QColor& color, int height, int y )
        { return backgroundColor( color, qMin( qreal( 1.0 ), qreal( y )/qMin( 200, 3*height/4 ) ) ); }

        //* color
        inline QColor calcMidColor( const QColor& color );

        //* merge active and inactive palettes based on ratio, for smooth enable state change transition
        QPalette disabledPalette( const QPalette&, qreal ratio ) const;

        //@}

        //* overloaded window decoration buttons for MDI windows
        virtual QPixmap dockWidgetButton( const QColor& color, bool pressed, int size = 21 );

        //* round corners( used for Menus, combobox drop-down, detached toolbars and dockwidgets
        TileSet roundCorner( const QColor&, int size = 5 );

        //* groupbox background
        TileSet slope( const QColor&, qreal shade, int size = TileSet::DefaultSize );

        //*@name slabs
        //@{

        //* inverse (inner-hole) shadow
        /** this method must be public because it is used directly by OxygenStyle to draw dials */
        void drawInverseShadow( QPainter&, const QColor&, int pad, int size, qreal fuzz ) const;

        //* fill a slab of given size with brush set on painter
        void fillSlab( QPainter&, const QRect&, int size = TileSet::DefaultSize ) const;

        //* linear gradient used to fill buttons
        virtual void fillButtonSlab( QPainter&, const QRect&, const QColor&, bool sunken );

        //* default slab
        virtual TileSet slab( const QColor& color, qreal shade, int size = TileSet::DefaultSize )
        { return slab( color, QColor(), shade, size );  }

        //* default slab (with glow)
        virtual TileSet slab( const QColor&, const QColor& glow, qreal shade, int size = TileSet::DefaultSize );

        //* sunken slab
        virtual TileSet slabSunken( const QColor&, int size = TileSet::DefaultSize );

        //* progressbar
        TileSet progressBarIndicator( const QPalette&, int );

        //* dial
        QPixmap dialSlab( const QColor& color, qreal shade, int size = TileSet::DefaultSize )
        { return dialSlab( color, QColor(), shade, size ); }

        //* dial
        QPixmap dialSlab( const QColor&, const QColor&, qreal shade, int size = TileSet::DefaultSize );

        // round slabs
        QPixmap roundSlab( const QColor& color, qreal shade, int size = TileSet::DefaultSize )
        { return roundSlab( color, QColor(), shade, size ); }

        // round slab
        QPixmap roundSlab( const QColor&, const QColor& glow, qreal shade, int size = TileSet::DefaultSize );

        //* slider slab
        QPixmap sliderSlab( const QColor&, const QColor& glow, bool sunken, qreal shade, int size = TileSet::DefaultSize );

        //@}

        //* debug frame
        void renderDebugFrame( QPainter*, const QRect& ) const;

        //*@name holes
        //@{

        void fillHole( QPainter&, const QRect&, int offset = 2 ) const;

        //* generic hole
        void renderHole( QPainter *painter, const QColor& color, const QRect &rect, StyleOptions options = 0, TileSet::Tiles tiles = TileSet::Ring )
        { renderHole( painter, color, rect, options, -1, Oxygen::AnimationNone, tiles ); }

        //* generic hole (with animated glow)
        void renderHole( QPainter*, const QColor&, const QRect&, StyleOptions, qreal, Oxygen::AnimationMode, TileSet::Tiles = TileSet::Ring );

        TileSet holeFlat( const QColor&, qreal shade, bool fill = true, int size = TileSet::DefaultSize );

        //* scrollbar hole
        TileSet scrollHole( const QColor&, Qt::Orientation orientation, bool smallShadow = false );

        //* scrollbar handle
        TileSet scrollHandle( const QColor&, const QColor&, int size = TileSet::DefaultSize );

        //@}

        //* focus rect for flat toolbuttons
        TileSet slitFocused( const QColor& );

        //* dock frame
        TileSet dockFrame( const QColor&, const QColor& );

        //* selection
        TileSet selection( const QColor&, int height, bool custom );

        //* inverse glow
        /** this method must be public because it is used directly by OxygenStyle to draw dials */
        void drawInverseGlow( QPainter&, const QColor&, int pad, int size, int rsize ) const;

        //*@name utility functions

        //* returns true if compositing is active
        bool compositingActive( void ) const;

        //* returns true if a given widget supports alpha channel
        inline bool hasAlphaChannel( const QWidget* ) const;

        //* returns true if given widget will get a decoration
        bool hasDecoration( const QWidget* ) const;

        //@}

        protected:

        //*@name holes
        //@{

        //* holes
        TileSet hole( const QColor& color, int size = TileSet::DefaultSize, StyleOptions options = 0 )
        { return hole( color, QColor(), size, options ); }

        //* holes
        TileSet hole( const QColor&, const QColor& glow, int size = TileSet::DefaultSize, StyleOptions = 0 );

        //@}

        //* generic slab painting (to be stored in tilesets)
        virtual void drawSlab( QPainter&, const QColor&, qreal shade );

        // round slabs
        void drawRoundSlab( QPainter&, const QColor&, qreal );

        // slider slabs
        void drawSliderSlab( QPainter&, const QColor&, bool sunken, qreal );

        private:

        //* initialize
        void init( void );

        //* background grandient
        bool _useBackgroundGradient;

        Cache<QPixmap> _dialSlabCache;
        Cache<QPixmap> _roundSlabCache;
        Cache<QPixmap> _sliderSlabCache;
        Cache<TileSet> _holeCache;
        Cache<TileSet> _scrollHandleCache;
        Cache<TileSet> _slabCache;

        //* mid color cache
        ColorCache _midColorCache;

        //* dock button cache
        PixmapCache _dockWidgetButtonCache;


        using TileSetCache = BaseCache<TileSet>;
        TileSetCache _slabSunkenCache;
        TileSetCache _cornerCache;
        TileSetCache _holeFlatCache;
        TileSetCache _slopeCache;
        TileSetCache _slitCache;
        TileSetCache _dockFrameCache;
        TileSetCache _scrollHoleCache;
        TileSetCache _selectionCache;
        TileSetCache _progressBarCache;

        #if OXYGEN_HAVE_X11

        //* atom used for compositing manager
        xcb_atom_t _compositingManagerAtom;

        #endif

    };

    //____________________________________________________________________
    QColor StyleHelper::calcMidColor( const QColor& color )
    {
        const quint64 key( color.rgba() );
        if( QColor* cachedColor = _midColorCache.object( key ) )
        { return *cachedColor; }

        QColor out = KColorScheme::shade( color, KColorScheme::MidShade, _contrast - 1.0 );
        _midColorCache.insert( key, new QColor( out ) );

        return out;
    }

    //____________________________________________________________________
    bool StyleHelper::hasAlphaChannel( const QWidget* widget ) const
    { return compositingActive() && widget && widget->testAttribute( Qt::WA_TranslucentBackground ); }

}
#endif