#include "astrorefractiondialog.h"
#include "ui_astrorefractiondialog.h"
#include <QListView>
#include <QDebug>

extern float latitude;
extern float longitude;
extern float presure;
extern float temperature;
extern bool enabledAstroRefCorrect;
extern QString presureUnit;

AstroRefractionDialog::AstroRefractionDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AstroRefractionDialog)
{
    ui->setupUi(this);

    //this line make sure the QCombox item setting work in QSS
    ui->comboBox_presure->setView(new QListView());

    this->setFixedSize(this->width(), this->height());//设置窗口不可改变大小
    this->setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);// hide the "?" help button ont the title bar
}

AstroRefractionDialog::~AstroRefractionDialog()
{
    delete ui;
}

void AstroRefractionDialog::initValues()
{
    ui->doubleSpinBox_latitude->setValue(latitude);
    ui->doubleSpinBox_longitude->setValue(longitude);
    if(presureUnit.compare("hPa") == 0)
        ui->comboBox_presure->setCurrentIndex(1);
    else
        ui->comboBox_presure->setCurrentIndex(0);
    ui->doubleSpinBox_presure->setValue(presure);
    ui->doubleSpinBox_temperature->setValue(temperature);
    ui->checkBox_enableAstroRef->setChecked(enabledAstroRefCorrect);
}

void AstroRefractionDialog::uiReset()
{
    ui->retranslateUi(this);
}

void AstroRefractionDialog::on_comboBox_presure_currentIndexChanged(int index)
{
    double tempP = ui->doubleSpinBox_presure->value();
    if(index == 0)
    {   //kPa
        //qDebug() << "current unit kPa" << index;
        ui->doubleSpinBox_presure->setMaximum(110.0);
        ui->doubleSpinBox_presure->setMinimum(90.0);
        ui->doubleSpinBox_presure->setValue(tempP / 10.0);
    }
    else if(index == 1)
    {   //hPa
        //qDebug() << "current unit hPa" << index;
        ui->doubleSpinBox_presure->setMaximum(1100.0);
        ui->doubleSpinBox_presure->setMinimum(900.0);
        ui->doubleSpinBox_presure->setValue(tempP * 10.0);
    }

    presure = ui->doubleSpinBox_presure->value();
}

void AstroRefractionDialog::on_checkBox_enableAstroRef_toggled(bool checked)
{
    enabledAstroRefCorrect = checked;
}

void AstroRefractionDialog::on_doubleSpinBox_latitude_valueChanged(double arg1)
{
    latitude = arg1;
}

void AstroRefractionDialog::on_doubleSpinBox_longitude_valueChanged(double arg1)
{
    longitude = arg1;
}

void AstroRefractionDialog::on_doubleSpinBox_presure_valueChanged(double arg1)
{
    presure = arg1;
}

void AstroRefractionDialog::on_doubleSpinBox_temperature_valueChanged(double arg1)
{
    temperature = arg1;
}


