#include "TaskNodeList.h"


// =====================================================================
// OCCTShapeModelNode
//
// =====================================================================

OCCTShapeModelNode::OCCTShapeModelNode(QWidget* parent) : TaskNode(parent)
{

	this->setChecked(true);
	this->Filepath = QString("");
	// 绑定事件
	QObject::connect(this, &QCheckBox::stateChanged, this, &OCCTShapeModelNode::oncheckBoxStateChanged);
	this->initContextMenu();
}

OCCTShapeModelNode::~OCCTShapeModelNode()
{
	if ( this->Data_AIS) {
		this->myContext->Remove(this->Data_AIS, Standard_False);
		this->myContext->UpdateCurrentViewer();
	}
}

void OCCTShapeModelNode::initContextMenu()
{
	qDebug() << u8"初始化initContentListContextMenu，模型编辑模块";
	this->setContextMenuPolicy(Qt::CustomContextMenu);
	//this->setFocusPolicy(Qt::NoFocus); // 允许快捷键
	this->ContentListContextMenu = new QMenu(this); // 表格控件的右键菜单

	QAction* show_hideAction = this->ContentListContextMenu->addAction(u8"显示/隐藏"); // 
	QObject::connect(show_hideAction, SIGNAL(triggered()), this, SLOT(ShowOrHide()));

	QAction* copyAction = this->ContentListContextMenu->addAction(u8"复制对象"); // 
	copyAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_C));
	QObject::connect(copyAction, SIGNAL(triggered()), this, SLOT(copyItem()));

	QAction* RenameAction = this->ContentListContextMenu->addAction(u8"重命名"); // 
	QObject::connect(RenameAction, SIGNAL(triggered()), this, SLOT(renameItem()));

	QAction* showExtendAction = this->ContentListContextMenu->addAction(u8"缩放至"); // 
	QObject::connect(showExtendAction, SIGNAL(triggered()), this, SLOT(showExtend()));

	QAction* TranslationdAction = this->ContentListContextMenu->addAction(u8"模型操作"); // 
	QObject::connect(TranslationdAction, SIGNAL(triggered()), this, SLOT(TranslationObject()));

	QAction* removeItemAction = this->ContentListContextMenu->addAction(u8"移除对象"); // 
	QObject::connect(removeItemAction, SIGNAL(triggered()), this, SLOT(removeItem()));

	QAction* saveAction = this->ContentListContextMenu->addAction(u8"保存"); // 
	QObject::connect(saveAction, SIGNAL(triggered()), this, SLOT(saveItem()));

	QAction* saveAsAction = this->ContentListContextMenu->addAction(u8"另存为"); // 
	QObject::connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAsItem()));

	QAction* exportAction = this->ContentListContextMenu->addAction(u8"导出"); // 
	QObject::connect(exportAction, SIGNAL(triggered()), this, SLOT(ExportItem()));

	QObject::connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(ShowContentListContextMenu(QPoint)));
	qDebug() << u8"初始化contextMenu结束";
}

void OCCTShapeModelNode::HideShape()
{
	this->myContext->Erase(this->Data_AIS, Standard_True);
	this->setChecked(false);
}

void OCCTShapeModelNode::ShowShape()
{
	this->myContext->Display(this->Data_AIS, Standard_True);
	this->setChecked(true);
}

void OCCTShapeModelNode::removeDataShapeShow()
{
	if (this->myContext) {
		this->myContext->Remove(this->Data_AIS, Standard_False);
		this->myContext->UpdateCurrentViewer();
	}
}

void OCCTShapeModelNode::setContext(Handle(AIS_InteractiveContext) myContext)
{
	this->myContext = myContext;
}


void OCCTShapeModelNode::setShape(const TopoDS_Shape& Data_Shape)
{
	this->Data_AIS = new AIS_Shape(Data_Shape);
	modelRepair = false;
}

// 将模型保存到指定文件路径上
void OCCTShapeModelNode::SaveShape(QString Filepath)
{
	SaveTopoDs(Filepath, this->Shape(), this->shapetype);
}



Handle(AIS_Shape)& OCCTShapeModelNode::AIS()
{
	return this->Data_AIS;
}

const TopoDS_Shape& OCCTShapeModelNode::Shape()
{
	return this->Data_AIS->Shape();
}

 

