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

#include <QSpinBox>
#include <QAbstractItemModel>

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

void SpinBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
	const int val = index.model()->data(index, Qt::EditRole).toInt();
	QSpinBox* spin = static_cast<QSpinBox*>(editor);
	spin->setValue(val);
}

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

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

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