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

QClipRasterByRowCols::QClipRasterByRowCols(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);


 
	connect(this->ui.accepBtn, SIGNAL(accepted()), this, SLOT(accepBtnaccept()));
	connect(this->ui.accepBtn, SIGNAL(rejected()), this, SLOT(accepBtnreject()));
	connect(this->ui.InRasterBtn, SIGNAL(clicked(bool)), this, SLOT(onInRasterBtnClicked(bool)));
	connect(this->ui.OutRasterBtn, SIGNAL(clicked(bool)), this, SLOT(onOutRasterBtnClicked(bool)));

}

QClipRasterByRowCols::~QClipRasterByRowCols()
{


}

void QClipRasterByRowCols::accepBtnaccept()
{
	QString inRasterPath = this->ui.lineEdit_InRaster->text();
	QString outRasterPath = this->ui.lineEdit_OutRaster->text();

	long minRow = this->ui.lineEdit_topRow->value();
	long maxRow = this->ui.lineEdit_bottomRow->value();
	long minCol = this->ui.lineEdit_LeftCol->value();
	long maxCol = this->ui.lineEdit_RightCol->value();

	if (maxCol < minCol || maxRow < minRow) {
		QMessageBox::warning(nullptr, u8"警告", u8"裁剪行列范围填写错误");
		return;
	}
	else {}


	// 图像裁剪
	this->ui.progressBar->setValue(10);
	clipRaster(inRasterPath, outRasterPath, minRow, maxRow, minCol, maxCol);
	this->ui.progressBar->setValue(100);
	QMessageBox::information(nullptr, u8"信息", u8"影像处理完成");
	this->ui.progressBar->setValue(0);


}

void QClipRasterByRowCols::accepBtnreject()
{
	this->close();
}

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

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

	if (!fileName.isEmpty()) {
		gdalImage inRaster(this->ui.lineEdit_InRaster->text());

		this->ui.lineEdit_topRow->setMinimum(0);
		this->ui.lineEdit_bottomRow->setMinimum(0);
		this->ui.lineEdit_LeftCol->setMinimum(0);
		this->ui.lineEdit_RightCol->setMinimum(0);

		this->ui.lineEdit_topRow->setMaximum(inRaster.height);
		this->ui.lineEdit_bottomRow->setMaximum(inRaster.height);

		this->ui.lineEdit_LeftCol->setMaximum(inRaster.width);
		this->ui.lineEdit_RightCol->setMaximum(inRaster.width);
	}

}

void QClipRasterByRowCols::onOutRasterBtnClicked(bool)
{
	QString fileName = QFileDialog::getSaveFileName(
		this,                           // 父窗口
		tr(u8"选择xml文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"dat Files (*.dat);;bin Files (*.bin);;All Files (*)")  // 文件过滤器
	);

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