bool OCCTShapeModelNode::setDataFile(QString filepath)
{
	QFileInfo fileinfo(filepath);
	QString filename = fileinfo.fileName();
	TopoDS_Shape shape_TopoDs;
	this->shapetype = ReadTopoDs_Shape(filepath, shape_TopoDs);
	if (this->shapetype == OCCTShapeType::NoneType) {
		return false;
	}
	this->setText(filename); // 设置文件
	this->setShape(shape_TopoDs);
	return true;
}

bool OCCTShapeModelNode::setSaveFilePath(QString filepath)
{
	this->Filepath = filepath;
	return true;
}

bool OCCTShapeModelNode::SaveFile()
{
	this->SaveShape(this->Filepath);
	return false;
}

bool OCCTShapeModelNode::CheckFilePath()
{
	if (this->Filepath.isEmpty()) {
		qDebug() << this->text() + QString(u8",  文件路径为空！！");
		return false;
	}
	if (!QDir::isAbsolutePath(this->Filepath)) {
		qDebug() << this->text() + QString(u8",  不是文件路径！！");
		return false;
	}
	return true;
}

int OCCTShapeModelNode::ExcuteTask()
{

	this->ShowShape();
	this->status = TaskStatusEnum::excuting;
	return 0;
}

int OCCTShapeModelNode::FinishTask()
{
	if (this->modelRepair) {
		// 询问是否关闭模型
		QMessageBox msgBox;
		msgBox.setText(u8"模型已经修改，是否保存模型");
		msgBox.setInformativeText("模型已经修改，是否保存模型");
		msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
		msgBox.setDefaultButton(QMessageBox::No);
		int ret = msgBox.exec();

		if (ret == QMessageBox::Yes) { // 保存模型
			this->saveItem();
		}
		else if (ret == QMessageBox::No) { // 选择不保存模型

		}
	}
	this->myContext->Remove(this->AIS(), Standard_False); // 删除模型
	this->myContext->UpdateCurrentViewer();					// 删除模型
	this->status = TaskStatusEnum::finish;
	QObject::disconnect(this, nullptr, this, nullptr);
	return 0;
}

TaskNode* OCCTShapeModelNode::CopyToNew()
{



	return nullptr;
}



void OCCTShapeModelNode::removeItem()
{
	this->FinishTask();
	emit this->deleteItem(this->text());
}

void OCCTShapeModelNode::renameItem()
{
	bool ok;
	QString oldname = this->getTaskName();
	QString  newName = QInputDialog::getText(this, "Rename Item", "Enter new name:", QLineEdit::Normal, this->text(), &ok);
	if (newName.isEmpty()) { 
		newName = oldname;
		return;
	}
	else {}
	emit this->renameItem(newName);
}

void OCCTShapeModelNode::copyItem()
{
	emit this->copyNew();
}

void OCCTShapeModelNode::showExtend()
{
	// 缩放到特定模型
	myContext->ClearSelected(Standard_True);
	myContext->AddOrRemoveSelected(this->Data_AIS, Standard_True);
	emit this->ShowFullExtend(this->Shape());
}


void OCCTShapeModelNode::saveItem()
{
	qDebug() << u8"保存模型:" << this->Filepath;
	QFileInfo  fi(this->Filepath); // 判断文件是否存在
	if (fi.exists()) {
		qDebug() << "  Path exists." << this->Filepath;
		this->SaveFile();
	}
	else {
		this->saveAsItem();
	}

}

void OCCTShapeModelNode::saveAsItem()
{
	qDebug() << "Item SaveAs.";
	QString saveFilePath = getSaveFilePath(nullptr,
		QString::fromUtf8(u8"另保存模型"),
		getOCCTShapeTypeFilterString(this->shapetype));
	// 另存为模型
	this->Filepath = saveFilePath;
	this->SaveFile();  // 设置文件路径，重新保存

}

void OCCTShapeModelNode::ExportItem() {
	OcctExportClass exportwindows(this);
	exportwindows.setDataShape(this->Shape());
	exportwindows.exec();
}

void OCCTShapeModelNode::TranslationObject()
{
	emit this->ModelModify(this);
}



void OCCTShapeModelNode::ShowContentListContextMenu(QPoint p)
{
	this->ContentListContextMenu->exec(QCursor::pos());
}

void OCCTShapeModelNode::ShowOrHide()
{
	qDebug() << u8"切换显示 \n";
	if (this->isChecked()) { // 当前显示
		qDebug() << u8"True \n";
		this->HideShape();
	}
	else {
		qDebug() << u8"false \n";
		this->ShowShape();
	}
}



