/*
    QAD - Qt ADvanced

    Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef BLOCKEDITOR_H
#define BLOCKEDITOR_H

#include "blockitem.h"
#include "qad_blockview_export.h"

#include <QStyledItemDelegate>
#include <QTreeWidgetItem>
#include <QWidget>


namespace Ui {
class BlockEditor;
}


class QAD_BLOCKVIEW_EXPORT BlockEditor: public QWidget {
	Q_OBJECT
	Q_PROPERTY(bool editorMode READ editorMode WRITE setEditorMode)
	Q_PROPERTY(bool pinsEditable READ pinsEditable WRITE setPinsEditable)

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


	bool editorMode() const { return m_editorMode; }
	bool pinsEditable() const { return m_pinsEditable; }

public slots:
	void loadFile(QString path);
	void loadModel(const QByteArray & model);
	QByteArray saveModel();
	void setEditorMode(bool editorMode);
	void setPinsEditable(bool pinsEditable);

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);

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

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


class QAD_BLOCKVIEW_EXPORT 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 QAD_BLOCKVIEW_EXPORT 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
