/***************************************************************************
 *   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/dateeditdelegate.h"
#include "gui/athletelogtree.h"

#include "util/dateedit/kdateedit.h"

#include <QAbstractItemModel>
#include <QApplication>

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

void DateEditDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	QStyleOptionViewItemV4 opt = option;
	initStyleOption(&opt, index);

	// Workaround for http://bugreports.qt.nokia.com/browse/QTBUG-1871
	const AthleteLogTree* view = qobject_cast<const AthleteLogTree*>(parent());
	const bool isEditing = view ? view->isEditing(index) : false;
	if (!(opt.state & QStyle::State_Editing) && !isEditing)
		QStyledItemDelegate::paint(painter, option, index);
	else
		QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter);
}
void DateEditDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
	QDate date = index.model()->data(index, Qt::EditRole).toDate();
	KDateEdit* e = static_cast<KDateEdit*>(editor);
	e->setDate(date);
}

void DateEditDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
	KDateEdit* e = static_cast<KDateEdit*>(editor);
	model->setData(index, e->date(), Qt::EditRole);
}

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

QWidget* DateEditDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const
{
	KDateEdit* e = new KDateEdit(parent);
	// looks better when resized to contents, but that breask background painting again
	e->setSizeAdjustPolicy(QComboBox::AdjustToContentsOnFirstShow);
	return e;
}