void OCCTShapeModelNode::oncheckBoxStateChanged(int state) {
	OCCTShapeModelNode* senderCheckBox = qobject_cast<OCCTShapeModelNode*>(sender());

	if (senderCheckBox) {
		// 根据状态变化的 QCheckBox 显示相应的消息
		QString message = QString("%1 state changed to %2").arg(senderCheckBox->text()).arg(state == Qt::Checked ? "Checked" : "Unchecked");
		qDebug() << "Checkbox State Changed\n";
		qDebug() << message;
		if (state == Qt::Checked) {
			this->ShowShape();
		}
		else {
			this->HideShape();
		}
	}
}




// =====================================================================
// DataShowNode
//
// =====================================================================









// =====================================================================
// ComplexDataShowNode
//
// =====================================================================



ComplexDataShowNode::ComplexDataShowNode(QWidget* parent) :TaskNode(parent)
{

	this->m = nullptr;
	this->datapath = QString();
	this->setName("NOData");

	QAction* action_Load_dB_Image = this->ContentListContextMenu->addAction(u8"加载dB");
	QAction* action_Load_amp_Image = this->ContentListContextMenu->addAction(u8"加载振幅");
	QAction* action_Load_real_Image = this->ContentListContextMenu->addAction(u8"加载实部");
	QAction* action_Load_imag_Image = this->ContentListContextMenu->addAction(u8"加载虚部");
	QAction* action_Load_pha_Image = this->ContentListContextMenu->addAction(u8"加载相位");

	QObject::connect(action_Load_dB_Image, SIGNAL(triggered()), this, SLOT(Load_dB_Image()));
	QObject::connect(action_Load_amp_Image, SIGNAL(triggered()), this, SLOT(Load_amp_Image()));
	QObject::connect(action_Load_real_Image, SIGNAL(triggered()), this, SLOT(Load_real_Image()));
	QObject::connect(action_Load_imag_Image, SIGNAL(triggered()), this, SLOT(Load_imag_Image()));
	QObject::connect(action_Load_pha_Image, SIGNAL(triggered()), this, SLOT(Load_pha_Image()));

}


void ComplexDataShowNode::bangdindWindows(QMainWindow* m)
{
	this->m = m;
}


ComplexDataShowNode::~ComplexDataShowNode()
{

}


void ComplexDataShowNode::loadData()
{
	QFileInfo fileinfo(this->TaskXmlPath);
	if (fileinfo.exists()) {
		//this->echo = readMatrixXcd2ENVI_CFloat64(this->TaskXmlPath);
	}
	else {}
}

void ComplexDataShowNode::OpenData(QString TaskXmlPath)
{
	this->TaskXmlPath = TaskXmlPath;
	QFileInfo fileinfo(this->TaskXmlPath);
	if (fileinfo.exists()) {
		if (fileinfo.makeAbsolute()) {
			this->datapath = fileinfo.filePath();
		}
		else {
			this->datapath = TaskXmlPath;
		}
		this->setName(fileinfo.fileName());
	}
	else {}
	
}

int ComplexDataShowNode::ExcuteTask()
{
	this->status = TaskStatusEnum::excuting;
	this->Load_amp_Image();
	return 0;
}

void ComplexDataShowNode::Load_amp_Image()
{
	ImageShowDialogClass* DataShowDockWidget = new ImageShowDialogClass(this);
	gdalImageComplex img(this->TaskXmlPath);
	Eigen::MatrixXcd im_final = img.getDataComplex(0, 0, img.height, img.width, 1);
	Eigen::MatrixXd gt = img.getGeoTranslation();

	Eigen::MatrixXd Y(img.height, 1);
	Eigen::MatrixXd X(1, img.width);

	for (int i = 0; i < img.height; i++) {
		Y(i, 0) = gt(1, 0) + gt(1, 1) * 0 - gt(1, 2) * i;
	}
	for (int i = 0; i < img.width; i++) {
		X(i, 0) = gt(0, 0) + gt(0, 1) * i - gt(0, 2) * 0;
	}

	Eigen::MatrixXd im_amp = Complex2Amplitude(im_final);
	DataShowDockWidget->load_double_MatrixX_data(X, Y, im_amp, this->getName());

	DataShowDockWidget->show();
}

