#ifndef DATABASEDEVICES_H
#define DATABASEDEVICES_H

#include <QMenu>
#include <QDoubleSpinBox>
#include <QStyledItemDelegate>
#include <QFontDialog>
#include <colorbutton.h>
#include <drawtools.h>
#include "model_delegates.h"
#include "core.h"
#include "ui_database_devices.h"

class QToolBar;


class DatabaseDevices: public QWidget, public Ui::DatabaseDevices {
	Q_OBJECT
public:
	explicit DatabaseDevices(QWidget * parent = 0);
	~DatabaseDevices();

	void resaveAll();

	bool ask_for_save;
	
private:
	bool eventFilter(QObject * o, QEvent * e);
	void reloadPluginsByType(QComboBox * combo, SHPluginBase::PluginType type);
	void reloadTable(QComboBox * combo, QString table);
	void setPluginByName(QComboBox * combo, QString plugin);
	QStringList pinsNames(QTreeWidgetItem * self = 0);
	QSqlQuery updateQuery(int id);
	
	QMenu menu;
	QSqlQueryModel model;
	BlockItem blockitem;
	QToolBar * bar;
	PinBusDelegate * bus_delegate;
	PinPluginDelegate * pin_plugin_delegate;
	QSqlQuery prev_query;
	QMap<int, QTreeWidgetItem*> pin_tli;
	int cur_id, prev_id;
	
public slots:
	void reload();
	void reloadPlugins();
	void showID(int id);

private slots:
	void popupMenu(QPoint p);
	void updateBlock();
	void arrangePins();
	void filterList();
	void addItem(QGraphicsItem * item);
	void selectionChanged();
	void treeSelectionChanged();
	void treePinsSelectionChanged();
	void treePinsClear();
	void selectLastDevice();
	void treeClicked(const QModelIndex & index);
	void on_actionAddDevice_triggered();
	void on_actionRemoveDevice_triggered();
	void on_actionCopyDevice_triggered();
	void on_actionReset_triggered();
	void on_actionRemove_items_triggered();
	void on_buttonReset_clicked();
	void on_buttonCommit_clicked();
	void on_buttonPinAdd_clicked();
	void on_buttonPinDup_clicked();
	void on_buttonPinDelete_clicked();
	void on_buttonPinClear_clicked();
	void on_treePins_currentItemChanged(QTreeWidgetItem * current, QTreeWidgetItem * previous);
	void on_treePins_itemChanged(QTreeWidgetItem * item, int column);
	void on_propertiesPins_propertiesChanged();
	
};

#endif // DATABASEDEVICES_H
