#include "WidgetFactory.h"
#include "ComboBoxWidgetFactory.h"
#include "LineEditWidgetFactory.h"
#include "PathSelectorWidgetFactory.h"
#include "TagEditWidgetFactory.h"
#include "AutoCompleteTagEditWidgetFactory.h"
#include "DateTimeWidgetFactory.h"
#include "AutoCompleteComBoxWidgetFactory.h"
#include "CheckComboBoxWidgetFactory.h"

WidgetFactory::WidgetFactory(QObject *parent)
	: QObject(parent)
{
	init();
}

WidgetFactory::~WidgetFactory()
{

}

void WidgetFactory::init()
{
	factorys << std::make_shared<LineEditWidgetFactory>()   //默认第一个为输入框
		<< std::make_shared<ComboBoxWidgetFactory>()
        << std::make_shared<CheckComboBoxWidgetFactory>()
		<< std::make_shared<AutoCompleteComBoxWidgetFactory>()
		<< std::make_shared<DateTimeWidgetFactory>()
		<< std::make_shared<TagEditWidgetFactory>()
		<< std::make_shared<AutoCompleteTagEditWidgetFactory>()
		<< std::make_shared<PathSelectorWidgetFactory>();
}

IxWidgetFactory_ptr  WidgetFactory::getFactory(const QString &type)
{
	IxWidgetFactory_ptr widgetFactory = nullptr;
	if (type.isEmpty())
	{
		widgetFactory = factorys.at(0);
	}
    else
	{
		foreach(IxWidgetFactory_ptr factory, factorys)
		{
            if (factory->type() == type)
            {
                widgetFactory = factory;
                break;
            }
		}
	}
	return widgetFactory;
}

WidgetFactory& WidgetFactory::operator<<(IxWidgetFactory_ptr widgetFactory)
{
	factorys << widgetFactory;
	return *this;
}

IxWidgetFactory_ptr WidgetFactory::getFactory(qx::IxDataMember *dataMember)
{
	return getFactory(getFactoryType(dataMember));
}

QString WidgetFactory::getFactoryType(qx::IxDataMember *dataMember)
{
	QString type;
	type = dataMember->getPropertyBag("type").toString();
	if (dataMember->getType() == "QDateTime")
		type = "DateTime";

	return type;
}
