/***************************** LICENSE START ***********************************

 Copyright 2012 ECMWF and INPE. This software is distributed under the terms
 of the Apache License version 2.0. In applying this license, ECMWF does not
 waive the privileges and immunities granted to it by virtue of its status as
 an Intergovernmental Organization or submit itself to any jurisdiction.

 ***************************** LICENSE END *************************************/

#include "SimpleEditor.h"

#include <QHBoxLayout>
#include <QPainter>
#include <QScrollArea>

#include "EditorFactory.h"
#include "IconObject.h"
//#include "Log.h"

#include "IconClass.h"
#include "IconFactory.h"
#include "RequestPanel.h"

#include "MvQRequestPanelWidget.h"


SimpleEditor::SimpleEditor(const IconClass& kind,const string& name):
	MvQEditor(kind,name)
{
 	//Panel	
	panelArea_=new QScrollArea(this);
	panelArea_->setWidgetResizable(true);
	
	centralLayout_->addWidget(panelArea_);
	
	vector<string> classes;
	classes.push_back(kind.name());
	
	MvQRequestPanelWidget* w=new MvQRequestPanelWidget(classes,this);
	panel_=new RequestPanel(kind,w,this);
	
	panelArea_->setWidget(w);
	
	connect(w,SIGNAL(iconDropped(IconObject*)),
		 this,SLOT(slotIconDropped(IconObject*)));
}

SimpleEditor::~SimpleEditor()
{
	delete panel_;   
}


IconObject* SimpleEditor::copy(const string& /*name*/)
{
	//IconObject* o = IconFactory::create(name, class_);
	//panel_.apply();
	//o->request(panel_.request());
	//return o;
	return 0;
}

void SimpleEditor::edit()
{  
	if(!current_)
	  	return;
  
	MvQEditor::edit();
}


void SimpleEditor::apply()
{
	panel_->apply();
	current_->request(panel_->request());	
}

void SimpleEditor::reset()
{
	panel_->reset(current_);
	
	if(current_ && current_->locked())
	{
	  	if(QWidget *w=panelArea_->widget())	  
	  		w->setEnabled(false);
	}
}

void SimpleEditor::close()
{
	panel_->close();
	//Enable again the widget
	if(QWidget *w=panelArea_->widget())
		w->setEnabled(true);
}

//Request SimpleEditor::currentRequest(long custom_expand)
//{
	//panel_.apply();
	//return custom_expand ? panel_.request(custom_expand) : panel_.request();
//}

void SimpleEditor::replace(IconObjectH obj)
{
   	panel_->replace(obj);
}

void SimpleEditor::merge(IconObjectH obj)
{
	panel_->merge(obj);
}

void SimpleEditor::slotIconDropped(IconObject* obj)
{	
	merge(obj);	 
}  


static EditorMaker<SimpleEditor> editorMaker1("SimpleEditor");
static EditorMaker<SimpleEditor> editorMaker2("RetrieveEditor");

