Blame view

src/ParamOutputImpl/Plot/LayoutAuto.cc 6.33 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
/*
 * LayoutAuto.hh
 *
 *  Created on: Sep, 2, 2014
 *      Author: AKKA
 */


#include "LayoutAuto.hh"
#include "PlotLogger.hh"
2f0b202e   Benjamin Renard   Reverse panels or...
11
12
13
#include "DecoratorPlot.hh"

#include <boost/range/adaptor/reversed.hpp>
fbe3c2bb   Benjamin Renard   First commit
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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

namespace plot {

/**
 * @Brief Add a panel characterized by it's constraint to the LayoutAuto
 */

void LayoutAuto::addPanel (PanelConstraint panelConstraint, double /*preferedWidth=-1*/, double /*preferedHeight=-1*/) {
	switch (panelConstraint) {
	case PanelConstraint::MaxWidth:
		_nbMaxWidthPanel++;
		break;
	case PanelConstraint::Square:
		_nbSquarePanel++;
		break;
	default:
		// TODO lever exception ici ???
		break;
	}
}

/**
 * @Brief Reset LayoutAuto internal parameters and compute optimized parameters
 * like _panelHeight, _panelWidth, _panelXSpacing, _panelYSpacing...
 */

void LayoutAuto::reset (void) {
	// Reset current panels
	_curMaxWidthPanel	= 0;
	_curSquarePanel		= 0;
	_curSquarePanelLine = 0;

	// Compute general data for the layout depending on
	// - the number of panel
	// - their constraint
	_panelHeight 	= _requestPanelHeight;
	_panelYSpacing	= _requestPanelSpacing;

	double layoutHeight = computeLayoutHeight ();

	// Shrink _requestPanelHeight if necessary
	if (layoutHeight > 1) {
		shrink (0.01);
		expand (0.001);
	}

	// Expand _requestPanelHeight if necessary and asked
	else if ((layoutHeight < 1) && (_autoExpand == true)) {
		expand (0.01);
		expand (0.001);
		expand (0.0001);
	}
}

/**
 * @Brief Decrease panelHeight by step while layoutHeight is more than 1.0
 */

void LayoutAuto::shrink (double step) {
	double layoutHeight;
	do
	{
		// Decrease panelHeight by step and keep the same ratio for panelSpacing
		_panelHeight 	-= step;
		_panelYSpacing	= _requestPanelSpacing * (_panelHeight / _requestPanelHeight);
		layoutHeight = computeLayoutHeight ();
	} while (layoutHeight > 1.0);
}

/**
 * @Brief Increase panelHeight by step until layoutHeight reach (but not over step) 1.0
 */

void LayoutAuto::expand (double step) {
	double layoutHeight;
	do
	{
		// Increase panelHeight by step and keep the same ratio for panelSpacing
		_panelHeight 	+= step;
		_panelYSpacing	= _requestPanelSpacing * (_panelHeight / _requestPanelHeight);
		layoutHeight = computeLayoutHeight ();
	} while (layoutHeight < 1.0);

	// Restore last valid value for panelHeight and panelSpacing
	_panelHeight 	-= step;
	_panelYSpacing	= _requestPanelSpacing * (_panelHeight / _requestPanelHeight);
	computeLayoutHeight ();
}

double LayoutAuto::computeLayoutHeight (void) {
	double layoutHeight = 0;
	_nbLineForSquarePanel = 0;
	_firstPanelHeightDelta = 0;

	_panelWidth 	= _panelHeight / _xyRatio;
	_panelXSpacing	= _panelYSpacing / _xyRatio;

	// Computes _nbMaxSquarePanelByLine and nbLineForSquarePanel for the layout
	// then computes panelHeight
	_nbMaxSquarePanelByLine = (int) ((1.0 + _panelXSpacing) / (_panelWidth + _panelXSpacing));

	_nbLineForSquarePanel = _nbSquarePanel / _nbMaxSquarePanelByLine;
	if ((_nbSquarePanel % _nbMaxSquarePanelByLine) != 0)
		_nbLineForSquarePanel++;

	// Computes height for MaxWidth Panel
	if (_nbMaxWidthPanel > 0) {
		layoutHeight += _nbMaxWidthPanel * _panelHeight + (_nbMaxWidthPanel - 1) * _panelYSpacing;
		_firstPanelHeightDelta = (_firstPanelHeightFactor-1.0) * _panelHeight;
		layoutHeight += _firstPanelHeightDelta;
	}

	// Computes height for Square Panel
	if (_nbSquarePanel > 0)
		layoutHeight += _nbLineForSquarePanel * _panelHeight + (_nbLineForSquarePanel - 1) * _panelYSpacing;

	// Add space between MaxWidth and Square ?
	if ((_nbMaxWidthPanel > 0) && (_nbSquarePanel > 0))
		layoutHeight += _panelYSpacing;

//	LOG4CXX_DEBUG(gLogger,"Layout::computeLayoutHeight _nbMaxPanelSquareByLine = " << _nbMaxSquarePanelByLine);
//	LOG4CXX_DEBUG(gLogger,"Layout::computeLayoutHeight _nbLineForSquarePanel = " << _nbLineForSquarePanel);
//	LOG4CXX_DEBUG(gLogger,"Layout::computeLayoutHeight layoutHeight = " << layoutHeight);
	return layoutHeight;
}

Bounds & LayoutAuto::getNextBounds (PanelConstraint panelConstraint) {
	int nbSquarePanelOnLine;
2f0b202e   Benjamin Renard   Reverse panels or...
142
	double lastSquarePanelXPosition;
fbe3c2bb   Benjamin Renard   First commit
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

	// Computes panels position on the layout depending on their constraints
	switch (panelConstraint) {

	case PanelConstraint::MaxWidth:
		_computedPanelBounds._x = 0;
		_computedPanelBounds._y = _curMaxWidthPanel *(_panelHeight + _panelYSpacing);
		_computedPanelBounds._width = 1.0;
		_computedPanelBounds._height = _panelHeight;

		// The first MaxWidth panel can have an extra panel height
		if (_curMaxWidthPanel != 0) {
			_computedPanelBounds._y 		+= _firstPanelHeightDelta;
		}
		if (_curMaxWidthPanel == 0) {
			_computedPanelBounds._height	+= _firstPanelHeightDelta;
		}

		_curMaxWidthPanel++;
		break;

	case PanelConstraint::Square:
		// Compute number of square panel on the current line
		// Are we on the last PanelSquareLine ?
		if (_curSquarePanelLine == (_nbLineForSquarePanel - 1))
			nbSquarePanelOnLine = _nbSquarePanel - (_curSquarePanelLine * _nbMaxSquarePanelByLine);
		else
			nbSquarePanelOnLine = _nbMaxSquarePanelByLine;

2f0b202e   Benjamin Renard   Reverse panels or...
172
173
174
		// Compute x position of the last SquarePanel on the line (centered)
		lastSquarePanelXPosition = 0.5
				+ ((((nbSquarePanelOnLine) * _panelWidth) + ((nbSquarePanelOnLine-1) * _panelXSpacing)) / 2.0) - _panelWidth;
fbe3c2bb   Benjamin Renard   First commit
175

2f0b202e   Benjamin Renard   Reverse panels or...
176
177
		_computedPanelBounds._x = lastSquarePanelXPosition
								- (_curSquarePanel % _nbMaxSquarePanelByLine) * (_panelWidth + _panelXSpacing);;
fbe3c2bb   Benjamin Renard   First commit
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
		_computedPanelBounds._y = (_nbMaxWidthPanel + _curSquarePanelLine) *(_panelHeight + _panelYSpacing);
		if (_nbMaxWidthPanel != 0) {
			_computedPanelBounds._y += _firstPanelHeightDelta;
		}

		_computedPanelBounds._width = _panelWidth;
		_computedPanelBounds._height = _panelHeight;

		_curSquarePanel++;

		// Next line for square panel
		if ((_curSquarePanel % _nbMaxSquarePanelByLine) == 0)
			_curSquarePanelLine++;

		break;

	default:
		// TODO lever exception ici ???
		break;
	}

	return _computedPanelBounds;
}
2f0b202e   Benjamin Renard   Reverse panels or...
201
202
203
204

void LayoutAuto::computePanelsPosition (std::vector<boost::shared_ptr<PanelPlotOutput>> _plots) {
	// Add panels constraint to the layout
	for (auto plot : boost::adaptors::reverse(_plots)) {
8c860e4f   Benjamin Renard   Rework for tickpl...
205
206
		if (!plot->isStandalone())
			continue;
2f0b202e   Benjamin Renard   Reverse panels or...
207
208
209
210
211
212
213
214
215
216
217

		this->addPanel ( plot->getLayoutConstraint(),
							plot->_panel->_preferedWidth,
							plot->_panel->_preferedHeight);
	}

	// Reset layout
	this->reset();


	for (auto plot : boost::adaptors::reverse(_plots)) {
8c860e4f   Benjamin Renard   Rework for tickpl...
218
219
		if (!plot->isStandalone())
			continue;
2f0b202e   Benjamin Renard   Reverse panels or...
220
221
222
223
224

		plot->_panel->_bounds = this->getNextBounds (plot->getLayoutConstraint());
	}
}

fbe3c2bb   Benjamin Renard   First commit
225
}