#include "QMergeRasterProcessDialog.h"
#include "QToolProcessBarDialog.h"
#include "ImageOperatorBase.h"
#include <QString>
#include <QVector>
#include <QFileDialog>
#include <QMessageBox>
#include "ui_QMergeRasterProcessDialog.h"


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

	QObject::connect(ui->pushButtonSelect, SIGNAL(clicked(bool)), this, SLOT(onpushButtonSelectClick(bool)));
	QObject::connect(ui->AddpushButton, SIGNAL(clicked(bool)), this, SLOT(onAddpushButtonClick(bool)));
	QObject::connect(ui->RemovepushButton, SIGNAL(clicked(bool)), this, SLOT(onRemovepushButtonClick(bool)));
	QObject::connect(ui->buttonBoxDialog, SIGNAL(accepted()), this, SLOT(acceptclick()));
	QObject::connect(ui->buttonBoxDialog, SIGNAL(rejected()), this, SLOT(rejectclick()));
}

QMergeRasterProcessDialog::~QMergeRasterProcessDialog()
{}


void QMergeRasterProcessDialog::onpushButtonSelectClick(bool checked  ) {

	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getSaveFileName(this,
		u8"选择 TIF 文件",          // 对话框标题
		"",                        // 初始目录，可以设置为路径
		u8"Image Files (*.tif);;All Files (*)"); // 文件类型过滤器

	if (!fileName.isEmpty()) {
		this->ui->lineEditOutPath->setText(fileName);
	}
	else {
		QMessageBox::information(this, u8"没有选择文件", u8"没有选择任何文件");
	}
 
}

void QMergeRasterProcessDialog::onAddpushButtonClick(bool checked )
{
	QStringList fileNames = QFileDialog::getOpenFileNames(
		this,                           // 父窗口
		tr(u8"选择TIF文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"TIF Files (*.tif);;All Files (*)")  // 文件过滤器
	);

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

void QMergeRasterProcessDialog::onRemovepushButtonClick(bool checked )
{
	QList<QListWidgetItem*> selectedItems =this->ui->listWidgetRaster->selectedItems();
	for (QListWidgetItem* item : selectedItems) {
		delete this->ui->listWidgetRaster->takeItem(this->ui->listWidgetRaster->row(item));
	}
}

void QMergeRasterProcessDialog::acceptclick()
{
	QToolProcessBarDialog* processdialog = new QToolProcessBarDialog(this);

	QList<QString> infile;// (ui->listWidgetRaster->count());
	QString outstring = ui->lineEditOutPath->text();
	for (long i = 0; i < ui->listWidgetRaster->count(); i++) {
		infile.append(ui->listWidgetRaster->item(i)->text());
	}
	processdialog->show();
	processdialog->showProcess(0.0,u8"合并影像");
 
	MergeTiffs(infile, outstring);
	//MergeRasterProcess(infile, outstring, infile[0], MERGEMODE::MERGE_GEOCODING, false, processdialog);
	processdialog->close();
	QMessageBox::information(this, u8"info", u8"completed!!!");
}

void QMergeRasterProcessDialog::rejectclick()
{
	this->close();
}