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

#include "core/profile.h"

#include "veneer/settings.h"

#include <QDebug>
#include <QPushButton>
#include <QDialogButtonBox>

#include "ui_chooseprofiledialog.h"

ChooseProfileDialog::ChooseProfileDialog(QWidget* parent) :
	QDialog(parent)
{
	setupUi(this);

	setupDialog();
	setupConnections();

	Veneer::restoreDialogSize("chooseProfileDialog", this);
}

ChooseProfileDialog::~ChooseProfileDialog()
{
	Veneer::saveDialogSize("chooseProfileDialog", this);
}

void ChooseProfileDialog::setupDialog()
{
	Profiles profiles;

	if (!Profile::list(profiles))
	{
		qDebug() << "list failed";
		return;
	}

	foreach(const Profile* p, profiles)
	{
		QListWidgetItem* item = new QListWidgetItem();
		item->setText(p->name());
		item->setData(Qt::UserRole, p->name());
		profileList->addItem(item);
	}

	buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
}

void ChooseProfileDialog::setupConnections()
{
	connect(profileList, SIGNAL(itemActivated(QListWidgetItem*)), SLOT(onItemActivated(QListWidgetItem*)));
	connect(profileList, SIGNAL(itemSelectionChanged()), SLOT(onItemSelectionChanged()));

}

void ChooseProfileDialog::onItemSelectionChanged()
{
	buttonBox->button(QDialogButtonBox::Ok)->setEnabled(profileList->selectedItems().size() > 0);
}

QString ChooseProfileDialog::profileName() const
{
	if (profileList->selectedItems().size() == 1)
		return profileList->selectedItems()[0]->data(Qt::UserRole).toString();

	return QString();
}

void ChooseProfileDialog::onItemActivated(QListWidgetItem*)
{
	accept();
}