void ComplexDataShowNode::Load_pha_Image()
{
	ImageShowDialogClass* DataShowDockWidget = new ImageShowDialogClass(this);
	gdalImageComplex img(this->TaskXmlPath);
	Eigen::MatrixXcd im_final = img.getDataComplex(0, 0, img.height, img.width, 1);
	Eigen::MatrixXd gt = img.getGeoTranslation();

	Eigen::MatrixXd Y(img.height, 1);
	Eigen::MatrixXd X(1, img.width);

	for (int i = 0; i < img.height; i++) {
		Y(i, 0) = gt(1, 0) + gt(1, 1) * 0 + gt(1, 2) * i;
	}
	for (int i = 0; i < img.width; i++) {
		X(i, 0) = gt(0, 0) + gt(0, 1) * i + gt(0, 2) * 0;
	}

	Eigen::MatrixXd im_pha = Complex2phase(im_final);
	DataShowDockWidget->load_double_MatrixX_data(X, Y, im_pha, this->getName());


	DataShowDockWidget->show();
}

void ComplexDataShowNode::Load_real_Image()
{
	ImageShowDialogClass* DataShowDockWidget = new ImageShowDialogClass(this);
	gdalImageComplex img(this->TaskXmlPath);
	Eigen::MatrixXcd im_final = img.getDataComplex(0, 0, img.height, img.width, 1);
	Eigen::MatrixXd gt = img.getGeoTranslation();

	Eigen::MatrixXd Y(img.height, 1);
	Eigen::MatrixXd X(1, img.width);

	for (int i = 0; i < img.height; i++) {
		Y(i, 0) = gt(1, 0) + gt(1, 1) * 0 + gt(1, 2) * i;
	}
	for (int i = 0; i < img.width; i++) {
		X(i, 0) = gt(0, 0) + gt(0, 1) * i + gt(0, 2) * 0;
	}

	Eigen::MatrixXd im_amp = im_final.array().real();
	DataShowDockWidget->load_double_MatrixX_data(X, Y, im_amp, this->getName());

	DataShowDockWidget->show();
}

void ComplexDataShowNode::Load_imag_Image()
{
	ImageShowDialogClass* DataShowDockWidget = new ImageShowDialogClass(this);
	gdalImageComplex img(this->TaskXmlPath);
	Eigen::MatrixXcd im_final = img.getDataComplex(0, 0, img.height, img.width, 1);
	Eigen::MatrixXd gt = img.getGeoTranslation();

	Eigen::MatrixXd Y(img.height, 1);
	Eigen::MatrixXd X(1, img.width);

	for (int i = 0; i < img.height; i++) {
		Y(i, 0) = gt(1, 0) + gt(1, 1) * 0 + gt(1, 2) * i;
	}
	for (int i = 0; i < img.width; i++) {
		X(i, 0) = gt(0, 0) + gt(0, 1) * i + gt(0, 2) * 0;
	}

	Eigen::MatrixXd im_amp = im_final.array().imag();
	DataShowDockWidget->load_double_MatrixX_data(X, Y, im_amp, this->getName());

	DataShowDockWidget->show();
}

void ComplexDataShowNode::Load_dB_Image()
{
	ImageShowDialogClass* DataShowDockWidget = new ImageShowDialogClass(this);
	gdalImageComplex img(this->TaskXmlPath);
	Eigen::MatrixXcd im_final = img.getDataComplex(0, 0, img.height, img.width, 1);
	Eigen::MatrixXd gt = img.getGeoTranslation();
	Eigen::MatrixXd Y(img.height, 1);
	Eigen::MatrixXd X(1, img.width);

	for (int i = 0; i < img.height; i++) {
		Y(i, 0) = gt(1, 0) + gt(1, 1) * 0 + gt(1, 2) * i;
	}
	for (int i = 0; i < img.width; i++) {
		X(i, 0) = gt(0, 0) + gt(0, 1) * i + gt(0, 2) * 0;
	}

	Eigen::MatrixXd im_dB = Complex2dB(im_final);
	DataShowDockWidget->load_double_MatrixX_data(X, Y, im_dB, this->getName());
	DataShowDockWidget->show();
}

DataShowNode::DataShowNode(QWidget* parent) :TaskNode(parent)
{
	this->m = nullptr;
	this->datapath = QString();
	this->setName("NOData");

	QAction* action_Load_Image = this->ContentListContextMenu->addAction(u8"加载图像");
	QObject::connect(action_Load_Image, SIGNAL(triggered()), this, SLOT(Load_Image()));
}

void DataShowNode::Load_Image()
{
	this->loadData();
}




