/*
 * Copyright 2009-2010  Stefan Gehn <stefan@srcbox.net>
 * 
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include "settings.hpp"

#include <qcoreapplication.h>
#include <qdebug.h>
#include <qstringlist.h>
#include <qsettings.h>
#include <qdir.h>

namespace IrTouch
{

Settings* Settings::sSelf = 0L;

Settings *Settings::self()
{
  if (!sSelf)
    sSelf = new Settings(QCoreApplication::instance());
  return sSelf;
}


Settings::Settings(QObject *parent) : QObject(parent)
{
  mSettings = new QSettings(QCoreApplication::applicationDirPath() + QDir::separator() + "irtouch.ini", QSettings::IniFormat, this);
  Q_FOREACH(const QString key, mSettings->allKeys())
  {
	  qDebug() << key << "=>" << mSettings->value(key);
  }
}

Settings::~Settings()
{
  delete mSettings;
  mSettings = 0;
}

int Settings::physicalWidth() const
{
  return mSettings->value("PhysicalWidth", 4096).toInt();
}

int Settings::physicalHeight() const
{
  return mSettings->value("PhysicalHeight", 4096).toInt();
}

bool Settings::flipXPos() const
{
  return mSettings->value("FlipXPos", false).toBool();
}

bool Settings::flipYPos() const
{
  return mSettings->value("FlipYPos", false).toBool();
}

bool Settings::correctXPos() const
{
  return mSettings->value("CorrectXPos", false).toBool();
}

bool Settings::correctYPos() const
{
  return mSettings->value("CorrectYPos", false).toBool();
}

const QString Settings::device() const
{
	return mSettings->value("Device", QString()).toString();
}

bool Settings::record() const
{
	return mSettings->value("Record", false).toBool();
}

const QMatrix Settings::calibrationMatrix() const
{
  qreal dx = - mSettings->value("LeftOffset", 0).toDouble();
  qreal dy = - mSettings->value("TopOffset", 0).toDouble();
  qreal m11 = 4096.0 / (4096.0 - mSettings->value("RightOffset", 0).toDouble());
  qreal m22 = 4096.0 / (4096.0 - mSettings->value("BottomOffset", 0).toDouble());
  // x-scale, x-shear, y-scale, y-shear, x-offset, y-offset
  return QMatrix(m11, 0, 0, m22, dx, dy);
}

int Settings::deltaMoveX() const
{
	double div = physicalWidth() / 4096.0;
	return (int)(mSettings->value("DeltaMove", 30.0).toDouble() / div);
}

int Settings::deltaMoveY() const
{
	double div = physicalHeight() / 4096.0;
	return (int)(mSettings->value("DeltaMove", 30.0).toDouble() / div);
}

int Settings::deltaMergeX() const
{
	double div = physicalWidth() / 4096.0;
	return (int)(mSettings->value("DeltaMerge", 7.5).toDouble() / div);
}

int Settings::deltaMergeY() const
{
	double div = physicalHeight() / 4096.0;
	return (int)(mSettings->value("DeltaMerge", 7.5).toDouble() / div);
}


} // namespace IrTouch

#include "moc_settings.cpp"

