/***********************************************************************
**
**   settingspageairspace.cpp
**
**   This file is part of Cumulus
**
************************************************************************
**
**   Copyright (c):  2002 by Eggert Ehmke
**
**   This file is distributed under the terms of the General Public
**   Licence. See the file COPYING for more information.
**
**   $Id: settingspageairspace.cpp,v 1.11 2004/03/13 23:02:09 andre Exp $
**
***********************************************************************/

#include <cmath>
#include <qlayout.h>
#include <qlabel.h>
#include "distance.h"
#include "basemapelement.h"
#include "airspace.h"
#include "settingspageairspace.h"
#include "generalconfig.h"

SettingsPageAirspace::SettingsPageAirspace(QWidget *parent, const char *name )
  : QWidget(parent,name)
{
  // save current altitude unit. This unit must be considered during
  // storage. The internal storage is always in meters.

  altUnit = Altitude::getUnit();

  const char *unit = (altUnit == Altitude::meters) ? "m" : "ft";

  QGridLayout * topLayout = new QGridLayout(this, 3,5,5);
  int row=0;


  lvLoadOptions = new QListView(this, "loadoptionlist");
  lvLoadOptions->addColumn(tr("Enable Airspace Drawing"),192);
  lvLoadOptions->setAllColumnsShowFocus(true);
  topLayout->addMultiCellWidget(lvLoadOptions,row,row,0,4);
  row++;

  enableWarning=new QCheckBox(tr("Enable Airspace Warning"), this, "enabledraw");
  enableWarning->setChecked(true);
  topLayout->addMultiCellWidget( enableWarning, row, row, 0, 4, Qt::AlignLeft );
  row++;

  topLayout->addWidget(new QLabel(tr("Warn. dist.:"), this), row, 0);
  topLayout->setColStretch(0, 20);
  topLayout->addMultiCellWidget(new QLabel(tr("Near"), this), row, row, 1, 2);
  topLayout->addMultiCellWidget(new QLabel(tr("Very Near"), this), row, row, 3, 4);
  row++;

  topLayout->addWidget(new QLabel(tr("Horizontal:"), this),row,0);
  comboHoriWarnDist = new QSpinBox(0, 99999,1, this);
  comboHoriWarnDist->setButtonSymbols(QSpinBox::PlusMinus);
  topLayout->addWidget(comboHoriWarnDist, row, 1);
  topLayout->addWidget(new QLabel(unit, this), row, 2);

  comboHoriWarnDistVC = new QSpinBox(0, 99999,1, this);
  comboHoriWarnDistVC->setButtonSymbols(QSpinBox::PlusMinus);
  topLayout->addWidget(comboHoriWarnDistVC,row,3);
  topLayout->addWidget(new QLabel(unit, this), row++, 4);

  topLayout->addWidget(new QLabel(tr("Above:"), this),row,0);
  comboAboveWarnDist = new QSpinBox(0, 99999, 1, this);
  comboAboveWarnDist->setButtonSymbols(QSpinBox::PlusMinus);
  topLayout->addWidget(comboAboveWarnDist,row,1);
  topLayout->addWidget(new QLabel(unit, this), row, 2);

  comboAboveWarnDistVC = new QSpinBox(0, 99999, 1, this);
  comboAboveWarnDistVC->setButtonSymbols(QSpinBox::PlusMinus);
  topLayout->addWidget(comboAboveWarnDistVC, row, 3);
  topLayout->addWidget(new QLabel(unit, this), row++, 4);

  topLayout->addWidget(new QLabel(tr("Below:"), this),row,0);
  comboBelowWarnDist = new QSpinBox(0, 99999, 1, this);
  comboBelowWarnDist->setButtonSymbols(QSpinBox::PlusMinus);
  topLayout->addWidget(comboBelowWarnDist,row,1);
  topLayout->addWidget(new QLabel(unit, this), row, 2);

  comboBelowWarnDistVC = new QSpinBox(0, 99999, 1, this);
  comboBelowWarnDistVC->setButtonSymbols(QSpinBox::PlusMinus);
  topLayout->addWidget(comboBelowWarnDistVC, row, 3);
  topLayout->addWidget(new QLabel(unit, this), row++, 4);

  drawAirspaceA=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::AirA), QCheckListItem::CheckBox);
  drawAirspaceB=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::AirB), QCheckListItem::CheckBox);
  drawAirspaceC=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::AirC), QCheckListItem::CheckBox);
  drawControlC=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::ControlC), QCheckListItem::CheckBox);
  drawAirspaceD=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::AirD), QCheckListItem::CheckBox);
  drawControlD=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::ControlD), QCheckListItem::CheckBox);
  drawAirspaceElow=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::AirElow), QCheckListItem::CheckBox);
  drawAirspaceEhigh=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::AirEhigh), QCheckListItem::CheckBox);
  drawAirspaceF=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::AirF), QCheckListItem::CheckBox);
  drawRestricted=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::Restricted), QCheckListItem::CheckBox);
  drawDanger=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::Danger), QCheckListItem::CheckBox);
  drawTMZ=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::Tmz), QCheckListItem::CheckBox);
  drawLowFlight=new QCheckListItem(lvLoadOptions, Airspace::getTypeName(BaseMapElement::LowFlight), QCheckListItem::CheckBox);
 }

