/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#if !defined(UNIT__H)

#define UNIT__H

#include <qglobal.h>

class QString;
class QDoubleSpinBox;
class QSpinBox;

class Unit
{
	public:
		enum Energy
		{
			kj,
			kcal
		};

	public:
		static QString formatDuration(quint64 msecs, bool include_hundredths = true);

		static QString formatDistance(quint64 mm);
		static QString formatDistance(quint64 mm, double& value, QString& unit);
		static void setSpinBoxDistance(QDoubleSpinBox& spinBox, quint64 mm);

		static QString formatPace(quint64 pace_in_msec_per_km);
		static QString formatPace(quint64 pace_in_msec_per_km, double& value, QString& unit);

		static QString formatSpeed(quint64 speed);
		static QString formatSpeed(quint64 speed, double& value, QString& unit);
		static void setSpinBoxSpeed(QDoubleSpinBox& spinBox, quint64 speed);

		template<typename T> static T toImperialHeight(T val) { return val * 3.2808399; }
		template<typename T> static T toMetricHeight(T val) { return val / 3.2808399; }
		static QString formatHeight(quint64 mm);
		static QString formatHeight(quint64 mm, double& value, QString& unit);
		static void setSpinBoxHeight(QDoubleSpinBox& spinBox, quint64 mm);

		static QString formatEnergy(quint32 kj);
		static QString formatEnergy(quint32 kj, quint32& value, QString& unit);
		static void setSpinBoxEnergy(QSpinBox& spinBox, quint32 kj);
		static QString energyUnit();

		template<typename T> static T toImperialWeight(T val) { return val * 2.20462262185; }
		template<typename T> static T toMetricWeight(T val) { return val / 2.20462262185; }
		static QString formatWeight(quint64 mm);
		static QString formatWeight(quint64 mm, double& value, QString& unit);
		static void setSpinBoxWeight(QDoubleSpinBox& spinBox, quint64 mm);
		static QString weightUnit();

		template<typename T> static T toKiloJoule(T val) { return val * 4.1868; }
		template<typename T> static T toKiloCalories(T val) { return val / 4.1868; }

		template<typename T> static T toFahrenheit(T val) { return 32 + val * 9 / 5.0; }
		template<typename T> static T toCelsius(T val) { return (val - 32) / 9.0 * 5; }
		static QString formatTemperature(quint64 deg);
		static QString formatTemperature(quint64 deg, double& value, QString& unit);
		static void setSpinBoxTemperature(QDoubleSpinBox& spinBox, quint64 deg);
		static QString temperatureUnit();
};

#endif
