/* -*- 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 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 "messagebox.h"

////////////////////////////////////////////////////////////////////////////////////////
/* 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 char* name, 
                       const char* text, 
                       msg_type type, 
                       msg_icon icon)
	: QDialog(parent, name, TRUE)
{
  setCaption(name);
	QFrame *topf = new QFrame(this);
		topf->setFrameStyle(QFrame::Box | QFrame::Raised);
		topf->setLineWidth(3);	
	QVBoxLayout *bl = new QVBoxLayout(this);
	bl->addWidget(topf);
	
	QLabel *lbl_text = new QLabel(topf);
	lbl_text->setText(text);
	lbl_text->setFont(QFont("Arial", 18));
	QFrame *f = new QFrame(topf);

	QVBoxLayout *blayout = new QVBoxLayout(topf, 20, 20);
	blayout->addWidget(lbl_text);
	blayout->addWidget(f);

  // 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;
    }
  }
  
  // Setup the buttons
  switch(type) {
  case TYPE_OK: 
    {
      QPushButton *bok = createButton(f, TXT_OK );
      QGridLayout *glayout = new QGridLayout(f, 1, 1, 20, 20);
      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, 1, 2, 20, 20);
      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 *byes = createButton(f, TXT_YES );
      QPushButton *bno = createButton(f, TXT_NO );
      QGridLayout *glayout = new QGridLayout(f, 1, 2, 20, 20);
      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 *byes = createButton(f, TXT_YES );
      QPushButton *bno = createButton(f, TXT_NO );
      QPushButton *bmaybe = createButton(f, TXT_MAYBE );
      QGridLayout *glayout = new QGridLayout(f, 1, 3, 20, 20);
      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 *byes = createButton(f, TXT_YES );
      QPushButton *bcancel = createButton(f, TXT_CANCEL );
      QPushButton *bno = createButton(f, TXT_NO );
      QGridLayout *glayout = new QGridLayout(f, 1, 3, 20, 20);
      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 *byes = createButton(f, TXT_YES );
      QPushButton *bcancel = createButton(f, TXT_CANCEL );
      QPushButton *bno = createButton(f, TXT_NO );
      QPushButton *bmaybe = createButton(f, TXT_MAYBE );
      QGridLayout *glayout = new QGridLayout(f, 1, 4, 20, 20);
      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("Arial", 18));
	q->setFixedSize(200, 75);
	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);
}	

#endif/*USE_GUI*/