DataShowNode::~DataShowNode()
{
}

void DataShowNode::bangdindWindows(QMainWindow* m)
{
	
}

void DataShowNode::loadData()
{
	QFileInfo fileinfo(this->datapath);
	if (fileinfo.exists()) {
		gdalImage img(this->datapath);
		Eigen::MatrixXd im_final = img.getData(0, 0, img.height, img.width, 1);
		Eigen::MatrixXd gt = img.getGeoTranslation();
		Eigen::MatrixXd Y(img.height, 1);
		Eigen::MatrixXd X(1, img.width);

		for (int i = 0; i < img.height; i++) {
			Y(i, 0) = gt(1, 0) + gt(1, 1) * 0 + gt(1, 2) * i;
		}
		for (int i = 0; i < img.width; i++) {
			X(i, 0) = gt(0, 0) + gt(0, 1) * i + gt(0, 2) * 0;
		}

 
		DataShowDockWidget->load_double_MatrixX_data(X, Y, im_final, this->getName());
		DataShowDockWidget->show();

	}
}

void DataShowNode::OpenData(QString TaskXmlPath)
{
	this->TaskXmlPath = TaskXmlPath;
	QFileInfo fileinfo(this->TaskXmlPath);
	if (fileinfo.exists()) {
		if (fileinfo.makeAbsolute()) {
			this->datapath = fileinfo.filePath();
		}
		else {
			this->datapath = TaskXmlPath;
		}
		
		this->setName(fileinfo.fileName());
	}
	else {}
}

int DataShowNode::ExcuteTask()
{
	this->status = TaskStatusEnum::excuting;
	this->loadData();
	return 0;
}

// =====================================================================
// FEKOResultImportTaskNode
//
// =====================================================================


FEKOResultImportTaskNode::FEKOResultImportTaskNode()
{
}

FEKOResultImportTaskNode::~FEKOResultImportTaskNode()
{
	if (nullptr != this->TaskWindows) {
		this->TaskWindows->close();
		delete this->TaskWindows;
		this->TaskWindows = nullptr;
	}
}

int FEKOResultImportTaskNode::ExcuteTask()
{
	this->status = TaskStatusEnum::excuting;
	this->TaskWindows = new FEKOResultImport();
	this->TaskWindows->setAttribute(Qt::WA_DeleteOnClose); // 关闭时自动释放
	//this->TaskWindows->setWindowModality(Qt::WindowModal);
	QObject::connect((this->TaskWindows), SIGNAL(callbackFekoResultImport(FEKOResultImport*)), this, SLOT(getExcuteTaskResult(FEKOResultImport*)));
	// 更新参数
	this->TaskWindows->setFEKOPreProjectFolderPath(this->FolderPath);
	this->TaskWindows->setNearFieldNames(this->nearFieldNames);
	this->TaskWindows->setFarFieldNames(this->farFieldNames);
	this->TaskWindows->setSelectFieldNames(this->selectFieldNames, this->nearField);
	this->TaskWindows->setFEKOResultCSVPath(this->outPath);
	this->TaskWindows->setFEKOPreFileName(this->prename);
	this->TaskWindows->initView();
	this->TaskWindows->show();								// 展示图像
	//FEKOImportWindows->close();
	//delete FEKOImportWindows;
	return 0;
}

