#pragma once

#include <QtCore/QAbstractItemModel>
#include <QtCore/QWeakPointer>
#include <QtCore/QPointer>


class TrackedObject
{
public:
    TrackedObject() {}
    TrackedObject(QObject* a_obj) : m_object(a_obj) {}
    operator bool() { return !m_object.isNull(); }
    operator QObject*() { return m_object.data(); }
private:
    QPointer<QObject> m_object;
};


typedef QObject*  ObjPtr;
//typedef TrackedObject  ObjPtr;


class ObjectTreeModel : public QAbstractItemModel
{
	Q_OBJECT
public:
    ObjectTreeModel(QObject* parent);
    //void setFilter(ObjectFilter *filter);
    //QList<ObjPtr> search(const QString& query) const;
    void setRootObjects(const QList<ObjPtr>& roots);
    QModelIndex index(ObjPtr object) const;
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    virtual int columnCount(const QModelIndex& parent) const;
    virtual int rowCount(const QModelIndex& parent) const;
    virtual QModelIndex index(int row, int column, const QModelIndex& parent) const;
    virtual QModelIndex parent(const QModelIndex& index) const;
    virtual QVariant data(const QModelIndex& index, int role) const;
private:
    int indexInParent(QObject* item) const;
    QObject* index(ObjPtr object, const QList<QObject*>& items) const;
    //bool matches(ObjPtr object, const QString& query) const;
    //void search(QList<ObjPtr>* matches, ObjectItem* item, const QString& query) const;
    QString displayText(QObject* object) const;
    QList<QObject*> m_roots;
};

