#include "EyeMapInfo.h" #include "OneEyeMap.h" #include #include #include AllEyeMapInfo::~AllEyeMapInfo() { } /* 初始化函数 */ void AllEyeMapInfo::initEyeMapInfo() { m_saveFilePath = QApplication::applicationDirPath() + "/config/"; /* 判断文件夹是否存在 */ QDir dir; if(!dir.exists(m_saveFilePath)) { dir.mkpath(m_saveFilePath); } /* 判断文件是否存在 */ QString filePath = m_saveFilePath + m_saveFileName; QFile file(filePath); if(!file.exists()) { SPDLOG_INFO("设置数据文件不存在,创建文件"); createSaveFile(m_saveFileName); } else { readSaveFile(m_saveFileName); } /* 设置可用通道,给设置页面使用 */ // for(int i = 1; i <= 8; i++) // { // OneChannelInfo info; // info.channel = static_cast(i); // info.channelName = getChannelName(static_cast(i)); // GEyeMapInfo.appendChannelInfo(info); // } } /* 读取保存的文件 */ void AllEyeMapInfo::readSaveFile(const QString& fileName) { QString filePath = m_saveFilePath + fileName; QFile file(filePath); if(!file.open(QIODevice::ReadOnly)) { SPDLOG_ERROR("打开文件失败, 文件路径: {}", filePath.toStdString()); return; } QByteArray data = file.readAll(); file.close(); try { listInitEyeMapInfo.clear(); nJson json = nJson::parse(data.toStdString()); nJson jsonArray = json["EyeMapInfo"]; for(const auto& it : jsonArray) { OneEyeMapInfo info; // info.uid = static_cast(it["uid"].is_null() ? 0 : it["uid"].get()); info.num = it["num"]; info.title = QString::fromStdString(it["title"].get()); info.titleBarColor = QColor(QString::fromStdString(it["titleBarColor"].get())); info.isShow = it["isShow"]; info.channelInfo.channel = static_cast(it["OscChannelNum"].get()); info.channelInfo.channelName = QString::fromStdString(it["OscChannelName"]); info.voltageRange = static_cast(it["voltageRange"]); info.tGridValue = static_cast(it["timeGridValue"]); listInitEyeMapInfo.append(info); } nJson jsonBase = json["BaseInfo"]; row = jsonBase["row"]; column = jsonBase["column"]; } catch (const nJson::parse_error& e) { SPDLOG_ERROR("解析json文件失败, 错误信息:{}, 文件路径: {}", e.what(), filePath.toStdString()); createSaveFile(m_saveFileName); } catch (const nJson::exception& e) { SPDLOG_ERROR("解析json文件失败, 错误信息:{}, 文件路径: {}", e.what(), filePath.toStdString()); createSaveFile(m_saveFileName); } catch(...) { SPDLOG_ERROR("解析json文件失败, 文件路径: {}", filePath.toStdString()); createSaveFile(m_saveFileName); } } /* 写入保存的文件 */ void AllEyeMapInfo::createSaveFile(const QString& fileName) { QString filePath = m_saveFilePath + fileName; QFile file(filePath); if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { SPDLOG_ERROR("打开文件失败,文件路径: {}", filePath.toStdString()); return; } SPDLOG_INFO("创建数据文件成功,文件路径: {}", filePath.toStdString()); /* 设置初始化的数据 */ listInitEyeMapInfo.clear(); for(int i = 0; i < 8; i++) { OneEyeMapInfo info = getInitEyeMapInfo(i + 1); listInitEyeMapInfo.append(info); } row = 4; column = 2; nJson json0; nJson jsonArray1 = json0.array(); for(const auto& it : listInitEyeMapInfo) { nJson json2; // json2["uid"] = static_cast(it.uid); json2["num"] = it.num; json2["title"] = it.title.toStdString(); json2["titleBarColor"] = it.titleBarColor.name().toStdString(); json2["isShow"] = it.isShow; json2["OscChannelNum"] = it.channelInfo.channel; json2["OscChannelName"] = it.channelInfo.channelName.toStdString(); json2["voltageRange"] = static_cast(it.voltageRange); json2["timeGridValue"] = static_cast(it.tGridValue); jsonArray1.push_back(json2); } json0["EyeMapInfo"] = jsonArray1; /* 保存行和列 */ nJson json1; json1["row"] = 4; json1["column"] = 2; json0["BaseInfo"] = json1; auto data = json0.dump(4); file.write(data.c_str()); file.close(); } /* 更新保存的文件 */ void AllEyeMapInfo::updateSaveFile() { QString filePath = m_saveFilePath + m_saveFileName; QFile file(filePath); if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { SPDLOG_ERROR("打开文件失败, 文件路径: {}", filePath.toStdString()); return; } nJson json0; nJson jsonArray1 = json0.array(); for(const auto& it : listInitEyeMapInfo) { // SPDLOG_DEBUG("UID: {}", static_cast(it.uid)); nJson json2; // json2["uid"] = static_cast(it.uid); json2["num"] = it.num; json2["title"] = it.title.toStdString(); json2["titleBarColor"] = it.titleBarColor.name().toStdString(); json2["isShow"] = it.isShow; json2["OscChannelNum"] = it.channelInfo.channel; json2["OscChannelName"] = it.channelInfo.channelName.toStdString(); json2["voltageRange"] = static_cast(it.voltageRange); json2["timeGridValue"] = static_cast(it.tGridValue); jsonArray1.push_back(json2); } json0["EyeMapInfo"] = jsonArray1; nJson json1; json1["row"] = row; json1["column"] = column; json0["BaseInfo"] = json1; auto data = json0.dump(4); file.write(data.c_str()); file.close(); } /* 获取眼图列表 */ const QList& AllEyeMapInfo::getEyeMapList() { return listEyeMapPtr; } /* 添加眼图指针 */ void AllEyeMapInfo::addEyeMapPtr(OneEyeMap* ptr) { /* 先查找有没有重复的指针 */ for(auto it = listEyeMapPtr.begin(); it != listEyeMapPtr.end(); it++) { if(*it == ptr) { return; } } listEyeMapPtr.append(ptr); } /* 获取每个眼图的序号、标题和颜色 */ QList AllEyeMapInfo::getEyeMapInfo() { QList list; for(const auto& it : listEyeMapPtr) { OneEyeMapInfo info; info.isShow = it->getShow(); info.num = it->getNum(); info.title = it->getTitle(); info.titleBarColor = it->getTitleBarColor(); info.channelInfo = it->getChannelInfo(); info.voltageRange = it->getVoltageRange(); info.tGridValue = it->getTimeGridValue(); list.append(info); } /* 打印每个项的信息 */ // for(const auto& it : list) // { // SPDLOG_DEBUG("序号: {}, 标题: {}, 通道号: {}", it.num, it.title.toStdString(), it.channelInfo.channelName.toStdString()); // } return list; } /* 根据序号查找指针 */ OneEyeMap* AllEyeMapInfo::findEyeMap(int num) { for(const auto& it : listEyeMapPtr) { if(it->getNum() == num) { return it; } } return nullptr; } /* 更新模块信息 */ void AllEyeMapInfo::updateModuleInfo(OneEyeMapInfo& info) { for(auto& it : listEyeMapPtr) { if(it->getNum() == info.num) { it->updateInfo(info); break; } } } /* 更新设置组屏页面的信息 */ void AllEyeMapInfo::updateSettingNum(OneEyeMapInfo& info) { /* 根据通道号设置眼图模块编号 */ for(auto& it : listEyeMapPtr) { /* 根据通道号设置颜色标题等信息,不用重新获取眼图对应的示波器实例了 */ if(it->getChannelInfo().channel == info.channelInfo.channel) { SPDLOG_DEBUG("更新设置组屏页面的信息: {}, {}", info.num, info.channelInfo.channelName.toStdString()); it->updateSettingNum(info); break; } } } /* 清空所有的编号 */ void AllEyeMapInfo::clearAllNum() { for(auto& it : listEyeMapPtr) { it->setNum(0); } } /* 重置所有的颜色矩阵 */ void AllEyeMapInfo::resetAllEyeMap() { for(auto& it : listEyeMapPtr) { it->resetEyeMap(); } } /* 获取所有的通道信息,每次都是获取最新的状态 */ QList AllEyeMapInfo::getChannelInfo() { QList list; for(auto& it : listEyeMapPtr) { list.append(it->getChannelInfo()); } return list; } /* 设置通道号对应的示波器是否连接成功 */ // void AllEyeMapInfo::setChannelConnected(OscChnNum channel, bool isConnected) // { // } /* 更新显示的电压刻度信息,这里只更新电压相关的参数 */ void AllEyeMapInfo::updateVoltageAndTimeGardInfo(const OneEyeMapInfo& info) { for(auto& it : listEyeMapPtr) { if(it->getChannelInfo().channel == info.channelInfo.channel) { it->setVoltageRange(info.voltageRange); it->setTimeGridValue(info.tGridValue); break; } } } /* 更新初始化数组 */ void AllEyeMapInfo::updateInitEyeMapInfo() { listInitEyeMapInfo.clear(); for (const auto& it : listEyeMapPtr) { OneEyeMapInfo info; // info.uid = it->getUID(); info.num = it->getNum(); info.title = it->getTitle(); info.titleBarColor = it->getTitleBarColor(); info.isShow = it->getShow(); info.channelInfo = it->getChannelInfo(); info.voltageRange = it->getVoltageRange(); info.tGridValue = it->getTimeGridValue(); listInitEyeMapInfo.append(info); } } /* 获取通道号对应的通道名称 */ QString AllEyeMapInfo::getChannelName(OscChnNum channel, bool isConnected) { if(isConnected) { switch (channel) { case OscChnNum::Osc_None: return QString("请选择通道"); case OscChnNum::Osc1_CHA: return QString("示波器1通道A"); case OscChnNum::Osc1_CHB: return QString("示波器1通道B"); case OscChnNum::Osc2_CHA: return QString("示波器2通道A"); case OscChnNum::Osc2_CHB: return QString("示波器2通道B"); case OscChnNum::Osc3_CHA: return QString("示波器3通道A"); case OscChnNum::Osc3_CHB: return QString("示波器3通道B"); case OscChnNum::Osc4_CHA: return QString("示波器4通道A"); case OscChnNum::Osc4_CHB: return QString("示波器4通道B"); default: return QString("请选择通道"); } } else { switch (channel) { case OscChnNum::Osc_None: return QString("请选择通道"); case OscChnNum::Osc1_CHA: return QString("示波器1通道A (未连接)"); case OscChnNum::Osc1_CHB: return QString("示波器1通道B (未连接)"); case OscChnNum::Osc2_CHA: return QString("示波器2通道A (未连接)"); case OscChnNum::Osc2_CHB: return QString("示波器2通道B (未连接)"); case OscChnNum::Osc3_CHA: return QString("示波器3通道A (未连接)"); case OscChnNum::Osc3_CHB: return QString("示波器3通道B (未连接)"); case OscChnNum::Osc4_CHA: return QString("示波器4通道A (未连接)"); case OscChnNum::Osc4_CHB: return QString("示波器4通道B (未连接)"); default: return QString("请选择通道"); } } } /* 判断一个示波器是否有显示的眼图,有一个就返回true */ bool AllEyeMapInfo::hasShowEyeMap(int oscNum) { for(const auto& it : listEyeMapPtr) { if(it->getChannelInfo().channel == static_cast(oscNum) && it->getShow()) { return true; } } return false; } /* 根据示波器编号设置眼图状态 */ void AllEyeMapInfo::setEyeMapOscOpen(int oscNum, bool isOpen) { OscChnNum oscChn1 = static_cast(oscNum * 2 - 1); OscChnNum oscChn2 = static_cast(oscNum * 2); for(const auto& it : listEyeMapPtr) { if(it->getChannelInfo().channel == oscChn1 || it->getChannelInfo().channel == oscChn2) { it->setOpen(isOpen); } } } /* 获取一项初始化数据,通道号和通道名设置为none */ OneEyeMapInfo AllEyeMapInfo::getOneEyeMapInfo(int num) { OneEyeMapInfo info; // info.uid = static_cast(num); info.num = num; // info.title = "通道" + QString::number(num); info.titleBarColor = QColor("#2D2D31"); info.isShow = false; info.channelInfo.channel = OscChnNum::Osc_None; info.channelInfo.channelName = getChannelName(info.channelInfo.channel, false); info.title = info.channelInfo.channelName; info.voltageRange = OscVoltageRange::CR_2V5; info.tGridValue = OscTimeGridValue::TGV_200NS; return info; } OneEyeMapInfo AllEyeMapInfo::getInitEyeMapInfo(int num) { OneEyeMapInfo info; // info.uid = static_cast(num); info.num = num; // info.title = "通道" + QString::number(num); info.titleBarColor = QColor("#2D2D31"); info.isShow = false; info.channelInfo.channel = static_cast(num); info.channelInfo.channelName = getChannelName(info.channelInfo.channel, true); info.title = info.channelInfo.channelName; info.voltageRange = OscVoltageRange::CR_2V5; info.tGridValue = OscTimeGridValue::TGV_200NS; return info; } /* 获取未使用的通道号 */ OscChnNum AllEyeMapInfo::getUnusedChannel() { for(int i = 1; i <= 8; i++) { bool isUsed = false; for(const auto& it : listEyeMapPtr) { if(it->getChannelInfo().channel == static_cast(i)) { isUsed = true; break; } } if(!isUsed) { return static_cast(i); } } return OscChnNum::Osc_None; }