/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   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; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#include "gui/editprofiledialog.h"

#include "core/profile.h"

#include "veneer/settings.h"

#include "util/unit.h"

#include <QDebug>

EditProfileDialog::EditProfileDialog(QWidget* parent, Profile& profile) :
	QDialog(parent),
	m_Profile(profile)
{
	setupUi(this);

	setupDialog();
	setupConnections();

	Veneer::restoreDialogSize("editProfileDialog", this);

	if (!profile.name().isEmpty())
	{
		setWindowTitle(tr("Edit Profile"));
		buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
	}
	else
		setWindowTitle(tr("Create a New Profile"));
}

EditProfileDialog::~EditProfileDialog()
{
	Veneer::saveDialogSize("editProfileDialog", this);
}

void EditProfileDialog::setupDialog()
{
	athleteWidget->setupPage(&m_Profile);
	categoriesWidget->setupPage(&m_Profile);
	equipmentWidget->setupPage(&m_Profile);
}

void EditProfileDialog::setupConnections()
{
	athleteWidget->setupConnections();
	categoriesWidget->setupConnections();
	equipmentWidget->setupConnections();

	connect(athleteWidget, SIGNAL(modified()), SLOT(setDirty()));
	connect(categoriesWidget, SIGNAL(modified()), SLOT(setDirty()));
	connect(equipmentWidget, SIGNAL(modified()), SLOT(setDirty()));
}

void EditProfileDialog::accept()
{
	if (!athleteWidget->accept())
		return;

	if (!categoriesWidget->accept())
		return;

	if (!equipmentWidget->accept())
		return;

	QDialog::accept();
}

void EditProfileDialog::reject()
{
	athleteWidget->reject();
	categoriesWidget->reject();
	equipmentWidget->reject();
	QDialog::reject();
}

void EditProfileDialog::setDirty(bool b)
{
	buttonBox->button(QDialogButtonBox::Ok)->setEnabled(b);
}
