/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            mainwindow.cc
 *
 *  Sat Aug 21 19:49:34 2004
 *  Copyright  2004  deva
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <config.h>
#ifdef USE_GUI

#include "mainwindow.h"

#include <qpainter.h>
#include <qpicture.h>

#include <qpushbutton.h>
#include <qfont.h>
#include <qpixmap.h>
#include <qimage.h>
#include <qlayout.h>
#include <qstatusbar.h>

//#include "mgui_alert.h"
//#include "mgui_datasocket.h"

#include "miav_config.h"

#include <config.h>
//"miav-grab.h"


//#define WITH_DV
MainWindow::MainWindow( QWidget* parent, const char* name )
	: QWidget( parent, name, WStyle_Customize | WStyle_NoBorder )
{
  MiavConfig cfg("miav.conf");
  int resolution_w = cfg.readInt("pixel_width");
  int resolution_h = cfg.readInt("pixel_height");
  unit = ((float)resolution_w / (float)(cfg.readFloat("screensize") * 3.1f));

  move(0,0);
  resize(resolution_w, resolution_h);

  // Load images
  pix_record = new QPixmap();
  pix_record->load( PIXMAPS"/record.png" );
/*
	QPainter *paint = new QPainter();
	QPicture *pic = new QPicture();
	pic->load(fname, "svg");
	paint->begin(this);
	paint->setWindow(pic->boundingRect());
	paint->drawPicture(0, 0, *pic);
	paint->end();
 */

  pix_stop = new QPixmap();
  pix_stop->load( PIXMAPS"/stop.png" );

  pix_camera = new QPixmap();
  pix_camera->load( PIXMAPS"/camera.png" );

  pix_dummy = new QPixmap();
  pix_dummy->load( PIXMAPS"/dummy.png" );
  
  pix_logo = new QPixmap();
  pix_logo->load( PIXMAPS"/miav-logo-64x64.png" );

  createGui();
  show();

  camera = new Camera();
  camera->connect(cfg.readString("server_addr")->c_str(), 
                  cfg.readInt("server_port"));

  cam_error = camera->errorObject();
  while(cam_error->hasError()) {
    MessageBox(this, "", cam_error->popErrorString().c_str(), TYPE_OK, ICON_ERROR).exec();
  }

  recording = false;
  frozen = false;

  // Open the CPR Dialog
  cpr_clicked();
}

MainWindow::~MainWindow()
{
  if(camera) delete camera;
  delete btn_cpr;
}

