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>
32 #include "keramikrc.h"
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 );
50 static PixmapLoader
& the()
53 s_instance
= new PixmapLoader
;
65 struct KeramikCacheEntry
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
)
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
);
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
;
120 TilePainter( int name
) : m_columns(1),m_rows(1),m_name( name
) {}
121 virtual ~TilePainter() {}
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
);
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 +
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
173 TileMode
rowMode( unsigned int row
) const
179 TileMode colMde
[5], rowMde
[5];
180 unsigned int m_columns
;
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
); }
202 class ScaledPainter
: public TilePainter
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() {}
216 Direction m_direction
;
219 class CenteredPainter
: public TilePainter
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() {}
231 class RectTilePainter
: public TilePainter
234 explicit RectTilePainter( int name
,
235 bool scaleH
= true, bool scaleV
= true,
236 unsigned int columns
= 3, unsigned int rows
= 3 );
238 virtual ~RectTilePainter() {}
241 virtual int tileName( unsigned int column
, unsigned int row
) const;
247 class RowPainter
: public TilePainter
250 RowPainter(int name
): TilePainter(name
)
252 colMde
[0] = colMde
[2] = Fixed
;
258 virtual ~RowPainter() {}
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
269 ProgressBarPainter(int name
, bool reverse
): TilePainter(name
), m_reverse(reverse
)
271 //We use only of the tip bitmaps..
287 virtual ~ProgressBarPainter() {}
289 virtual int tileName( unsigned int column
, unsigned int /*row*/ ) const
293 return column
+ 3; //So can use cl, cc, cr
296 return column
+ 4; //So can use cl, cc, cr + we start from cc.
304 class ActiveTabPainter
: public RectTilePainter
307 ActiveTabPainter( bool bottom
);
308 virtual ~ActiveTabPainter() {}
311 virtual int tileName( unsigned int column
, unsigned int row
) const;
317 class InactiveTabPainter
: public RectTilePainter
320 InactiveTabPainter( QStyleOptionTab::TabPosition mode
, bool bottom
);
321 virtual ~InactiveTabPainter() {}
324 virtual int tileName( unsigned int column
, unsigned int row
) const;
327 QStyleOptionTab::TabPosition m_mode
;
331 class ScrollBarPainter
: public TilePainter
334 ScrollBarPainter( int type
, int count
, bool horizontal
);
335 virtual ~ScrollBarPainter() {}
337 static int name( bool horizontal
);
340 virtual int tileName( unsigned int column
, unsigned int row
) const;
347 class SpinBoxPainter
: public TilePainter
350 SpinBoxPainter() : TilePainter( keramik_spinbox
) {
351 colMde
[0] = colMde
[2] = Fixed
;
353 rowMde
[0]=rowMde
[1]=rowMde
[2] = Scaled
;
356 virtual ~SpinBoxPainter() {}
359 virtual int tileName( unsigned int column
, unsigned int row
) const;
365 // vim: ts=4 sw=4 noet
366 // kate: indent-width 4; replace-tabs off; tab-width 4;