Blame view

src/ParamOutputImpl/Plot/CommonNode.cc 6.05 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * 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) {
b96aa975   Benjamin Renard   Draw border aroun...
24
		element->_xLoadedMargin = atof((const char*) value);
fbe3c2bb   Benjamin Renard   First commit
25
26
27
28
29
30
		xmlFree(value);
	}

	// -- vertical margin
	value = xmlGetProp(pNode, (const xmlChar *) "y");
	if (value) {
b96aa975   Benjamin Renard   Draw border aroun...
31
		element->_yLoadedMargin = atof((const char*) value);
fbe3c2bb   Benjamin Renard   First commit
32
33
34
35
36
37
		xmlFree(value);
	}
}

void LabelNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pContext) {
f6eaec4e   Benjamin Renard   Optimize plot ele...
38
	LOG4CXX_DEBUG(gLogger, "LabelNode::proceed");
fbe3c2bb   Benjamin Renard   First commit
39
40
41
42
43
44
45
46
47
48
49
	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
f6eaec4e   Benjamin Renard   Optimize plot ele...
50
	Font labelFont(label->getFont());
fbe3c2bb   Benjamin Renard   First commit
51
52
	value = xmlGetProp(pNode, (const xmlChar *) "fontName");
	if (value) {
df45df5e   Benjamin Renard   Introduce AxisLeg...
53
		labelFont.setName((const char*) value);
fbe3c2bb   Benjamin Renard   First commit
54
55
56
57
58
59
		xmlFree(value);
	}

	// -- font size
	value = xmlGetProp(pNode, (const xmlChar *) "fontSize");
	if (value) {
f6eaec4e   Benjamin Renard   Optimize plot ele...
60

df45df5e   Benjamin Renard   Introduce AxisLeg...
61
		labelFont.setSize(atoi((const char*) value));
fbe3c2bb   Benjamin Renard   First commit
62
63
64
		xmlFree(value);
	}

df45df5e   Benjamin Renard   Introduce AxisLeg...
65
66
67
68
69
70
	// -- font style
	value = xmlGetProp(pNode, (const xmlChar *) "fontStyle");
	if (value) {
		labelFont.setStyle(Font::getStyleByStr((const char*) value));
		xmlFree(value);
	}
f6eaec4e   Benjamin Renard   Optimize plot ele...
71

df45df5e   Benjamin Renard   Introduce AxisLeg...
72
73
	// -- font weight
	value = xmlGetProp(pNode, (const xmlChar *) "fontWeight");
fbe3c2bb   Benjamin Renard   First commit
74
	if (value) {
df45df5e   Benjamin Renard   Introduce AxisLeg...
75
		labelFont.setWeight(Font::getWeightByStr((const char*) value));
fbe3c2bb   Benjamin Renard   First commit
76
77
78
		xmlFree(value);
	}

df45df5e   Benjamin Renard   Introduce AxisLeg...
79
80
	label->setFont(labelFont);

fbe3c2bb   Benjamin Renard   First commit
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
	// -- 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 */