/*
 *  Copyright (C) 2007-2008 CMM Sigma Andrzej Chmielowiec <cmmsigma@cmmsigma.eu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3
 *  as published by the Free Software Foundation.
 *
 *  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.
 */

// QDetailsMessageBox.cpp: implementation of the QDetailsMessageBox class.
//
//////////////////////////////////////////////////////////////////////

#include "QDetailsMessageBox.h"

QDetailsMessageBox::QDetailsMessageBox(QWidget *parent, const QString &title, const QString& text, const QString& details) : QDialog(parent)
{
	mExpanded = false;

	setWindowTitle(title);

	mDetails = new QTextEdit();
	mDetails->setReadOnly(true);
	mDetails->setVisible(false);
	mDetails->setText(details.trimmed());
	
	QGridLayout *layout = new QGridLayout();

	mIcon = new QLabel();
	layout->addWidget(mIcon, 0, 0, Qt::AlignTop);

	layout->addWidget(new QLabel(), 0, 1, Qt::AlignTop);

	QLabel *label = new QLabel(text);
	layout->addWidget(label, 0, 2, Qt::AlignLeft | Qt::AlignTop);
	layout->setColumnStretch(2, 1);
	layout->setRowStretch(0, 0);

	QGridLayout *buttonsLayout = new QGridLayout();
	QPushButton *btnOK = new QPushButton(tr("OK"));
	connect(btnOK, SIGNAL(clicked()), this, SLOT(accept()));

	if(details.isEmpty())
	{
		buttonsLayout->addWidget(btnOK, 0, 0, Qt::AlignCenter);
	}
	else
	{
		buttonsLayout->addWidget(new QLabel(), 0, 0);
		buttonsLayout->addWidget(btnOK, 0, 1, Qt::AlignCenter);

		mBtnDetails = new QPushButton(tr("Show details >>"));
		connect(mBtnDetails, SIGNAL(clicked()), this, SLOT(detailsSlot()));
		buttonsLayout->addWidget(mBtnDetails, 0, 2, Qt::AlignRight);
	}
	layout->addLayout(buttonsLayout, 1, 0, 1, 3);

	setLayout(layout);

	mMinHeight = baseSize().height();
	mMaxHeight = mMinHeight + 240;

	layout->addWidget(mDetails, 2, 0, 1, 3);
	setMaximumHeight(mMinHeight);

	if(!details.isEmpty() && baseSize().width() < 400)
	{
		resize(400, mMinHeight);
	}
}

void
QDetailsMessageBox::detailsSlot()
{
	if(mExpanded)
	{
		mMaxHeight = height();

		mDetails->setVisible(false);
		mBtnDetails->setText(tr("Show details >>"));

		setMaximumHeight(mMinHeight);
		resize(width(), mMinHeight);

		setSizeGripEnabled(false);
	}
	else
	{
		mDetails->setVisible(true);
		mBtnDetails->setText(tr("<< Hide details"));

		setMaximumHeight(1024);
		resize(width(), mMaxHeight);
		
		setSizeGripEnabled(true);
	}
	mExpanded = !mExpanded;
}

int
QDetailsMessageBox::information(QWidget *parent, const QString &title, const QString& text, const QString& details)
{
	QDetailsMessageBox dlg(parent, title, text, details);
	dlg.mIcon->setPixmap(QMessageBox::standardIcon(QMessageBox::Information));
	return dlg.exec();
}

int
QDetailsMessageBox::warning(QWidget *parent, const QString &title, const QString& text, const QString& details)
{
	QDetailsMessageBox dlg(parent, title, text, details);
	dlg.mIcon->setPixmap(QMessageBox::standardIcon(QMessageBox::Warning));
	return dlg.exec();
}

int
QDetailsMessageBox::critical(QWidget *parent, const QString &title, const QString& text, const QString& details)
{
	QDetailsMessageBox dlg(parent, title, text, details);
	dlg.mIcon->setPixmap(QMessageBox::standardIcon(QMessageBox::Critical));
	return dlg.exec();
}
