summaryrefslogtreecommitdiffstats
path: root/kstyles/keramik/pixmaploader.h
blob: 28b2afd789cb6a0d076d64c5c8ca6d0f1504b025 (plain)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
   Copyright (c) 2002 Malte Starostik <malte@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   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.
*/

// $Id$

#ifndef __pixmaploader_h__
#define __pixmaploader_h__

#include <qintcache.h>
#include <qdict.h>
#include <qimage.h>

class QPixmap;
class QImage;

#include "keramikrc.h"

namespace Keramik
{
	class PixmapLoader
	{
	public:
		PixmapLoader();

		QPixmap pixmap( int name, const QColor& color,  const QColor& bg,
										 bool disabled = false, bool blend = true );

		QPixmap scale( int name, int width, int height, const QColor& color,  const QColor& bg,
										bool disabled = false, bool blend = true );
		QSize size( int id );

		void clear();

		static PixmapLoader& the()
		{
			 if (!s_instance)
			 	s_instance = new PixmapLoader;
			return *s_instance;
		}

		static void release()
		{
			delete s_instance;
			s_instance = 0;
		}

	private:

		struct KeramikCacheEntry
		{
			int m_id;
			int m_width;
			int m_height;
			QRgb m_colorCode;
			QRgb m_bgCode;
			bool    m_disabled;
			bool    m_blended;

			QPixmap* m_pixmap;

			KeramikCacheEntry(int id, const QColor& color, const QColor& bg, bool disabled,
											bool blended, int width, int height, QPixmap* pixmap = 0 ):
				m_id(id), m_width(width), m_height(height), m_colorCode(color.rgb()),m_bgCode(bg.rgb()),
				m_disabled(disabled),  m_blended(blended), m_pixmap(pixmap)
			{}

			int key()
			{
				return (int)m_disabled ^ (m_blended << 1) ^ (m_id<<2) ^ (m_width<<14) ^ (m_height<<24) ^ m_colorCode ^ (m_bgCode<<8);
			}

			bool operator == (const KeramikCacheEntry& other)
			{
				return (m_id        == other.m_id) &&
							(m_width   == other.m_width) &&
							(m_height == other.m_height) &&
							(m_blended == other.m_blended) &&
							(m_bgCode == other.m_bgCode) &&
							(m_colorCode == other.m_colorCode) &&
							(m_disabled == other.m_disabled);
			}

			~KeramikCacheEntry()
			{
				delete m_pixmap;
			}
		};



		QImage* getColored(int id, const QColor& color, const QColor& bg, bool blended);
		QImage* getDisabled(int id, const QColor& color, const QColor& bg, bool blended);
		QIntCache <KeramikCacheEntry>  m_pixmapCache;


		unsigned char clamp[540];

		static PixmapLoader* s_instance;
	};

	class TilePainter
	{
	public:
		TilePainter( int name ) : m_columns(1),m_rows(1),m_name( name ) {};
		virtual ~TilePainter() {};

		enum PaintMode
		{
			PaintNormal,
			PaintMask,
			PaintFullBlend,
			PaintTrivialMask
		};

		void draw( QPainter *p, int x, int y, int width, int height, const QColor& color, const QColor& bg,
		                 bool disabled = false, PaintMode mode = PaintNormal );
		void draw( QPainter *p, const QRect& rect, const QColor& color, const QColor& bg, bool disabled = false, PaintMode mode = PaintNormal )
		{
			draw( p, rect.x(), rect.y(), rect.width(), rect.height(), color, bg, disabled, mode );
		}

	protected:
		enum TileMode { Fixed, Scaled, Tiled };

		unsigned int columns() const { return m_columns; }
		unsigned int rows() const { return m_rows; }

		/**
		The idea behind all this stuff is that for performance reasons, we want to
		use only integers to refer to widgets. So we give each widget a base ID
		that's divisible by 256, and have all the various tiles be referred to as that ID +
		some small number.

		genembed generates and assigns the base widget IDs, and maps various pixmaps suffixes
		into the adjustment numbers; using that info it writes out the tables mapping
		the IDs to pixmaps, and keramikrc.h, which provides nice symbolic constants for base IDs.

		When painting, however, we essentially represent the widget as a table, providing
		fixed/tiled/stretched modes for each column and row. So to draw the widget knowing its
		base ID, we need to know how many rows and columns there, what the scaling modes
		are, and how to get to each of the tiles -- i.e. the tiles' offset from the base ID.

		The various painter subclasses simply fill in most of that info into the members
		here during their construction, and implement the virtual tileName to get the offsets.

		Note that the IDs and offsets are separated since we can reuse the same code in many
		cases by splitting the widget identify from tile identity (as there are many
		different widgets that have the same or similar tile layout)
		*/
		virtual int tileName( unsigned int, unsigned int ) const { return 0; }

		TileMode columnMode( unsigned int col) const
		{
			return colMde[col];
		}

		TileMode rowMode( unsigned int row) const
		{
			return rowMde[row];
		}

