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

#include <QDoubleSpinBox>
#include <QAbstractItemModel>

DoubleSpinBoxDelegate::DoubleSpinBoxDelegate(QObject* parent, double min, double max, int decimals, double singleStep, const QString& prefix, const QString& suffix) :
	QStyledItemDelegate(parent),
	m_Min(min),
	m_Max(max),
	m_Decimals(decimals),
	m_SingleStep(singleStep),
	m_Prefix(prefix),
	m_Suffix(suffix)
{
}

void DoubleSpinBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
	const double val = index.model()->data(index, Qt::EditRole).toDouble();
	QDoubleSpinBox* spin = static_cast<QDoubleSpinBox*>(editor);
	spin->setValue(val);
}

void DoubleSpinBoxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
	QDoubleSpinBox* spin = static_cast<QDoubleSpinBox*>(editor);
	model->setData(index, spin->value(), Qt::EditRole);
}

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

QWidget* DoubleSpinBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const
{
	QDoubleSpinBox* spin = new QDoubleSpinBox(parent);
	spin->setRange(m_Min, m_Max);
	spin->setSingleStep(m_SingleStep);
	spin->setPrefix(m_Prefix);
	spin->setSuffix(m_Suffix);
	spin->setDecimals(m_Decimals);
	return spin;
}

