#include "QSimulationBPImage.h"
#include <QFileDialog>
#include <QMessageBox>
#include "TBPImageAlgCls.h"
#include "EchoDataFormat.h"
#include <boost/thread.hpp>
#include <thread>
#include "ui_QSimulationBPImage.h"


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

	QObject::connect(ui->pushButtonEchoSelect, SIGNAL(clicked()), this, SLOT(onpushButtonEchoSelectClicked()));
	QObject::connect(ui->pushButtonImageSelect, SIGNAL(clicked()), this, SLOT(onpushButtonImageSelectClicked()));
	QObject::connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(onbtnaccepted()));
	QObject::connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(onbtnrejected()));

}

QSimulationBPImage::~QSimulationBPImage()
{}

void QSimulationBPImage::onpushButtonEchoSelectClicked()
{
	QString  fileNames = QFileDialog::getOpenFileName(
		this,                           // 父窗口
		tr(u8"选择影像文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"xml Files (*.xml);;All Files (*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileNames.isEmpty()) {
		QString message = "选择的文件有：\n";
		this->ui->lineEditEchoPath->setText(fileNames);
	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QSimulationBPImage::onpushButtonImageSelectClicked()
{
	QString  fileNames = QFileDialog::getSaveFileName(
		this,                           // 父窗口
		tr(u8"选择影像文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"All Files(*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileNames.isEmpty()) {
		this->ui->lineEditImagePath->setText(fileNames);
	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QSimulationBPImage::onbtnaccepted()
{
	QString echofile = this->ui->lineEditEchoPath->text().trimmed();
	QString outImageFolder = getParantFromPath(this->ui->lineEditImagePath->text().trimmed());
	QString imagename = getFileNameFromPath(this->ui->lineEditImagePath->text().trimmed());
	std::shared_ptr<EchoL0Dataset> echoL0ds(new EchoL0Dataset);
	echoL0ds->Open(echofile);

	std::shared_ptr< SARSimulationImageL1Dataset> imagL1(new SARSimulationImageL1Dataset);
	imagL1->setCenterAngle(echoL0ds->getCenterAngle());
	imagL1->setCenterFreq(echoL0ds->getCenterFreq());
	imagL1->setNearRange(echoL0ds->getNearRange());
	imagL1->setRefRange((echoL0ds->getNearRange() + echoL0ds->getFarRange()) / 2);
	imagL1->setFarRange(echoL0ds->getFarRange());
	imagL1->setFs(echoL0ds->getFs());
	imagL1->setLookSide(echoL0ds->getLookSide());
	imagL1->OpenOrNew(outImageFolder, imagename, echoL0ds->getPluseCount(), echoL0ds->getPlusePoints());


	TBPImageAlgCls TBPimag;
	TBPimag.setEchoL0(echoL0ds);
	TBPimag.setImageL1(imagL1);
	long cpucore_num = std::thread::hardware_concurrency();
	TBPimag.setGPU(true);
	TBPimag.Process(cpucore_num);
	QMessageBox::information(this,u8"成像",u8"成像结束");
	
}

void QSimulationBPImage::onbtnrejected()
{
	this->close();
}
