AxisLegend.hh 3.04 KB
/*
 * AxisLegend.hh
 *
 *  Created on: 01 jun. 2018
 *      Author: AKKA
 */

#ifndef AXISLEGEND_HH_
#define AXISLEGEND_HH_

#include "Label.hh"
#include <vector>
#include <list>

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();
	}

	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;
	}

	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;
		}
		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_ */