/***********************************************************************
**
**   settingspagepersonal.cpp
**
**   This file is part of Cumulus.
**
************************************************************************
**
**   Copyright (c):  2002 by André Somers
**
**   This file is distributed under the terms of the General Public
**   Licence. See the file COPYING for more information.
**
**   $Id: settingspagepersonal.cpp,v 1.12 2004/05/27 21:07:23 axel Exp $
**
***********************************************************************/

#include <cmath>
#include <qlayout.h>
#include <qlabel.h>

#include "generalconfig.h"
#include "altitude.h"
#include "settingspagepersonal.h"

SettingsPagePersonal::SettingsPagePersonal(QWidget *parent, const char *name ) :
  QWidget(parent,name), loadConfig(true)
{
  QGridLayout * topLayout = new QGridLayout(this, 8, 3, 5);
  int row=0;

  QLabel * lbl = new QLabel(tr("Pilot name:"), this);
  topLayout->addWidget(lbl, row, 0);
  edtName = new QLineEdit(this);
  topLayout->addMultiCellWidget(edtName, row, row, 1, 2);
  row++;

  lbl = new QLabel(tr("Date of birth:"), this);
  topLayout->addWidget(lbl, row, 0);
  edtBirth = new QLineEdit(this);
  topLayout->addMultiCellWidget(edtBirth, row, row, 1, 2);
  row++;

  topLayout->addRowSpacing(row++, 10);

  lbl = new QLabel(tr("Home site lat.:"), this);
  topLayout->addWidget(lbl, row, 0);
  edtHomeLat = new LatEdit(this, "HomeLat");
  topLayout->addMultiCellWidget(edtHomeLat, row, row, 1, 2);
  row++;

  lbl = new QLabel(tr("Home site lon.:"), this);
  topLayout->addWidget(lbl, row, 0);
  edtHomeLong = new LongEdit(this, "HomeLong");
  topLayout->addMultiCellWidget(edtHomeLong, row, row, 1, 2);
  row++;

  topLayout->addRowSpacing(row++,10);

  lbl = new QLabel(tr("Minimal arrival alt.:"), this);
  topLayout->addWidget(lbl, row, 0);

  // save current set altitude unit. This unit must be considered during
  // storage. The internal storage is always in meters.

  altUnit = Altitude::getUnit();

  const char *unit = "";

  // Input accept only feet and meters all other make no sense. Therefore all
  // other (FL) is treated as feet.

  if( altUnit == Altitude::meters )
    {
      edtMinimalArrival = new QSpinBox(0, 1000, 10, this, "MinArr");
      unit = "m";
    }
  else
    {
      edtMinimalArrival = new QSpinBox(0, 3000, 10, this, "MinArr");
      unit = "ft";
    }

  edtMinimalArrival->setButtonSymbols(QSpinBox::PlusMinus);
  topLayout->addWidget(edtMinimalArrival, row, 1);
  topLayout->addWidget(new QLabel(unit, this), row++, 2);
  topLayout->setColStretch(1,10);

  topLayout->setRowStretch(row++,10);
}

SettingsPagePersonal::~SettingsPagePersonal()
{
}

/** Called to initiate loading of the configurationfile. */
void SettingsPagePersonal::slot_load()
{
  GeneralConfig *conf = GeneralConfig::instance();

  edtName->setText( conf->getSurname() );
  edtBirth->setText( conf->getBirthday() );

  // @AP: Arrival Altitude is always saved as meter.

  Altitude minArrival = conf->getSafetyAltitude();

  if( altUnit == Altitude::meters ) // user wants meters
    {
      edtMinimalArrival->setValue((int) rint(minArrival.getMeters()));
    }
  else // user get feet
    {
      edtMinimalArrival->setValue((int) rint(minArrival.getFeet()) );
    }

  edtHomeLat->setKFLogDegree(conf->getHomeLat());
  edtHomeLong->setKFLogDegree(conf->getHomeLon());
}

/** called to initiate saving to the configurationfile */
void SettingsPagePersonal::slot_save()
{
  GeneralConfig *conf = GeneralConfig::instance();

  conf->setSurname( edtName->text() );
  conf->setBirthday( edtBirth->text() );

  // @AP: Store altitude always as meter.

  if( altUnit == Altitude::meters )
    {
      conf->setSafetyAltitude( Altitude(edtMinimalArrival->value()) );
    }
  else
    {
      Altitude currentAlt; // Altitude will be converted to feet

      currentAlt.setFeet( (double) edtMinimalArrival->value() );
      conf->setSafetyAltitude( currentAlt );
    }

  // Check, if string input values have been changed. If not, no storage to
  // avoid roundings errors. They can appear if the position formats will be
  // changed between DMS <-> DDM vise versa.

  if( edtHomeLat->isInputChanged() )
    {
      conf->setHomeLat( edtHomeLat->KFLogDegree() );
    }

  if( edtHomeLong->isInputChanged() )
    {
      conf->setHomeLon( edtHomeLong->KFLogDegree() );
    }

  conf->save();
}
