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

#include "ui_bloodpressureeditor.h"

#include <QSpinBox>
#include <QAbstractItemModel>
#include <QPainter>

class BloodPressureEditor : public QWidget, public Ui::BloodPressureEditor
{
	public:
		BloodPressureEditor(QWidget* parent) : QWidget(parent) { setupUi(this); }

	public:
		void setValues(const QList<QVariant>& v) { m_SystolicSpin->setValue(v[0].toInt()); m_DiastolicSpin->setValue(v[1].toInt()); }
		QList<QVariant> values() const { return QList<QVariant>() << m_SystolicSpin->value() << m_DiastolicSpin->value(); }
};

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

void BloodPressureDelegate::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 BloodPressureDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
	QList<QVariant> val = index.model()->data(index, Qt::EditRole).toList();
	BloodPressureEditor* e = static_cast<BloodPressureEditor*>(editor);
	e->setValues(val);
}

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

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

QWidget* BloodPressureDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const
{
	return new BloodPressureEditor(parent);
}
