#ifndef BLOCKEDITOR_H
#define BLOCKEDITOR_H

#include <QWidget>
#include <QTreeWidgetItem>
#include <QStyledItemDelegate>
#include "blockitem.h"


namespace Ui {
class BlockEditor;
}


class BlockEditor : public QWidget
{
	Q_OBJECT

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


public slots:
	void loadFile(QString path);
	void loadModel(const QByteArray & model);
	QByteArray saveModel();

private slots:
	void selectionChanged();
	void addItem(QGraphicsItem * item);
	void updateBlock();
	void treePinsClear();
	void arrangePins();
	void on_actionRemove_items_triggered();
	void on_buttonSave_clicked();
	void on_buttonSaveAs_clicked();
	void on_buttonLoad_clicked();
	void on_buttonClear_clicked();
	void on_buttonPinAdd_clicked();
	void on_buttonPinDup_clicked();
	void on_buttonPinDelete_clicked();
	void on_buttonPinClear_clicked();
	void on_treePins_itemChanged(QTreeWidgetItem *item, int column);
//	void on_treePins_itemSelectionChanged();

private:
	bool eventFilter(QObject * o, QEvent * e);

	Ui::BlockEditor *ui;
	QMap<int, QTreeWidgetItem*> pin_tli;
	BlockItem block;
	QString src_title, cur_file;
	bool init;
};



class PinAlignDelegate: public QStyledItemDelegate {
	Q_OBJECT
public:
	PinAlignDelegate(QObject * parent = 0): QStyledItemDelegate(parent) {}
	QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const;
	QString displayText(const QVariant & value, const QLocale & locale) const;
	void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
	QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const {return QSize(60, 26);}
};


class PinBusDelegate: public QStyledItemDelegate {
	Q_OBJECT
public:
	PinBusDelegate(QObject * parent = 0): QStyledItemDelegate(parent) {}
	QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const;
	QString displayText(const QVariant & value, const QLocale & locale) const;
	void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
	QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const {return QSize(60, 26);}
private:
	typedef QPair<int, QString> ISPair;
	QVector<ISPair> buses;
};

#endif // BLOCKEDITOR_H
