#ifndef SHDESIGNER_H
#define SHDESIGNER_H

#include <QMainWindow>
#include <QListWidget>
#include <QTreeWidget>
#include "emainwindow.h"
#include "ui_SH_designer.h"
#include "preferences_window.h"
#include "database_window.h"
#include "plugins_window.h"
#include "help_window.h"
#include "schemeprocess.h"
#include "designer_view.h"
#include "export_to_pdf.h"
#include "block_properties.h"
#include "bus_properties.h"
#include "drawtools.h"
#include "session_manager.h"


class SHDesigner: public EMainWindow, protected Ui::SHDesigner {
	Q_OBJECT
public:
	explicit SHDesigner(QWidget * parent = 0);
	~SHDesigner();
	
	enum Action {
		SchemeNew = 0x20,
		SchemeOpen,
		SchemePropertyChanged,
		BlockPropertyChanged = 0x30,
		BusPropertyChanged = 0x40,
		DecorAdd = 0x50,
		DecorEdit,
		DecorZChanged
	};
	
private:
	void closeEvent(QCloseEvent * );
	void reset(bool full = false);
	bool load(const QString & path);
	bool save(const QString & path);
	void loadingSession(QPIConfig & conf);
	void savingSession(QPIConfig & conf);
	QString loadFilter() {return "SH Scheme(*.shs)";}
	QString saveFilter() {return "SH Scheme(*.shs)";}
	void checkAllowEditDB();
	bool newProject(const QString & name = QString());
	BlockItem * addBlock(int id, int source, QPoint pos);
	
	QPIConfig config;
	SHDesignerPreferences preferences;
	SHDesignerDatabase database;
	SHDesignerPlugins plugins;
	HelpWindow help;
	BlockProperties block_properties;
	BusProperties bus_properties;
	ExportToPDF export_to_pdf;
	DrawTools * draw_tools;
	QString pdf_path, spec_path;
	
private slots:
	void loadScheme_v0(const QByteArray & data);
	void loadScheme(const QByteArray & data);
	void saveScheme(QByteArray & data);
	int  schemeVersion(QDataStream & s);
	void setTitle();
	void applyPreferences();
	void reloadDB();
	void filterBlocks();
	void selectionChanged();
	void generateKey();
	void decorChanged(QGraphicsItem * item) {historyView->addEntry(DecorEdit, 1);}
	void addDecor(QGraphicsItem * item) {historyView->addEntry(DecorAdd, 1);}
	void decorZChanged(QGraphicsItem * item) {historyView->addEntry(DecorZChanged, 1);}
	void editDatabaseBlock(int id);
	void editDatabaseBus(int id);
	void on_actionPreferences_triggered();
	void on_actionEdit_database_triggered() {database.show();}
	void on_actionReload_triggered();
	void on_actionLoaded_Plugins_triggered();
	void on_actionReveal_all_triggered();
	void on_actionEnable_resize_toggled(bool on) {draw_tools->setResizeHandlesEnabled(on);}
	void on_actionContent_triggered() {help.show(); help.raise();}
	void on_actionGenerate_key_triggered() {generateKey();}
	void on_actionGenerate_specification_triggered();
	void on_actionExport_to_PDF_triggered();
	void on_actionShow_project_in_file_manager_triggered();
	void on_actionAbout_SHS_triggered();
	void on_blockView_newBlockRequest(int id, int source, QPoint view_pos);
	void on_blockView_changedScheme();
	void on_blockView_blockDoubleClicked(BlockItem * block);
	void on_blockView_busDoubleClicked(BlockBusItem * bus);
	void on_blockView_schemeAction(BlockItemBase::Action action, QList<QGraphicsItem * > items);
	void on_propertiesProject_propertiesChanged();

public slots:
	void loadFile(const QString & fp) {load(fp);}
	void saveFile(const QString & fp) {save(fp);}

};

#endif // SHDESIGNER_H
