123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- #include "EyeMapInfo.h"
- #include "OneEyeMap.h"
- #include <QFile>
- #include <QApplication>
- #include <QDir>
- 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<OscChnNum>(i);
- // info.channelName = getChannelName(static_cast<OscChnNum>(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<EyeMapUID>(it["uid"].is_null() ? 0 : it["uid"].get<int>());
- info.num = it["num"];
- info.title = QString::fromStdString(it["title"].get<std::string>());
- info.titleBarColor = QColor(QString::fromStdString(it["titleBarColor"].get<std::string>()));
- info.isShow = it["isShow"];
- info.channelInfo.channel = static_cast<OscChnNum>(it["OscChannelNum"].get<int>());
- info.channelInfo.channelName = QString::fromStdString(it["OscChannelName"]);
- info.voltageRange = static_cast<OscVoltageRange>(it["voltageRange"]);
- info.tGridValue = static_cast<OscTimeGridValue>(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<int>(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<int>(it.voltageRange);
- json2["timeGridValue"] = static_cast<int>(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<int>(it.uid));
- nJson json2;
- // json2["uid"] = static_cast<int>(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<int>(it.voltageRange);
- json2["timeGridValue"] = static_cast<int>(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<OneEyeMap*>& 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<OneEyeMapInfo> AllEyeMapInfo::getEyeMapInfo()
- {
- QList<OneEyeMapInfo> 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<OneChannelInfo> AllEyeMapInfo::getChannelInfo()
- {
- QList<OneChannelInfo> 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<OscChnNum>(oscNum) && it->getShow())
- {
- return true;
- }
- }
- return false;
- }
- /* 根据示波器编号设置眼图状态 */
- void AllEyeMapInfo::setEyeMapOscOpen(int oscNum, bool isOpen)
- {
- OscChnNum oscChn1 = static_cast<OscChnNum>(oscNum * 2 - 1);
- OscChnNum oscChn2 = static_cast<OscChnNum>(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<EyeMapUID>(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<EyeMapUID>(num);
- info.num = num;
- // info.title = "通道" + QString::number(num);
- info.titleBarColor = QColor("#2D2D31");
- info.isShow = false;
- info.channelInfo.channel = static_cast<OscChnNum>(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<OscChnNum>(i))
- {
- isUsed = true;
- break;
- }
- }
- if(!isUsed)
- {
- return static_cast<OscChnNum>(i);
- }
- }
- return OscChnNum::Osc_None;
- }
|