/***********************************************************************
**
**   altimetermodedialog.cpp
**
**   This file is part of Cumulus
**
************************************************************************
**
**   Copyright (c):  2004 by Eckhard Voellm 
**
**   This file is distributed under the terms of the General Public
**   Licence. See the file COPYING for more information.
**
**   $Id: altimetermodedialog.cpp,v 1.9 2004/07/16 10:57:29 andre Exp $
**
***********************************************************************/

#include <qlayout.h>
#include <qlabel.h>
#include <qfont.h>
#include <qradiobutton.h>
#include "generalconfig.h"
#include "altimetermodedialog.h"
#include "cucalc.h"
#include "glider.h"
#include "mapconfig.h"

extern MapConfig * _globalMapConfig;

AltimeterModeDialog::AltimeterModeDialog (QWidget *parent)
        : QDialog(parent, "altimetermodedialog", true, Qt::WStyle_StaysOnTop)
{
    setCaption (tr("Altimeter"));
    QGridLayout* topLayout = new QGridLayout(this, 2,3,5);

    QFont fnt( "Helvetica", 16, QFont::Bold  );
    this->setFont(fnt);

    GeneralConfig *conf = GeneralConfig::instance();

    _mode = conf->getAltimeterGndMode();
    _toggling_mode = conf->getAltimeterToggleMode();

    int row = 0;

    altMode=new QButtonGroup(1, Qt::Vertical, tr("Altitmeter Mode"),this);
    QRadioButton * msl=new QRadioButton(tr("MSL"),altMode);
    QRadioButton * gnd=new QRadioButton(tr("AGL"),altMode);
    msl->setChecked(true);
    topLayout->addMultiCellWidget(altMode,row,row,0,2, Qt::AlignCenter);
    row++;
    msl->setEnabled(true);
    gnd->setEnabled(true);
    altMode->setButton(_mode);

    buttonOK = new QPushButton(tr("OK"), this);
    // buttonOK->setFont(fnt1);
    topLayout->addWidget (buttonOK, row, 2);
    buttonCancel = new QPushButton (tr("Cancel"), this);
    // buttonCancel->setFont(fnt1);
    topLayout->addWidget (buttonCancel, row++, 0);

    timeout = new QTimer(this);
    connect (timeout, SIGNAL(timeout()), this, SLOT(reject()));
    connect (buttonOK, SIGNAL(clicked()), this, SLOT(accept()));
    connect (buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
}

QString AltimeterModeDialog::Pretext()
{
if( _mode  )
  return QString("Agl");
else
  return QString("Msl");
}

AltimeterModeDialog::~AltimeterModeDialog()
{
}

int AltimeterModeDialog::_mode = 0;

void AltimeterModeDialog::load()
{
    GeneralConfig *conf = GeneralConfig::instance();

    _mode = conf->getAltimeterGndMode();
    _toggling_mode = conf->getAltimeterToggleMode();

    setTimer();
}

void AltimeterModeDialog::work()
{
    // qDebug("_toggling_mode %d", _toggling_mode );

    GeneralConfig *conf = GeneralConfig::instance();

    _mode = conf->getAltimeterGndMode();

    if( _toggling_mode )
    {
        // qDebug("_mode %d", _mode );
        if( _mode )
            save( 0 );
        else
            save( 1 );
    }
    else
    {
        load();
        show();
    }
}

void AltimeterModeDialog::save(int mode)
{
    _mode = mode;
    GeneralConfig *conf = GeneralConfig::instance();
    conf->setAltimeterGndMode(_mode);
    conf->save();
    // qDebug("Save new val %d", _mode );
    emit newAltimeterMode();
    emit settingsChanged();
}

void AltimeterModeDialog::accept()
{
    int selected_mode = altMode->id(altMode->selected());

    if(  selected_mode != _mode );
    {
        _mode = selected_mode;
        // qDebug("New Altimeter Mode: %d", _mode );
        save(_mode);
    }

    QDialog::accept();
}

void AltimeterModeDialog::setTimer()
{
    // @AP: let us take the user's defined info display time
    GeneralConfig *conf = GeneralConfig::instance();

    _time = conf->getInfoDisplayTime();

    timeout->start (_time * 1000);
}
