#include "QCreateSARIntensityByLookTableDialog.h"
#include "ui_QCreateSARIntensityByLookTableDialog.h"

#include <QMessageBox>
#include <QFileDialog>
#include "ImageOperatorBase.h"

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


	connect(ui->dialogBtn, SIGNAL(accepted()), this, SLOT(ondialogBtnaccepted()));
	connect(ui->dialogBtn, SIGNAL(rejected()), this, SLOT(ondialogBtnrejected()));
	connect(ui->BtnInRaster, SIGNAL(clicked(bool)), this, SLOT(onBtnInRasterClicked(bool)));
	connect(ui->BtnOutRaster, SIGNAL(clicked(bool)), this, SLOT(onBtnOutRasterClicked(bool)));
	connect(ui->BtnRefRaster, SIGNAL(clicked(bool)), this, SLOT(onBtnRefRasterClicked(bool)));

}

QCreateSARIntensityByLookTableDialog::~QCreateSARIntensityByLookTableDialog()
{}


void QCreateSARIntensityByLookTableDialog::onBtnInRasterClicked(bool)
{
	QString fileName = QFileDialog::getOpenFileName(
		this,                           // 父窗口
		tr(u8"选择影像"),               // 标题
		QString(),                      // 默认路径
		tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->lineEditInRaster->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QCreateSARIntensityByLookTableDialog::onBtnOutRasterClicked(bool)
{
	QString fileName = QFileDialog::getSaveFileName(
		this,                           // 父窗口
		tr(u8"保存影像"),               // 标题
		QString(),                      // 默认路径
		tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->lineEditOutRaster->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QCreateSARIntensityByLookTableDialog::onBtnRefRasterClicked(bool)
{
	QString fileName = QFileDialog::getOpenFileName(
		this,                           // 父窗口
		tr(u8"选择参考影像"),               // 标题
		QString(),                      // 默认路径
		tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->lineEditRefRaster->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QCreateSARIntensityByLookTableDialog::ondialogBtnaccepted()
{
	QString inRasterPath = this->ui->lineEditInRaster->text();
	QString RefRasterPath = this->ui->lineEditRefRaster->text();
	QString OutRasterPath = this->ui->lineEditOutRaster->text();

	long minRid = ui->spinBoxMinRid->value();
	long maxRid = ui->spinBoxMaxRid->value();
	long minCid = ui->spinBoxMinCid->value();
	long maxCid = ui->spinBoxMaxCid->value();
	this->ui->progressBar->setValue(0);
	auto func = [this](long v, long maxv) { this->showPrcess(v, maxv); };	// 调用函数
	CreateSARIntensityByLookTable(inRasterPath, RefRasterPath, OutRasterPath, 
		minRid, maxRid, minCid, maxCid, func);

	//alignRaster(inRasterPath, RefRasterPath, OutRasterPath,GDALResampleAlg::GRA_Bilinear);

	QMessageBox::information(this, tr(u8"提示"), tr(u8"completed!!!"));
}

void QCreateSARIntensityByLookTableDialog::ondialogBtnrejected()
{
	this->close();
}

void QCreateSARIntensityByLookTableDialog::showPrcess(long v, long maxv)
{
	this->ui->progressBar->setMaximum(maxv);
	this->ui->progressBar->setValue(v);
}
