ColormapManager.cc 1.92 KB
/*
 * ColormapManager.cc
 *
 *  Created on: 8 nov. 2013
 *      Author: guillaume
 */

#include "ColormapManager.hh"
#include "PlotConfigurationLoader.hh"

#include <fstream>
#include <boost/lexical_cast.hpp>

namespace plot {

const int ColormapManager::DEFAULT_COLORMAP_0 = 0;
const int ColormapManager::DEFAULT_COLORMAP_1 = 1;

std::unique_ptr<ColormapManager> ColormapManager::_instance = nullptr;
std::once_flag ColormapManager::_instanceOnceFlag;

ColormapManager::ColormapManager() :
		_path("") {
	PlotConfigurationLoader lPlotLoader = PlotConfigurationLoader();
	lPlotLoader.load<ColormapManager>(this);
}

ColormapManager::~ColormapManager() {
}

/**
 * @brief Return file name corresponding to the given index and mode.
 */
std::string ColormapManager::get(PlotCommon::Mode& pMode, int pIndex) {
	ColorFileMap& lColorFileMap(_colormap[pMode]);

	std::string lFile = "";

	if ((pIndex != -1) && lColorFileMap.find(pIndex) != lColorFileMap.end()) {
		// Concatenate path and filename.
		lFile = _path + lColorFileMap[pIndex];
	}

	return lFile;
}

int ColormapManager::getDefault(PlotCommon::Mode& mode) {
	return _defaultColormap[mode];
}

int ColormapManager::getDefaultSize(PlotCommon::Mode& mode)
{
	std::map<PlotCommon::Mode, int>::iterator it;

	it = _defaultSize.find(mode);

	if (it != _defaultSize.end())
		return it->second;

	std::string filePath = ColormapManager::getInstance().get(mode,
			ColormapManager::getInstance().getDefault(mode));

	std::ifstream mapFile(filePath);
	if (!mapFile.good())
		return 0;

	try {
		std::string firstLine;
		std::getline(mapFile, firstLine);
		int nbCol = boost::lexical_cast<int>(firstLine);
		return nbCol;
	} catch (...)
	{
	}

	return 0;
}

std::string ColormapManager::getColorAxis(int index)
{
	std::string lFile = "";

	if ((index != -1) && _coloraxis.find(index) != _coloraxis.end()) {
		// Concatenate path and filename.
		lFile = _path + _coloraxis[index];
	}

	return lFile;
}

} /* namespace plot */