#include "ObjectTreeModel.h"
#include <QtCore/QMetaProperty>


ObjectTreeModel::ObjectTreeModel(QObject* parent)
: QAbstractItemModel(parent)
{
}

int ObjectTreeModel::columnCount(const QModelIndex&) const
{
	return 1;
}

int ObjectTreeModel::rowCount(const QModelIndex& parent) const
{
    QObject* obj = static_cast<QObject*>(parent.internalPointer());
    return (obj) ? obj->children().count() : m_roots.count();
}

void ObjectTreeModel::setRootObjects(const QList<ObjPtr>& roots)
{
    beginResetModel();
    m_roots = roots;
    endResetModel();
}

QModelIndex ObjectTreeModel::index(int row, int column, const QModelIndex& parent) const
{
    QObject* parentItem = static_cast<QObject*>(parent.internalPointer());
    const QObjectList& children = (parentItem) ? parentItem->children() : m_roots;
    bool outOfBounds = (row < 0 || column != 0 || row >= children.count());
    return outOfBounds ? QModelIndex() : createIndex(row,column,children.at(row));
}

int ObjectTreeModel::indexInParent(QObject* item) const
{
    QObject* parent = item->parent();
    return (parent && !m_roots.contains(item)) ? parent->children().indexOf(item) : m_roots.indexOf(item);
}

QModelIndex ObjectTreeModel::parent(const QModelIndex& index) const
{
    QObject* item = static_cast<QObject*>(index.internalPointer());
    QObject* parent = (item && !m_roots.contains(item)) ? item->parent() : 0;
    return (parent) ? createIndex(indexInParent(parent),index.column(),parent) : QModelIndex();
}

QVariant ObjectTreeModel::data(const QModelIndex& index, int role) const
{
    QObject* item = static_cast<QObject*>(index.internalPointer());
    return (role == Qt::DisplayRole) ? ((item) ? displayText(item) : "[deleted]") : QVariant();
}

QString ObjectTreeModel::displayText(QObject* object) const
{
    QString text = object->metaObject()->className();
    if (!object->objectName().isEmpty())
	{
		text += QString("[%1]").arg(object->objectName());
	}
	return text;
}

QVariant ObjectTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole)
	{
		return tr("Object");
	}
    return QAbstractItemModel::headerData(section,orientation,role);
}

QObject* ObjectTreeModel::index(ObjPtr object, const QList<QObject*>& items) const
{
    //inline QList<T> findChildren(const QRegularExpression &re, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
    Q_FOREACH(QObject* item, items)
	{
        if (item == object)
			return item;
        QObject* match = index(object, item->children());
		if (match)
			return match;
	}
	return 0;
}

QModelIndex ObjectTreeModel::index(ObjPtr object) const
{
    QObject* item = index(object, m_roots);
    return (item) ? createIndex(indexInParent(item),0,item) : QModelIndex();
}




/*
bool ObjectTreeModel::matches(ObjPtr object, const QString& query) const
{
	return displayText(object).contains(query,Qt::CaseInsensitive);
}

void ObjectTreeModel::search(QList<ObjPtr>* hits, ObjectItem* item, const QString& query) const
{
    ObjPtr object = item;//->object;
	if (!object)
	{
		return;
	}
	if (matches(object,query))
	{
		*hits << object;
	}
    Q_FOREACH(ObjectItem* child, item->children())
	{
		search(hits,child,query);
	}
}

QList<ObjPtr> ObjectTreeModel::search(const QString& query) const
{
	if (query.isEmpty())
	{
        return QList<ObjPtr>();
	}

    QList<ObjPtr> matches;
	Q_FOREACH(ObjectItem* item, m_roots)
	{
		search(&matches,item,query);
	}
	return matches;
}

*/
