/***************************************************************************
	kjequalizer.cpp  -  links noatun VEqualizer and KJofol
	--------------------------------------
	Maintainer: Stefan Gehn <sgehn@gmx.net>

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

#include "kjequalizer.h"
#include "kjequalizer.moc"

#include <tqpainter.h>
#include <tqtimer.h>

#include <kdebug.h>
#include <kpixmap.h>

#include <noatun/vequalizer.h>

KJEqualizer::KJEqualizer(const TQStringList &l, KJLoader *p)
	: TQObject(0), KJWidget(p), mBack(0), mView(0), mInterpEq(0)
{
	int x=l[1].toInt();
	int y=l[2].toInt();
	int xs=l[3].toInt()-x;
	int ys=l[4].toInt()-y;
	setRect(x,y,xs,ys);

	mBars = p->pixmap(parser()["equalizerbmp"][3]);

	mBands = l[6].toInt();
	mXSpace = l[7].toInt();

	// background under equalizer
	// needed to only blit onto screen ONCE and not for every band
	TQPixmap tmp = p->pixmap(p->item("backgroundimage")[1]);
	mBack = new KPixmap ( TQSize(xs,ys) );
	bitBlt( mBack, 0, 0, &tmp, x, y, xs, ys, TQt::CopyROP );

	// buffer for view
	mView = new TQPixmap ( xs, ys );

	mBandWidth=parser()["EqualizerBmp"][1].toInt();
	mBandHalfHeight=parser()["EqualizerBmp"][2].toInt();
	
	kdDebug(66666) << "[KJEqualizer] mBands=" << mBands << ", mXSpace=" << mXSpace << ", mBandWidth=" << mBandWidth << ", mBandHalfHeight=" <<  mBandHalfHeight << "." << endl;

	kdDebug(66666) << "[KJEqualizer] creating VInterpolation for " << mBands << " bands..." << endl;
	mInterpEq = new VInterpolation(mBands);
//	napp->vequalizer()->setBands(mBands); // FIXME: hack because spline sucks :P
	connect(napp->vequalizer(), TQT_SIGNAL(changed()), this, TQT_SLOT(slotUpdateBuffer()));

	slotUpdateBuffer(); // fill mView pixmap with valid data
}

KJEqualizer::~KJEqualizer(void)
{
	delete mInterpEq;
	delete mView;
	delete mBack;
}

int KJEqualizer::barNum(const TQPoint &pos) const
{
	int x = pos.x();
	x = x / mXSpace;
	return mInterpEq->bands() * x / mBands;
}

int KJEqualizer::level(const TQPoint &pos) const
{
	int y = ((-pos.y()) + mBandHalfHeight+1) * (200/mBandHalfHeight);
	return y;
}

void KJEqualizer::paint(TQPainter *p, const TQRect &)
{
	TQPixmap temp(rect().width(), rect().height());
	// draw background into buffer
	bitBlt ( &temp, 0, 0, mBack, 0, 0, -1, -1, TQt::CopyROP );
	// draw band sliders into buffer
	bitBlt( &temp, 0, 0, mView, 0, 0, rect().width(), rect().height(), TQt::CopyROP);
	// and draw it on screen
	bitBlt(p->device(), rect().topLeft(), &temp, TQRect(0,0,-1,-1), TQt::CopyROP);
}

void KJEqualizer::slotUpdateBuffer()
{
//	kdDebug(66666) << "[KJEqualizer] slotUpdateBuffer() called." << endl;

	TQBitmap regionMask( rect().width(), rect().height(), true); // fully transparent mask
	TQPainter mask( &regionMask );

	TQPoint destX = TQPoint(0, 0);

	for (int band=0; band<mBands; band++)
	{
		int level = mInterpEq->level(band);
		if (level>200) level=200;
		if (level<-200) level=-200;
		int picNum = ((int)(level+200)*(mBandHalfHeight-1) / 400) + 1;
		int xPos = (picNum * mBandWidth) - mBandWidth;

//		kdDebug(66666) << "[KJEqualizer] band=" << band << ", level=" << level << ", picNum=" << picNum << " @ xpos=" << xPos << "." << endl;

		bitBlt(mView, destX, &mBars, TQRect(xPos,0,mBandWidth,rect().height()), TQt::CopyROP);
		// make slider opaque in mask so you see something on screen
		mask.fillRect ( destX.x(), 0, mBandWidth, rect().height(), TQt::color1 );
		destX += TQPoint(mXSpace,0);

	} // for()
	// whole thingy has been drawn, now set the mask
	mView->setMask( regionMask );
	repaint();
}

void KJEqualizer::mouseMove(const TQPoint &p, bool in)
{
	if (!in) return;
	mousePress(p);
}

bool KJEqualizer::mousePress(const TQPoint &p)
{
	kdDebug(66666) << "[KJEqualizer] setting band " << mBands << "/" << barNum(p)+1 << " to level " << level(p) << endl;
	VBand b = mInterpEq->band( barNum(p) );
	b.setLevel( level(p) );
//	mouseMove(p, true);
	return true;
}
