#include "block_editor_dialog.h"
#include "SH_plugin_widgets.h"


BlockEditorDialog::BlockEditorDialog(QWidget * parent): QDialog(parent), Ui::BlockEditorDialog() {
	setupUi(this);
	setWindowFlag(Qt::WindowMaximizeButtonHint);
	editor = 0;
	was_opened = false;
}


BlockEditorDialog::~BlockEditorDialog() {
}


void BlockEditorDialog::show(SHDesignerCore::Editor * e) {
	if (!e) return;
	editor = e;
	if (was_opened) widgetDevice->open();
	layoutEditor->addWidget(e->center);
	foreach (auto * m, e->menus) {
		m->setIcon(QIcon());
		menuBar->addAction(m);
	}
	e->center->show();
	e->base->loadSession();
	exec();
	e->base->saveSession();
	e->center->hide();
	layoutEditor->removeWidget(e->center);
	foreach (auto * m, e->menus)
		menuBar->removeAction(m);
	was_opened = widgetDevice->isOpened() && widgetDevice->isLocalDevice();
	widgetDevice->close();
	editor = 0;
}


void BlockEditorDialog::on_widgetDevice_opened() {
	if (editor) editor->base->deviceOpened();
}


void BlockEditorDialog::on_widgetDevice_closed() {
	if (editor) editor->base->deviceClosed();
}


void BlockEditorDialog::on_widgetDevice_readed(QByteArray data) {
	if (editor) editor->base->deviceReaded(data);
}
