/***************************************************************************
 * pixel.cpp: implementation of Pixel class
 *
 * This file is part of KTabEdit, a KDE tabulature editor
 *
 * copyright (C) 2003-2007 the KTabEdit development team <mrgwen@gmail.com>
 ***************************************************************************/

/***************************************************************************
 * 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.
 *
 * See the file COPYING for more information.
 ***************************************************************************/

#include "pixel.h"

#include <QtDebug>

#include "inches.h"
#include "cm.h"

Pixel::Pixel(uint pixel, uint dpi) throw(std::exception)
: Unit(dpi),
  mPixel(pixel)
{
}

Pixel::~Pixel()
{

}

Cm Pixel::toCm() const
{
	return Cm(static_cast<double>(mPixel) * inchesForACm() / dpi(), dpi());
}

Inches Pixel::toInches() const
{
	return Inches(mPixel / dpi(), dpi());
}

void Pixel::fromOtherDpi(uint oldDpi) throw(std::exception)
{
	if (oldDpi == 0) {
		qDebug() << "[Pixel::fromOtherDpi] oldDpi == 0\n";
		throw std::exception();
	}
	
	mPixel = static_cast<uint>(static_cast<double>(dpi()) * (static_cast<double>(mPixel) / static_cast<double>(oldDpi)));
}

uint Pixel::value() const
{
	return mPixel;
}

bool Pixel::operator>(const Pixel& b) const
{
	return (this->mPixel > b.mPixel);
}

bool Pixel::operator<(const Pixel& b) const
{
	return (this->mPixel < b.mPixel);
}
