/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            messagebox.cc
 *
 *  Fri Feb 25 20:23:19 CET 2005
 *  Copyright  2005 Bent Bisballe
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *    This file is part of MIaV.
 *
 *    MIaV 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.
 *
 *    MIaV 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 MIaV; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "messagebox.h"

#include "configuration.h"

#define INCH_IN_CM 2
#define BUTTON_HEIGHT 1
#define BUTTON_WIDTH 3
////////////////////////////////////////////////////////////////////////////////////////
/* If the cpr input by the user is not valid, this dialog
 * ask the user what to do. Edit the number, use it as it is, 
 * or clear the number.
 */

MessageBox::MessageBox(QWidget* parent, 
                       const QString name, 
                       const QString text, 
                       msg_type type, 
                       msg_icon icon)
	: QDialog(parent)
{
  int resolution_w;
  if(MIaV::config->get("pixel_width", &resolution_w))
    resolution_w = 1024; // fallback!

  //int resolution_h = config->readInt("pixel_height");
  double screensize;
  if(MIaV::config->get("screensize", &screensize))
    screensize = 17.0; // fallback!

  unit = ((float)resolution_w / screensize) / INCH_IN_CM;

  setModal(true);
  setWindowTitle(name);
	QFrame *topf = new QFrame(this);
  topf->setFrameStyle(QFrame::Box | QFrame::Raised);
  topf->setLineWidth(3);	
	QVBoxLayout *bl = new QVBoxLayout(this);
	bl->addWidget(topf);
	
  // Setup the icon
  pix_icon = new QPixmap();
  switch(icon) {
  case ICON_NONE:    // No icon is used
    {
      break;
    }
  case ICON_DEFAULT: // An icon matching the buttons is used
    {
      switch(type) {
      case TYPE_OK:
        pix_icon->load( PIXMAP_INFO );
        break;
      case TYPE_OK_CANCEL: 
        pix_icon->load( PIXMAP_WARNING );
        break;
      case TYPE_YES_NO: 
      case TYPE_YES_NO_MAYBE: 
      case TYPE_YES_NO_CANCEL: 
      case TYPE_YES_NO_MAYBE_CANCEL: 
        pix_icon->load( PIXMAP_QUESTION );
        break;
      }
      break;
    }
  case ICON_INFO:    // An info icon (matching the ok button)
    {
      pix_icon->load( PIXMAP_INFO );
      break;
    }
  case ICON_WARNING:    // An warning icon (matching the ok/cancel button)
    {
      pix_icon->load( PIXMAP_WARNING );
      break;
    }
  case ICON_ERROR:   // An critical error  icon
    {
      pix_icon->load( PIXMAP_ERROR );
      break;
    }
  case ICON_QUESTION:// An question icon (matching the yes/no and yes/no/cancel buttons)
    {
      pix_icon->load( PIXMAP_QUESTION );
      break;
    }
  }
  QLabel *lbl_icon = new QLabel(topf);
  lbl_icon->setPixmap(*pix_icon);

	QLabel *lbl_text = new QLabel(topf);
	lbl_text->setText(text);
  lbl_text->setFont( QFont( "Sans Serif", 
                       //(height>1)?(int)(unit * height / 2):(int)(unit * height / 2), 
                       (int)(unit * 0.7 * BUTTON_HEIGHT / 2), 
                       QFont::Normal ) );
  lbl_text->setFixedHeight((int)(unit * BUTTON_HEIGHT));
  //	lbl_text->setFont(QFont("Arial", 18));
	QFrame *f = new QFrame(topf);

	QVBoxLayout *blayout = new QVBoxLayout(topf);

  blayout->addWidget(lbl_icon);
	blayout->addWidget(lbl_text);
	blayout->addWidget(f);

  // Setup the buttons
  switch(type) {
  case TYPE_OK: 
    {
      QPushButton *bok = createButton(f, TXT_OK );
      QGridLayout *glayout = new QGridLayout(f);
      glayout->addWidget(bok, 0, 0);
      connect(bok, SIGNAL( clicked() ), SLOT(bok_clicked()));	
      break;
    }
  case TYPE_OK_CANCEL: 
    {
      QPushButton *bok = createButton(f, TXT_OK );
      QPushButton *bcancel = createButton(f, TXT_CANCEL );
      QGridLayout *glayout = new QGridLayout(f);
      glayout->addWidget(bcancel, 0, 1);
      glayout->addWidget(bok, 0, 2);
      connect(bcancel, SIGNAL( clicked() ), SLOT(bcancel_clicked()));	
      connect(bok, SIGNAL( clicked() ), SLOT(bok_clicked()));	
      break;
    }
  case TYPE_YES_NO: 
    {
      QPushButton *bno = createButton(f, TXT_NO );
      QPushButton *byes = createButton(f, TXT_YES );
      QGridLayout *glayout = new QGridLayout(f);
      glayout->addWidget(bno, 0, 0);
      glayout->addWidget(byes, 0, 1);
      connect(byes, SIGNAL( clicked() ), SLOT(byes_clicked()));
      connect(bno, SIGNAL( clicked() ), SLOT(bno_clicked()));	
      break;
    }
  case TYPE_YES_NO_MAYBE: 
    {
      QPushButton *bmaybe = createButton(f, TXT_MAYBE );
      QPushButton *bno = createButton(f, TXT_NO );
      QPushButton *byes = createButton(f, TXT_YES );
      QGridLayout *glayout = new QGridLayout(f);
      glayout->addWidget(bno, 0, 0);
      glayout->addWidget(byes, 0, 1);
      glayout->addWidget(bmaybe, 0, 2);
      connect(byes, SIGNAL( clicked() ), SLOT(byes_clicked()));
      connect(bno, SIGNAL( clicked() ), SLOT(bno_clicked()));	
      connect(bmaybe, SIGNAL( clicked() ), SLOT(bmaybe_clicked()));	
      break;
    }
  case TYPE_YES_NO_CANCEL: 
    {
      QPushButton *bcancel = createButton(f, TXT_CANCEL );
      QPushButton *bno = createButton(f, TXT_NO );
      QPushButton *byes = createButton(f, TXT_YES );
      QGridLayout *glayout = new QGridLayout(f);
      glayout->addWidget(bno, 0, 0);
      glayout->addWidget(bcancel, 0, 1);
      glayout->addWidget(byes, 0, 2);
      connect(byes, SIGNAL( clicked() ), SLOT(byes_clicked()));
      connect(bcancel, SIGNAL( clicked() ), SLOT(bcancel_clicked()));	
      connect(bno, SIGNAL( clicked() ), SLOT(bno_clicked()));	
      break;
    }
  case TYPE_YES_NO_MAYBE_CANCEL: 
    {
      QPushButton *bmaybe = createButton(f, TXT_MAYBE );
      QPushButton *bcancel = createButton(f, TXT_CANCEL );
      QPushButton *bno = createButton(f, TXT_NO );
      QPushButton *byes = createButton(f, TXT_YES );
      QGridLayout *glayout = new QGridLayout(f);
      glayout->addWidget(bno, 0, 0);
      glayout->addWidget(bcancel, 0, 1);
      glayout->addWidget(byes, 0, 2);
      glayout->addWidget(bmaybe, 0, 3);
      connect(bmaybe, SIGNAL( clicked() ), SLOT(bmaybe_clicked()));
      connect(byes, SIGNAL( clicked() ), SLOT(byes_clicked()));
      connect(bcancel, SIGNAL( clicked() ), SLOT(bcancel_clicked()));	
      connect(bno, SIGNAL( clicked() ), SLOT(bno_clicked()));	
      break;
    }
  }

}

MessageBox::~MessageBox() 
{
  delete pix_icon;
}

QPushButton *MessageBox::createButton(QWidget *parent, const char *text)
{
	QPushButton *q = new QPushButton(parent);
	q->setText(text);
  q->setFont( QFont( "Sans Serif", (int)(unit * 0.7 * BUTTON_HEIGHT / 2 ), QFont::Normal ) );
	q->setFixedSize((int)(BUTTON_WIDTH * unit), (int)(BUTTON_HEIGHT * unit));
	return q;
}

void MessageBox::bok_clicked() 
{
	done(MSG_OK);
}	

void MessageBox::bcancel_clicked() 
{
	done(MSG_CANCEL);
}	

void MessageBox::byes_clicked() 
{
	done(MSG_YES);
}	

void MessageBox::bno_clicked() 
{
	done(MSG_NO);
}	

void MessageBox::bmaybe_clicked() 
{
	done(MSG_MAYBE);
}