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

QResampleRefrenceRaster::QResampleRefrenceRaster(QWidget *parent)
	: QDialog(parent),ui(new Ui::QResampleRefrenceRasterClass)
{
	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)));

}

QResampleRefrenceRaster::~QResampleRefrenceRaster()
{}


void QResampleRefrenceRaster::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 QResampleRefrenceRaster::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 QResampleRefrenceRaster::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 QResampleRefrenceRaster::ondialogBtnaccepted()
{
	QString inRasterPath = this->ui->lineEditInRaster->text();
	QString RefRasterPath = this->ui->lineEditRefRaster->text();
	QString OutRasterPath = this->ui->lineEditOutRaster->text();

	std::shared_ptr<double> gt(new double[6], delArrPtr);
	gdalImage refimage(RefRasterPath);
	
	gt.get()[0] = refimage.gt(0, 0);
	gt.get()[1] = refimage.gt(0, 1);
	gt.get()[2] = refimage.gt(0, 2);
	gt.get()[3] = refimage.gt(1, 0);
	gt.get()[4] = refimage.gt(1, 1);
	gt.get()[5] = refimage.gt(1, 2);



	ResampleGDAL(inRasterPath.toLocal8Bit().constData(), OutRasterPath.toLocal8Bit().constData(),
		gt.get(), refimage.width, refimage.height,
		GDALResampleAlg::GRA_Bilinear);
	//alignRaster(inRasterPath, RefRasterPath, OutRasterPath,GDALResampleAlg::GRA_Bilinear);



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

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