#ifndef PROPERTYEDITOR_H
#define PROPERTYEDITOR_H

#include <QDebug>
#include <QEvent>
#include <QHeaderView>
#include <QMetaProperty>
#include <QStyledItemDelegate>
#include <QTreeWidget>
#include <qpiconfigwidget.h>


class Delegate: public QStyledItemDelegate {
	Q_OBJECT

public:
	Delegate(QObject * parent = 0): QStyledItemDelegate() { ab = QBrush(QImage(":/icons/alpha.png")); }

	QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const {
		cmi = const_cast<QModelIndex &>(index);
		return widgetForProperty(parent, index);
	}
	void setEditorData(QWidget * editor, const QModelIndex & index) const { setWidgetProperty(editor, index.data(Qt::UserRole)); }
	void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
	void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const {
		editor->setGeometry(option.rect);
	}
	void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;

private:
	QWidget * widgetForProperty(QWidget * parent, const QModelIndex & index) const;
	void setWidgetProperty(QWidget * w, const QVariant & value) const;
	const QVariant widgetProperty(QWidget * w) const;
	QString pointString(const QPoint & p) const { return QString::number(p.x()) + " x " + QString::number(p.y()); }
	QString pointString(const QPointF & p) const { return QString::number(p.x()) + " x " + QString::number(p.y()); }
	QString rectString(const QRect & r) const {
		return QString::number(r.x()) + " x " + QString::number(r.y()) + " : " + QString::number(r.width()) + " x " +
		       QString::number(r.height());
	}
	QString rectString(const QRectF & r) const {
		return QString::number(r.x()) + " x " + QString::number(r.y()) + " : " + QString::number(r.width()) + " x " +
		       QString::number(r.height());
	}

	QBrush ab;
	mutable QModelIndex cmi;

private slots:
	void changed() { setModelData((QWidget *)sender(), const_cast<QAbstractItemModel *>(cmi.model()), cmi); }
	void changedFlag();
};

typedef QPair<QMetaProperty, QVariant> PropertyValuePair;
Q_DECLARE_METATYPE(PropertyValuePair)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Q_DECLARE_METATYPE(QMetaProperty)
#endif


class PropertyEditor: public QTreeWidget {
	Q_OBJECT

public:
	explicit PropertyEditor(QWidget * parent = 0);
	virtual ~PropertyEditor();

	void assignObject(QObject * o) {
		object = o;
		rebuild();
	}

protected:
	void changeEvent(QEvent * e);

private:
	void configTree();
	void setItemBackColor(QTreeWidgetItem * i, const QColor & c) {
		i->setBackground(0, c);
		i->setBackground(1, c);
	}
	void setItemForeColor(QTreeWidgetItem * i, const QColor & c) {
		i->setForeground(0, c);
		i->setForeground(1, c);
	}
	void rebuild();
	void refresh();

	QObject * object;
	QFont font_b;
	QList<QMetaProperty> props;
	bool active_;

private slots:
	void itemClicked(QTreeWidgetItem * item, int column);
	void itemChanged(QTreeWidgetItem * item, int column);
};

#endif // PROPERTYEDITOR_H
