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

« back to all changes in this revision

Viewing changes to kstyles/keramik/pixmaploader.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
/*
 
2
   Copyright (c) 2003-2005 Maksim Orlovich <maksim@kde.org>
 
3
   Copyright (c) 2002 Malte Starostik <malte@kde.org>
 
4
   
 
5
 
 
6
   This library is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU Library General Public
 
8
   License as published by the Free Software Foundation; either
 
9
   version 2 of the License, or (at your option) any later version.
 
10
 
 
11
   This library is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
   Library General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU Library General Public License
 
17
   along with this library; see the file COPYING.LIB.  If not, write to
 
18
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
   Boston, MA 02110-1301, USA.
 
20
*/
 
21
 
 
22
#ifndef __pixmaploader_h__
 
23
#define __pixmaploader_h__
 
24
 
 
25
#include <QtCore/QCache>
 
26
#include <QtGui/QImage>
 
27
#include <QtGui/QStyleOption>
 
28
 
 
29
class QPixmap;
 
30
class QImage;
 
31
 
 
32
#include "keramikrc.h"
 
33
 
 
34
namespace Keramik
 
35
{
 
36
        class PixmapLoader
 
37
        {
 
38
        public:
 
39
                PixmapLoader();
 
40
 
 
41
                QPixmap pixmap( int name, const QColor& color,  const QColor& bg,
 
42
                                                                                 bool disabled = false, bool blend = true );
 
43
 
 
44
                QPixmap scale( int name, int width, int height, const QColor& color,  const QColor& bg,
 
45
                                                                                bool disabled = false, bool blend = true );
 
46
                QSize size( int id );
 
47
 
 
48
                void clear();
 
49
 
 
50
                static PixmapLoader& the()
 
51
                {
 
52
                         if (!s_instance)
 
53
                                s_instance = new PixmapLoader;
 
54
                        return *s_instance;
 
55
                }
 
56
 
 
57
                static void release()
 
58
                {
 
59
                        delete s_instance;
 
60
                        s_instance = 0;
 
61
                }
 
62
 
 
63
        private:
 
64
 
 
65
                struct KeramikCacheEntry
 
66
                {
 
67
                        int m_id;
 
68
                        int m_width;
 
69
                        int m_height;
 
70
                        QRgb m_colorCode;
 
71
                        QRgb m_bgCode;
 
72
                        bool    m_disabled;
 
73
                        bool    m_blended;
 
74
 
 
75
                        QPixmap* m_pixmap;
 
76
 
 
77
                        KeramikCacheEntry(int id, const QColor& color, const QColor& bg, bool disabled,
 
78
                                                                                        bool blended, int width, int height, QPixmap* pixmap = 0 ):
 
79
                                m_id(id), m_width(width), m_height(height), m_colorCode(color.rgb()),m_bgCode(bg.rgb()),
 
80
                                m_disabled(disabled),  m_blended(blended), m_pixmap(pixmap)
 
81
                        {}
 
82
 
 
83
                        int key()
 
84
                        {
 
85
                               return (m_disabled ? 1 : 0) ^ (m_blended << 1) ^ (m_id<<2) ^ (m_width<<14) ^ (m_height<<24) ^ m_colorCode ^ (m_bgCode<<8);
 
86
                        }
 
87
 
 
88
                        bool operator == (const KeramikCacheEntry& other)
 
89
                        {
 
90
                                return (m_id        == other.m_id) &&
 
91
                                                        (m_width   == other.m_width) &&
 
92
                                                        (m_height == other.m_height) &&
 
93
                                                        (m_blended == other.m_blended) &&
 
94
                                                        (m_bgCode == other.m_bgCode) &&
 
95
                                                        (m_colorCode == other.m_colorCode) &&
 
96
                                                        (m_disabled == other.m_disabled);
 
97
                        }
 
98
 
 
99
                        ~KeramikCacheEntry()
 
100
                        {
 
101
                                delete m_pixmap;
 
102
                        }
 
103
                };
 
104
 
 
105
 
 
106
 
 
107
                QImage* getColored(int id, const QColor& color, const QColor& bg, bool blended);
 
108
                QImage* getDisabled(int id, const QColor& color, const QColor& bg, bool blended);
 
109
                QCache <int, KeramikCacheEntry>  m_pixmapCache;
 
110
 
 
111
 
 
112
                unsigned char clamp[540];
 
113
 
 
114
                static PixmapLoader* s_instance;
 
115
        };
 
116
 
 
117
        class TilePainter
 
118
        {
 
119
        public:
 
120
                TilePainter( int name ) : m_columns(1),m_rows(1),m_name( name ) {}
 
121
                virtual ~TilePainter() {}
 
122
 
 
123
                enum PaintMode
 
124
                {
 
125
                        PaintNormal,
 
126
                        PaintMask,
 
127
                        PaintFullBlend,
 
128
                        PaintTrivialMask
 
129
                };
 
130
 
 
131
                void draw( QPainter *p, int x, int y, int width, int height, const QColor& color, const QColor& bg,
 
132
                                 bool disabled = false, PaintMode mode = PaintNormal );
 
133
                void draw( QPainter *p, const QRect& rect, const QColor& color, const QColor& bg, bool disabled = false, PaintMode mode = PaintNormal )
 
134
                {
 
135
                        draw( p, rect.x(), rect.y(), rect.width(), rect.height(), color, bg, disabled, mode );
 
136
                }
 
137
 
 
138
        protected:
 
139
                enum TileMode { Fixed, Scaled, Tiled };
 
140
 
 
141
                unsigned int columns() const { return m_columns; }
 
142
                unsigned int rows() const { return m_rows; }
 
143
 
 
144
                /**
 
145
                The idea behind all this stuff is that for performance reasons, we want to
 
146
                use only integers to refer to widgets. So we give each widget a base ID
 
147
                that's divisible by 256, and have all the various tiles be referred to as that ID +
 
148
                some small number.
 
149
 
 
150
                genembed generates and assigns the base widget IDs, and maps various pixmaps suffixes
 
151
                into the adjustment numbers; using that info it writes out the tables mapping
 
152
                the IDs to pixmaps, and keramikrc.h, which provides nice symbolic constants for base IDs.
 
153
 
 
154
                When painting, however, we essentially represent the widget as a table, providing
 
155
                fixed/tiled/stretched modes for each column and row. So to draw the widget knowing its
 
156
                base ID, we need to know how many rows and columns there, what the scaling modes
 
157
                are, and how to get to each of the tiles -- i.e. the tiles' offset from the base ID.
 
158
 
 
159
                The various painter subclasses simply fill in most of that info into the members
 
160
                here during their construction, and implement the virtual tileName to get the offsets.
 
161
 
 
162
                Note that the IDs and offsets are separated since we can reuse the same code in many
 
163
                cases by splitting the widget identify from tile identity (as there are many
 
164
                different widgets that have the same or similar tile layout)
 
165
                */
 
166
                virtual int tileName( unsigned int, unsigned int ) const { return 0; }
 
167
 
 
168
                TileMode columnMode( unsigned int col) const
 
169
                {
 
170
                        return colMde[col];
 
171
                }
 
172
 
 
173
                TileMode rowMode( unsigned int row) const
 
174
                {
 
175
                        return rowMde[row];
 
176
                }
 
177
 
 
178
        protected:
 
179
                TileMode colMde[5], rowMde[5];
 
180
                unsigned int m_columns;
 
181
                unsigned int m_rows;
 
182
        private:
 
183
 
 
184
                int absTileName( unsigned int column, unsigned int row ) const
 
185
                {
 
186
                        int name = tileName( column, row );
 
187
                        return m_name + name;
 
188
                }
 
189
 
 
190
 
 
191
                QPixmap tile( unsigned int column, unsigned int row, const QColor& color, const QColor& bg, bool disabled, bool blend)
 
192
                        { return PixmapLoader::the().pixmap( absTileName( column, row ), color, bg, disabled, blend ); }
 
193
                QPixmap scale( unsigned int column, unsigned int row, int width, int height, const QColor& color, const QColor& bg,
 
194
                                                        bool disabled, bool blend )
 
195
                        { return PixmapLoader::the().scale( absTileName( column, row ), width, height, color,
 
196
                                                        bg, disabled, blend ); }
 
197
 
 
198
                int m_name;
 
199
 
 
200
        };
 
201
 
 
202
        class ScaledPainter : public TilePainter
 
203
        {
 
204
        public:
 
205
                enum Direction { Horizontal = 1, Vertical = 2, Both = Horizontal | Vertical };
 
206
                explicit ScaledPainter( int name, Direction direction = Both )
 
207
                        : TilePainter( name ), m_direction( direction )
 
208
                {
 
209
                        colMde[0] =  ( m_direction & Horizontal ) ? Scaled : Tiled;
 
210
                        rowMde[0] =  ( m_direction & Vertical ) ? Scaled : Tiled;
 
211
                }
 
212
 
 
213
                virtual ~ScaledPainter() {}
 
214
 
 
215
        private:
 
216
                Direction m_direction;
 
217
        };
 
218
 
 
219
        class CenteredPainter : public TilePainter
 
220
        {
 
221
        public:
 
222
                CenteredPainter( int name ) : TilePainter( name ) {
 
223
                        colMde[0] = colMde[1] = colMde[2] = colMde[3] =  Fixed;
 
224
                        rowMde[0] = rowMde[1] = rowMde[2] = rowMde[3] = Fixed;
 
225
                }
 
226
                virtual ~CenteredPainter() {}
 
227
 
 
228
        protected:
 
229
        };
 
230
 
 
231
        class RectTilePainter : public TilePainter
 
232
        {
 
233
        public:
 
234
                explicit RectTilePainter( int name,
 
235
                                 bool scaleH = true, bool scaleV = true,
 
236
                                 unsigned int columns = 3, unsigned int rows = 3 );
 
237
 
 
238
                virtual ~RectTilePainter() {}
 
239
 
 
240
        protected:
 
241
                virtual int tileName( unsigned int column, unsigned int row ) const;
 
242
        private:
 
243
                bool m_scaleH;
 
244
                bool m_scaleV;
 
245
        };
 
246
 
 
247
        class RowPainter: public TilePainter
 
248
        {
 
249
        public:
 
250
                RowPainter(int name): TilePainter(name)
 
251
                {
 
252
                        colMde[0] = colMde[2] = Fixed;
 
253
                        colMde[1] = Tiled;
 
254
                        rowMde[0] = Scaled;
 
255
                        m_columns = 3;
 
256
                }
 
257
 
 
258
                virtual ~RowPainter() {}
 
259
        protected:
 
260
                virtual int tileName( unsigned int column, unsigned int /*row*/) const
 
261
                {
 
262
                        return column + 3; //So can use cl, cc, cr
 
263
                }
 
264
        };
 
265
 
 
266
        class ProgressBarPainter: public TilePainter
 
267
        {
 
268
        public:
 
269
                ProgressBarPainter(int name, bool reverse): TilePainter(name), m_reverse(reverse)
 
270
                {
 
271
                        //We use only of the tip bitmaps..
 
272
                        if (reverse)
 
273
                        {
 
274
                                colMde[0] = Fixed;
 
275
                                colMde[1] = Tiled;
 
276
                        }
 
277
                        else
 
278
                        {
 
279
                                colMde[0] = Tiled;
 
280
                                colMde[1] = Fixed;
 
281
                        }
 
282
                        rowMde[0] = Scaled;
 
283
 
 
284
                        m_columns = 2;
 
285
                }
 
286
 
 
287
                virtual ~ProgressBarPainter() {}
 
288
        protected:
 
289
                virtual int tileName( unsigned int column, unsigned int /*row*/ ) const
 
290
                {
 
291
                        if (m_reverse)
 
292
                        {
 
293
                                return column + 3; //So can use cl, cc, cr
 
294
                        }
 
295
                        else
 
296
                                return column + 4; //So can use cl, cc, cr + we start from cc.
 
297
 
 
298
                }
 
299
 
 
300
                bool m_reverse;
 
301
        };
 
302
 
 
303
 
 
304
        class ActiveTabPainter : public RectTilePainter
 
305
        {
 
306
        public:
 
307
                ActiveTabPainter( bool bottom );
 
308
                virtual ~ActiveTabPainter() {}
 
309
 
 
310
        protected:
 
311
                virtual int tileName( unsigned int column, unsigned int row ) const;
 
312
 
 
313
        private:
 
314
                bool m_bottom;
 
315
        };
 
316
 
 
317
        class InactiveTabPainter : public RectTilePainter
 
318
        {
 
319
        public:
 
320
                InactiveTabPainter( QStyleOptionTab::TabPosition mode, bool bottom );
 
321
                virtual ~InactiveTabPainter() {}
 
322
 
 
323
        protected:
 
324
                virtual int tileName( unsigned int column, unsigned int row ) const;
 
325
 
 
326
        private:
 
327
                QStyleOptionTab::TabPosition m_mode;
 
328
                bool m_bottom;
 
329
        };
 
330
 
 
331
        class ScrollBarPainter : public TilePainter
 
332
        {
 
333
        public:
 
334
                ScrollBarPainter( int type, int count, bool horizontal );
 
335
                virtual ~ScrollBarPainter() {}
 
336
 
 
337
                static int name( bool horizontal );
 
338
 
 
339
        protected:
 
340
                virtual int tileName( unsigned int column, unsigned int row ) const;
 
341
        private:
 
342
                int m_type;
 
343
                int m_count;
 
344
                bool m_horizontal;
 
345
        };
 
346
 
 
347
        class SpinBoxPainter : public TilePainter
 
348
        {
 
349
        public:
 
350
                SpinBoxPainter() : TilePainter( keramik_spinbox ) {
 
351
                        colMde[0] = colMde[2] = Fixed;
 
352
                        colMde[1] = Scaled;
 
353
                        rowMde[0]=rowMde[1]=rowMde[2] = Scaled;
 
354
                        m_columns = 3;
 
355
                }
 
356
                virtual ~SpinBoxPainter() {}
 
357
 
 
358
        protected:
 
359
                virtual int tileName( unsigned int column, unsigned int row ) const;
 
360
        };
 
361
}
 
362
 
 
363
#endif
 
364
 
 
365
// vim: ts=4 sw=4 noet
 
366
// kate: indent-width 4; replace-tabs off; tab-width 4;