QJson数据用法实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Giser_D/article/details/86615494
SonicAttribute::SonicAttribute(QWidget *parent, iDataEntity* ent_select,QList<QString> attrList, QList<QString> attr_values, int flag, QString surveyfilepath) //attrList:属性名列表,attr_values:属性值列表
	: QDialog(parent)
{
	surveyfilepath_copy = surveyfilepath;
	ent_copy = ent_select;
	QDir dir;
	dir.setCurrent(current_path); //设置相对路径
	if (flag == 0)
	{
		setPlotType(FLOWPOINT);
	}
	else if (flag == 1)
	{
		setPlotType(SONICPOINT);
	}

	ui.setupUi(this);

	//将测量文件数据添加到ui.listWidget_Filelist  FLOWPOINT:验流点 SONICPOINT:声速点
	if (!surveyfilepath.isEmpty())
	{
		QJsonObject jsonSurveyFileObject = convertQStringToQJsonObject(surveyfilepath); //jsonSurveyFileObject:测量文件列表的Json格式
		if (getPlotType() == FLOWPOINT) //判断是否为验流点
		{
			if (jsonSurveyFileObject.contains(SURVEYFILELIST_FLOWPOINT)) //判断是否key包含验流点
			{
				QJsonValue flowarray_value = jsonSurveyFileObject.take(SURVEYFILELIST_FLOWPOINT);
				if (flowarray_value.isArray())//判断是否为json数组
				{
					QJsonArray flowPointArray = flowarray_value.toArray();//转成数组
					for (int i = 0; i < flowPointArray.size(); i++)
					{
						QJsonValue flowPointValue = flowPointArray.at(i);
						if (flowPointValue.isString())
						{
							ui.listWidget_Filelist->addItem(flowPointValue.toString());
						}
					}
				}
			}
		}
		else if (getPlotType() == SONICPOINT) //判断是否为声速点
		{
			if (jsonSurveyFileObject.contains(SURVEYFILELIST_SONICPOINT))
			{
				QJsonValue sonicarray_value = jsonSurveyFileObject.take(SURVEYFILELIST_SONICPOINT);
				if (sonicarray_value.isArray())//判断是否为json数组
				{
					QJsonArray sonicPointArray = sonicarray_value.toArray();//转成数组
					for (int i = 0; i < sonicPointArray.size(); i++)
					{
						QJsonValue sonicPointValue = sonicPointArray.at(i);
						if (sonicPointValue.isString())
						{
							ui.listWidget_Filelist->addItem(sonicPointValue.toString());//添加项到测量文件列表
						}
					}
				}
			}
		}
	}
	//设置tableWidget行列
	ui.tableWidget_EntAttribute->setColumnCount(2);
	ui.tableWidget_EntAttribute->setRowCount(attrList.size());	
	ui.tableWidget_EntAttribute->setSelectionMode(QAbstractItemView::NoSelection); //设置不可选
	
	//设置tableWidget表头内容
	QStringList header;
	header.append("属性名");
	header.append("属性值");
	ui.tableWidget_EntAttribute->setHorizontalHeaderLabels(header);
	//ui.tableWidget_EntAttribute->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
	//设置宽度自适应
	//ui.tableWidget_EntAttribute->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); 
	//ui.tableWidget_EntAttribute->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
	//添加数据
	for (int i = 0; i < attrList.size(); i++)
	{ 
		//item_one:属性表名 item_two:属性值
		auto item_one = new QTableWidgetItem(attrList[i]);
		item_one->setFlags(item_one->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable); //设置不可编辑状态
		auto item_two = new QTableWidgetItem(attr_values[i]);
		item_two->setFlags(item_two->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
		ui.tableWidget_EntAttribute->setItem(i, 0, item_one);
		ui.tableWidget_EntAttribute->setItem(i, 1, item_two);
	}
	//添加右键取消功能
    ui.listWidget_Filelist->setContextMenuPolicy(Qt::CustomContextMenu);
	ui.tableWidget_EntAttribute->show();
}

//刷新属性表
void SonicAttribute::refreshTableWidget(iDataEntity* curentity)
{
	QMap<QString, QVariant> ent_attr_map; //实体属性集合
	ent_attr_map = curentity->getXData().toMap(); //返回实体属性集合
	QMap<QString, QVariant>::iterator iter = ent_attr_map.begin();
	QList<QString> attr_keys;
	QList<QString> attr_values;
	while (iter != ent_attr_map.end())
	{
		QString ent_key = iter.key();
		QString ent_value = iter.value().toString();
		attr_keys.append(ent_key);
		attr_values.append(ent_value);
		iter++;
	}

	for (int i = 0; i < attr_keys.size(); i++)
	{
		auto item_one = new QTableWidgetItem(attr_keys[i]);
		item_one->setFlags(item_one->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable); //设置不可编辑状态
		auto item_two = new QTableWidgetItem(attr_values[i]);
		item_two->setFlags(item_two->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
		ui.tableWidget_EntAttribute->setItem(i, 0, item_one);
		ui.tableWidget_EntAttribute->setItem(i, 1, item_two);
	}
}

//绝对路径转成相对路径
QString  getRelativePath(QString filepath)  //将绝对路径转成相对路径
{
	if (!filepath.contains(current_path)) //current_path为相对路径 
	{
		return filepath;
	}
	else
	{
		filepath.replace(current_path, "");
		QString relative_path = "." + filepath;
		return relative_path;
	}
}

void SonicAttribute::on_listWidget_Filelist_customContextMenuRequested(const QPoint &pos)
{
	QListWidgetItem* curItem = ui.listWidget_Filelist->itemAt(pos);
	if (curItem == NULL)
		return;
	QMenu *popMenu = new QMenu(this);
	QAction *cancelSeed = new QAction(tr("取消关联"), this);
	popMenu->addAction(cancelSeed);
	connect(cancelSeed, SIGNAL(triggered()), this, SLOT(cancelConnect())); //取消关联
	popMenu->exec(QCursor::pos());
	delete popMenu;
	delete cancelSeed;
}

//取消关联操作TXTDSC
void SonicAttribute::cancelConnect()
{
	QString remove_path;
	if (ui.listWidget_Filelist->currentIndex().row() != 0)
	{
		remove_path = ui.listWidget_Filelist->currentItem()->text(); //
		ui.listWidget_Filelist->takeItem(ui.listWidget_Filelist->currentIndex().row()); //去除一个item;
	}
	if (!remove_path.isEmpty())
	{
		//移除remove_path :判断移除完的size大小
		QJsonObject jsonSurveyObject_bef = convertQStringToQJsonObject(surveyfilepath_copy);
		if (getPlotType() == FLOWPOINT)
		{
			QJsonValue surveyfileValue = jsonSurveyObject_bef.take(SURVEYFILELIST_FLOWPOINT);
			if (surveyfileValue.isArray())
			{
				QJsonArray surveyfileArray = surveyfileValue.toArray();
				for (int i = 0; i < surveyfileArray.size(); i++)
				{
					if (surveyfileArray.at(i).toString() == remove_path)
					{
						surveyfileArray.removeAt(i);
						break;
					}
				}
				jsonSurveyObject_bef.remove(SURVEYFILELIST_FLOWPOINT);
				if (surveyfileArray.size() > 0)
				{
					jsonSurveyObject_bef.insert(SURVEYFILELIST_FLOWPOINT, surveyfileArray);
				}
				surveyfilepath_copy = convertQJsonObjectToQString(jsonSurveyObject_bef);
				ent_copy->setXData("TXTDSC",surveyfilepath_copy);
			}
		}
		else if (getPlotType() == SONICPOINT)
		{
			QJsonValue surveyfileValue = jsonSurveyObject_bef.take(SURVEYFILELIST_SONICPOINT);
			if (surveyfileValue.isArray())
			{
				QJsonArray surveyfileArray = surveyfileValue.toArray();
				for (int i = 0; i < surveyfileArray.size(); i++)
				{
					if (surveyfileArray.at(i).toString() == remove_path)
					{
						surveyfileArray.removeAt(i);
						break;
					}
				}
				jsonSurveyObject_bef.remove(SURVEYFILELIST_SONICPOINT);
				if (surveyfileArray.size() > 0)
				{
					jsonSurveyObject_bef.insert(SURVEYFILELIST_SONICPOINT, surveyfileArray);
				}
				surveyfilepath_copy = convertQJsonObjectToQString(jsonSurveyObject_bef);
				ent_copy->setXData("TXTDSC", surveyfilepath_copy);
			}
		}
	}
	refreshTableWidget(ent_copy);//刷新属性表信息
}

//关联CSV文件信息
void SonicAttribute::readFileData()
{
	if (getPlotType() == SONICPOINT)
	{
		QString filepath = QFileDialog::getOpenFileName(NULL, "请选择数据文件", QCoreApplication::applicationFilePath(), "CSV(*.CSV)");
		if (filepath.isEmpty())
		{
			QMessageBox::warning(NULL, "提示", "未选择数据文件", "确定");
			return;
		}
		QString relativePath = getRelativePath(filepath);//得到文件的相对路径

		int flag = 1;
		for (int i = 0; i < ui.listWidget_Filelist->count(); i++)
		{
			if (ui.listWidget_Filelist->item(i)->text() == relativePath)
			{
				flag = 0;
			}
		}
		if (flag == 1)
		{
			ui.listWidget_Filelist->addItem(relativePath); //添加列表
			if (surveyfilepath_copy.isEmpty()) //判断原先文件列表是否为空
			{
				QJsonArray surveyarray;
				surveyarray.insert(0, relativePath);
				QJsonObject surveyFilesObject;
				surveyFilesObject.insert(SURVEYFILELIST_SONICPOINT, surveyarray);
				QString surveyfilepath_add = convertQJsonObjectToQString(surveyFilesObject);
				surveyfilepath_copy = surveyfilepath_add;
				ent_copy->setXData("TXTDSC", surveyfilepath_copy);
			}
			else
			{
				QJsonObject surveyFilesObject = convertQStringToQJsonObject(surveyfilepath_copy);
				if (surveyFilesObject.contains(SURVEYFILELIST_SONICPOINT))
				{
					QJsonValue surveyFilesValue = surveyFilesObject.take(SURVEYFILELIST_SONICPOINT);
					if (surveyFilesValue.isArray())
					{
						QJsonArray surveyFilesArray = surveyFilesValue.toArray();
						//int surveycount = surveyFilesArray.size();
						surveyFilesArray.append(relativePath);
						//surveyFilesArray.insert(surveycount, relativePath);
						surveyFilesObject.remove(SURVEYFILELIST_SONICPOINT);
						surveyFilesObject.insert(SURVEYFILELIST_SONICPOINT, surveyFilesArray);
					}
				}
				else
				{
					QJsonArray surveyFilesArray;
					surveyFilesArray.insert(0, relativePath);
					surveyFilesObject.insert(SURVEYFILELIST_SONICPOINT, surveyFilesArray);
				}
				QString surveyfilepath_add = convertQJsonObjectToQString(surveyFilesObject);
				surveyfilepath_copy = surveyfilepath_add;
				ent_copy->setXData("TXTDSC", surveyfilepath_copy);
			}
		}

		QMainWindow *idata_window;
		g_pInterface->GetMainWindow(idata_window);
		ReadSonicPoint 	sonicpoint(idata_window, relativePath);
		sonicpoint.setWindowTitle("查看声速点");
		sonicpoint.exec();
	}
	else if (getPlotType() == FLOWPOINT)
	{
		QString filepath = QFileDialog::getOpenFileName(NULL, "请选择数据文件", QCoreApplication::applicationFilePath(), "TID(*.TID)");
		if (filepath.isEmpty())
		{
			QMessageBox::warning(NULL, "提示", "未选择数据文件", "确定");
			return;
		}
		QString relativePath = getRelativePath(filepath);
		//判断是否列表当中是否存在路径
		int flag = 1;
		for (int i = 0; i < ui.listWidget_Filelist->count(); i++)
		{
			if (ui.listWidget_Filelist->item(i)->text() == relativePath)
			{
				flag = 0;
			}
		}
		if (flag == 1)
		{
			ui.listWidget_Filelist->addItem(relativePath); //添加列表
			if (surveyfilepath_copy.isEmpty()) //判断原先文件列表是否为空
			{
				QJsonArray surveyarray;
				surveyarray.insert(0, relativePath);
				QJsonObject surveyFilesObject;
				surveyFilesObject.insert(SURVEYFILELIST_FLOWPOINT, surveyarray);
				QString surveyfilepath_add = convertQJsonObjectToQString(surveyFilesObject);
				surveyfilepath_copy = surveyfilepath_add;
				ent_copy->setXData("TXTDSC", surveyfilepath_copy);
			}
			else
			{
				QJsonObject surveyFilesObject = convertQStringToQJsonObject(surveyfilepath_copy);
				if (surveyFilesObject.contains(SURVEYFILELIST_FLOWPOINT))
				{
					QJsonValue surveyFilesValue = surveyFilesObject.take(SURVEYFILELIST_FLOWPOINT);
					if (surveyFilesValue.isArray())
					{
						QJsonArray surveyFilesArray = surveyFilesValue.toArray();
						int surveycount = surveyFilesArray.size();
						surveyFilesArray.insert(surveycount, relativePath);
						surveyFilesObject.remove(SURVEYFILELIST_FLOWPOINT);
						surveyFilesObject.insert(SURVEYFILELIST_FLOWPOINT, surveyFilesArray);
					}
				}
				else
				{
					QJsonArray surveyFilesArray;
					surveyFilesArray.append(relativePath);
					surveyFilesObject.insert(SURVEYFILELIST_FLOWPOINT, surveyFilesArray);
				}
				QString surveyfilepath_add = convertQJsonObjectToQString(surveyFilesObject);
				surveyfilepath_copy = surveyfilepath_add;
				ent_copy->setXData("TXTDSC", surveyfilepath_copy);
			}
		}
		QMainWindow *idata_window;
		g_pInterface->GetMainWindow(idata_window);
		ReadFlowPoint 	flowpoint(idata_window, relativePath);
		flowpoint.setWindowTitle("查看验流点");
		flowpoint.exec();
	}
	refreshTableWidget(ent_copy);//刷新属性表
}

猜你喜欢

转载自blog.csdn.net/Giser_D/article/details/86615494