summaryrefslogtreecommitdiffstats
path: root/libkdegames/kgameprogress.cpp
blob: 98876aa341c660bd9c79eeaf5909ba87e48fcf4f (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
/* This file is part of the KDE libraries
   Copyright (C) 1996 Martynas Kunigelis

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   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.
*/
/**
 * KGameProgress -- a progress indicator widget for KDE.
 */

#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqstring.h>
#include <tqregexp.h>
#include <tqstyle.h>

#include "kgameprogress.h"

#include <kapplication.h>

KGameProgress::KGameProgress(TQWidget *parent, const char *name)
	: TQFrame(parent, name),
	TQRangeControl(0, 100, 1, 10, 0),
	orient(Qt::Horizontal)
{
	initialize();
}

KGameProgress::KGameProgress(Qt::Orientation orientation, TQWidget *parent, const char *name)
	: TQFrame(parent, name),
	TQRangeControl(0, 100, 1, 10, 0),
	orient(orientation)
{
	initialize();
}

KGameProgress::KGameProgress(int minValue, int maxValue, int value,
                     Qt::Orientation orientation, TQWidget *parent, const char *name)
	: TQFrame(parent, name),
	TQRangeControl(minValue, maxValue, 1, 10, value),
	orient(orientation)
{
	initialize();
}

KGameProgress::~KGameProgress()
{
	delete bar_pixmap;
}

void KGameProgress::advance(int offset)
{
	setValue(value() + offset);
}

void KGameProgress::initialize()
{
	format_ = "%p%";
	use_supplied_bar_color = false;
	bar_pixmap = 0;
	bar_style = Solid;
	text_enabled = TRUE;
	setBackgroundMode( PaletteBackground );
	connect(kapp, TQT_SIGNAL(appearanceChanged()), this, TQT_SLOT(paletteChange()));
	paletteChange();
}

void KGameProgress::paletteChange()
{
	TQPalette p = kapp->palette();
	const TQColorGroup &tqcolorGroup = p.active();
	if (!use_supplied_bar_color)
		bar_color = tqcolorGroup.highlight();
	bar_text_color = tqcolorGroup.highlightedText();
	text_color = tqcolorGroup.text();
	setPalette(p);

	adjustStyle();
}


void KGameProgress::setBarPixmap(const TQPixmap &pixmap)
{
	if (pixmap.isNull())
		return;
	if (bar_pixmap)
		delete bar_pixmap;

	bar_pixmap = new TQPixmap(pixmap);
}

void KGameProgress::setBarColor(const TQColor &color)
{
	bar_color = color;
	use_supplied_bar_color = true;
	if (bar_pixmap) {
		delete bar_pixmap;
		bar_pixmap = 0;
	}
}

void KGameProgress::setBarStyle(BarStyle style)
{
	if (bar_style != style) {
		bar_style = style;
		update();
	}
}

void KGameProgress::setOrientation(Qt::Orientation orientation)
{
	if (orient != orientation) {
		orient = orientation;
		update();
	}
}

void KGameProgress::setValue(int value)
{
	TQRangeControl::setValue(value);
}

void KGameProgress::setTextEnabled(bool enable)
{
	text_enabled = enable;
}

const TQColor & KGameProgress::barColor() const
{
	return bar_color;
}

const TQPixmap * KGameProgress::barPixmap() const
{
	return bar_pixmap;
}

bool KGameProgress::textEnabled() const
{
	return text_enabled;
}

TQSize KGameProgress::tqsizeHint() const
{
	TQSize s( size() );

	if(orientation() == Qt::Vertical) {
		s.setWidth(24);
	} else {
		s.setHeight(24);
	}

	return s;
}

TQSize KGameProgress::tqminimumSizeHint() const
{
	return tqsizeHint();
}

TQSizePolicy KGameProgress::sizePolicy() const
{
	if ( orientation()==Qt::Vertical )
		return TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Expanding );
	else
		return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed );
}

KGameProgress::Orientation KGameProgress::orientation() const
{
	return orient;
}

KGameProgress::BarStyle KGameProgress::barStyle() const
{
	return bar_style;
}

int KGameProgress::recalcValue(int range)
{
	int abs_value = value() - minValue();
	int abs_range = maxValue() - minValue();
	return abs_range ? range * abs_value / abs_range : 0;
}

void KGameProgress::valueChange()
{
	tqrepaint(contentsRect(), FALSE);
	emit percentageChanged(recalcValue(100));
}

