/*
* This file is part of the KALEngine project.
* Copyright (C) 2007 Laurent Gomila <laurent.gom@gmail.com>
* Copyright (C) 2009 Sacha Schutz <istdasklar@free.fr>
* Copyright (C) 2009 Guillaume Martres <smarter@ubuntu.com>
*
* 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; see the file COPYING.  If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kcamwidget.h"
#include <KFileDialog>
#include <QPainter>
KCamWidget::KCamWidget(QWidget * parent):
        QWidget(parent)
{
    m_image  = NULL;
    m_error  = false;
    m_qimage  = new QImage();
    m_timer  = new QTimer(this);
    m_capture = cvCaptureFromCAM(0);
    if (!m_capture)
    {
        kDebug()<<"cannot get webcam...";
        m_error = true;
    }
    else
    {
        cvGrabFrame(m_capture);                 //find the webcam Resolution!
        m_image = cvRetrieveFrame(m_capture);
        m_size = QSize(m_image->width,m_image->height);
        resize(m_size);
        m_timer->start(100);
        connect(m_timer,SIGNAL(timeout()), this,SLOT(captureLoop()));
    }
}
//=======================================================
KCamWidget::~KCamWidget()
{
    cvReleaseCapture( &m_capture );
}
//=======================================================
QImage *KCamWidget::Ipl2QImage(const IplImage *img) //fct recuperer sur le net, converti un ldImage en QImage
{
    IplImage *imgNew=cvCloneImage(img);
    cvConvertImage(img,imgNew, CV_CVTIMG_SWAP_RB  );
    QImage * qimage = new QImage(reinterpret_cast<uchar*>(imgNew->imageData),
                                 imgNew->width,
                                 imgNew->height,
                                 3* imgNew->width,
                                 QImage::Format_RGB888);
    return qimage;

}
//===============================================================
void KCamWidget::captureLoop() // appeler par le timer, recupere l'image courante de la webcam et convertit
{
   if ( !m_capture)
    {
        m_error = true;
        kDebug()<<"cannot get capture";
        return;
    }
    cvGrabFrame(m_capture);
    m_image = cvRetrieveFrame(m_capture);

    if (m_image)
    {
        m_qimage = Ipl2QImage(m_image);
        update();
    }
}
//================================================================
void KCamWidget::paintEvent ( QPaintEvent * event )
{
    QPainter * paint = new QPainter;
    paint->begin(this);
if ( !m_error)
 paint->drawImage(event->rect(),*m_qimage);

else
{
    paint->drawPixmap(event->rect().center(),KIcon("window-close").pixmap(32,32));
    paint->drawText(event->rect(),"error cannot find your device...");
}
    paint->end();
}
//===================================================================
void KCamWidget::takeShoot()
{
    m_timer->stop();
    QString fileNameFromDialog = KFileDialog::getSaveFileName(KUrl(),QString("*.png, *.jpg"));


    m_qimage->save (fileNameFromDialog);

    m_timer->start();
}


