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

#include <QAbstractItemModel>
#include <QTimeEdit>

TimeEditDelegate::TimeEditDelegate(QObject* parent) :
	QStyledItemDelegate(parent)
{
}

void TimeEditDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
	QTime val = index.model()->data(index, Qt::EditRole).toTime();
	QTimeEdit* edit = static_cast<QTimeEdit*>(editor);
	edit->setTime(val);
}

void TimeEditDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
	QTimeEdit* edit = static_cast<QTimeEdit*>(editor);
	model->setData(index, edit->time(), Qt::EditRole);
}

void TimeEditDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex&) const
{
	editor->setGeometry(option.rect);
}

QWidget* TimeEditDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const
{
	QTimeEdit* edit = new QTimeEdit(parent);
	edit->setDisplayFormat("hh:mm");
	return edit;
}
