Blame view

src/ParamOutputImpl/Plot/AxisLegend.hh 3.06 KB
df45df5e   Benjamin Renard   Introduce AxisLeg...
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * AxisLegend.hh
 *
 *  Created on: 01 jun. 2018
 *      Author: AKKA
 */

#ifndef AXISLEGEND_HH_
#define AXISLEGEND_HH_

#include "Label.hh"
#include <vector>
8bb0f02b   Benjamin Renard   Set colors in axi...
13
#include <list>
df45df5e   Benjamin Renard   Introduce AxisLeg...
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

namespace plot {

class AxisLegend {
public:
	AxisLegend() : _color(0, 0, 0), _font(Font("", 0)) {
	}

	AxisLegend(Font font) : _color(0, 0, 0), _font(font) {
	}

	AxisLegend(Font font, Color color) : _color(color), _font(font) {
	}

	AxisLegend(const AxisLegend& legend) :
			_labels(legend._labels), _color(legend._color), _font(legend._font) {
	}

	virtual ~AxisLegend() {
		_labels.clear();
	}

	const std::vector<Label> getLabels() const {
		return _labels;
	}

	const Color& getColor() const {
		return _color;
	}

	const Font& getFont() const {
		return _font;
	}

	void setFont(const Font& font) {
		_font = font;
	}

	void setLabel(Label &label) {
		if (!label._text.empty()) {
			_labels.clear();
			_labels.push_back(label);
		}
		_color = label._color;
		_font = label.getFont();
	}

	void pushLabel(Label &label) {
		if (!label._text.empty()) {
			_labels.push_back(label);
		}
	}

	void clearLabels() {
		_labels.clear();
	}

8bb0f02b   Benjamin Renard   Set colors in axi...
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
	int getNbLines() {
		int nbLines = 0;
		for (auto label : _labels) {
			LabelRowInfo labelRow = Label::getRowNumber(label);
			for (auto row : labelRow) {
				if (!row.empty()) {
					++nbLines;
				}
			}
		}
		return nbLines;
	}

	std::list< std::pair< std::string, Color > > getLines() {
		std::list< std::pair< std::string, Color > > lines;
		for (auto label : _labels) {
			LabelRowInfo labelRow = Label::getRowNumber(label);
			for (auto row : labelRow) {
				if (row.empty()) {
					continue;
				}
				Color lineColor = label._color;
				if (!lineColor.isSet()) {
					lineColor = _color;
				}
				lines.push_back(std::make_pair(row,lineColor));
			}
		}
		return lines;
	}

df45df5e   Benjamin Renard   Introduce AxisLeg...
102
103
104
105
106
107
108
109
110
111
112
	std::string getText() {
		std::string result = "";
		bool isFirstLabel = true;
		for (auto label : _labels) {
			if (label._text.empty()) {
				continue;
			}
			if (!isFirstLabel) {
				result += Label::DELIMITER;
			}
			result += label._text;
fb86cf88   Benjamin Renard   Add legend delimi...
113
			isFirstLabel = true;
df45df5e   Benjamin Renard   Introduce AxisLeg...
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
		}
		return result;
	}

	bool isEmpty() {
		if (_labels.empty()) {
			return true;
		}
		for (auto label : _labels) {
			if (!label._text.empty()) {
				return false;
			}
		}
		return true;
	}

	/**
	 * @brief Determine number of line that compose the legend.
	 */
	static LabelRowInfo getRowNumber(AxisLegend const& pLegend);

	/**
	 * Dumps legend properties, used for test.
	 */
	void dump(std::ostream& out, std::string& prefix);

private:
	/**
	 * List of labels that compose the legend.
	 */
	std::vector<Label> _labels;

	/**
	 * Text color
	 */
	Color _color;

	/**
	 * Legend font name and size.
	 */
	Font _font;
};

inline std::ostream& operator <<(std::ostream& out, AxisLegend& legend) {
	out << "[AXIS LEGEND]" << std::endl;
	out << "legend.color=" << legend.getColor() << std::endl;
	std::string prefix = "legend";
	Font font = legend.getFont();
	font.dump(out, prefix);
	prefix = "legend.label";
	for (auto label : legend.getLabels()) {
		label.dump(out,prefix);
        }
	return out;
}


} /* namespace plot */
#endif /* AXISLEGEND_HH_ */