Panel.hh 5.44 KB
/*
 * Panel.hh
 *
 *  Created on: 28 oct. 2013
 *      Author: CS
 */

#ifndef PANEL_HH_
#define PANEL_HH_

#include <string>
#include "Bounds.hh"
#include "Font.hh"
#include "Color.hh"
#include "PlotCommon.hh"
#include <boost/shared_ptr.hpp>
#include <memory>
#include <map>
#include "plplot/plstream.h"
#include "Axis.hh"
#include "ColorAxis.hh"
#include "TextPlot.hh"
#include "CurvePlot.hh"
#include "FillSerieConstant.hh"
#include "FillSerieSerie.hh"
#include "ParamsLegendProperties.hh"
#include "TextLegendProperties.hh"
#include "Layout.hh"
#include "ContextFileWriter.hh"

namespace plot {

class Page;
class DefaultPlotConfiguration;

typedef std::map<std::string, boost::shared_ptr<Axis>> Axes;
typedef std::list<boost::shared_ptr<TextPlot>> TextPlots;
typedef std::list<boost::shared_ptr<CurvePlot>> CurvePlots;
typedef std::list<boost::shared_ptr<FillSerieConstant>> FillSerieConstants;
typedef std::list<boost::shared_ptr<FillSerieSerie>> FillSerieSeries;
typedef std::list<boost::shared_ptr<TextLegendProperties>> TextLegendPropertiesList;

/**
 * @brief A panel is a part of Page and owns a plot (and eventually 
 *        its decorator).
 */
class Panel {
public:
	/**
	 * @brief Counter for panel unique id
	 */
	static int PANEL_COUNTER;

	/**
	 * @brief Transform coordinate from AMDA page to Plplot page.
	 * !!!! WARNING !!!! This function modify the current plPlot font
	 */
	Bounds getBoundsInPlPage();

	Panel();
	Panel(DefaultPlotConfiguration& defaults);
	Panel(Page* page);
	virtual ~Panel();

	/**
	 * @brief Draws panel.
	 */
	void draw(std::shared_ptr<plstream>& pls);

	/**
	 * @brief Write panel context
	 */
	void writeContext(std::shared_ptr<plstream>& pls, ContextFileWriter& writer);

	/**
	 * @brief Adds axis to panel, depends on plot type.
	 */
	void addAxis(const std::string& pAxisId, boost::shared_ptr<Axis> pAxis) {
		_axes[pAxisId] = pAxis; }

	/**
	 * @brief Gets Axis from id, new axis is created if not found.
	 */
	boost::shared_ptr<Axis> getAxis(const std::string& pAxisId) { return _axes[pAxisId]; }

	/**
	 * @brief Adds color axis to panel
	 */
	void addColorAxis(const std::string& pAxisId, boost::shared_ptr<ColorAxis> pColorAxis) {
		_colorAxis = pColorAxis;
		_axes[pAxisId] = pColorAxis;
	}

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

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

	Label* getTitle() {
		return &_title;
	}

	void setTitleText(const char* text) {
		_title._text = text;
	}

	Font getTitleFont() ;

	/**
	 * @brief Reset for next time interval plot
	 */
	void resetPlot() {
		for (auto axe : _axes)
		{
			if (axe.second != nullptr)
				axe.second->resetPlot();
		}
		_paramsLegendProperties.resetPlot();
		for (auto legend : _textLegendPropertiesList)
			legend->resetPlot();
		for (auto text : _textPlots)
			text->resetPlot();
		for (auto curve : _curvePlots)
			curve->resetPlot();
		_drawn = false;
	}

	/**
	 * @brief Gets color axis
	 */
	boost::shared_ptr<ColorAxis> getColorAxis(void) { return _colorAxis; }


	/**
	 * @brief Panel unique identifier
	 */
	int _id;

	/**
	 * @brief Panel index
	 */
	int _index;

	/**
	 * Indicates if it's necessary to show int info
	 */
	bool _showIntInfo;

	/**
	 * @brief Number of points per plot
	 */
	int _resolution;

	/**
	 * @brief Flag to know if the title must be updated during the next interval plot
	 */
	bool _updateTitleOnNextInterval;

	/**
	 * @brief Panel position and size.
	 */
	Bounds _bounds;

	/**
	 * @brief Background color
	 */
	Color _backgroundColor;

	/**
	 * @brief Plot Area Background color
	 */
	Color _plotAreaBackgroundColor;

	/**
	 * @brief Panel title Align (center, left, right)
	 */
	PlotCommon::Align _titleAlign;

	/**
	 * @brief Panel title Position (top, bottom)
	 */
	PlotCommon::Position _titlePosition;

	/**
	 * @brief _drawn Define if panel is already drawn (true) or not (false).
	 */
	bool _drawn;

	/**
	 * @brief Page container
	 */
	Page* _page;

	/**
	 * @brief List of axis.
	 */
	Axes _axes;

	/**
	 * @brief List of axis.
	 */
	boost::shared_ptr<ColorAxis> _colorAxis;

	/**
	 * @brief Prefered panel width and height.
	 * @note Number set is in unit of panel character width or height.
	 */
	double _preferedWidth;
	double _preferedHeight;

	/**
	 * @brief Space between border of panel and border of plot area.
	 * @note Number set is in unit of panel character height.
	 */
	int _leftMargin;
	int _rightMargin;
	int _topMargin;
	int _bottomMargin;

	/**
	 * @brief List of textplots.
	 */
	TextPlots _textPlots;

	/**
	 * @brief List of curveplots.
	 */
	CurvePlots _curvePlots;

	/**
	 * @brief List of FillSerieConstant.
	 */
	FillSerieConstants _fillSerieConstants;

	/**
	 * @brief List of FillSerieSerie.
	 */
	FillSerieSeries _fillSerieSeries;


	/**
	 * @brief Parameter legend properties
	 */
	ParamsLegendProperties _paramsLegendProperties;

	/**
	 * @brief Text legend properties list
	 */
	TextLegendPropertiesList _textLegendPropertiesList;


	void drawEmptyPanel(std::shared_ptr<plstream>& pls);

	void drawNoData(std::shared_ptr<plstream>& pls);

	void reserveSpaceForTitle(double& top, double& bottom, double& titleHeight);

private:
	/**
	 * @brief Panel font, may be different from page font
	 */
	Font _font;

	/**
	 * @brief Panel title, may be empty
	 */
	Label _title;


	void fillBackground(std::shared_ptr<plstream>& pls);

	void drawTitle(std::shared_ptr<plstream>& pls);

	void drawMessage(std::shared_ptr<plstream>& pls, std::string& message);
};

std::ostream& operator <<(std::ostream& out, Panel& panel);

} /* namespace plot */
#endif /* PANEL_HH_ */