#include "QImportGF3StripL1ADataset.h"
#include <QtWidgets>
#include <QFileDialog>

QImportGF3StripL1ADataset::QImportGF3StripL1ADataset(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
 
    QListWidget* listWidgetMetaxml;
    QObject::connect( ui.pushButtonAdd,SIGNAL(clicked(clicked)),this,SLOT(onpushButtonAddClicked(bool)));
    QObject::connect(ui.pushButtonRemove, SIGNAL(clicked(clicked)), this, SLOT(onpushButtonRemoveClicked(bool)));
    QObject::connect(ui.pushButtonWorkSpace, SIGNAL(clicked(clicked)), this, SLOT(onpushButtonWorkSpaceClicked(bool)));
	QObject::connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
 
}

QImportGF3StripL1ADataset::~QImportGF3StripL1ADataset()
{

}

void QImportGF3StripL1ADataset::accept()
{
	QProgressDialog progressDialog(u8"导入GF3条带L1A影像", u8"终止", 0, ui.listWidgetMetaxml->count());
	progressDialog.setWindowTitle(u8"导入GF3条带L1A影像");
	progressDialog.setWindowModality(Qt::WindowModal);
	progressDialog.setAutoClose(true);
	progressDialog.setValue(0);
	progressDialog.setMaximum(ui.listWidgetMetaxml->count());
	progressDialog.setMinimum(0);
	progressDialog.show();
	QString outworkdir = ui.lineEditWorkDir->text();
	for (long i = 0; i < ui.listWidgetMetaxml->count(); i++) {
		QString xmlmetafilepath = ui.listWidgetMetaxml->item(i)->text();
		progressDialog.setLabelText(u8"导入影像："+getFileNameWidthoutExtend(xmlmetafilepath));
		ImportGF3L1AProcess(xmlmetafilepath, outworkdir);
		progressDialog.setValue(i);
	}
	progressDialog.close();
}

void QImportGF3StripL1ADataset::reject()
{
    this->close();
}

void QImportGF3StripL1ADataset::onpushButtonAddClicked(bool)
{
	QStringList fileNames = QFileDialog::getOpenFileNames(
		this,                           // 父窗口
		tr(u8"选择元文件"),               // 标题
		QString(),                      // 默认路径
		tr(u8"meta xml Files (*.meta.xml);;All Files (*)")  // 文件过滤器
	);

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

void QImportGF3StripL1ADataset::onpushButtonRemoveClicked(bool)
{
	QList<QListWidgetItem*> selectedItems = this->ui.listWidgetMetaxml->selectedItems();
	for (QListWidgetItem* item : selectedItems) {
		delete this->ui.listWidgetMetaxml->takeItem(this->ui.listWidgetMetaxml->row(item));
	}
}

void QImportGF3StripL1ADataset::onpushButtonWorkSpaceClicked(bool)
{
	// 调用文件选择对话框并选择一个 .tif 文件
	QString fileName = QFileDialog::getExistingDirectory(this,u8"选择工作空间路径","");
		
	if (!fileName.isEmpty()) {
		this->ui.lineEditWorkDir->setText(fileName);
	}
	else {
		QMessageBox::information(this, u8"没有选择文件夹", u8"没有选择任何文件夹");
	}
}
