#include "OtherConfig.h" #include "ui_OtherConfig.h" #include #include #include #include "ftpclient.h" COtherConfig::COtherConfig(QWidget *parent) : QWidget(parent), ui(new Ui::COtherConfig) { ui->setupUi(this); ui->widget->setProperty("flag", "Title"); ui->widget_2->setProperty("flag", "normal"); QRegExp exp("[0-9\\.-]+$"); QValidator *Validator = new QRegExpValidator(exp); ui->lineEdit_Trigger->setValidator(Validator); ui->lineEdit_peaktopeak->setValidator(Validator); LoadWorkingConditionConfig(); } COtherConfig::~COtherConfig() { delete ui; } void COtherConfig::LoadWorkingConditionConfig() { QString fileName = QCoreApplication::applicationDirPath() + "\\config\\TriggerConfig.json"; if(!fileName.isEmpty()) { qDebug() << "打开" << fileName ; QFile file(fileName); file.open(QIODevice::ReadOnly | QIODevice::Text); QString value = file.readAll(); file.close(); QJsonParseError parseJsonErr; QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr); if (!(parseJsonErr.error == QJsonParseError::NoError)) { QMessageBox::about(NULL, "提示", "读取文件错误!"); return; } QJsonObject jsonObject = document.object(); if(jsonObject.contains(QStringLiteral("TriggerBuffer"))) { QJsonObject TriggerBufferObj = jsonObject["TriggerBuffer"].toObject(); QJsonObject TriggerBufferOption = TriggerBufferObj["option"].toObject(); QString strAfterTime = TriggerBufferOption["afterTime"].toString(); QString strBeforeTime = TriggerBufferOption["beforeTime"].toString(); m_strTriggerTime = QString("%1").arg(strAfterTime.toInt() + strBeforeTime.toInt()); ui->lineEdit_Trigger->setText(m_strTriggerTime); } } fileName = QCoreApplication::applicationDirPath() + "\\config\\ConfidenceDegree.json"; if(!fileName.isEmpty()) { qDebug() << "打开" << fileName ; QFile file(fileName); file.open(QIODevice::ReadOnly | QIODevice::Text); QString value = file.readAll(); file.close(); QJsonParseError parseJsonErr; QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr); if (!(parseJsonErr.error == QJsonParseError::NoError)) { QMessageBox::about(NULL, "提示", "读取文件错误!"); return; } QJsonObject jsonObject = document.object(); int confidence_degree = jsonObject["confidence_degree"].toInt(); int section_num = jsonObject["section_num"].toInt(); m_strConfidence = QString("%1").arg(confidence_degree); m_strSegments = QString("%1").arg(section_num); } //读取ini QSettings settingsread(QCoreApplication::applicationDirPath() + "\\config\\config.ini",QSettings::IniFormat); settingsread.setIniCodec(QTextCodec::codecForName("UTF8")); g_strVersion = settingsread.value("main/Version").toString(); g_strProject = settingsread.value("main/Project").toString(); QTextCodec* codec = QTextCodec::codecForName("UTF-8");//添加编码格式 QTextCodec::setCodecForLocale(codec); QString str = codec->toUnicode(g_strProject.toLatin1()); qDebug() << "str" << str << QString::fromLocal8Bit(str.toLatin1()) << QString::fromLocal8Bit(g_strProject.toLatin1()) << endl; qDebug() << "code" << QTextCodec::codecForLocale()->name() << endl; ui->textEdit_project->setText(str); ui->textEdit_version->setText(codec->toUnicode(g_strVersion.toLatin1())); for (int i = 0; i < g_ChannelBaseInfo.size(); i++) { if(g_ChannelBaseInfo[i].channelType == "TACHOMETER"){ ui->comboBox_ch->addItem(g_ChannelBaseInfo[i].channelName); }else{ ui->comboBox_ch2->addItem(g_ChannelBaseInfo[i].channelName); } } ui->comboBox_logic->addItem("与"); ui->comboBox_logic->addItem("或"); fileName = QCoreApplication::applicationDirPath() + "\\config\\ZeroDrift.json"; if(!fileName.isEmpty()) { qDebug() << "打开" << fileName ; QFile file(fileName); file.open(QIODevice::ReadOnly | QIODevice::Text); QString value = file.readAll(); file.close(); QJsonParseError parseJsonErr; QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr); if (!(parseJsonErr.error == QJsonParseError::NoError)) { QMessageBox::about(NULL, "提示", "读取文件错误!"); return; } QJsonObject jsonObject = document.object(); QJsonObject tachometerObj = jsonObject["tachometerObj"].toObject(); QJsonObject ref_channelObj = jsonObject["ref_channel"].toObject(); int tachometerSlot = tachometerObj["slot"].toInt(); int tachometerChannel = tachometerObj["channel"].toInt(); int ref_channelObjSlot = ref_channelObj["slot"].toInt(); int ref_channelObjChannel = ref_channelObj["channel"].toInt(); double ref_channelObjpeaktopeak = ref_channelObj["peaktopeak"].toDouble(); int logic = jsonObject["logic"].toInt(); int operation = jsonObject["operation"].toInt(); for (int i = 0; i < g_ChannelBaseInfo.size(); i++) { if(g_ChannelBaseInfo[i].bordNo == tachometerSlot && \ g_ChannelBaseInfo[i].channelNoInBoard == tachometerChannel){ ui->comboBox_ch->setCurrentText(g_ChannelBaseInfo[i].channelName); } if(g_ChannelBaseInfo[i].bordNo == ref_channelObjSlot && \ g_ChannelBaseInfo[i].channelNoInBoard == ref_channelObjChannel){ ui->comboBox_ch2->setCurrentText(g_ChannelBaseInfo[i].channelName); } } QString str = QString("%1").arg(ref_channelObjpeaktopeak); ui->lineEdit_peaktopeak->setText(str); if(logic){ ui->comboBox_logic->setCurrentText("或"); }else{ ui->comboBox_logic->setCurrentText("与"); } if(operation){ ui->radioButton_open->setChecked(1); ui->radioButton_close->setChecked(0); }else{ ui->radioButton_open->setChecked(0); ui->radioButton_close->setChecked(1); } } } void COtherConfig::on_pushButto_Trigger_clicked() { QString str = ui->lineEdit_Trigger->text(); if(str.toInt() > 300 || str.toInt() <= 0){ QMessageBox::information(this, QStringLiteral("提示"), "请填写正确的触发时长!"); ui->lineEdit_Trigger->setText(m_strTriggerTime); return; } m_strTriggerTime = str; QString fileName = QCoreApplication::applicationDirPath() + "\\config\\TriggerConfig.json"; if(!fileName.isEmpty()) { qDebug() << "打开" << fileName ; QFile file(fileName); file.open(QIODevice::ReadOnly | QIODevice::Text); QString value = file.readAll(); file.close(); QJsonParseError parseJsonErr; QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr); if (!(parseJsonErr.error == QJsonParseError::NoError)) { QMessageBox::about(NULL, "提示", "读取文件错误!"); return; } QJsonObject jsonObject = document.object(); if(jsonObject.contains(QStringLiteral("TriggerBuffer"))) { int AfterTime = ui->lineEdit_Trigger->text().toInt()/2; int BeforeTime = ui->lineEdit_Trigger->text().toInt() - AfterTime; QJsonObject tempObj,optionObj; tempObj["afterTime"] = QString("%1").arg(AfterTime); tempObj["beforeTime"] = QString("%1").arg(BeforeTime); optionObj["option"] = tempObj; jsonObject["TriggerBuffer"] = optionObj; } QJsonDocument jsonDoc; jsonDoc.setObject(jsonObject); QString fileName = QCoreApplication::applicationDirPath() + "\\config\\TriggerConfig.json"; file.open(QIODevice::WriteOnly); file.write(jsonDoc.toJson()); file.close(); } QString strURL = QString("ftp://%1/CIDW/qtconfig/%2").arg(IP).arg("TriggerConfig.json"); g_FtpClient->SetServerInfo(strURL); g_FtpClient->SetUserInfo("root","@#cidw!@123456"); g_FtpClient->UpLoadFile(fileName,"TriggerConfig.json"); } QString COtherConfig::GBK2UTF8(QByteArray inStr) { QTextCodec *gbk = QTextCodec::codecForName("gbk"); QTextCodec *utf8 = QTextCodec::codecForName("UTF-8"); char *p = inStr.data(); QString str = gbk->toUnicode(p); QByteArray utf8_bytes=utf8->fromUnicode(str); p = utf8_bytes.data(); str = p; return str; } inline QString UTF82GBK(QByteArray &inStr) { QTextCodec *gbk = QTextCodec::codecForName("gbk"); QTextCodec *utf8 = QTextCodec::codecForName("UTF-8"); char *p = inStr.data(); QString str = utf8->toUnicode(p); QByteArray utf8_bytes=gbk->fromUnicode(str); p = utf8_bytes.data(); str = p; return str; } void COtherConfig::on_pushButton_main_clicked() { QSettings *settings = new QSettings(QCoreApplication::applicationDirPath() + "\\config\\config.ini", QSettings::IniFormat); settings->setIniCodec(QTextCodec::codecForName("UTF-8")); settings->setValue("main/Version", ui->textEdit_version->toPlainText()); settings->setValue("main/Project", ui->textEdit_project->toPlainText()); delete settings; QMessageBox::information(this, QStringLiteral("提示"), "保存成功!"); } void COtherConfig::slotNetMgr(QString sAddr, const QVariant &msg) { QJsonObject objec = msg.value(); if(objec.contains("cmd")) { QJsonValue arrays_value = objec.take("cmd"); qDebug()<<"cmd ="<comboBox_ch->currentText()){ tachometerObj["slot"] = g_ChannelBaseInfo[i].bordNo; tachometerObj["channel"] = g_ChannelBaseInfo[i].channelNoInBoard; } if(g_ChannelBaseInfo[i].channelName == ui->comboBox_ch2->currentText()){ ref_channelObj["slot"] = g_ChannelBaseInfo[i].bordNo; ref_channelObj["channel"] = g_ChannelBaseInfo[i].channelNoInBoard; } } ref_channelObj["peaktopeak"] = ui->lineEdit_peaktopeak->text().toDouble(); if(ui->comboBox_logic->currentText() == "或"){ ZeroDriftObj["logic"] = 1; }else{ ZeroDriftObj["logic"] = 0; } ZeroDriftObj["operation"] = 1; ZeroDriftObj["tachometer"] = tachometerObj; ZeroDriftObj["ref_channel"] = ref_channelObj; QJsonDocument jsonDoc; jsonDoc.setObject(ZeroDriftObj); QString fileName = QCoreApplication::applicationDirPath() + "\\config\\ZeroDrift.json"; QFile file(fileName); file.open(QIODevice::WriteOnly); file.write(jsonDoc.toJson()); file.close(); QJsonObject allObj; allObj.insert("cmd", "88"); allObj["cmdBody"] = ZeroDriftObj; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); g_NetMgr->PostJson(req,allObj); connect(g_NetMgr,SIGNAL(sigNetMgr(QString, const QVariant&)), this, SLOT(slotNetMgr(QString,const QVariant&))); } ui->comboBox_ch->setEnabled(0); ui->comboBox_ch2->setEnabled(0); ui->comboBox_logic->setEnabled(0); ui->lineEdit_peaktopeak->setEnabled(0); } void COtherConfig::on_radioButton_close_clicked() { QString fileName = QCoreApplication::applicationDirPath() + "\\config\\ZeroDrift.json"; if(!fileName.isEmpty()) { qDebug() << "打开" << fileName ; QFile file(fileName); file.open(QIODevice::ReadOnly | QIODevice::Text); QString value = file.readAll(); file.close(); QJsonParseError parseJsonErr; QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr); if (!(parseJsonErr.error == QJsonParseError::NoError)) { QMessageBox::about(NULL, "提示", "读取文件错误!"); return; } QJsonObject ZeroDriftObj; ZeroDriftObj["operation"] = 0; QJsonDocument jsonDoc; jsonDoc.setObject(ZeroDriftObj); QString fileName = QCoreApplication::applicationDirPath() + "\\config\\ZeroDrift.json"; file.open(QIODevice::WriteOnly); file.write(jsonDoc.toJson()); file.close(); QJsonObject allObj; allObj.insert("cmd", "88"); allObj["cmdBody"] = ZeroDriftObj; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); g_NetMgr->PostJson(req,allObj); connect(g_NetMgr,SIGNAL(sigNetMgr(QString, const QVariant&)), this, SLOT(slotNetMgr(QString,const QVariant&))); } ui->comboBox_ch->setEnabled(1); ui->comboBox_ch2->setEnabled(1); ui->comboBox_logic->setEnabled(1); ui->lineEdit_peaktopeak->setEnabled(1); }