/***************************************************************************
  MultiStateWidget.cpp  -  provides methods of multistateWidget a Class that
                           switches the image it, displays on clicking, used
                           for the channel enable/disable lamps...
			     -------------------
    begin                : Sun Jun 04 2000
    copyright            : (C) 2000 by Martin Wilz
    email                : martin@wilz.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
#include "config.h"
#include <qdir.h>
#include <qimage.h>
#include <qstring.h>
#include <qptrlist.h>
#include <qstringlist.h>
#include <qdir.h>
#include <qpixmap.h>
#include "kstddirs.h"
#include "MultiStateWidget.h"

QPtrList <QPixmap> *pixmaps = 0;
QStringList *pixnames = 0;

//***************************************************************************
MultiStateWidget::MultiStateWidget(QWidget *parent, int num, int count)
    :QWidget(parent)
{
    this->act = 0;
    this->count = count;
    this->number = num;
    this->states = new int[count];
    Q_ASSERT(states);
    if (!states) return;

    for (int i = 0; i < count; i++)
    	states[i] = 0;

    if (pixmaps == 0) pixmaps = new QPtrList<QPixmap>();
    Q_ASSERT(pixmaps);

    if (pixnames == 0) pixnames = new QStringList();
    Q_ASSERT(pixnames);

    resize(20, 20);
}

//***************************************************************************
void MultiStateWidget::setNumber(int number)
{
    this->number = number;
}

//***************************************************************************
int MultiStateWidget::addPixmap(const QString &filename)
{
    Q_ASSERT(pixmaps);
    Q_ASSERT(pixnames);
    if (!pixmaps) return -1;
    if (!pixnames) return -1;

    int result = pixnames->findIndex(filename);
    if (result == -1) {
	QPixmap *newpix = new QPixmap();
	Q_ASSERT(newpix);
	if (!newpix) return -1;

	QString file = locate("data", QString("kwave/pics/")+filename);
	QImage img(file);
	newpix->convertFromImage(img);
	pixmaps->append(newpix);
	pixnames->append(filename);
	return pixmaps->at();
    } else
	return result;

    return -1;
}

//***************************************************************************
void MultiStateWidget::setStates(int *newstates)
{
    for (int i = 0; i < count; i++)
	states[i] = newstates[i];
}

//***************************************************************************
void MultiStateWidget::setState(int newstate)
{
    act = newstate;
    if (act >= count) act = count-1;
    if (act < 0) act = 0;
    repaint();
}

//***************************************************************************
void MultiStateWidget::nextState()
{
    act++;
    if (act >= count) act = 0;
    repaint();
}

//***************************************************************************
void MultiStateWidget::mouseReleaseEvent( QMouseEvent *e)
{
    Q_ASSERT(e);
    if (!e) return;

    if (e->button() == LeftButton) {
	nextState ();
	emit clicked(number);
    }
}

//***************************************************************************
MultiStateWidget::~MultiStateWidget()
{
    if (states) delete[] states;
}

//***************************************************************************
void MultiStateWidget::paintEvent (QPaintEvent *)
{
    Q_ASSERT(pixmaps);
    if (!pixmaps) return;

    QPixmap *img;
    img = pixmaps->at(states[act]);

    if (img) bitBlt(this, 0, 0,	img, 0, 0,
                    img->width(), img->height(), CopyROP);
}

//***************************************************************************
#include "MultiStateWidget.moc"
//***************************************************************************
//***************************************************************************