void KGameProgress::rangeChange()
{
	tqrepaint(contentsRect(), FALSE);
	emit percentageChanged(recalcValue(100));
}

void KGameProgress::styleChange(TQStyle&)
{
	adjustStyle();
}

void KGameProgress::adjustStyle()
{
	switch (tqstyle().tqstyleHint(TQStyle::SH_GUIStyle)) {
		case WindowsStyle:
			setFrameStyle(TQFrame::WinPanel | TQFrame::Sunken);
			break;
		case MotifStyle:
		default:
			setFrameStyle(TQFrame::Panel | TQFrame::Sunken);
			setLineWidth( 2 );
			break;
	}
	update();
}

void KGameProgress::paletteChange( const TQPalette &p )
{
	// This never gets called for global color changes 
	// because we call setPalette() ourselves.
	TQFrame::paletteChange(p);
}

void KGameProgress::drawText(TQPainter *p)
{
	TQRect r(contentsRect());
	//TQColor c(bar_color.rgb() ^ backgroundColor().rgb());

	// Rik: Replace the tags '%p', '%v' and '%m' with the current percentage,
	// the current value and the maximum value respectively.
	TQString s(format_);

	s.replace(TQRegExp(TQString::tqfromLatin1("%p")), TQString::number(recalcValue(100)));
	s.replace(TQRegExp(TQString::tqfromLatin1("%v")), TQString::number(value()));
	s.replace(TQRegExp(TQString::tqfromLatin1("%m")), TQString::number(maxValue()));

	p->setPen(text_color);
	TQFont font = p->font();
	font.setBold(true);
	p->setFont(font);
	//p->setRasterOp(XorROP);
	p->drawText(r, AlignCenter, s);
	p->setClipRegion( fr );
	p->setPen(bar_text_color);
	p->drawText(r, AlignCenter, s);
}

void KGameProgress::drawContents(TQPainter *p)
{
	TQRect cr = contentsRect(), er = cr;
	fr = cr;
	TQBrush fb(bar_color), eb(backgroundColor());

	if (bar_pixmap)
		fb.setPixmap(*bar_pixmap);

	if (backgroundPixmap())
		eb.setPixmap(*backgroundPixmap());

	switch (bar_style) {
		case Solid:
			if (orient ==Qt::Horizontal) {
				fr.setWidth(recalcValue(cr.width()));
				er.setLeft(fr.right() + 1);
			} else {
				fr.setTop(cr.bottom() - recalcValue(cr.height()));
				er.setBottom(fr.top() - 1);
			}

			p->setBrushOrigin(cr.topLeft());
			p->fillRect(fr, fb);

			p->fillRect(er, eb);

			break;

		case Blocked:
			const int margin = 2;
			int max, num, dx, dy;
			if (orient ==Qt::Horizontal) {
				fr.setHeight(cr.height() - 2 * margin);
				fr.setWidth((int)(0.67 * fr.height()));
				fr.moveTopLeft(TQPoint(cr.left() + margin, cr.top() + margin));
				dx = fr.width() + margin;
				dy = 0;
				max = (cr.width() - margin) / (fr.width() + margin) + 1;
				num = recalcValue(max);
			} else {
				fr.setWidth(cr.width() - 2 * margin);
				fr.setHeight((int)(0.67 * fr.width()));
				fr.moveBottomLeft(TQPoint(cr.left() + margin, cr.bottom() - margin));
				dx = 0;
				dy = - (fr.height() + margin);
				max = (cr.height() - margin) / (fr.height() + margin) + 1;
				num = recalcValue(max);
			}
			p->setClipRect(cr.x() + margin, cr.y() + margin,
			               cr.width() - margin, cr.height() - margin);
			for (int i = 0; i < num; i++) {
				p->setBrushOrigin(fr.topLeft());
				p->fillRect(fr, fb);
				fr.moveBy(dx, dy);
			}
			
			if (num != max) {
				if (orient ==Qt::Horizontal)
					er.setLeft(fr.right() + 1);
				else
					er.setBottom(fr.bottom() + 1);
				if (!er.isNull()) {
					p->setBrushOrigin(cr.topLeft());
					p->fillRect(er, eb);
				}
			}

			break;
	}

	if (text_enabled && bar_style != Blocked)
		drawText(p);
}

void KGameProgress::setFormat(const TQString & format)
{
	format_ = format;
}

TQString KGameProgress::format() const
{
	return format_;
}

#include "kgameprogress.moc"