Blame view

src/ParamOutputImpl/Plot/Panel.cc 11.8 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * Panel.cc
 *
 *  Created on: 28 oct. 2013
 *      Author: CS
 */

#include "Panel.hh"
#include "DefaultPlotConfiguration.hh"
#include "ColormapManager.hh"
#include "Label.hh"
#include "PlotLogger.hh"
f6eaec4e   Benjamin Renard   Optimize plot ele...
13
#include "PlPlotUtil.hh"
fbe3c2bb   Benjamin Renard   First commit
14
15
16
17


namespace plot {

85bb5117   Benjamin Renard   Give the possibil...
18
19
//Auto id set to a negative number to preserve id defined in request
int Panel::PANEL_COUNTER = -1;
fbe3c2bb   Benjamin Renard   First commit
20

337411e4   Erdogan Furkan   #5390 - Kernel pa...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Panel::Panel() : _id(--PANEL_COUNTER),
				 _index(-1),
				 _resolution(0),
				 _updateTitleOnNextInterval(false),
				 _bounds(Bounds()),
				 _backgroundColor(Color()),
				 _plotAreaBackgroundColor(Color()),
				 _titleAlign(PlotCommon::Align::CENTER),
				 _titlePosition(PlotCommon::Position::POS_TOP),
				 _drawn(false),
				 _page(),
				 _preferedWidth(-1),
				 _preferedHeight(-1),
				 _leftMargin(-1),
				 _rightMargin(-1),
				 _topMargin(-1),
				 _bottomMargin(-1),
				 _paramsLegendProperties(),
				 _textLegendPropertiesList(),
				 _font(Font("", 0)),
				 _title(Font("", 0))
fbe3c2bb   Benjamin Renard   First commit
42
43
44
{
}

337411e4   Erdogan Furkan   #5390 - Kernel pa...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Panel::Panel(DefaultPlotConfiguration &defaults) : _id(--PANEL_COUNTER),
												   _index(defaults._defaultPanel._index),
												   _resolution(defaults._defaultPanel._resolution),
												   _updateTitleOnNextInterval(false),
												   _bounds(Bounds()),
												   _backgroundColor(defaults._defaultPanel._backgroundColor),
												   _plotAreaBackgroundColor(defaults._defaultPanel._backgroundColor),
												   _titleAlign(defaults._defaultPanel._titleAlign),
												   _titlePosition(defaults._defaultPanel._titlePosition),
												   _drawn(false),
												   _page(),
												   _preferedWidth(defaults._defaultPanel._preferedWidth),
												   _preferedHeight(defaults._defaultPanel._preferedHeight),
												   _leftMargin(defaults._defaultPanel._leftMargin),
												   _rightMargin(defaults._defaultPanel._rightMargin),
												   _topMargin(defaults._defaultPanel._topMargin),
												   _bottomMargin(defaults._defaultPanel._bottomMargin),
												   _textLegendPropertiesList(defaults._defaultPanel._textLegendPropertiesList),
												   _font(defaults._defaultPanel.getFont()),
												   _title(defaults._defaultPanel.getFont())
fbe3c2bb   Benjamin Renard   First commit
65
66
67
{
}

337411e4   Erdogan Furkan   #5390 - Kernel pa...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Panel::Panel(Page *page) : _id(--PANEL_COUNTER),
						   _index(-1),
						   _resolution(DefaultPlotConfiguration::getInstance()._defaultPanel._resolution),
						   _updateTitleOnNextInterval(false),
						   _bounds(Bounds()),
						   _backgroundColor(DefaultPlotConfiguration::getInstance()._defaultPanel._backgroundColor),
						   _plotAreaBackgroundColor(DefaultPlotConfiguration::getInstance()._defaultPanel._backgroundColor),
						   _titleAlign(DefaultPlotConfiguration::getInstance()._defaultPanel._titleAlign),
						   _titlePosition(DefaultPlotConfiguration::getInstance()._defaultPanel._titlePosition),
						   _drawn(false),
						   _page(page),
						   _preferedWidth(DefaultPlotConfiguration::getInstance()._defaultPanel._preferedWidth),
						   _preferedHeight(DefaultPlotConfiguration::getInstance()._defaultPanel._preferedHeight),
						   _leftMargin(DefaultPlotConfiguration::getInstance()._defaultPanel._leftMargin),
						   _rightMargin(DefaultPlotConfiguration::getInstance()._defaultPanel._rightMargin),
						   _topMargin(DefaultPlotConfiguration::getInstance()._defaultPanel._topMargin),
						   _bottomMargin(DefaultPlotConfiguration::getInstance()._defaultPanel._bottomMargin),
						   _textLegendPropertiesList(DefaultPlotConfiguration::getInstance()._defaultPanel._textLegendPropertiesList),
						   _font(Font(
							   DefaultPlotConfiguration::getInstance()._defaultPanel.getFont().getName().empty() ? page->getFont().getName() : DefaultPlotConfiguration::getInstance()._defaultPanel.getFont().getName(),
							   DefaultPlotConfiguration::getInstance()._defaultPanel.getFont().getSize() != 0 ? DefaultPlotConfiguration::getInstance()._defaultPanel.getFont().getSize() : page->getFont().getSize())),
						   _title(_font)
fbe3c2bb   Benjamin Renard   First commit
90
91
92
93
94
95
96
{
}

Panel::~Panel() {

}

f6eaec4e   Benjamin Renard   Optimize plot ele...
97
98
Font Panel::getTitleFont() {
	Font titleFont(_title.getFont());
df45df5e   Benjamin Renard   Introduce AxisLeg...
99
100
101
102
	if (titleFont.getName() == "")
		titleFont.setName(_font.getName());
	if (titleFont.getSize() == 0)
		titleFont.setSize(_font.getSize());
f6eaec4e   Benjamin Renard   Optimize plot ele...
103
104
	return titleFont;
}
fbe3c2bb   Benjamin Renard   First commit
105

f6eaec4e   Benjamin Renard   Optimize plot ele...
106
107
void Panel::reserveSpaceForTitle(double& topSpace, double& bottomSpace, double& titleHeight) {
	LabelRowInfo titleRows(Label::getRowNumber(_title));
df45df5e   Benjamin Renard   Introduce AxisLeg...
108
	if (titleRows.empty())
f6eaec4e   Benjamin Renard   Optimize plot ele...
109
		return;
fbe3c2bb   Benjamin Renard   First commit
110

f6eaec4e   Benjamin Renard   Optimize plot ele...
111
	CharSize lCharSizeTitle;
fbe3c2bb   Benjamin Renard   First commit
112

f6eaec4e   Benjamin Renard   Optimize plot ele...
113
	Font titleFont(getTitleFont());
df45df5e   Benjamin Renard   Introduce AxisLeg...
114
	PlPlotUtil::setPlFont(titleFont);
f6eaec4e   Benjamin Renard   Optimize plot ele...
115
116
	lCharSizeTitle = PlPlotUtil::getCharacterSizeInPlPage(_page);

df45df5e   Benjamin Renard   Introduce AxisLeg...
117
	titleHeight = (titleRows.size() + PlPlotUtil::LINE_SPACE_TITLE * (titleRows.size() + 1)) * lCharSizeTitle.second;
f6eaec4e   Benjamin Renard   Optimize plot ele...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

	switch (_titlePosition)
	{
		case PlotCommon::Position::POS_TOP :
			topSpace += titleHeight;
			break;
		case PlotCommon::Position::POS_BOTTOM :
			bottomSpace += titleHeight;
			break;
		default :
			//Not possible - Nothing to do
			;
	}
}

void Panel::drawTitle(std::shared_ptr<plstream>& pls) {
	LabelRowInfo titleRows(Label::getRowNumber(_title));
df45df5e   Benjamin Renard   Introduce AxisLeg...
135
	if (titleRows.empty())
fbe3c2bb   Benjamin Renard   First commit
136
137
		return;

f6eaec4e   Benjamin Renard   Optimize plot ele...
138
139
	if ((_titlePosition != PlotCommon::Position::POS_TOP) && (_titlePosition != PlotCommon::Position::POS_BOTTOM))
		return;
fbe3c2bb   Benjamin Renard   First commit
140

f6eaec4e   Benjamin Renard   Optimize plot ele...
141
142
	// Compute panel coordinate in the page
	Bounds lBounds = getBoundsInPlPage();
2d40f7de   Benjamin Renard   Fix bug with pane...
143
144
145

	Color lInitialColor = changeColor(pls, _title._color, _page->_mode);

f6eaec4e   Benjamin Renard   Optimize plot ele...
146
	Font titleFont(getTitleFont());
df45df5e   Benjamin Renard   Introduce AxisLeg...
147
	PlPlotUtil::setPlFont(titleFont);
fbe3c2bb   Benjamin Renard   First commit
148

f6eaec4e   Benjamin Renard   Optimize plot ele...
149
150
151
	// Specify viewport for the panel
	pls->vpor(lBounds._x, lBounds._x + lBounds._width,
			  lBounds._y, lBounds._y + lBounds._height);
fbe3c2bb   Benjamin Renard   First commit
152

f6eaec4e   Benjamin Renard   Optimize plot ele...
153
	float lRowDisp = -0.5 - PlPlotUtil::LINE_SPACE_TITLE;
fbe3c2bb   Benjamin Renard   First commit
154

fbe3c2bb   Benjamin Renard   First commit
155
	// Draw each line of title
df45df5e   Benjamin Renard   Introduce AxisLeg...
156
	for (auto row : titleRows) {
f6eaec4e   Benjamin Renard   Optimize plot ele...
157
158
159
160
161
162
163
		pls->mtex(
				getPlSide(_titlePosition).c_str(),
				lRowDisp,
				getPlPos(_titleAlign),
				getPlJust(_titleAlign),
				row.c_str()
				);
fbe3c2bb   Benjamin Renard   First commit
164
165

		// 1 is for full character and then let space between two line.
f6eaec4e   Benjamin Renard   Optimize plot ele...
166
		lRowDisp -= (1 + PlPlotUtil::LINE_SPACE_TITLE);
fbe3c2bb   Benjamin Renard   First commit
167
168
	}

2d40f7de   Benjamin Renard   Fix bug with pane...
169
170
	// Restore initial color.
	restoreColor(pls, lInitialColor, _page->_mode);
f6eaec4e   Benjamin Renard   Optimize plot ele...
171
172
173
174
175
176
177
178
179
180
181
182
183
}


void Panel::draw(std::shared_ptr<plstream>& pls) {
	if (_drawn)
		//panel is already drawn => no draw background and title
		return;

	// Draw background
	fillBackground(pls);

	// write title with dedicated position and align
	drawTitle(pls);
2d40f7de   Benjamin Renard   Fix bug with pane...
184

fbe3c2bb   Benjamin Renard   First commit
185
186
187
	_drawn = true;
}

8499fbdd   Benjamin Renard   Context file gene...
188
189
190
/**
 * @brief Write panel context
 */
f6eaec4e   Benjamin Renard   Optimize plot ele...
191
192
void Panel::writeContext(std::shared_ptr<plstream>& /*pls*/, ContextFileWriter& writer) {
	Bounds lBounds = getBoundsInPlPage();
85bb5117   Benjamin Renard   Give the possibil...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
	writer.addAttribute("id", std::to_string(_id).c_str());
	if (_page->_orientation == PlotCommon::Orientation::PORTRAIT)
	{
		writer.addAttribute("x", std::to_string(lBounds._y*std::get<0>(_page->getSize())).c_str());
		writer.addAttribute("y", std::to_string(lBounds._x*std::get<1>(_page->getSize())).c_str());
		writer.addAttribute("width", std::to_string(lBounds._height*std::get<0>(_page->getSize())).c_str());
		writer.addAttribute("height", std::to_string(lBounds._width*std::get<1>(_page->getSize())).c_str());
	}
	else
	{
		writer.addAttribute("x", std::to_string(lBounds._x*std::get<0>(_page->getSize())).c_str());
		writer.addAttribute("y", std::to_string(lBounds._y*std::get<1>(_page->getSize())).c_str());
		writer.addAttribute("width", std::to_string(lBounds._width*std::get<0>(_page->getSize())).c_str());
		writer.addAttribute("height", std::to_string(lBounds._height*std::get<1>(_page->getSize())).c_str());
	}
8499fbdd   Benjamin Renard   Context file gene...
208

8499fbdd   Benjamin Renard   Context file gene...
209
210
}

f6eaec4e   Benjamin Renard   Optimize plot ele...
211
void Panel::fillBackground(std::shared_ptr<plstream>& pls) {
fbe3c2bb   Benjamin Renard   First commit
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
	// If color map index is not equal to 0, need to change cmap0.
	if (_backgroundColor._colorMapIndex != -1) {
		std::string lFilePath = ColormapManager::getInstance().get(_page->_mode,
				_backgroundColor._colorMapIndex);
		pls->spal0(lFilePath.c_str());
	}

	PLINT lInitialRed = -1, lInitialGreen = -1, lInitialBlue = -1;
	// if color index is equal to 0 choose the first color in cmap 0
	if (_backgroundColor._colorIndex != -1) {
		pls->col0(_backgroundColor._colorIndex);
	} else if (_backgroundColor._red != -1 && _backgroundColor._green != -1
			&& _backgroundColor._blue != -1) {
		// Store initial color in first index.
		pls->gcol0(0, lInitialRed, lInitialGreen, lInitialBlue);
		pls->scol0(0, _backgroundColor._red, _backgroundColor._green,
				_backgroundColor._blue);
		pls->col0(0);
	} else {
		// chose first color in cmap0.
		pls->col0(0);
	}

f6eaec4e   Benjamin Renard   Optimize plot ele...
235
236
237
238
239
240
241
242
243
244
	// Compute panel coordinate in the page
	Bounds lBounds = getBoundsInPlPage();

	// Specify viewport for the panel
	pls->vpor(lBounds._x, lBounds._x + lBounds._width,
			  lBounds._y, lBounds._y + lBounds._height);

	// Set window size.
	pls->wind(0, 1, 0, 1);

fbe3c2bb   Benjamin Renard   First commit
245
	// Fill background.
f6eaec4e   Benjamin Renard   Optimize plot ele...
246
247
	PLFLT px[] = { 0, 0, 1, 1 };
	PLFLT py[] = { 0, 1, 1, 0 };
fbe3c2bb   Benjamin Renard   First commit
248
249
250
251
252
253
254
255
256
257
258
	pls->fill(4, px, py);

	// Restore initial color in first index.
	if (lInitialRed != -1 && lInitialGreen != -1 && lInitialBlue != -1) {
		pls->scol0(0, lInitialRed, lInitialGreen, lInitialBlue);
	} else if (_backgroundColor._colorMapIndex != -1) {
		// Restore default cmap0 (default colormap for cmap0 is the first index).
		std::string lFilePath = ColormapManager::getInstance().get(_page->_mode,
				0);
		pls->spal0(lFilePath.c_str());
	}
f6eaec4e   Benjamin Renard   Optimize plot ele...
259
260

	pls->col0(0);
fbe3c2bb   Benjamin Renard   First commit
261
262
}

d57f00dc   Benjamin Renard   Draw NO DATA
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
void Panel::drawMessage(std::shared_ptr<plstream>& pls, std::string& message) {
	Bounds lBounds = getBoundsInPlPage();

	// Specify viewport for the panel
	pls->vpor(lBounds._x, lBounds._x + lBounds._width,
			lBounds._y, lBounds._y + lBounds._height);

	// Set window size.
	pls->wind(0, 1, 0, 1);

	pls->col0(0);

	PlPlotUtil::setPlFont(_font);

	pls->ptex (0.5, 0.5, 0.5, 0, 0.5, message.c_str());

	// Set window size.
	pls->wind(lBounds._x, lBounds._x + lBounds._width,
			lBounds._y, lBounds._y + lBounds._height);
}

b96aa975   Benjamin Renard   Draw border aroun...
284
285
void Panel::drawEmptyPanel(std::shared_ptr<plstream>& pls)
{
f6eaec4e   Benjamin Renard   Optimize plot ele...
286
	Bounds lBounds = getBoundsInPlPage();
b96aa975   Benjamin Renard   Draw border aroun...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305

	// Specify viewport for the panel
	pls->vpor(lBounds._x, lBounds._x + lBounds._width,
			  lBounds._y, lBounds._y + lBounds._height);

	// Set window size.
	pls->wind(0, 1, 0, 1);


	pls->col0(0);

	//Draw panel border
	PLFLT xBorder[5] = {0,0,1,1,0};
	PLFLT yBorder[5] = {0,1,1,0,0};

	pls->lsty(static_cast<int>(getPlLineStyle(LineStyle::LONG_SPACED_DOT)));
	pls->width(1);
	pls->line(5,xBorder,yBorder);

d57f00dc   Benjamin Renard   Draw NO DATA
306
307
308
309
	// Set window size.
	pls->wind(lBounds._x, lBounds._x + lBounds._width,
			lBounds._y, lBounds._y + lBounds._height);

b96aa975   Benjamin Renard   Draw border aroun...
310
	//Draw text
f6eaec4e   Benjamin Renard   Optimize plot ele...
311
	PlPlotUtil::setPlFont(_font);
99a73aaf   Benjamin Renard   Add panel index s...
312
313
314
315
316
317
318
319
320
321
322
	std::string emptyText = "Empty panel ";
	if (_index > -1)
	{
		emptyText += "#";
		emptyText += std::to_string(_index);
	}
	else {
		emptyText += "(Id : ";
		emptyText += std::to_string(_id);
		emptyText += ")";
	}
d57f00dc   Benjamin Renard   Draw NO DATA
323
324
	drawMessage(pls, emptyText);
}
99a73aaf   Benjamin Renard   Add panel index s...
325

d57f00dc   Benjamin Renard   Draw NO DATA
326
327
328
void Panel::drawNoData(std::shared_ptr<plstream>& pls) {
	std::string noDataText = "NO DATA";
	drawMessage(pls, noDataText);
b96aa975   Benjamin Renard   Draw border aroun...
329
330
}

f6eaec4e   Benjamin Renard   Optimize plot ele...
331
332
333
334
Bounds Panel::getBoundsInPlPage() {
	Bounds drawableBounds;
	//Drawable bounds in PlPlot page
	_page->getDrawableBoundsInPlPage(drawableBounds);
fbe3c2bb   Benjamin Renard   First commit
335

f6eaec4e   Benjamin Renard   Optimize plot ele...
336
337
338
339
340
341
	//Panel bounds is relative to the drawable bounds => rewrite it relative to the PlPlot page
	Bounds panelBounds;
	panelBounds._x = drawableBounds._x + drawableBounds._width * _bounds._x;
	panelBounds._y = drawableBounds._y + drawableBounds._height * _bounds._y;
	panelBounds._width = drawableBounds._width * _bounds._width;
	panelBounds._height = drawableBounds._height * _bounds._height;
fbe3c2bb   Benjamin Renard   First commit
342

f6eaec4e   Benjamin Renard   Optimize plot ele...
343
	return panelBounds;
fbe3c2bb   Benjamin Renard   First commit
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
}

std::ostream& operator <<(std::ostream& out, Bounds& bounds) {
	out << "[BOUNDS]" << std::endl;
	out << "bounds.x=" << bounds._x << std::endl;
	out << "bounds.y=" << bounds._y << std::endl;
	out << "bounds.width=" << bounds._width << std::endl;
	out << "bounds.height=" << bounds._height << std::endl;

	return out;
}

std::ostream& operator <<(std::ostream& out, Panel& panel) {
	out << "[PANEL]" << std::endl;
	out << "panel.resolution=" << panel._resolution << std::endl;
f6eaec4e   Benjamin Renard   Optimize plot ele...
359
	out << "panel.title=" << panel.getTitle()->_text << std::endl;
fbe3c2bb   Benjamin Renard   First commit
360
361
	out << "panel.title.align=" << panel._titleAlign << std::endl;
	out << "panel.title.position=" << panel._titlePosition << std::endl;
f6eaec4e   Benjamin Renard   Optimize plot ele...
362
	out << panel._bounds << panel._backgroundColor << panel.getFont();
fbe3c2bb   Benjamin Renard   First commit
363
364
365
366
367
368
369
370
	for (auto axe : panel._axes) {
		if (axe.second != nullptr)
			axe.second.get()->dump(out);
	}
	return out;
}

} /* namespace plot */