summaryrefslogtreecommitdiffstats
path: root/kdm/kfrontend/themer/kdmpixmap.cpp
blob: 9b90799d58948465066d5776b98696e9ee8ca10e (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
/*
 *  Copyright (C) 2003 by Unai Garro <ugarro@users.sourceforge.net>
 *  Copyright (C) 2004 by Enrico Ros <rosenric@dei.unipd.it>
 *  Copyright (C) 2004 by Stephan Kulow <coolo@kde.org>
 *  Copyright (C) 2004 by Oswald Buddenhagen <ossi@kde.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <config.h>

#include "kdmpixmap.h"
#include <kdmconfig.h>

#include <kimageeffect.h>
#ifdef HAVE_LIBART
#include <ksvgiconengine.h>
#endif

#include <kdebug.h>
#include <kstandarddirs.h>

#include <tqfile.h>
#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqimage.h>

KdmPixmap::KdmPixmap( KdmItem *parent, const TQDomNode &node, const char *name )
    : KdmItem( parent, node, name )
{
	itemType = "pixmap";

	// Set default values for pixmap (note: strings are already Null)
	pixmap.normal.tint.setRgb( 0x800000 );
	pixmap.normal.alpha = 0.0;
	pixmap.active.present = false;
	pixmap.prelight.present = false;

	// Read PIXMAP ID
	// it rarely happens that a pixmap can be a button too!
	TQDomNode n = node;
	TQDomElement elPix = n.toElement();

	// Read PIXMAP TAGS
	TQDomNodeList childList = node.childNodes();
	for (uint nod = 0; nod < childList.count(); nod++) {
		TQDomNode child = childList.item( nod );
		TQDomElement el = child.toElement();
		TQString tagName = el.tagName();

		if (tagName == "normal") {
			pixmap.normal.fullpath = fullPath( el.attribute( "file", "" ) );
			parseColor( el.attribute( "tint", "#ffffff" ), pixmap.normal.tint );
			pixmap.normal.alpha = el.attribute( "alpha", "1.0" ).toFloat();

			if (el.attribute( "file", "" ) == "@@@KDMBACKGROUND@@@") {
				// Use the preset KDM background...
				KStandardDirs *m_pDirs = KGlobal::dirs();
				KSimpleConfig *config = new KSimpleConfig( TQFile::decodeName( _backgroundCfg ) );
				config->setGroup("Desktop0");
				pixmap.normal.fullpath = m_pDirs->findResource("wallpaper", config->readPathEntry("Wallpaper"));
				// TODO: Detect when there is no wallpaper and use the background settings instead
				delete config;
			}

		} else if (tagName == "active") {
			pixmap.active.present = true;
			pixmap.active.fullpath = fullPath( el.attribute( "file", "" ) );
			parseColor( el.attribute( "tint", "#ffffff" ), pixmap.active.tint );
			pixmap.active.alpha = el.attribute( "alpha", "1.0" ).toFloat();
		} else if (tagName == "prelight") {
			pixmap.prelight.present = true;
			pixmap.prelight.fullpath = fullPath(el.attribute( "file", "" ) );
			parseColor( el.attribute( "tint", "#ffffff" ), pixmap.prelight.tint );
			pixmap.prelight.alpha = el.attribute( "alpha", "1.0" ).toFloat();
		}
	}

	// look if we have to have the aspect ratio ready
	if (((pos.wType == DTnone && pos.hType != DTnone) ||
	     (pos.wType != DTnone && pos.hType == DTnone) ||
	     (pos.wType == DTnone && pos.hType == DTnone)) &&
	    !pixmap.normal.fullpath.endsWith( ".svg" ))
	  loadPixmap( &pixmap.normal );
}

TQSize
KdmPixmap::tqsizeHint()
{
	// choose the correct pixmap class
	PixmapStruct::PixmapClass * pClass = &pixmap.normal;
	if (state == Sactive && pixmap.active.present)
		pClass = &pixmap.active;
	if (state == Sprelight && pixmap.prelight.present)
		pClass = &pixmap.prelight;
	// use the pixmap size as the size hint
	if (!pClass->pixmap.isNull())
		return pClass->pixmap.size();
	return KdmItem::tqsizeHint();
}

void
KdmPixmap::setGeometry( const TQRect &newGeometry, bool force )
{
	KdmItem::setGeometry( newGeometry, force );
	pixmap.active.readyPixmap.resize( 0, 0 );
	pixmap.prelight.readyPixmap.resize( 0, 0 );
	pixmap.normal.readyPixmap.resize( 0, 0 );
}


TQString
KdmPixmap::fullPath( const TQString &fileName)
{
        if (fileName.isEmpty())
		return TQString::null;

	TQString fullName = fileName;
	if (fullName.tqat( 0 ) != '/')
		fullName = baseDir() + "/" + fileName;
	return fullName;
}

void
KdmPixmap::renderSvg( PixmapStruct::PixmapClass *pClass, const TQRect &area )
{
#ifdef HAVE_LIBART
	// Special stuff for SVG icons
	KSVGIconEngine *svgEngine = new KSVGIconEngine();

	if (svgEngine->load( area.width(), area.height(), pClass->fullpath )) {
		TQImage *t = svgEngine->image();
		pClass->pixmap = *t;
		pClass->readyPixmap.resize( 0, 0 );
		delete t;
	} else {
		kdWarning() << "failed to load " << pClass->fullpath << endl;
		pClass->fullpath = TQString::null;
	}

	delete svgEngine;
#else
        Q_UNUSED(pClass);
        Q_UNUSED(area);
#endif
}

void
KdmPixmap::loadPixmap( PixmapStruct::PixmapClass *pClass )
{
  TQString fullpath = pClass->fullpath;

  kdDebug() << timestamp() << " load " << fullpath << endl;
  int index = fullpath.tqfindRev('.');
  TQString ext = fullpath.right(fullpath.length() - index);
  fullpath = fullpath.left(index);
  kdDebug() << timestamp() << " ext " << ext << " " << fullpath << endl;
  TQString testpath = TQString("-%1x%2").tqarg(area.width()).arg(area.height()) + ext;
  kdDebug() << timestamp() << " testing for " << fullpath + testpath << endl;
  if (KStandardDirs::exists(fullpath + testpath)) 
    pClass->pixmap.load(fullpath + testpath);
  else
    pClass->pixmap.load( fullpath + ext );
  kdDebug() << timestamp() << " done\n";
}

void
KdmPixmap::drawContents( TQPainter *p, const TQRect &r )
{
	// choose the correct pixmap class
	PixmapStruct::PixmapClass *pClass = &pixmap.normal;
	if (state == Sactive && pixmap.active.present)
		pClass = &pixmap.active;
	if (state == Sprelight && pixmap.prelight.present)
		pClass = &pixmap.prelight;

	kdDebug() << "draw " << id << " " << pClass->pixmap.isNull() << endl;
 
	if (pClass->pixmap.isNull()) {
	        
	        if (pClass->fullpath.isEmpty())	// if neither is set, we're empty
			return;
		
		if (!pClass->fullpath.endsWith( ".svg" ) ) {
		  loadPixmap(pClass);
		} else {
		  kdDebug() << timestamp() << " renderSVG\n";
		  renderSvg( pClass, area );
		  kdDebug() << timestamp() << " done\n";
		}
	}

	int px = area.left() + r.left();
	int py = area.top() + r.top();
	int sx = r.x();
	int sy = r.y();
	int sw = r.width();
	int sh = r.height();
	if (px < 0) {
		px *= -1;
		sx += px;
		px = 0;
	}
	if (py < 0) {
		py *= -1;
		sy += py;
		py = 0;
	}


	if (pClass->readyPixmap.isNull()) {
	  
		bool haveTint = pClass->tint.rgb() != 0xFFFFFF;
		bool haveAlpha = pClass->alpha < 1.0;

		TQImage scaledImage;
		
		// use the loaded pixmap or a scaled version if needed

		kdDebug() << timestamp() << " prepare readyPixmap " << pClass->fullpath << " " << area.size() << " " << pClass->pixmap.size() << endl;
		if (area.size() != pClass->pixmap.size()) {
			if (pClass->fullpath.endsWith( ".svg" )) {
				kdDebug() << timestamp() << " renderSVG\n";
				renderSvg( pClass, area );
				scaledImage = pClass->pixmap.convertToImage();
			} else {
				kdDebug() << timestamp() << " convertFromImage smoothscale\n";
				TQImage tempImage = pClass->pixmap.convertToImage();
				kdDebug() << timestamp() << " convertToImage done\n";
				scaledImage = tempImage.smoothScale( area.width(), area.height() );
				kdDebug() << timestamp() << " done\n";
			}
		} else {
		  if (haveTint || haveAlpha)
                  {
			scaledImage = pClass->pixmap.convertToImage();
                        // enforce rgba values for the later
                        scaledImage = scaledImage.convertDepth( 32 );
                  }
		  else
		    pClass->readyPixmap = pClass->pixmap;
		}

		if (haveTint || haveAlpha) {
			// blend image(pix) with the given tint

			scaledImage = scaledImage.convertDepth( 32 );
			int w = scaledImage.width();
			int h = scaledImage.height();
			float tint_red = float( pClass->tint.red() ) / 255;
			float tint_green = float( pClass->tint.green() ) / 255;
			float tint_blue = float( pClass->tint.blue() ) / 255;
			float tint_alpha = pClass->alpha;

			for (int y = 0; y < h; ++y) {
				QRgb *ls = (QRgb *)scaledImage.scanLine( y );
				for (int x = 0; x < w; ++x) {
					QRgb l = ls[x];
					int r = int( tqRed( l ) * tint_red );
					int g = int( tqGreen( l ) * tint_green );
					int b = int( tqBlue( l ) * tint_blue );
					int a = int( tqAlpha( l ) * tint_alpha );
					ls[x] = tqRgba( r, g, b, a );
				}
			}

		}

		if (!scaledImage.isNull()) {
		  kdDebug() << timestamp() << " convertFromImage " << id << " " << area << endl;
		  pClass->readyPixmap.convertFromImage( scaledImage );
		}
	}
	kdDebug() << timestamp() << " Pixmap::drawContents " << pClass->readyPixmap.size() << " " << px << " " << py << " " << sx << " " << sy << " " << sw << " " << sh << endl;
	p->drawPixmap( px, py, pClass->readyPixmap, sx, sy, sw, sh );
}

void
KdmPixmap::statusChanged()
{
	KdmItem::statusChanged();
	if (!pixmap.active.present && !pixmap.prelight.present)
		return;
	if ((state == Sprelight && !pixmap.prelight.present) ||
	    (state == Sactive && !pixmap.active.present))
		return;
	needUpdate();
}

#include "kdmpixmap.moc"