summaryrefslogtreecommitdiffstats
path: root/noatun/modules/kjofol-skin/kjsliders.cpp
blob: 8cadd04f2591dfe40a7fc259a1f4c3138082e56a (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
/***************************************************************************
	kjsliders.cpp
	---------------------------------------------
	Sliders for Volume and Pitch
	---------------------------------------------
	Maintainer: Stefan Gehn <sgehn@gmx.net>

 ***************************************************************************/

// local includes
#include "kjsliders.h"
#include "kjtextdisplay.h"
#include "kjprefs.h"

#include "helpers.cpp"

// kde includes
#include <klocale.h>
#include <kdebug.h>

// arts-includes, needed for pitch
#include <artsmodules.h>
#include <reference.h>
#include <soundserver.h>
#include <kmedia2.h>

// noatun includes
#include <noatun/player.h>
#include <noatun/engine.h>

/*******************************************
 * KJVolumeBar
 *******************************************/

KJVolumeBar::KJVolumeBar(const QStringList &i, KJLoader *p)
	: KJWidget(p), mVolume(0), mText(0)
{
	int x, y, xs, ys;
	x=i[1].toInt();
	y=i[2].toInt();
	xs=i[3].toInt()-x;
	ys=i[4].toInt()-y;
	setRect ( x, y, xs, ys );

//	kdDebug(66666) << "x: " << x << " y: " << y << " w: " << xs << " h: " << ys << endl;

	mBack   = parent()->pixmap(parser()["backgroundimage"][1]);
	mSlider = parent()->pixmap(parser()["volumecontrolimage"][1]);
}

QString KJVolumeBar::tip()
{
	return i18n("Volume");
}

void KJVolumeBar::paint(QPainter *p, const QRect &)
{
//	kdDebug(66666) << "x: " << rect().x() << " y: " << rect().y() << endl;
//	kdDebug(66666) << "vol x: " << rect().x()+(mVolume*rect().width())/100 << endl;

	// center of that slider-pixmap
//	QPoint hotSpot = QPoint( mSlider.width()/2, mSlider.height()/2 );

	// draw our background
	bitBlt(
		p->device(),
			rect().x() /*- hotSpot.x()*/,
			rect().y() /*- hotSpot.y()*/,
		&mBack,
			rect().x() /*- hotSpot.x()*/,
			rect().y() /*- hotSpot.y()*/,
			rect().width()  /*+ (2*hotSpot.x())*/,
			rect().height() /*+ (2*hotSpot.y())*/,
		Qt::CopyROP);

	// draw our slider
	bitBlt(
		p->device(),
			rect().x() + ((mVolume*rect().width())/100) /*- hotSpot.x()*/,
			rect().y() /*- hotSpot.y()*/,
		&mSlider,
			0,
			0,
			mSlider.width(),
			mSlider.height(),
		Qt::CopyROP);

	if (mText)
		mText->repaint();
}

bool KJVolumeBar::mousePress(const QPoint &pos)
{
	mVolume = (pos.x()*100) / rect().width();
//	kdDebug(66666) << "volume: " << mVolume << endl;
	repaint();
	napp->player()->setVolume(mVolume);
	return true;
}

void KJVolumeBar::mouseRelease(const QPoint &, bool)
{
}

void KJVolumeBar::mouseMove(const QPoint &pos, bool in)
{
	if (!in)
		return;
	mousePress(pos);
}

void KJVolumeBar::timeUpdate(int)
{
	mVolume = napp->player()->volume();
	repaint();
}


/*******************************************
 * KJVolumeBMP
 *******************************************/

KJVolumeBMP::KJVolumeBMP(const QStringList &i, KJLoader *p)
	: KJWidget(p), mVolume(0), mOldVolume(0), mText(0)
{
	int x, y, xs, ys;
	x=i[1].toInt();
	y=i[2].toInt();
	xs=i[3].toInt()-x;
	ys=i[4].toInt()-y;
	setRect ( x, y, xs, ys );

	mWidth = parser()["volumecontrolimagexsize"][1].toInt();
	mCount = parser()["volumecontrolimagenb"][1].toInt()-1;

	mImages = parent()->pixmap(parser()["volumecontrolimage"][1]);
	mPos = parent()->image(parser()["volumecontrolimageposition"][1]);
	timeUpdate(0);
}

QString KJVolumeBMP::tip()
{
	return i18n("Volume");
}

void KJVolumeBMP::paint(QPainter *p, const QRect &)
{
	QRect from(mVolume*mCount/100*mWidth, 0, mWidth, mImages.height());
	bitBlt(p->device(), rect().topLeft(), &mImages, from, Qt::CopyROP);
	if (mText)
		mText->repaint();
}

bool KJVolumeBMP::mousePress(const QPoint &pos)
{
	QRgb color = mPos.pixel ( rect().topLeft().x()+pos.x(), rect().topLeft().y()+pos.y() );

	if (!isGray(color))
		return false;

	mVolume = grayRgb(color)*100/255;
//	kdDebug(66666) << "gray  : " << grayRgb(color) << endl;
//	kdDebug(66666) << "volume: " << mVolume << endl;

	repaint();

	napp->player()->setVolume(mVolume);

	return true;
}

void KJVolumeBMP::mouseRelease(const QPoint &, bool)
{}

void KJVolumeBMP::mouseMove(const QPoint &pos, bool in)
{
	if (!in) return;
	mousePress(pos);
}

void KJVolumeBMP::timeUpdate(int)
{
	mVolume = napp->player()->volume();

	if ( mVolume == mOldVolume ) // dont redraw if nothing changed
		return;

	mOldVolume = mVolume;

	repaint();
}


/*******************************************
 * KJPitchBMP
 *******************************************/

KJPitchBMP::KJPitchBMP(const QStringList &i, KJLoader *p)
	: KJWidget(p), mText(0)
{
	int x  = i[1].toInt();
	int y  = i[2].toInt();
	int xs = i[3].toInt() - x;
	int ys = i[4].toInt() - y;
	
	setRect ( x, y, xs, ys );

	mWidth = parser()["pitchcontrolimagexsize"][1].toInt();
	mCount = parser()["pitchcontrolimagenb"][1].toInt()-1;

	mImages = parent()->pixmap(parser()["pitchcontrolimage"][1]);
	mPos = parent()->image(parser()["pitchcontrolimageposition"][1]);

	// makes all pixels with rgb(255,0,255) transparent
	QImage ibackground;
	ibackground = parent()->image(parser()["pitchcontrolimage"][1]);
	mImages.setMask( getMask(ibackground) );

	Arts::PlayObject playobject = napp->player()->engine()->playObject();
	Arts::PitchablePlayObject pitchable = Arts::DynamicCast(playobject);

	if ( pitchable.isNull() )
		mCurrentPitch = 1.0;
	else
		mCurrentPitch = pitchable.speed();

//	kdDebug() << "[KJPitchBMP] starting with pitch: " << mCurrentPitch << endl;
/*
	mMinPitch = 0.5;
	mMaxPitch = 2.0;

	mMinPitch = 0.8;
	mMaxPitch = 1.2;
*/

	readConfig();

	if (mText)
		mText->repaint();
}

QString KJPitchBMP::tip()
{
	return i18n("Pitch");
}

void KJPitchBMP::paint(QPainter *p, const QRect &)
{
	float xPos = (int)((mCurrentPitch-mMinPitch)*100.0) * mCount / (int)((mMaxPitch-mMinPitch)*100.0) * mWidth;

	QRect from( (int)xPos, 0, mWidth, mImages.height());

	bitBlt(p->device(), rect().topLeft(), &mImages, from, Qt::CopyROP);

	if (mText)
		mText->repaint();
}

bool KJPitchBMP::mousePress(const QPoint &pos)
{
	QRgb color = mPos.pixel ( rect().topLeft().x()+pos.x(), rect().topLeft().y()+pos.y() );

	if (!isGray(color))
		return false;

	mCurrentPitch = mMinPitch + ( (grayRgb(color)*(mMaxPitch-mMinPitch)) / 255 );
//	kdDebug(66666) << "[KJPitchBMP] mousePress()  mCurrentPitch: " << mCurrentPitch << endl;

	repaint();

	newFile(); // wrong naming, in fact it just sets pitch

	return true;
}

void KJPitchBMP::mouseRelease(const QPoint &, bool)
{}

void KJPitchBMP::mouseMove(const QPoint &pos, bool in)
{
	if (!in) return;
	mousePress(pos);
}

void KJPitchBMP::timeUpdate(int)
{
//	kdDebug(66666) << "[KJPitchBMP] :timeUpdate(int)" << endl;

	Arts::PlayObject playobject = napp->player()->engine()->playObject();
	Arts::PitchablePlayObject pitchable = Arts::DynamicCast(playobject);

	if ( !pitchable.isNull() )
	{
		mCurrentPitch = pitchable.speed();
//		kdDebug(66666) << "[KJPitchBMP] mCurrentPitch: " << mCurrentPitch << endl;
	}

	if ( mCurrentPitch == mOldPitch ) // dont redraw if nothing changed
		return;

	mOldPitch = mCurrentPitch;

	repaint();
}

void KJPitchBMP::newFile()
{
//	kdDebug(66666) << "[KJPitchBMP] newFile()" << endl;

	Arts::PlayObject playobject = napp->player()->engine()->playObject();
	Arts::PitchablePlayObject pitchable = Arts::DynamicCast(playobject);

	if (!pitchable.isNull())
	{
//		kdDebug(66666) << "[KJPitchBMP] new speed: " << mCurrentPitch << endl;
		pitchable.speed( mCurrentPitch );
	}
}

void KJPitchBMP::readConfig()
{
//	kdDebug(66666) << "KJPitchBMP::readConfig()" << endl;

	mMinPitch = KJLoader::kjofol->prefs()->minimumPitch() / 100.0;
	mMaxPitch = KJLoader::kjofol->prefs()->maximumPitch() / 100.0;

	// Now comes the range checking if the user changed the setting :)
	if ( mCurrentPitch < mMinPitch || mCurrentPitch > mMaxPitch )
	{
		if ( mCurrentPitch < mMinPitch  )
			mCurrentPitch = mMinPitch;
		if ( mCurrentPitch > mMaxPitch )
			mCurrentPitch = mMaxPitch;
		newFile(); // wrong naming, in fact it just sets pitch
	}
}