int FEKOResultImportTaskNode::loadXmlFile(QString xmlFilePath)
{
	this->TaskXmlPath = xmlFilePath;
	if (isExists(xmlFilePath)) {
		QDomDocument doc;
		this->loadXmlDocument(this->TaskXmlPath, doc);

		// 解析xml文件
		QDomElement root = doc.documentElement();
		// 采用DSF
		QDomNodeList rootchild = root.childNodes();
		for (int i = 0; i < rootchild.length(); i++) {
			QDomNode tempnode = rootchild.at(i);
			if (strcmp(tempnode.nodeName().toUtf8().constData(), "TaskName") == 0) {
				this->setName(tempnode.firstChild().nodeValue());
			}
			else if (strcmp(tempnode.nodeName().toUtf8().constData(), "Description") == 0) {
				this->description = tempnode.firstChild().nodeValue();
			}
			else if (strcmp(tempnode.nodeName().toUtf8().constData(), "TaskStatus") == 0) {
				QString temptext = tempnode.firstChild().nodeValue();
				if (strcmp(temptext.toUtf8().constData(), "wait") == 0) {
					this->status = TaskStatusEnum::wait;
				}
				else if (strcmp(temptext.toUtf8().constData(), "success") == 0) {
					this->status = TaskStatusEnum::success;
				}
				else if (strcmp(temptext.toUtf8().constData(), "fail") == 0) {
					this->status = TaskStatusEnum::fail;
				}
				else {}
			}
			else if (strcmp(tempnode.nodeName().toUtf8().constData(), "preName") == 0) {
				this->prename = tempnode.firstChild().nodeValue().toUtf8().constData();
			}
			else if (strcmp(tempnode.nodeName().toUtf8().constData(), "FEKOProjectFolderPath") == 0) {
				this->FolderPath = tempnode.firstChild().nodeValue().toUtf8().constData();
			}
			else if (strcmp(tempnode.nodeName().toUtf8().constData(), "NearField") == 0) {
				QString temptext = tempnode.firstChild().nodeValue().toUpper().toUtf8().constData();
				if (strcmp(temptext.toUtf8().constData(), "FALSE") == 0) {
					this->nearField = false;
				}
				else if (strcmp(temptext.toUtf8().constData(), "TRUE") == 0) {
					this->nearField = true;
				}
				else {
					this->nearField = false;
				}
			}
			else if (strcmp(tempnode.nodeName().toUtf8().constData(), "FarField") == 0) {
				QString temptext = tempnode.firstChild().nodeValue().toUpper().toUtf8().constData();
				if (strcmp(temptext.toUtf8().constData(), "FALSE") == 0) {
					this->farField = false;
				}
				else if (strcmp(temptext.toUtf8().constData(), "TRUE") == 0) {
					this->farField = true;
				}
				else {
					this->farField = false;
				}
			}
			else if (strcmp(tempnode.nodeName().toUtf8().constData(), "OutPath") == 0) {
				this->outPath = tempnode.firstChild().nodeValue().toUtf8().constData();
			}
			else if (strcmp(tempnode.nodeName().toUtf8().constData(), "NearFieldNodeList") == 0) {
				QDomNodeList tempnodechild = tempnode.childNodes();
				this->nearFieldNames = std::vector<QString>(0);
				for (int ii = 0; ii < tempnodechild.count(); ii++) {
					QDomNode tempnode = tempnodechild.at(ii);
					this->nearFieldNames.push_back(tempnode.firstChild().nodeValue().toUtf8().constData());
				}
			}
			else if (strcmp(tempnode.nodeName().toUtf8().constData(), "FarFieldNodeList") == 0) {
				QDomNodeList tempnodechild = tempnode.childNodes();
				this->farFieldNames = std::vector<QString>(0);
				for (int ii = 0; ii < tempnodechild.count(); ii++) {
					QDomNode tempnode = tempnodechild.at(ii);
					this->farFieldNames.push_back(tempnode.firstChild().nodeValue().toUtf8().constData());
				}
			}
			else if (strcmp(tempnode.nodeName().toUtf8().constData(), "SelectNodeList") == 0) {
				QDomNodeList tempnodechild = tempnode.childNodes();
				this->selectFieldNames = std::vector<QString>(0);
				for (int ii = 0; ii < tempnodechild.count(); ii++) {
					QDomNode tempnode = tempnodechild.at(ii);
					this->selectFieldNames.push_back(tempnode.firstChild().nodeValue().toUtf8().constData());
				}
			}
		}

		if (this->nearField || this->farField) {
			// 保存结果
			return -1;
		}
		else {
			QMessageBox::warning(this, u8"错误", u8"request Field 错误");
			return 1;
		}
	}
	else {
		this->TaskXmlPath = xmlFilePath;
		this->selectFieldNames = std::vector<QString>(0);
		this->farFieldNames = std::vector<QString>(0);
		this->nearFieldNames = std::vector<QString>(0);
		return 0;
	}
	return 0;
}

