CommonNode.hh 5.75 KB
/*
 * CommonNode.hh
 *
 *  Created on: 30 oct. 2013
 *      Author: CS
 */

#ifndef COMMONNODE_HH_
#define COMMONNODE_HH_

#include "NodeCfg.hh"
#include "PlotLogger.hh"
#include "PlotCommon.hh"
#include "Font.hh"
#include "Color.hh"
#include "Label.hh"

namespace plot {
/**
 * Exception to raise when plot xml parsing fails.
 */
struct PlotNodeException: AMDA::AMDA_exception {
};

class LabelNode: public AMDA::XMLConfigurator::NodeCfg {
public:
	LabelNode() :
			AMDA::XMLConfigurator::NodeCfg() {
	}
	virtual ~LabelNode() {
	}

	void proceed(xmlNodePtr pNode,
			const AMDA::Parameters::CfgContext& pContext);
};


template<class T>
class TitleNode: public LabelNode {
public:
	TitleNode() :
			LabelNode() {
	}
	virtual ~TitleNode() {
	}

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

		xmlChar* value = NULL;

		// -- horizontal margin
		value = xmlGetProp(pNode, (const xmlChar *) "position");
		if (value) {
			PlotCommon::setPosition((const char*) value,
					element->_titlePosition);
			xmlFree(value);
		}

		// -- vertical margin
		value = xmlGetProp(pNode, (const xmlChar *) "align");
		if (value) {
			PlotCommon::setTitleAlign((const char*) value,
					element->_titleAlign);
			xmlFree(value);
		}

		if (pNode->children) {
			element->setTitleText((const char*) pNode->children->content);
		}

		AMDA::Parameters::CfgContext context;
		context.push<Label*>(element->getTitle());
		LabelNode::proceed(pNode, context);
	}
};

/*
 template<class T>
 class ColorNode: public AMDA::XMLConfigurator::NodeCfg {
 public:
 ColorNode() :
 NodeCfg() {
 }
 virtual ~ColorNode() {
 }

 void getColor(xmlNodePtr pNode, Color& color) {
 xmlChar* value = NULL;

 // -- color map index
 value = xmlGetProp(pNode, (const xmlChar *) "colorMapIndex");
 if (value) {
 color._colorMapIndex = atoi((const char*) value);
 } else {
 color._colorMapIndex = -1;
 }
 // -- color Index
 value = xmlGetProp(pNode, (const xmlChar *) "colorIndex");
 if (value) {
 color._colorIndex = atoi((const char*) value);
 } else {
 color._colorIndex = -1;
 }
 // -- red
 value = xmlGetProp(pNode, (const xmlChar *) "red");
 if (value) {
 color._red = atoi((const char*) value);
 } else {
 color._red = -1;
 }
 // -- green
 value = xmlGetProp(pNode, (const xmlChar *) "green");
 if (value) {
 color._green = atoi((const char*) value);
 } else {
 color._green = -1;
 }
 // -- blue
 value = xmlGetProp(pNode, (const xmlChar *) "blue");
 if (value) {
 color._blue = atoi((const char*) value);
 } else {
 color._blue = -1;
 }

 if (value) {
 xmlFree(value);
 }
 }
 };*/

/*template<class T>
 class BackgroundColorNode: public ColorNode<T> {
 public:
 BackgroundColorNode() :
 ColorNode<T>() {
 }
 virtual ~BackgroundColorNode() {
 }

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

 this->getColor(pNode, element->_backgroundColor);

 }
 };*/

template<class T>
class FontNode: public AMDA::XMLConfigurator::NodeCfg {
public:
	FontNode() :
			NodeCfg() {
	}
	virtual ~FontNode() {
	}

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

		Font elementFont(element->getFont());

		// -- name
		value = xmlGetProp(pNode, (const xmlChar *) "name");
		if (value) {
			elementFont.setName((const char*) value);
			xmlFree(value);
		}

		// -- size
		value = xmlGetProp(pNode, (const xmlChar *) "size");
		if (value) {
			elementFont.setSize(atoi((const char*) value));
			xmlFree(value);
		}

		// -- style
		value = xmlGetProp(pNode, (const xmlChar *) "style");
		if (value) {
			elementFont.setStyle(Font::getStyleByStr((const char*) value));
			xmlFree(value);
		}

		// -- weight
		value = xmlGetProp(pNode, (const xmlChar *) "weight");
		if (value) {
			elementFont.setWeight(Font::getWeightByStr((const char*) value));
			xmlFree(value);
		}

		element->setFont(elementFont);
	}
};

class MarginNode: public AMDA::XMLConfigurator::NodeCfg {
public:
	MarginNode() :
			NodeCfg() {
	}
	virtual ~MarginNode() {
	}

	void proceed(xmlNodePtr pNode,
			const AMDA::Parameters::CfgContext& pContext);
};

//const Color parseColorAttributes(const xmlNodePtr pNode_, const xmlChar* colorAttributeName_, const xmlChar* colormapAttributName_);
/**
 * update Color object, according to attributes of  the given xml node.
 * Method handles two different format for color attribute : [r,g,b] or index value in a color map.
 * It also handles an attribute for identifying the color map to use (its an index pointing to the
 * list of available color maps).
 * The two format are mutually exclusive : updating a color with [r,g,b] format will automatically
 * set color index to -1; respectively, setting a zero or positive number for color
 * index will imply setting -1 for r,g,b part of the color object.
 * Color map index should be a zero or positive integer, but no control is done in this method.
 * @param color_ the color to update.
 * @param pNode_ xml node containing color informations.
 * @param colorTag_ color attribute. Can be either a [r,g,b] format or an int format (indicating an index into a color map).
 * @param colorMapTag_ color map index. Index of the color map to use.
 */
void updateColor(Color& color_, const xmlNodePtr pNode_, const xmlChar* colorTag_, const xmlChar* colorMapTag_);

void createColor(Color& color, std::string& value);

std::vector<std::string> &split(const std::string &s, char delim,
		std::vector<std::string> &elems);

std::vector<std::string> split(const std::string &s, char delim);

void extractMargins (const char *margins, int *margin1, int *margin2);


} /* namespace plot */
#endif /* COMMONNODE_HH_ */