delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kstyles / keramik / pixmaploader.h
blob1324ad6b4ba4109aa3c1d035d404cc673cf7f4b2
1 /*
2 Copyright (c) 2003-2005 Maksim Orlovich <maksim@kde.org>
3 Copyright (c) 2002 Malte Starostik <malte@kde.org>
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.
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.
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.
22 #ifndef __pixmaploader_h__
23 #define __pixmaploader_h__
25 #include <QtCore/QCache>
26 #include <QtGui/QImage>
27 #include <QtGui/QStyleOption>
29 class QPixmap;
30 class QImage;
32 #include "keramikrc.h"
34 namespace Keramik
36 class PixmapLoader
38 public:
39 PixmapLoader();
41 QPixmap pixmap( int name, const QColor& color, const QColor& bg,
42 bool disabled = false, bool blend = true );
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 );
48 void clear();
50 static PixmapLoader& the()
52 if (!s_instance)
53 s_instance = new PixmapLoader;
54 return *s_instance;
57 static void release()
59 delete s_instance;
60 s_instance = 0;
63 private:
65 struct KeramikCacheEntry
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;
75 QPixmap* m_pixmap;
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)
83 int key()
85 return (m_disabled ? 1 : 0) ^ (m_blended << 1) ^ (m_id<<2) ^ (m_width<<14) ^ (m_height<<24) ^ m_colorCode ^ (m_bgCode<<8);
88 bool operator == (const KeramikCacheEntry& other)
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);
99 ~KeramikCacheEntry()
101 delete m_pixmap;
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;
112 unsigned char clamp[540];
114 static PixmapLoader* s_instance;
117 class TilePainter
119 public:
120 TilePainter( int name ) : m_columns(1),m_rows(1),m_name( name ) {}
121 virtual ~TilePainter() {}
123 enum PaintMode
125 PaintNormal,
126 PaintMask,
127 PaintFullBlend,
128 PaintTrivialMask
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 )
135 draw( p, rect.x(), rect.y(), rect.width(), rect.height(), color, bg, disabled, mode );
138 protected:
139 enum TileMode { Fixed, Scaled, Tiled };
141 unsigned int columns() const { return m_columns; }
142 unsigned int rows() const { return m_rows; }
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.
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.
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.
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.
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)
166 virtual int tileName( unsigned int, unsigned int ) const { return 0; }
168 TileMode columnMode( unsigned int col) const
170 return colMde[col];
173 TileMode rowMode( unsigned int row) const
175 return rowMde[row];
178 protected:
179 TileMode colMde[5], rowMde[5];
180 unsigned int m_columns;
181 unsigned int m_rows;
182 private:
184 int absTileName( unsigned int column, unsigned int row ) const
186 int name = tileName( column, row );
187 return m_name + name;
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 ); }
198 int m_name;
202 class ScaledPainter : public TilePainter
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 )
209 colMde[0] = ( m_direction & Horizontal ) ? Scaled : Tiled;
210 rowMde[0] = ( m_direction & Vertical ) ? Scaled : Tiled;
213 virtual ~ScaledPainter() {}
215 private:
216 Direction m_direction;
219 class CenteredPainter : public TilePainter
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;
226 virtual ~CenteredPainter() {}
228 protected:
231 class RectTilePainter : public TilePainter
233 public:
234 explicit RectTilePainter( int name,
235 bool scaleH = true, bool scaleV = true,
236 unsigned int columns = 3, unsigned int rows = 3 );
238 virtual ~RectTilePainter() {}
240 protected:
241 virtual int tileName( unsigned int column, unsigned int row ) const;
242 private:
243 bool m_scaleH;
244 bool m_scaleV;
247 class RowPainter: public TilePainter
249 public:
250 RowPainter(int name): TilePainter(name)
252 colMde[0] = colMde[2] = Fixed;
253 colMde[1] = Tiled;
254 rowMde[0] = Scaled;
255 m_columns = 3;
258 virtual ~RowPainter() {}
259 protected:
260 virtual int tileName( unsigned int column, unsigned int /*row*/) const
262 return column + 3; //So can use cl, cc, cr
266 class ProgressBarPainter: public TilePainter
268 public:
269 ProgressBarPainter(int name, bool reverse): TilePainter(name), m_reverse(reverse)
271 //We use only of the tip bitmaps..
272 if (reverse)
274 colMde[0] = Fixed;
275 colMde[1] = Tiled;
277 else
279 colMde[0] = Tiled;
280 colMde[1] = Fixed;
282 rowMde[0] = Scaled;
284 m_columns = 2;
287 virtual ~ProgressBarPainter() {}
288 protected:
289 virtual int tileName( unsigned int column, unsigned int /*row*/ ) const
291 if (m_reverse)
293 return column + 3; //So can use cl, cc, cr
295 else
296 return column + 4; //So can use cl, cc, cr + we start from cc.
300 bool m_reverse;
304 class ActiveTabPainter : public RectTilePainter
306 public:
307 ActiveTabPainter( bool bottom );
308 virtual ~ActiveTabPainter() {}
310 protected:
311 virtual int tileName( unsigned int column, unsigned int row ) const;
313 private:
314 bool m_bottom;
317 class InactiveTabPainter : public RectTilePainter
319 public:
320 InactiveTabPainter( QStyleOptionTab::TabPosition mode, bool bottom );
321 virtual ~InactiveTabPainter() {}
323 protected:
324 virtual int tileName( unsigned int column, unsigned int row ) const;
326 private:
327 QStyleOptionTab::TabPosition m_mode;
328 bool m_bottom;
331 class ScrollBarPainter : public TilePainter
333 public:
334 ScrollBarPainter( int type, int count, bool horizontal );
335 virtual ~ScrollBarPainter() {}
337 static int name( bool horizontal );
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;
347 class SpinBoxPainter : public TilePainter
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;
356 virtual ~SpinBoxPainter() {}
358 protected:
359 virtual int tileName( unsigned int column, unsigned int row ) const;
363 #endif
365 // vim: ts=4 sw=4 noet
366 // kate: indent-width 4; replace-tabs off; tab-width 4;