#include "QtFreqParamsSetting.h"

 
QtFreqParamsSetting::QtFreqParamsSetting(QWidget* parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	QDoubleValidator* validator = new QDoubleValidator(); // 限制参数
	this->ui.lineEdit_centerfreq->setValidator(validator);
	this->ui.lineEdit_bandwidth->setValidator(validator);
	this->ui.lineEdit_Resolution->setValidator(validator);
	this->ui.lineEdit_SenceRange->setValidator(validator);
 
	// 初始化
	ui.lineEdit_centerfreq->setText(u8"9.6");
	ui.lineEdit_bandwidth->setText(u8"0.8"); 
	ui.lineEdit_Resolution->setText(u8"1.0");
	ui.lineEdit_SenceRange->setText(u8"10.0");
 
}

QtFreqParamsSetting::~QtFreqParamsSetting()
{}

void QtFreqParamsSetting::CalcFreqParams()
{
	bool resolutionRadio = this->ui.radioButton_Resolution->isChecked();
	bool bandwidthRadio = this->ui.radioButton_bandwidth->isChecked();
	QString centerfreq_str = this->ui.lineEdit_centerfreq->text();
	QString resolution_str = this->ui.lineEdit_Resolution->text();
	QString bandwidth_str = this->ui.lineEdit_bandwidth->text();
	QString	senceRange_str = this->ui.lineEdit_SenceRange->text();
	double centerfreq = 0;
	double resolution = 1;
	double bandwidth = 1;
	double senceRange = 1;
 
 
	{ // 检查参数
		if (centerfreq_str.isEmpty()) {
			QMessageBox::warning(this, u8"警告", u8"中心频率不能为空");
			return;
		}
		else {
			centerfreq = centerfreq_str.toDouble();
		}

		if (senceRange_str.isEmpty()) {
			QMessageBox::warning(this, u8"警告", u8"距离场景最小值不能为空");
			return;
		}
		else {
			senceRange = senceRange_str.toDouble();
		}

		if (resolution_str.isEmpty() && resolutionRadio) {
			QMessageBox::warning(this, u8"警告", u8"分辨率不能为空");
			return;
		}
		else {
			resolution = resolution_str.toDouble();
		}

		if (bandwidth_str.isEmpty() && bandwidthRadio) {
			QMessageBox::warning(this, u8"警告", u8"带宽不能为空");
			return;
		}
		else {
			bandwidth = bandwidth_str.toDouble();
		}

		if (senceRange < 0) {
			QMessageBox::warning(this, u8"警告", u8"距离场景最小值不能小于0");
			return;
		}
		else {}
	}

	{ // 合法性检查
		if (resolutionRadio && resolution <= 0) {
			QMessageBox::warning(this, u8"警告", u8"分辨率不能小于0");
			return;
		}
		if (bandwidthRadio && bandwidth <= 0) {
			QMessageBox::warning(this, u8"警告", u8"带宽不能小于0");
			return;
		}
		if(centerfreq<=0){
			QMessageBox::warning(this, u8"警告", u8"中心频率不能小于0");
			return;
		}
		if (senceRange <= 0) {
			QMessageBox::warning(this, u8"警告", u8"场景范围不能小于0");
			return;
		}
	}

	 // 计算
	this->freqParams=FEKOBase::getFreqSetting(centerfreq, resolution, bandwidth, senceRange, resolutionRadio);

	// 显示
	this->ui.lineEdit_Startfreq->setText(QString::number(freqParams.startfreqs));
	this->ui.lineEdit_endFreq->setText(QString::number(freqParams.endfreqs));
	this->ui.lineEdit_PointNum->setText(QString::number(freqParams.freqpoint));

	if (resolutionRadio) {
		this->ui.lineEdit_bandwidth->setText(QString::number(this->freqParams.endfreqs - this->freqParams.startfreqs));
	}
	else {
		
		//this->ui.lineEdit_Resolution->setText(QString::number(0.299792458 / bandwidthcal));
	}
	double bandwidthcal = this->freqParams.endfreqs - this->freqParams.startfreqs;
	resolution = 0.299792458 / bandwidth / 2;
	// 更新频率参数
	this->simulationparams->setStartfreq(freqParams.startfreqs);
	this->simulationparams->setEndfreq(freqParams.endfreqs);
	this->simulationparams->setFreqpoints(freqParams.freqpoint);
	this->simulationparams->setCenterFreq(centerfreq);
	this->simulationparams->setRangeResolution(resolution);
	this->simulationparams->setBandwidth(bandwidthcal);
	this->ui.lineEdit_freqdelta->setText(QString::number(bandwidthcal / (freqParams.freqpoint - 1) * 1e3));// GHz --> MHz
}
	 

FEKOBase::freqParams QtFreqParamsSetting::getParams()
{
	return this->freqParams;
}

void QtFreqParamsSetting::on_lineEdit_centerfreq_returnPressed()
{
	this->CalcFreqParams();
}

void QtFreqParamsSetting::on_lineEdit_bandwidth_returnPressed()
{
	this->CalcFreqParams();
}

void QtFreqParamsSetting::on_lineEdit_Resolution_returnPressed()
{
	this->CalcFreqParams();
}

void QtFreqParamsSetting::on_lineEdit_SenceRange_returnPressed()
{
	this->CalcFreqParams();
}

void QtFreqParamsSetting::FEKOSimulationDataparamsChanged_slots()
{
	this->ui.lineEdit_centerfreq->setText(QString::number(this->simulationparams->getCenterFreq()));
	this->ui.lineEdit_bandwidth->setText(QString::number(this->simulationparams->getBandwidth()));
	this->ui.lineEdit_Resolution->setText(QString::number(this->simulationparams->getRangeResolution()));
	this->ui.lineEdit_SenceRange->setText(QString::number((this->simulationparams->freqpoints-1)*this->simulationparams->rangeResolution));
	this->ui.lineEdit_Startfreq->setText(QString::number(this->simulationparams->getStartfreq()));
	this->ui.lineEdit_endFreq->setText(QString::number(this->simulationparams->getEndfreq()));
	this->ui.lineEdit_PointNum->setText(QString::number(this->simulationparams->getFreqpoints()));
	double bandwidthcal=this->simulationparams->getEndfreq() - this->simulationparams->getStartfreq();
	size_t freqpoints = this->simulationparams->getFreqpoints();
	this->ui.lineEdit_freqdelta->setText(QString::number(bandwidthcal / (freqpoints - 1) * 1e3)); // GHz --> MHz
}

void QtFreqParamsSetting::bandingsetFEKOSimulationDataparams()
{
	QObject::connect(this->simulationparams.get(), SIGNAL(FEKOSimulationDataparamsChanged()), this, SLOT(FEKOSimulationDataparamsChanged_slots()));

}



void QtFreqParamsSetting::on_pushButton_OK_clicked()
{
	this->CalcFreqParams();
	emit OKClick( );
}