SettingsPageAirspace::~SettingsPageAirspace()
{
}

void SettingsPageAirspace::slot_load()
{
  GeneralConfig * conf = GeneralConfig::instance();
  AirspaceWarningDistance awd=conf->getAirspaceWarningDistances();

  if( altUnit == Altitude::meters ) // user wants meters
    {
      comboHoriWarnDist->setValue((int) rint(awd.horClose.getMeters()));
      comboHoriWarnDistVC->setValue((int) rint(awd.horVeryClose.getMeters()));
      comboAboveWarnDist->setValue((int) rint(awd.verAboveClose.getMeters()));
      comboAboveWarnDistVC->setValue((int) rint(awd.verAboveVeryClose.getMeters()));
      comboBelowWarnDist->setValue((int) rint(awd.verBelowClose.getMeters()));
      comboBelowWarnDistVC->setValue((int) rint(awd.verBelowVeryClose.getMeters()));
    }
  else // user get feet
    {
      comboHoriWarnDist->setValue((int) rint(awd.horClose.getFeet()));
      comboHoriWarnDistVC->setValue((int) rint(awd.horVeryClose.getFeet()));
      comboAboveWarnDist->setValue((int) rint(awd.verAboveClose.getFeet()));
      comboAboveWarnDistVC->setValue((int) rint(awd.verAboveVeryClose.getFeet()));
      comboBelowWarnDist->setValue((int) rint(awd.verBelowClose.getFeet()));
      comboBelowWarnDistVC->setValue((int) rint(awd.verBelowVeryClose.getFeet()));
    }

  drawAirspaceA->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::AirA));
  drawAirspaceB->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::AirB));
  drawAirspaceC->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::AirC));
  drawControlC->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::ControlC));
  drawAirspaceD->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::AirD));
  drawControlD->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::ControlD));
  drawAirspaceElow->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::AirElow));
  drawAirspaceEhigh->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::AirEhigh));
  drawAirspaceF->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::AirF));
  drawRestricted->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::Restricted));
  drawDanger->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::Danger));
  drawTMZ->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::Tmz));
  drawLowFlight->setOn (conf->getAirspaceWarningEnabled(BaseMapElement::LowFlight));

  enableWarning->setChecked(conf->getAirspaceWarningEnabled());
}


void SettingsPageAirspace::slot_save()
{
  GeneralConfig * conf = GeneralConfig::instance();
  AirspaceWarningDistance awd;

  // @AP: Store warning distances always as meters
  if( altUnit == Altitude::meters )
    {
      awd.horClose.setMeters( comboHoriWarnDist->value() );
      awd.horVeryClose.setMeters( comboHoriWarnDistVC->value() );
      awd.verAboveClose.setMeters( comboAboveWarnDist->value() );
      awd.verAboveVeryClose.setMeters( comboAboveWarnDistVC->value() );
      awd.verBelowClose.setMeters( comboBelowWarnDist->value() );
      awd.verBelowVeryClose.setMeters( comboBelowWarnDistVC->value() );
    }
  else
    {
      awd.horClose.setFeet( comboHoriWarnDist->value() );
      awd.horVeryClose.setFeet( comboHoriWarnDistVC->value() );
      awd.verAboveClose.setFeet( comboAboveWarnDist->value() );
      awd.verAboveVeryClose.setFeet( comboAboveWarnDistVC->value() );
      awd.verBelowClose.setFeet( comboBelowWarnDist->value() );
      awd.verBelowVeryClose.setFeet( comboBelowWarnDistVC->value() );
    }
  conf->setAirspaceWarningDistances(awd);

  conf->setAirspaceWarningEnabled(BaseMapElement::AirA,drawAirspaceA->isOn());
  conf->setAirspaceWarningEnabled(BaseMapElement::AirB,drawAirspaceB->isOn());
  conf->setAirspaceWarningEnabled(BaseMapElement::AirC,drawAirspaceC->isOn());
  conf->setAirspaceWarningEnabled(BaseMapElement::ControlC,drawControlC->isOn());
  conf->setAirspaceWarningEnabled(BaseMapElement::AirD,drawAirspaceD->isOn());
  conf->setAirspaceWarningEnabled(BaseMapElement::ControlD,drawControlD->isOn());
  conf->setAirspaceWarningEnabled(BaseMapElement::AirElow,drawAirspaceElow->isOn());
  conf->setAirspaceWarningEnabled(BaseMapElement::AirEhigh,drawAirspaceEhigh->isOn());
  conf->setAirspaceWarningEnabled(BaseMapElement::AirF,drawAirspaceF->isOn());
  conf->setAirspaceWarningEnabled(BaseMapElement::Restricted,drawRestricted->isOn());
  conf->setAirspaceWarningEnabled(BaseMapElement::Danger,drawDanger->isOn());
  conf->setAirspaceWarningEnabled(BaseMapElement::Tmz,drawTMZ->isOn());
  conf->setAirspaceWarningEnabled(BaseMapElement::LowFlight,drawLowFlight->isOn());

  conf->setAirspaceWarningEnabled(enableWarning->isOn());

  conf->save();
}