void MainWindow::createGui()
{
  // Layout widgets

  /*  _________________________________________________
   *(0) __________________________  |  ______________  |
   * | |        |        |        | | |              | |
   * |(1)       |        |        | |(2)             | |
   * | |        |        |        | | |              | |
   * | |        |        |        | | |              | |
   * | |        |        |        | | |______________| |
   * | |        |        |        | | |              | |
   * | |        |        |        | | |              | |
   * | |        |        |        | | |              | |
   * | |________|________|________| | |              | |
   * | |        |        |        | | |______________| |
   * | |________|________|________| | |              | |
   * | |        |        |        | | |              | |
   * | |________|________|________| | |              | |
   * | |        |        |        | | |              | |
   * | |________|________|________| | |______________| |
   * |______________________________|__________________|
   * |______________________________|__________________|
   */

	QGridLayout *g0 = new QGridLayout(this, 2, 2, 0, -1, "g0");
	QGridLayout *g1 = new QGridLayout(3, 4, -1, "g1");
  g0->addLayout(g1, 0, 0);
	QGridLayout *g2 = new QGridLayout(1, NUM_HISTORY, -1, "g2");
  g0->addLayout(g2, 0, 1);

  img_recedge = new QLabel(this);
  img_recedge->setBackgroundColor(QColor(160,160,160));
  img_recedge->setFixedSize(740,596);
  
  img_live = new VideoWidget(img_recedge);
  //  img_live->setErasePixmap( *pix_dummy );
  img_live->setFixedSize(720,576);
  img_live->move(10,10);
  g1->addMultiCellWidget ( img_recedge, 0, 0, 0, 2, Qt::AlignHCenter);

  // CPR/NAME LABEL + CPR button
  lbl_cpr = createLabel("", 18, 3);
  g1->addMultiCellWidget ( lbl_cpr, 1, 1, 0, 1);

  btn_cpr = createButton("NYT CPR", 8, 3);
  btn_cpr->setFocus();
  QObject::connect( btn_cpr, SIGNAL(clicked()), this, SLOT(cpr_clicked()) );
  // Will also be connected in the MGUI code
  g1->addWidget(btn_cpr, 1, 2);

  lbl_name = createLabel("", 18, 2);
  g1->addMultiCellWidget ( lbl_name, 2, 2, 0, 2);

  // Rec + Shot + Freeze buttons
  btn_rec = createButton("", 8, 3);
  btn_rec->setPixmap(*pix_record);
  QObject::connect( btn_rec, SIGNAL(clicked()), this, SLOT(rec_clicked()) );
  g1->addWidget(btn_rec, 3, 0);
  
  btn_shoot = createButton("", 8, 3);
  btn_shoot->setPixmap(*pix_camera);
  QObject::connect( btn_shoot, SIGNAL(clicked()), this, SLOT(shoot_clicked()) );
  g1->addWidget(btn_shoot, 3, 1);
  
  btn_freeze = createButton("FREEZE", 8, 3);
  //  btn_freeze->setPixmap(*pix_logo);
  QObject::connect( btn_freeze, SIGNAL(clicked()), this, SLOT(freeze_clicked()) );
  g1->addWidget(btn_freeze, 3, 2);
  
  // History widgets
  for(int i = 0; i < NUM_HISTORY; i++) {
    img_history[i] = new QLabel(this);
    img_history[i]->setPixmap(*pix_dummy);
    img_history[i]->setFixedSize(240,192);
    g2->addWidget(img_history[i], i, 0, Qt::AlignHCenter);
  }

  // Statusbar
  status = new QStatusBar(this);
  status->setSizeGripEnabled(FALSE);
  g0->addMultiCellWidget(status, 4, 4, 0, 1);

  lbl_version = createLabel("MIaV-Grab v" VERSION, 18, 1);
  lbl_version->setFixedWidth(300);
  status->addWidget(lbl_version, 0, TRUE);

  status->message("Klar...");
}

QPushButton *MainWindow::createButton(char *caption, int width, int height)
{
  QPushButton *btn = new QPushButton(caption, this);
  btn->setFont( QFont( "Arial", (int)(unit * height / 2), QFont::Bold ) );
  btn->setFixedHeight((int)(unit * height));
  return btn;
}

QLabel *MainWindow::createLabel(char *caption, int width, int height)
{
  QLabel *lbl = new QLabel(caption, this);
  lbl->setFont( QFont( "Arial", (int)(unit * height / 2), (height>1)?QFont::Bold:QFont::Normal ) );
  lbl->setFixedHeight((int)(unit * height));
  return lbl;
}

void MainWindow::message(char *msg)
{
  //  lbl_status->setText(msg);
  status->message(msg);
}

void MainWindow::cpr_clicked()
{
  {
    // TODO: Save CPR and name, from the labels.
  }

  // Create and call the CPRQueryDialog.
  CPRQueryDialog dlg(lbl_cpr, lbl_name, this, "CPRQueryDialog", status);

  if(dlg.exec() == 0) {
    printf("Cancel!\n");
    // TODO: Restore old CPR and name, in the labels. 
  } else {
    printf("CPR changed\n");
    // TODO: Change CPR camera.
  }
}

void MainWindow::rec_clicked()
{
  recording = 1 - recording;
  if(recording) {
    img_recedge->setBackgroundColor(red);
    btn_rec->setPixmap(*pix_stop);
    camera->start();
  } else {
    img_recedge->setBackgroundColor(QColor(160,160,160));
    btn_rec->setPixmap(*pix_record);
    camera->stop();
  }
}

void MainWindow::shoot_clicked()
{
  QImage image;

  camera->snapshot();

  image = img_live->getScreenshot();
  image = image.smoothScale(img_history[0]->width(), img_history[0]->height());

  QPixmap pixmap;
  for(int cnt = (NUM_HISTORY-1); cnt > 0; cnt--) {
    pixmap = *img_history[cnt-1]->pixmap();
    img_history[cnt]->setPixmap(pixmap);
  }
  img_history[0]->setPixmap(image);

  if(frozen) {
    camera->unfreeze();
    btn_freeze->setOn(false);
    frozen = false;
  }
}

void MainWindow::freeze_clicked()
{
  if(frozen) {
    camera->unfreeze();
    frozen = false;
  } else {
    camera->freeze();
    frozen = true;
  }
}

#endif /*USE_GUI*/