AdditionalObjectsNode.hh 7.87 KB
/*
 * AdditionalObjectsNode.hh
 *
 *  Created on: 1/07/2014
 *      Author: AKKA
 */

#ifndef ADDITIONALOBJECTSNODE_HH_
#define ADDITIONALOBJECTSNODE_HH_

#include <cstdlib>

#include "NodeCfg.hh"
#include "Constant.hh"
#include "PlotLogger.hh"
#include "CommonNode.hh"
#include "TextPlot.hh"
#include "CurvePlot.hh"
#include "CurveFunction/CurveFunctionWriter.hh"

namespace plot {

/*
 * @brief Text plot node
 */
class TextPlotNode: public AMDA::XMLConfigurator::NodeGrpCfg {
public:
	TextPlotNode() :
			AMDA::XMLConfigurator::NodeGrpCfg() {
		getChildList()["font"] = AMDA::XMLConfigurator::NodeCfgSPtr(
				new FontNode<TextPlot>());
	}

	virtual ~TextPlotNode() {
	}

	void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pCtx) {
		LOG4CXX_DEBUG(gLogger, "TextPlotNode::proceed");

		Panel* panel = pCtx.get<Panel*>();

		boost::shared_ptr<TextPlot> textPlot (new TextPlot());

		xmlChar* value = NULL;

		// -- text
		if ((value = xmlGetProp(pNode, (const xmlChar *) "text"))) {
			textPlot->_text = (const char *) value;
			xmlFree(value);
		}

		// -- x
		if ((value = xmlGetProp(pNode, (const xmlChar *) "x"))) {
			textPlot->_x = (const char *) value;
			xmlFree(value);
		}

		// -- y
		if ((value = xmlGetProp(pNode, (const xmlChar *) "y"))) {
			textPlot->_y = (const char *) value;
			xmlFree(value);
		}

		// -- angle
		if ((value = xmlGetProp(pNode, (const xmlChar *) "angle"))) {
			textPlot->_angle = std::stod((const char *) value);
			xmlFree(value);
		}

		// -- align
		if ((value = xmlGetProp(pNode, (const xmlChar *) "align"))) {
			if (strcmp ((const char *) value, "left") == 0)
				textPlot->_align = 0;
			else if (strcmp ((const char *) value, "center") == 0)
				textPlot->_align = 0.5;
			else
				textPlot->_align = 1.0;
			xmlFree(value);
		}

		// -- color
		Color color;
		value = xmlGetProp(pNode, (const xmlChar *) "color");
		if (value) {
			try {
				std::string strValue((const char*) value);
				createColor(color, strValue);
			} catch (std::logic_error& e) {
				LOG4CXX_WARN(gLogger, "TextPlot Color : " << e.what());
				color =
						DefaultPlotConfiguration::getInstance()._defaultPanel._backgroundColor;
			}
			xmlFree(value);
		} else {
			color =
					DefaultPlotConfiguration::getInstance()._defaultPanel._backgroundColor;
		}
		textPlot->_color = color;

		panel->_textPlots.push_back(textPlot);

		AMDA::Parameters::CfgContext context;
		context.push<TextPlot *> (textPlot.get());
		AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context);
	}


};

/*
 * @brief Curve function attribute node
 */
class CurveFunctionAttributeNode: public AMDA::XMLConfigurator::NodeCfg {
public:
	CurveFunctionAttributeNode() :
			NodeCfg() {
	}
	virtual ~CurveFunctionAttributeNode() {
	}

	void proceed(xmlNodePtr pNode,
			const AMDA::Parameters::CfgContext& pContext) {
		LOG4CXX_DEBUG(gLogger, "CurveFunctionAttributeNode::proceed");
		CurveFunctionWriter* curveFunction = pContext.get<CurveFunctionWriter*>();

		xmlChar* value = NULL;
		// -- name
		std::string attName = "";
		if ((value = xmlGetProp(pNode, (const xmlChar *) "name"))) {
			attName = (const char*) value;
			xmlFree(value);
		}

		if (attName.empty())
		{
			LOG4CXX_DEBUG(gLogger, "CurveFunctionAttributeNode::proceed - Attribute don't have a name => skip");
			return;
		}

		// -- value
		double attValue = 0.;
		if ((value = xmlGetProp(pNode, (const xmlChar *) "value"))) {
			attValue = std::atof((const char*)value);
			xmlFree(value);
		}
		else
		{
			LOG4CXX_DEBUG(gLogger, "CurveFunctionAttributeNode::proceed - Attribute " << attName << "don't have a value => set to 0.");
		}

		curveFunction->addAttribute(attName,attValue);
	}
};

/*
 * @brief Curve function attributes (ie list of attribute) node
 */
class CurveFunctionAttributesNode: public AMDA::XMLConfigurator::NodeGrpCfg {
public:
	CurveFunctionAttributesNode() :
		NodeGrpCfg() {
		getChildList()["attribute"] = AMDA::XMLConfigurator::NodeCfgSPtr(
				new CurveFunctionAttributeNode());
	}

