#ifndef QCD_KMODEL_H
#define QCD_KMODEL_H

#include <QAbstractItemModel>
#include <QItemDelegate>
#include <QStyledItemDelegate>
#include "pistring.h"

namespace CDUtils {
class CDSection;
class CDType;
}
namespace QAD {
struct Enum;
}
class CDKItemModel;

class CDKItem {
	friend class CDKItemModel;
public:
	enum CDKItemType{ItemCDType, ItemCDSection};
	CDKItem(int index, CDKItemType type, CDKItem * parent);
	~CDKItem();
	QVariant data(int column, int role) const;
	QVariant value(CDUtils::CDType t, int role) const;
	bool setData(int column, const QVariant & value);

private:
	PIDeque<int> buildPath() const;
	QString stringType(const PIString & t) const;

	CDKItem * parent_;
	int index_;
	CDKItemType type_;
	QList<CDKItem *> childs;
};


class CDKDelegate : public QStyledItemDelegate
{
	Q_OBJECT
public:
	CDKDelegate(QObject *parent = 0);

	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
	void setEditorData(QWidget *editor, const QModelIndex &index) const;
	void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
	void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
	QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
};


class CDKItemModel : public QAbstractItemModel {
	Q_OBJECT
public:
	explicit CDKItemModel(QObject *parent = 0);
	~CDKItemModel();

	QVariant data(const QModelIndex &index, int role) const;
	QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
	QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
	QModelIndex parent(const QModelIndex &index) const;
	int rowCount(const QModelIndex &parent = QModelIndex()) const;
	int columnCount(const QModelIndex &parent = QModelIndex()) const;
	Qt::ItemFlags flags(const QModelIndex &index) const;
	bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);

	void buildItem(CDKItem * it, CDUtils::CDSection &r);

public slots:
	void rebuildModel();
	void updateModel();

private:
	void internalRebuild();
	CDKItem * getItem(const QModelIndex & index) const;

	CDKItem * root;

signals:

};

#endif // QCD_KMODEL_H