/// <summary>
/// 输出FEKOResult导入任务
/// </summary>
/// <returns></returns>
int FEKOResultImportTaskNode::saveXmlFile()
{
	if (isExists(this->TaskXmlPath)) {
		removeFile(this->TaskXmlPath);
	}
	else {}
	QDomDocument doc;  // 创建 任务输出xml

	QDomProcessingInstruction instruction;// 创建XML处理类，通常用于处理第一行描述信息
	instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");// 创建XML头部格式
	doc.appendChild(instruction);// 添加到XML文件中

	QDomElement root = doc.createElement("FEKOResultImportTaskNode");	// 创建根节点
	doc.appendChild(root);

	QDomElement TaskNameNode = doc.createElement("TaskName"); // 任务名称
	TaskNameNode.appendChild(doc.createTextNode(this->getName()));
	root.appendChild(TaskNameNode);

	QDomElement DescripNode = doc.createElement("Description"); // 描述
	DescripNode.appendChild(doc.createTextNode(this->description));
	root.appendChild(DescripNode);

	QDomElement TaskStatusNode = doc.createElement("TaskStatus"); // 执行状态
	if (this->status == TaskStatusEnum::wait) {
		TaskStatusNode.appendChild(doc.createTextNode("wait"));
	}
	else if (this->status == TaskStatusEnum::success) {
		TaskStatusNode.appendChild(doc.createTextNode("success"));
	}
	else if (this->status == TaskStatusEnum::fail) {
		TaskStatusNode.appendChild(doc.createTextNode("fail"));
	}
	else {}
	root.appendChild(TaskStatusNode);

	QDomElement preNameNode = doc.createElement("preName"); // 导入文件工程 .pre 文件名
	preNameNode.appendChild(doc.createTextNode(this->prename));
	root.appendChild(preNameNode);

	QDomElement FEKOProjectFolderPathNode = doc.createElement("FEKOProjectFolderPath"); // 导入文件工程 folder 文件名
	FEKOProjectFolderPathNode.appendChild(doc.createTextNode(this->FolderPath));
	root.appendChild(FEKOProjectFolderPathNode);

	QDomElement NearFieldNode = doc.createElement("NearField"); // 是否是近场文件
	NearFieldNode.appendChild(doc.createTextNode(this->nearField ? "True" : "False"));
	root.appendChild(NearFieldNode);

	QDomElement FarFieldNode = doc.createElement("FarField"); // 是否是远场文件
	FarFieldNode.appendChild(doc.createTextNode(this->farField ? "True" : "False"));
	root.appendChild(FarFieldNode);

	QDomElement outPathNode = doc.createElement("OutPath"); // 包含输出文件
	outPathNode.appendChild(doc.createTextNode(this->outPath));
	root.appendChild(outPathNode);

	QDomElement NearFieldListNode = doc.createElement("NearFieldNodeList"); // 近场数据列表性输出
	for (int i = 0; i < this->nearFieldNames.size(); i++) {
		QDomElement NearFieldMetaNode = doc.createElement("FileNode");
		NearFieldMetaNode.appendChild(doc.createTextNode(this->nearFieldNames[i]));
		NearFieldListNode.appendChild(NearFieldMetaNode);
	}
	root.appendChild(NearFieldListNode);

	QDomElement FarFieldListNode = doc.createElement("FarFieldNodeList"); // 近场数据列表性输出
	for (int i = 0; i < this->farFieldNames.size(); i++) {
		QDomElement FarFieldMetaNode = doc.createElement("FileNode");
		FarFieldMetaNode.appendChild(doc.createTextNode(this->farFieldNames[i]));
		FarFieldListNode.appendChild(FarFieldMetaNode);
	}
	root.appendChild(FarFieldListNode);

	QDomElement SelectNodeListNode = doc.createElement("SelectNodeList"); // 生成所选择数据列表
	for (int i = 0; i < this->selectFieldNames.size(); i++) {
		QDomElement SelectMetaNode = doc.createElement("FileNode");
		SelectMetaNode.appendChild(doc.createTextNode(this->selectFieldNames[i]));
		SelectNodeListNode.appendChild(SelectMetaNode);
	}
	root.appendChild(SelectNodeListNode);

	this->writeXmlDocument(doc);
	return 0;
}

int FEKOResultImportTaskNode::FinishTask()
{
	this->status = TaskStatusEnum::finish;
	return 0;
}

