#include "ConnectDialog.h" #include "ui_ConnectDialog.h" ConnectDialog::ConnectDialog(QWidget *parent) : QWidget(parent), ui(new Ui::ConnectDialog) { ui->setupUi(this); setWindowTitle("ConnectConfig"); //load qss QFile file(":/qss/ConnectDialog.qss"); file.open(QFile::ReadOnly); QTextStream filetext(&file); QString stylesheet = filetext.readAll(); this->setStyleSheet(stylesheet); file.close(); //载入上一次的连接数据 //读取配置文件 //配置文件是json,可扩展性比较好 readInfo(); //连接信号 connect(ui->pushButton_param1,SIGNAL(clicked()), this,SLOT(onParam1Clicked())); connect(ui->pushButton_param2,SIGNAL(clicked()), this,SLOT(onParam2Clicked())); connect(ui->pushButton_param3,SIGNAL(clicked()), this,SLOT(onParam3Clicked())); connect(ui->pushButton_param4,SIGNAL(clicked()), this,SLOT(onParam4Clicked())); connect(ui->pushButton_param5,SIGNAL(clicked()), this,SLOT(onParam5Clicked())); connect(ui->pushButton_Connect,SIGNAL(clicked()), this,SLOT(onConnectClicked())); } ConnectDialog::~ConnectDialog() { delete ui; } void ConnectDialog::setInterface(QVariant value) { m_Type = value; setInit(m_Type,m_usrName); } void ConnectDialog::setDevice(QVariant value) { m_usrName = value; setInit(m_Type,m_usrName); } void ConnectDialog::setInit(QVariant inter, QVariant device) { m_Type = inter; m_usrName = device; //根据名字查找上一次的连接,如果不存在,那么载入默认值 //查上一次的数据 bool isNew = true; for(QJsonValue info: Connect_info) { if (info.isObject()) { if(info.toObject().value("Name") == m_usrName) { //类型不一样不行 if(info.toObject().value("Type") == m_Type) { m_Param1 = info.toObject().value("Param1"); m_Param2 = info.toObject().value("Param2"); m_Param3 = info.toObject().value("Param3"); m_Param4 = info.toObject().value("Param4"); m_Param5 = info.toObject().value("Param5"); isNew = false; } else { } } } } if(isNew == true)//新建一个新的连接 { //根据接口类型新建一个界面 if(m_Type.toString() == tr("SerialPort")) { m_Param1 = tr("COM1"); m_Param2 = tr("115200"); m_Param3 = tr("8"); m_Param4 = tr("NONE"); m_Param5 = tr("1"); } else if(m_Type.toString() == tr("UDP")) { m_Param1 = tr("192.168.0.1"); m_Param2 = tr("6060"); m_Param3 = tr("192.168.0.1"); m_Param4 = tr("6061"); } } reBuildList(); } //重建列表 //usrname type local port remote port none close //usrname type com baud Len Parity Stop close void ConnectDialog::reBuildList(void) { ui->pushButton_param1->show(); ui->pushButton_param2->show(); ui->pushButton_param3->show(); ui->pushButton_param4->show(); ui->pushButton_param5->show(); if(m_Type.toString() == tr("SerialPort")) { ui->label_Param1->setText(tr("Port")); ui->label_Param2->setText(tr("Baud")); ui->label_Param3->setText(tr("Data")); ui->label_Param4->setText(tr("Parity")); ui->label_Param5->setText(tr("Stop")); QString checkbusy; foreach(QSerialPortInfo info, serialInfo->availablePorts()) { if(info.portName() == m_Param1.toString()) { ui->pushButton_Connect->setDisabled(false); if(info.isBusy()) { checkbusy.append(tr("this port is busy now\n")); ui->pushButton_Connect->setDisabled(true); } else checkbusy = ""; ui->label_Description->setText(checkbusy + info.description()); } } } else if(m_Type.toString() == tr("UDP")) { ui->label_Param1->setText(tr("Local")); ui->label_Param2->setText(tr("")); ui->label_Param3->setText(tr("Remote")); ui->label_Param4->setText(tr("")); ui->label_Param5->setText(tr("")); ui->pushButton_param5->hide(); ui->label_Description->setText(tr("NetWork")); } ui->pushButton_param1->setText(m_Param1.toString()); ui->pushButton_param2->setText(m_Param2.toString()); ui->pushButton_param3->setText(m_Param3.toString()); ui->pushButton_param4->setText(m_Param4.toString()); ui->pushButton_param5->setText(m_Param5.toString()); emit usrNameChanged(m_usrName); } void ConnectDialog::setUsrName(QVariant value) { if(value == tr("New")) { CharInputter *inputter = new CharInputter(this); inputter->setGeometry(0,0,this->width(),this->height()); inputter->setInitValue(value); connect(inputter,SIGNAL(confirmValue(QVariant)), this,SLOT(setUsrName(QVariant))); inputter->show(); } else { //查上一次的数据 bool isNew = true; for(QJsonValue info: Connect_info) { if (info.isObject()) { if(info.toObject().value("Name") == value) { m_Type = info.toObject().value("Type"); m_usrName = info.toObject().value("Name"); m_Param1 = info.toObject().value("Param1"); m_Param2 = info.toObject().value("Param2"); m_Param3 = info.toObject().value("Param3"); m_Param4 = info.toObject().value("Param4"); m_Param5 = info.toObject().value("Param5"); isNew = false; } } } if(isNew == true)//新建一个新的连接 { m_usrName = value; m_Param1 = ui->pushButton_param1->text(); m_Param2 = ui->pushButton_param2->text(); m_Param3 = ui->pushButton_param3->text(); m_Param4 = ui->pushButton_param4->text(); m_Param5 = ui->pushButton_param5->text(); } reBuildList(); } } void ConnectDialog::onUsrNameClicked(void)//英文输入 { Selector *selector = new Selector(this); selector->setGeometry(0,0,this->width(),this->height()); QStringList list; for(QJsonValue info: Connect_info) { if (info.isObject()) { //如果这个名称已经连接,那么将不会出现在列表里面 list.append(info.toObject().value("Name").toString()); } } if(!list.contains(tr("New"))) { list.append(tr("New")); } selector->setList(list,m_usrName.toString()); connect(selector,SIGNAL(confirmValue(QVariant)), this,SLOT(setUsrName(QVariant))); selector->show(); } void ConnectDialog::setType(QVariant value) { bool isContain = false; for(QJsonValue info: Connect_info) { if (info.isObject()) { if(info.toObject().value(tr("Name")).toString() == m_usrName.toString()) { if(info.toObject().value("Type") == value) { isContain = true; qDebug() << "isContain" << isContain; } } } } if(isContain)//如果存在一样名字的,那么读取旧的数据,否则设置默认值 { //如果存在,并且类型和当前选中一致,那么读取旧数据,否则新数据 for(QJsonValue info: Connect_info) { if (info.isObject()) { if(info.toObject().value("Name") == m_usrName.toString()) { m_Type = value; m_Param1 = info.toObject().value("Param1"); m_Param2 = info.toObject().value("Param2"); m_Param3 = info.toObject().value("Param3"); m_Param4 = info.toObject().value("Param4"); m_Param5 = info.toObject().value("Param5"); } } } } else { m_Type = value; //载入默认值 if(m_Type.toString() == tr("SerialPort")) { foreach(QSerialPortInfo info, serialInfo->availablePorts()) { if(!info.isBusy())//不忙的话就加入 { m_Param1 = info.portName(); } } m_Param2 = tr("115200"); m_Param3 = tr("8"); m_Param4 = tr("NONE"); m_Param5 = tr("1"); qDebug() << "set default"; } else if(m_Type.toString() == tr("UDP")) { m_Param1 = tr("127.0.0.1"); m_Param2 = tr("6061"); m_Param3 = tr("127.0.0.1"); m_Param4 = tr("6060"); m_Param5 = ""; } } reBuildList(); } void ConnectDialog::onTypeClicked(void)//选择器 { Selector *selector = new Selector(this); selector->setGeometry(0,0,this->width(),this->height()); QStringList list; list << tr("SerialPort") << tr("UDP"); //selector->setList(list,ui->pushButton_type->text()); connect(selector,SIGNAL(confirmValue(QVariant)), this,SLOT(setType(QVariant))); selector->show(); } /* //============================================================================ // 参数设置 //============================================================================ */ void ConnectDialog::setParam1(QVariant value) { m_Param1 = value; reBuildList(); } void ConnectDialog::onParam1Clicked(void)//name/localaddr 串口-选择 udp-输入 { if(m_Type.toString() == tr("SerialPort")) { Selector *selector = new Selector(this); selector->setGeometry(0,0,this->width(),this->height()); QStringList list; foreach(QSerialPortInfo info, serialInfo->availablePorts()) { if(!info.isBusy()) { list.append(info.portName()); } } selector->setList(list,ui->pushButton_param1->text()); connect(selector,SIGNAL(confirmValue(QVariant)), this,SLOT(setParam1(QVariant))); selector->show(); } else if(m_Type.toString() == tr("UDP")) { Inputter *inputter = new Inputter(this); inputter->setGeometry(0,0,this->width(),this->height()); inputter->setInputType(1); inputter->setInitValue(ui->pushButton_param1->text()); inputter->setDecimalPlaces(0); connect(inputter,SIGNAL(confirmValue(QVariant)), this,SLOT(setParam1(QVariant))); inputter->show(); } } void ConnectDialog::setParam2(QVariant value) { m_Param2 = value; reBuildList(); } void ConnectDialog::onParam2Clicked(void)//baud/localport 串口-输入 udp-输入 { Inputter *inputter = new Inputter(this); inputter->setGeometry(0,0,this->width(),this->height()); inputter->setInputType(0); inputter->setDecimalPlaces(0); inputter->setInitValue(ui->pushButton_param2->text()); connect(inputter,SIGNAL(confirmValue(QVariant)), this,SLOT(setParam2(QVariant))); inputter->show(); } void ConnectDialog::setParam3(QVariant value) { m_Param3 = value; reBuildList(); } void ConnectDialog::onParam3Clicked(void)//databit/remote addr 串口-选择 udp-输入 { if(m_Type.toString() == tr("SerialPort")) { Selector *selector = new Selector(this); selector->setGeometry(0,0,this->width(),this->height()); QStringList list; list << tr("7") << tr("8") << tr("9"); selector->setList(list,ui->pushButton_param3->text()); connect(selector,SIGNAL(confirmValue(QVariant)), this,SLOT(setParam3(QVariant))); selector->show(); } else//net { Inputter *inputter = new Inputter(this); inputter->setGeometry(0,0,this->width(),this->height()); inputter->setInputType(1); inputter->setInitValue(ui->pushButton_param3->text()); inputter->setDecimalPlaces(0); connect(inputter,SIGNAL(confirmValue(QVariant)), this,SLOT(setParam3(QVariant))); inputter->show(); } } void ConnectDialog::setParam4(QVariant value) { m_Param4 = value; reBuildList(); } void ConnectDialog::onParam4Clicked(void)//parity/remote port 串口-选择 udp-输入 { if(m_Type.toString() == tr("SerialPort")) { Selector *selector = new Selector(this); selector->setGeometry(0,0,this->width(),this->height()); QStringList list; list << tr("NONE") << tr("ODD") << tr("EVEN"); selector->setList(list,ui->pushButton_param4->text()); connect(selector,SIGNAL(confirmValue(QVariant)), this,SLOT(setParam4(QVariant))); selector->show(); } else//net { Inputter *inputter = new Inputter(this); inputter->setGeometry(0,0,this->width(),this->height()); inputter->setInitValue(ui->pushButton_param4->text()); inputter->setDecimalPlaces(0); connect(inputter,SIGNAL(confirmValue(QVariant)), this,SLOT(setParam4(QVariant))); inputter->show(); } } void ConnectDialog::setParam5(QVariant value) { m_Param5 = value; reBuildList(); } void ConnectDialog::onParam5Clicked()//stopbit/remote port 串口-选择 udp-输入 { if(m_Type.toString() == tr("SerialPort")) { Selector *selector = new Selector(this); selector->setGeometry(0,0,this->width(),this->height()); QStringList list; list << "1" << "1.5" << "2"; selector->setList(list,ui->pushButton_param5->text()); connect(selector,SIGNAL(confirmValue(QVariant)), this,SLOT(setParam5(QVariant))); selector->show(); } else//net { Inputter *inputter = new Inputter(this); inputter->setGeometry(0,0,this->width(),this->height()); inputter->setInitValue(ui->pushButton_param5->text()); inputter->setDecimalPlaces(0); connect(inputter,SIGNAL(confirmValue(QVariant)), this,SLOT(setParam5(QVariant))); inputter->show(); } } void ConnectDialog::onConnectClicked(void)//关闭本条 { if(ui->pushButton_Connect->text() == tr("Connect")) { writeInfo(m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5); //由于翻译问题,需要在这处理一下数据才能发送出去 QVariant type; if(m_Type == tr("SerialPort")) { type = "SerialPort"; QVariant Param4; if(m_Param4 == tr("NONE")) { Param4 = "NONE"; } else if(m_Param4 == tr("ODD")) { Param4 = "ODD"; } else if(m_Param4 == tr("EVEN")) { Param4 = "EVEN"; } emit connectSignal(true,m_usrName,type,m_Param1,m_Param2,m_Param3,Param4,m_Param5); } else { qDebug() << m_usrName << m_Type << m_Param1 << m_Param2 << m_Param3 << m_Param4 << m_Param5; emit connectSignal(true,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5); } } else if(ui->pushButton_Connect->text() == tr("Disconnect"))//断开连接 { QVariant type; if(m_Type == tr("SerialPort")) { type = "SerialPort"; QVariant Param4; if(m_Param4 == tr("NONE")) { Param4 = "NONE"; } else if(m_Param4 == tr("ODD")) { Param4 = "ODD"; } else if(m_Param4 == tr("EVEN")) { Param4 = "EVEN"; } emit connectSignal(false,m_usrName,type,m_Param1,m_Param2,m_Param3,Param4,m_Param5); } else { emit connectSignal(false,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5); } } } void ConnectDialog::on_pushButton_Cancel_clicked() { //清除相关的json emit cancelSignal(); } void ConnectDialog::writeInfo(QVariant Name, QVariant Type, QVariant Param1, QVariant Param2, QVariant Param3, QVariant Param4, QVariant Param5) { bool isContains = false; //读出文件,查找是否有当前的要写入的这个项,没有就新增,有就修改 QFile jsonFile(ConnectInfoFile); if (!jsonFile.open(QIODevice::ReadWrite | QIODevice::Text)) { qWarning() << "Unable to open file" << ConnectInfoFile << jsonFile.errorString(); return; } QByteArray bytes = jsonFile.readAll(); jsonFile.close(); QJsonParseError jsonParseError; QJsonDocument doc = QJsonDocument::fromJson(bytes, &jsonParseError); if (jsonParseError.error != QJsonParseError::NoError) { qWarning() << ConnectInfoFile << "Unable to open json document" << jsonParseError.errorString(); } QJsonObject root = doc.object(); QJsonArray Read_jsonArray = root.value("Connect").toArray(); int count = 0; for(QJsonValue info: Read_jsonArray) { if (info.isObject()) { if(info.toObject().value("Name") == Name) { isContains = true; break;//退出循环 } } count ++; } //使用QJsonArray添加值,并写入文件 QJsonObject jsonObject; jsonObject.insert("Name", Name.toString()); jsonObject.insert("Type", Type.toString()); jsonObject.insert("Param1", Param1.toString()); jsonObject.insert("Param2", Param2.toString()); jsonObject.insert("Param3", Param3.toString()); jsonObject.insert("Param4", Param4.toString()); jsonObject.insert("Param5", Param5.toString()); if(isContains == false) { Read_jsonArray.append(jsonObject); } else { Read_jsonArray.replace(count,jsonObject); } //设置根部和数组 root.insert("Connect",Read_jsonArray); //设置文档 doc.setObject(root); QFile file(ConnectInfoFile); if(!file.open(QIODevice::ReadWrite|QFile::Truncate)) { qDebug() << "File open error"; } file.write(doc.toJson()); file.close(); //重新刷新一下数组 readInfo(); } void ConnectDialog::readInfo(void) { QFile jsonFile(ConnectInfoFile); if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) { qWarning() << "Unable to open file" << ConnectInfoFile << jsonFile.errorString(); return; } QByteArray bytes = jsonFile.readAll(); jsonFile.close(); QJsonParseError jsonParseError; QJsonDocument doc = QJsonDocument::fromJson(bytes, &jsonParseError); if (jsonParseError.error != QJsonParseError::NoError) { qWarning() << ConnectInfoFile << "Unable to open json document" << jsonParseError.errorString(); return; } QJsonObject json = doc.object(); QJsonValue jsonValue = json.value("Connect"); if (!jsonValue.isArray()) { qWarning() << ConnectInfoFile << "Connect not array"; return; } // Iterate over MissionCommandUIInfo objects Connect_info = jsonValue.toArray(); for(QJsonValue info: Connect_info) { if (!info.isObject()) { qWarning() << ConnectInfoFile << "Connect_info should contain objects"; return; } } } void ConnectDialog::PortConnected(QVariant state, QVariant usrName, QVariant Type, QVariant Param1, QVariant Param2, QVariant Param3, QVariant Param4, QVariant Param5) { if(usrName == m_usrName) { if(state == true)//成功连接 { qDebug() << "connect success"; connectState = true; ui->pushButton_Connect->setText(tr("Disconnect")); ui->pushButton_Cancel->setEnabled(false); ui->pushButton_param1->setEnabled(false); ui->pushButton_param2->setEnabled(false); ui->pushButton_param3->setEnabled(false); ui->pushButton_param4->setEnabled(false); ui->pushButton_param5->setEnabled(false); } else { qDebug() << "connect fail"; connectState = false; ui->pushButton_Connect->setText(tr("Connect")); ui->pushButton_Cancel->setEnabled(true); ui->pushButton_param1->setEnabled(true); ui->pushButton_param2->setEnabled(true); ui->pushButton_param3->setEnabled(true); ui->pushButton_param4->setEnabled(true); ui->pushButton_param5->setEnabled(true); } } }