CommonNode.cc 6.05 KB
/*
 * CommonNode.cc
 *
 *  Created on: 30 oct. 2013
 *      Author: CS
 */

#include "CommonNode.hh"
#include "Page.hh"
#include <boost/algorithm/string.hpp>

namespace plot {

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

	xmlChar* value = NULL;

	// -- horizontal margin
	value = xmlGetProp(pNode, (const xmlChar *) "x");
	if (value) {
		element->_xLoadedMargin = atof((const char*) value);
		xmlFree(value);
	}

	// -- vertical margin
	value = xmlGetProp(pNode, (const xmlChar *) "y");
	if (value) {
		element->_yLoadedMargin = atof((const char*) value);
		xmlFree(value);
	}
}

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

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

	// -- font name
	Font labelFont(label->getFont());
	value = xmlGetProp(pNode, (const xmlChar *) "fontName");
	if (value) {
		labelFont.setName((const char*) value);
		xmlFree(value);
	}

	// -- font size
	value = xmlGetProp(pNode, (const xmlChar *) "fontSize");
	if (value) {

		labelFont.setSize(atoi((const char*) value));
		xmlFree(value);
	}

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

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

	label->setFont(labelFont);

	// -- axis color
	value = xmlGetProp(pNode, (const xmlChar *) "color");
	if (value) {
		std::string strValue((const char*) value);
		try {
			createColor(label->_color, strValue);
		} catch (std::exception& e) {
			LOG4CXX_WARN(gLogger, "Label color : " << e.what());
		}
		xmlFree(value);
	}

	// -- axis color map index
	value = xmlGetProp(pNode, (const xmlChar *) "colorMapIndex");
	if (value) {
		label->_color._colorMapIndex = atoi((const char*) value);
		xmlFree(value);
	} else if (label->_color._colorIndex != -1
			&& label->_color._colorMapIndex == -1) {
		LOG4CXX_WARN(gLogger,
				"No default color map is defined, color map 0 will be used");
		label->_color._colorMapIndex = 0;
	}
}


//const Color parseColorAttributes(const xmlNodePtr pNode_, const xmlChar* colorAttributeName_, const xmlChar* colormapAttributName_){
//	xmlChar* value = NULL;
//	std::unique_ptr<Color> pColor( new Color());
//	// color
//	value = xmlGetProp(pNode_, colorAttributeName_);
//	if ( value  ) {
//		std::string strValue((const char*) value);
//
//		try {
//				createColor(*pColor,
//						strValue);
//		} catch (std::exception& e) {
//				LOG4CXX_WARN(gLogger, "Parameter color : " << e.what());
//		}
//	}
//	// colormap
//	value = xmlGetProp(pNode_, colormapAttributName_);
//	if( value ){
//		(*pColor)._colorMapIndex = atoi((const char*) value);
//	} else if ((*pColor)._colorIndex != -1 &&
//			(*pColor)._colorMapIndex	== -1) {
//		LOG4CXX_WARN(gLogger,"No default color map is defined, color map 0 will be used");
//		(*pColor)._colorMapIndex = 0;
//	}
//
//	// cleanup resources
//	if( value ){
//		xmlFree(value);
//	}
//	return  *pColor;
//}
void updateColor(Color& color_, const xmlNodePtr pNode_, const xmlChar* colorTag_, const xmlChar* colorMapTag_){
	xmlChar* value = NULL;
	// gets the tag value for color tag
	value = xmlGetProp(pNode_, colorTag_);
	if ( value  ) {
		std::string strValue((const char*) value);

		try {
				createColor(color_,
						strValue);
		} catch (std::exception& e) {
				LOG4CXX_WARN(gLogger, "Parameter color : " << e.what());
		}
		xmlFree(value);
	}
	// getting color map index :
	value = xmlGetProp(pNode_, colorMapTag_);
	if( value ){
		color_._colorMapIndex = atoi( (const char*)value );
		xmlFree(value);
	}
	// check color map index range :
	if( color_._colorMapIndex < 0 && color_._colorIndex >= 0 ){
		LOG4CXX_WARN(gLogger,
					"No color map is defined, color map 0 will be used");
		color_._colorMapIndex = 0;
	}
}


void createColor(Color& color, std::string& value) {
	// trim value string
	boost::algorithm::trim(value);
	size_t pos = value.find("[");
	try {
		if (pos == std::string::npos) {
			// it is an index
			color._colorIndex = atoi(value.c_str());
			color._red = -1;
			color._green = -1;
			color._blue = -1;
		} else if (pos == 0) {
			// it is a rgb color as [r,g,b]
			// so first remove [ and ] and split string around ,
			std::vector<std::string> rgb = split(
					value.substr(1, value.length() - 2), ',');
			if (rgb.size() != 3) {
				throw std::exception();
			}
			color._colorIndex = -1;
			color._red = atoi(rgb[0].c_str());
			color._green = atoi(rgb[1].c_str());
			color._blue = atoi(rgb[2].c_str());
		} else {
			throw std::exception();
		}
	} catch (std::exception& e) {
		throw std::logic_error(
				"Color attribute value must be '[r,g,b]' where r,g,b are integers or 'index' where index is an interger");
	}
}

std::vector<std::string> &split(const std::string& s, char delim,
		std::vector<std::string>& elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

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

// Extract margins given as the following string : "[A,B]"
void extractMargins (const char *margins, int *margin1, int *margin2) {
	std::string marginsStr(margins);

	// Remove [, ] and space characters
	boost::erase_all (marginsStr, " ");
	boost::erase_all (marginsStr, "[");
	boost::erase_all (marginsStr, "]");

	// Extract values separated by a comma
	std::vector<std::string> marginList = split (marginsStr, ',');

	if (marginList.size() == 2) {
		*margin1 = atoi (marginList.at (0).c_str());
		*margin2 = atoi (marginList.at (1).c_str());
	} else {
		*margin1 = -1;
		*margin2 = -1;
	}
}

} /* namespace plot */