#include "CustomDelegate.h"
#include <QComboBox>
#include <QDebug>

CustomDelegate::CustomDelegate(QObject *parent)
    : QItemDelegate(parent)
    , factorys(new WidgetFactory(this))
{
}

CustomDelegate::~CustomDelegate()
{
}

QWidget * CustomDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
	qx::IxDataMember *dataMember = dataMembers->get(index.column());
    if (dataMember->getPropertyBag("notEditAble").toBool())
		return nullptr;
	IxWidgetFactory_ptr facory = factorys->getFactory(dataMember);
	facory->create(dataMember, parent);
	return facory->getWidget();
}

void CustomDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
//	if (editor == nullptr)
//		return;

//    IxWidgetFactory_ptr facory = factorys->getFactory(dataMembers->get(index.column()));
//	facory->getIxWidget()->setValue(index.model()->data(index));
    QItemDelegate::setEditorData(editor,index);
}

void CustomDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
	IxWidgetFactory_ptr facory = factorys->getFactory(dataMembers->get(index.column()));
	model->setData(index, facory->getIxWidget()->getValue());
}