	protected:
		TileMode colMde[5], rowMde[5];
		unsigned int m_columns;
		unsigned int m_rows;
	private:

		int absTileName( unsigned int column, unsigned int row ) const
		{
			int name = tileName( column, row );
			return m_name + name;
		}


		QPixmap tile( unsigned int column, unsigned int row, const QColor& color, const QColor& bg, bool disabled, bool blend)
			{ return PixmapLoader::the().pixmap( absTileName( column, row ), color, bg, disabled, blend ); }
		QPixmap scale( unsigned int column, unsigned int row, int width, int height, const QColor& color, const QColor& bg,
							bool disabled, bool blend )
			{ return PixmapLoader::the().scale( absTileName( column, row ), width, height, color,
							bg, disabled, blend ); }

		int m_name;

	};

	class ScaledPainter : public TilePainter
	{
	public:
		enum Direction { Horizontal = 1, Vertical = 2, Both = Horizontal | Vertical };
		ScaledPainter( int name, Direction direction = Both )
			: TilePainter( name ), m_direction( direction )
		{
			colMde[0] =  ( m_direction & Horizontal ) ? Scaled : Tiled;
			rowMde[0] =  ( m_direction & Vertical ) ? Scaled : Tiled;
		}

		virtual ~ScaledPainter() {};

	private:
		Direction m_direction;
	};

	class CenteredPainter : public TilePainter
	{
	public:
		CenteredPainter( int name ) : TilePainter( name ) {
			colMde[0] = colMde[1] = colMde[2] = colMde[3] =  Fixed;
			rowMde[0] = rowMde[1] = rowMde[2] = rowMde[3] = Fixed;
		};
		virtual ~CenteredPainter() {};

	protected:
	};

	class RectTilePainter : public TilePainter
	{
	public:
		RectTilePainter( int name,
		                 bool scaleH = true, bool scaleV = true,
		                 unsigned int columns = 3, unsigned int rows = 3 );

		virtual ~RectTilePainter() {};

	protected:
		virtual int tileName( unsigned int column, unsigned int row ) const;
	private:
		bool m_scaleH;
		bool m_scaleV;
	};

	class RowPainter: public TilePainter
	{
	public:
		RowPainter(int name): TilePainter(name)
		{
			colMde[0] = colMde[2] = Fixed;
			colMde[1] = Tiled;
			rowMde[0] = Scaled;
			m_columns = 3;
		}

		virtual ~RowPainter() {};
	protected:
		virtual int tileName( unsigned int column, unsigned int /*row*/) const
		{
			return column + 3; //So can use cl, cc, cr
		}
	};

	class ProgressBarPainter: public TilePainter
	{
	public:
		ProgressBarPainter(int name, bool reverse): TilePainter(name), m_reverse(reverse)
		{
			//We use only of the tip bitmaps..
			if (reverse)
			{
				colMde[0] = Fixed;
				colMde[1] = Tiled;
			}
			else
			{
				colMde[0] = Tiled;
				colMde[1] = Fixed;
			}
			rowMde[0] = Scaled;

			m_columns = 2;
		}

		virtual ~ProgressBarPainter() {};
	protected:
		virtual int tileName( unsigned int column, unsigned int /*row*/ ) const
		{
			if (m_reverse)
			{
				return column + 3; //So can use cl, cc, cr
			}
			else
				return column + 4; //So can use cl, cc, cr + we start from cc.

		}

		bool m_reverse;
	};


	class ActiveTabPainter : public RectTilePainter
	{
	public:
		ActiveTabPainter( bool bottom );
		virtual ~ActiveTabPainter() {};

	protected:
		virtual int tileName( unsigned int column, unsigned int row ) const;

	private:
		bool m_bottom;
	};

	class InactiveTabPainter : public RectTilePainter
	{
	public:
		enum Mode { First, Middle, Last };
		InactiveTabPainter( Mode mode, bool bottom );
		virtual ~InactiveTabPainter() {};

	protected:
		virtual int tileName( unsigned int column, unsigned int row ) const;

	private:
		Mode m_mode;
		bool m_bottom;
	};

	class ScrollBarPainter : public TilePainter
	{
	public:
		ScrollBarPainter( int type, int count, bool horizontal );
		virtual ~ScrollBarPainter() {};

		static int name( bool horizontal );

	protected:
		virtual int tileName( unsigned int column, unsigned int row ) const;
	private:
		int m_type;
		int m_count;
		bool m_horizontal;
	};

	class SpinBoxPainter : public TilePainter
	{
	public:
		SpinBoxPainter() : TilePainter( keramik_spinbox ) {
			colMde[0] = colMde[2] = Fixed;
			colMde[1] = Scaled;
			rowMde[0]=rowMde[1]=rowMde[2] = Scaled;
			m_columns = 3;
		};
		virtual ~SpinBoxPainter() {};

	protected:
		virtual int tileName( unsigned int column, unsigned int row ) const;
	};
}

#endif

// vim: ts=4 sw=4 noet
// kate: indent-width 4; replace-tabs off; tab-width 4;