	virtual ~CurveFunctionAttributesNode() {
	}

	void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
		LOG4CXX_DEBUG(gLogger, "CurveFunctionAttributesNode::proceed");
		CurveFunctionWriter* curveFunction = pContext.get<CurveFunctionWriter*>();

		AMDA::Parameters::CfgContext context;
		context.push<CurveFunctionWriter *> (curveFunction);
		AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context);
	}
};

/*
 * @brief Curve function node
 */
class CurveFunctionNode: public AMDA::XMLConfigurator::NodeGrpCfg {
public:
	CurveFunctionNode() :
		NodeGrpCfg() {
		getChildList()["attributes"] = AMDA::XMLConfigurator::NodeCfgSPtr(
				new CurveFunctionAttributesNode());
	}

	virtual ~CurveFunctionNode() {
	}

	void proceed(xmlNodePtr pNode,
			const AMDA::Parameters::CfgContext& pContext) {
		LOG4CXX_DEBUG(gLogger, "CurveFunctionNode::proceed");
		CurvePlot* curvePlot = pContext.get<CurvePlot*>();
		xmlChar* value = NULL;

		// -- name
		std::string functionName;
		if ((value = xmlGetProp(pNode, (const xmlChar *) "name"))) {
			functionName = (const char*) value;
			xmlFree(value);
		}
		else
		{
			ERROR_EXCEPTION(
				AMDA::XMLConfigurator::ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@name")
		}

		std::shared_ptr<CurveFunctionWriter> curveFunction (new CurveFunctionWriter());
		curveFunction->setFunctionName(functionName);

		curvePlot->_curveFunction = curveFunction;

		AMDA::Parameters::CfgContext context;
		context.push<CurveFunctionWriter *> (curveFunction.get());
		AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context);
	}
};

/*
 * @brief Curve plot node
 */
class CurvePlotNode: public AMDA::XMLConfigurator::NodeGrpCfg {
public:
	CurvePlotNode() :
			AMDA::XMLConfigurator::NodeGrpCfg() {
		getChildList()["function"] = AMDA::XMLConfigurator::NodeCfgSPtr(
				new CurveFunctionNode());
	}

	virtual ~CurvePlotNode() {
	}

	void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pCtx) {
		LOG4CXX_DEBUG(gLogger, "CurvePlotNode::proceed");
		Panel* panel = pCtx.get<Panel*>();

		boost::shared_ptr<CurvePlot> curvePlot (new CurvePlot());

		xmlChar* value = NULL;

		// -- serieId
		if ((value = xmlGetProp(pNode, (const xmlChar *) "serieId"))) {
			curvePlot->_serieId = atoi((const char*)value);
			LOG4CXX_DEBUG(gLogger, "CurvePlotNode::proceed - curvePlot->_serieId " << curvePlot->_serieId);
			xmlFree(value);
		}

		// -- style
		if((value = xmlGetProp(pNode, (const xmlChar *)"style"))) {
			try{
				curvePlot->_style = stoLineStyle.at(std::string((const char*)value));
			}
			catch( std::out_of_range& err ){
				std::ostringstream msg {};
				msg << "CurvePlotNode::proceed invalid LineStyle value:" << value <<". Using default value.";
				LOG4CXX_DEBUG(gLogger, msg.str());
			}
			xmlFree(value);
		}

		// -- width
		if((value = xmlGetProp(pNode, (const xmlChar *)"width"))) {
			curvePlot->_width = atoi((const char*)value);
			xmlFree(value);
		}

		// -- color attributes :
		updateColor(curvePlot->_color, pNode, (const xmlChar *)"color", (const xmlChar *)"colorMapIndex");

		panel->_curvePlots.push_back(curvePlot);

		AMDA::Parameters::CfgContext context;
		context.push<CurvePlot *> (curvePlot.get());
		AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context);
	}
};

/*
 * @brief Additional object node
 */
class AdditionalObjectsNode: public AMDA::XMLConfigurator::NodeGrpCfg {
public:
	AdditionalObjectsNode() :
			AMDA::XMLConfigurator::NodeGrpCfg() {
		getChildList()["textPlot"] = AMDA::XMLConfigurator::NodeCfgSPtr(
				new TextPlotNode());
		getChildList()["curvePlot"] = AMDA::XMLConfigurator::NodeCfgSPtr(
				new CurvePlotNode());
	}
	virtual ~AdditionalObjectsNode() {
	}

	void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pCtx) {
		LOG4CXX_DEBUG(gLogger, "AdditionalObjectsNode::proceed");
		Panel* panel = pCtx.get<Panel*>();

		AMDA::Parameters::CfgContext context;
		context.push<Panel *> (panel);
		AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context);
	}
};

} /* namespace plot */
#endif /* ADDITIONALOBJECTSNODE_HH_ */