void FEKOResultImportTaskNode::getExcuteTaskResult(FEKOResultImport* obj)
{
	// 更新状态
	this->status = obj->getSaveSucessfully() ? TaskStatusEnum::success : TaskStatusEnum::fail;
	this->nearField = obj->getNearChecked();
	this->farField = obj->getFarChecked();
	this->nearFieldNames = obj->getNearFieldNames();
	this->farFieldNames = obj->getFarFieldNames();
	this->prename = obj->getFEKOPreFileName();
	this->selectFieldNames = obj->getSelectFieldNames();
	this->outPath = obj->getFEKOResultCSVPath();
	this->FolderPath = obj->getFEKOPreProjectFolderPath();
	this->saveXmlFile();
	QMessageBox::information(nullptr, u8"正在打开FEKO结果文件", this->outPath);
	// 打开tableview界面进行确认修改
	EchoTableEditWindow* tablewindow = new EchoTableEditWindow(this); // 打开回波编辑页面
	tablewindow->setCheckFieldContextHasEmptyCeilLOCK(true);
	tablewindow->setAttribute(Qt::WA_DeleteOnClose);// 关闭时自动释放
	tablewindow->show();
	tablewindow->setWindowTitle(u8"正在打开文件");
	//FEKOResultCsvTableModel* tablemode = new FEKOResultCsvTableModel();
	std::shared_ptr< FEKOResultCsvTableModel> tablemode = std::make_shared< FEKOResultCsvTableModel>();
	tablemode->loadCSVFilePath((this->outPath));
	tablewindow->loadTablemode(tablemode);
	tablewindow->setTableViewAutoSort(true);
	tablewindow->LockFileOpen();
	QFileInfo fileInfo((this->outPath));
	QString titletext = QString::QString(u8"正在编辑 ") + fileInfo.fileName();
	tablewindow->setWindowTitle(titletext);
	tablewindow->show();
}























// =====================================================================
// FEKOImageSettingTaskNodeClass
//
// =====================================================================



FEKOImageSettingTaskNodeClass::FEKOImageSettingTaskNodeClass()
{
	this->simulationparams = std::make_shared<FEKOBase::FEKOSimulationDataparams>(); // 创建虚拟指针
	this->TaskWindows = new QtSARAntModelSettingClass();
}


FEKOImageSettingTaskNodeClass::~FEKOImageSettingTaskNodeClass()
{

}
int FEKOImageSettingTaskNodeClass::ExcuteTask()
{
	this->status = TaskStatusEnum::excuting;
	this->TaskWindows->show();
	return 0;
}

int FEKOImageSettingTaskNodeClass::FinishTask()
{
	this->status = TaskStatusEnum::finish;
	if (this->TaskWindows) {
		this->TaskWindows->saveFEKOImageSettingXML();
		this->TaskWindows->close();
	}
	return 0;
}

int FEKOImageSettingTaskNodeClass::loadXmlFile(QString xmlFilePath)
{
	this->TaskWindows->setFEKOSimulationDataparams(this->simulationparams);
	this->TaskWindows->loadFEKOImageSettingXML(xmlFilePath);
	this->TaskWindows->ReferenceWindows();
	return 0;
}

int FEKOImageSettingTaskNodeClass::saveXmlFile()
{
	this->TaskWindows->saveFEKOImageSettingXML();
	return 0;
}

QString FEKOImageSettingTaskNodeClass::getTaskName()
{
	return this->simulationparams->taskName;
}








// =====================================================================
// FEKOScatterSettingTaskNodeClass
//
// =====================================================================

FEKOScatterSettingTaskNodeClass::FEKOScatterSettingTaskNodeClass()
{

	this->TaskWindows = new LAMP_ScatterSettingClass();
}

FEKOScatterSettingTaskNodeClass::~FEKOScatterSettingTaskNodeClass()
{

}

int FEKOScatterSettingTaskNodeClass::ExcuteTask()
{
	this->status = TaskStatusEnum::excuting;
	this->TaskWindows->show();
	return 0;
}

int FEKOScatterSettingTaskNodeClass::loadXmlFile(QString xmlFilePath)
{
	QFile file(xmlFilePath);
	if (file.exists()) {
		if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
			QMessageBox::warning(nullptr, u8"警告", u8"新建文件");
			return 0;
		}
		else {
			this->TaskWindows->loadxml(xmlFilePath);
			return 0;
		}
	}
	else {
		bool ok;
		QString  newName = QInputDialog::getText(nullptr, u8"任务名称", u8"输入任务名:", QLineEdit::Normal, u8"TestTask", &ok);
		if (newName.isEmpty()) {
			newName = u8"TestTask";
		}
		else {

		}

		this->setName(newName);
		this->TaskWindows->setTaskName(newName);
		this->TaskWindows->setWorkSpacePath(xmlFilePath);
		return 0;


	}


}

int FEKOScatterSettingTaskNodeClass::saveXmlFile()
{
	this->TaskWindows->savexml();
	return 0;
}

QString FEKOScatterSettingTaskNodeClass::getTaskName()
{
	return this->TaskWindows->settingobj->taskName;
}
