#include <QtPlugin>
#include <QDesignerFormEditorInterface>
#include <QDesignerPropertyEditorInterface>
#include <QtPlugin>
#include <QEvent>
#include <QExtensionManager>
#include <QDesignerFormEditorInterface>
#include <QDesignerFormWindowInterface>
#include <QDesignerFormWindowManagerInterface>
#include <QDesignerFormWindowCursorInterface>
#include <QBoxLayout>
#include <QInputDialog>
#include <QDialogButtonBox>
#include <QAction>
#include <QDialog>
#include <QGroupBox>
#include <QMessageBox>
#include <QDir>
#include "plugin_button.h"
#include "ui_buttonmenuextension.h"



SHSButtonMenuExtension::SHSButtonMenuExtension(SHSButton * widget_, QObject * ): QDialog(), ui(new Ui::SHSButtonMenuExtension) {
	ui->setupUi(this);
	ui->treeObjects->setFilterButtonsVisible(false);
	ui->treeObjects->setPropertiesVisible(false);
	ui->treeObjects->setEventsVisible(false);
	ui->treeObjects->create(RUNTIME->server_data.blocks);
	widget = widget_;
	icon_command = QIcon(":/icons/code-function.png");
	action_default = new QAction("Assign command ...", this);
	connect(action_default, SIGNAL(triggered(bool)), this, SLOT(action_default_triggered()));
	//connect(table.tableView(), SIGNAL(doubleClicked(QModelIndex)), &dialog, SLOT(accept()));
	connect(this, SIGNAL(accepted()), this, SLOT(select_ok()));
}


SHSButtonMenuExtension::~SHSButtonMenuExtension() {
	delete ui;
}


QAction * SHSButtonMenuExtension::preferredEditAction() const {
	return action_default;
}


QList<QAction * > SHSButtonMenuExtension::taskActions() const {
	return (QList<QAction * >() << action_default);
}


void SHSButtonMenuExtension::on_treeObjects_commandDoubleClicked(SHS::Command cmd) {
	cur_cmd = cmd;
	accept();
}


void SHSButtonMenuExtension::on_treeObjects_currentCommandChanged(SHS::Command cmd) {
	cur_cmd = cmd;
}


void SHSButtonMenuExtension::action_default_triggered() {
	exec();
}


void SHSButtonMenuExtension::select_ok() {
	QStringList user_args;
	foreach (QString a, cur_cmd.user_arguments) {
		QString na = QInputDialog::getText(this, tr("Input argument"), tr("Input value for argument \"%1\":").arg(a));
		if (na.isEmpty()) return;
		if (na.toDouble() != 0) na.replace(",", ".");
		user_args << na;
	}
	QString cmd = QString("%1.%2(%3)").arg(cur_cmd.object_path.join("."), cur_cmd.name, user_args.join(","));
	QDesignerFormWindowInterface * formWindow = QDesignerFormWindowInterface::findFormWindow(widget);
	if (formWindow != 0) {
		formWindow->cursor()->setProperty("command", cmd);
	} else {
		widget->setCommand(cmd);
	}
}




QObject * SHSButtonTaskMenuFactory::createExtension(QObject * object, const QString & iid, QObject * parent) const {
	if (iid != Q_TYPEID(QDesignerTaskMenuExtension))
		return 0;
	if (SHSButton * w = qobject_cast<SHSButton * >(object))
		return new SHSButtonMenuExtension(w, parent);
	return 0;
}




SHSButtonPlugin::SHSButtonPlugin(QObject * parent): QObject(parent) {
	m_initialized = m_designer = false;
}


void SHSButtonPlugin::initialize(QDesignerFormEditorInterface * core) {
	m_designer = true;
	if (m_initialized)
		return;
	//RUNTIME->commandsDB.openFromArgs("-db=");
	RUNTIME->openFromCmdFile();
	QExtensionManager * manager = core->extensionManager();
	Q_ASSERT(manager != 0);
	manager->registerExtensions(new SHSButtonTaskMenuFactory(manager), Q_TYPEID(QDesignerTaskMenuExtension));
	m_initialized = true;
}


bool SHSButtonPlugin::isInitialized() const {
	return m_initialized;
}


QWidget * SHSButtonPlugin::createWidget(QWidget * parent) {
	SHSButton * w = new SHSButton(parent);
	if (m_designer)
		w->buildDebug();
	return w;
}


QString SHSButtonPlugin::name() const {
	return QLatin1String("SHSButton");
}


QString SHSButtonPlugin::group() const {
	return QLatin1String("SHS Widgets");
}


QIcon SHSButtonPlugin::icon() const {
	return QIcon(":/icons/touchbutton.png");
}


QString SHSButtonPlugin::toolTip() const {
	return QLatin1String("");
}


QString SHSButtonPlugin::whatsThis() const {
	return QLatin1String("");
}


bool SHSButtonPlugin::isContainer() const {
	return false;
}


QString SHSButtonPlugin::domXml() const {
	return QLatin1String("<widget class=\"SHSButton\" name=\"button\">\n</widget>\n");
}


QString SHSButtonPlugin::includeFile() const {
	return QLatin1String("button.h");
}

