#include "CommandUI.h" #include "ui_CommandUI.h" #include "QtTextToSpeech/QtTextToSpeech" CommandUI::CommandUI(QWidget *parent) : QWidget(parent), ui(new Ui::CommandUI) { ui->setupUi(this); this->setAcceptDrops(true); m_parent = parent; //检测文件夹,如果不存在,那么就新建一个,这里存着所有的qml文件 QDir *Dir = new QDir; if(!Dir->exists("./commands")) Dir->mkdir("./commands");//如果文件夹不存在就新建 //load qss QFile file(":/qss/CommandUI.qss"); file.open(QFile::ReadOnly); QTextStream filetext(&file); QString stylesheet = filetext.readAll(); this->setStyleSheet(stylesheet); file.close(); //查找默认的值 Config *config = new Config(); if(config->getCommandFile().size() >0) { CommandFile = config->getCommandFile(); } delete config; setCommandFile(CommandFile); DoubleClickTimer = new QTimer(this); DoubleClickTimer->setInterval(200);//0.2s之内再点击,那就相当于双击 connect(DoubleClickTimer,SIGNAL(timeout()), this,SLOT(DoubleClickTimeout())); ui->comboBox_seq->hide(); ui->label_seq->hide(); ui->pushButton_setSeq->hide(); ui->comboBox_uav->hide(); ui->label_uav->hide(); ui->pushButton_setUAV->hide(); } CommandUI::~CommandUI() { delete ui; } void CommandUI::wheelEvent(QWheelEvent *event) { int max = ui->tabWidget_Command->count(); int current = ui->tabWidget_Command->currentIndex(); if(event->delta() < 0) { if(current < (max - 1)) { current++; ui->tabWidget_Command->setCurrentIndex(current); } } else if(event->delta() > 0) { if(current > 0) { current--; ui->tabWidget_Command->setCurrentIndex(current); } } } void CommandUI::dragEnterEvent(QDragEnterEvent *event) { if(!event->mimeData()->urls()[0].fileName().right(5).compare(tr(".json"))) { event->acceptProposedAction(); } else { event->ignore(); } } void CommandUI::dropEvent(QDropEvent *event) { const QMimeData *qm = event->mimeData(); QString fileName = qm->urls()[0].toLocalFile(); if(fileName.right(5) == ".json") { setCommandFile(fileName); } } void CommandUI::keyPressEvent(QKeyEvent *event) //键盘按下事件 { switch(event->key()) { case Qt::Key_Space : { qInfo() << "command key" << event; } break; } } void CommandUI::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::MiddleButton) { } else if (event->button() == Qt::RightButton) { if(event->modifiers() == Qt::ControlModifier) { QFileDialog *dlg = new QFileDialog(); //dlg->show(); QString fileName = dlg->getOpenFileName(this, tr("Selete Command File..."), "./commands/", tr("command file (*.json)")); if(!fileName.isEmpty()) { setCommandFile(fileName); } } else if(event->modifiers() == Qt::AltModifier) { //CommandEditor *edit = new CommandEditor(); //edit->show(); } } } void CommandUI::updateUI(void) { loadCommandJson(CommandFile); for(QJsonValue info: CMD_infoArray) { if (!info.isObject()) { return; } if(!ui->tabWidget_Command->widget(info.toObject().find(_PageJsonKey).value().toInt())) { QWidget *w = new QWidget(); qInfo() << "Command Can't find index of " << info.toObject().find(_PageJsonKey).value().toInt(); if(info.toObject().find(_PageJsonKey).value().toInt() == 0) { ui->tabWidget_Command->insertTab(info.toObject().find(_PageJsonKey).value().toInt(), w, info.toObject().find(_TitleJsonKey).value().toString()); qInfo() << "Command Create Base Command Page"; } else { ui->tabWidget_Command->insertTab(info.toObject().find(_PageJsonKey).value().toInt(), w, info.toObject().find(_TitleJsonKey).value().toString()); qInfo() << "Command Create Advance Command " << info.toObject().find(_TitleJsonKey).value().toString() << "Page"; } } QJsonArray BtnArray = info.toObject().find(_CommandsJsonKey).value().toArray(); for(QJsonValue binfo: BtnArray) { if (!binfo.isObject()) { return; } CommandButton *btn = new CommandButton(ui->tabWidget_Command->widget(info.toObject().find(_PageJsonKey).value().toInt())); btn->setParameters(binfo.toObject().find(_rowJsonKey).value().toVariant(), binfo.toObject().find(_colunmJsonKey).value().toVariant(), binfo.toObject().find(_noticeJsonKey).value().toVariant(), binfo.toObject().find(_commandJsonKey).value().toVariant(), binfo.toObject().find(_confirmJsonKey).value().toVariant(), info.toObject().find(_PageJsonKey).value().toVariant(), binfo.toObject().find(_dialogJsonKey).value().toVariant(), binfo.toObject().find(_param1JsonKey).value().toVariant(), binfo.toObject().find(_param2JsonKey).value().toVariant(), binfo.toObject().find(_param3JsonKey).value().toVariant(), binfo.toObject().find(_param4JsonKey).value().toVariant(), binfo.toObject().find(_param5JsonKey).value().toVariant(), binfo.toObject().find(_param6JsonKey).value().toVariant(), binfo.toObject().find(_param7JsonKey).value().toVariant()); btn->show(); btn->raise(); btn->setText(binfo.toObject().find(_TextJsonKey).value().toString()); btn->resize(m_width,m_height); btn->move(binfo.toObject().find(_colunmJsonKey).value().toInt() * (m_width + m_spacing) + m_spacing, binfo.toObject().find(_rowJsonKey).value().toInt() * (m_height + m_spacing)+ m_spacing); btn->setProperty("state",state::success); btn->style()->unpolish(btn); btn->style()->polish(btn); connect(btn,&CommandButton::clicked, this,&CommandUI::on_commandClicked); } } } void CommandUI::setCommandFile(QString File) { Config *config = new Config(); config->setCommandFile(File); delete config; CommandFile = File; for(int i = 0;i < ui->tabWidget_Command->count();i++) { QObjectList wlist = ui->tabWidget_Command->widget(i)->children(); foreach (QObject *b, wlist) { CommandButton *btn = qobject_cast(b); if(btn) { disconnect(btn,SIGNAL(clicked(bool)), this,SLOT(on_commandClicked())); delete btn; } } delete ui->tabWidget_Command->widget(i); } ui->tabWidget_Command->clear(); updateUI(); } void CommandUI::loadCommandJson(const QString& jsonFilename) { if (jsonFilename.isEmpty()) { return; } qDebug() << "Loading " << jsonFilename; QFile jsonFile(jsonFilename); if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) { qWarning() << "Unable to open file" << jsonFilename << jsonFile.errorString(); return; } QByteArray bytes = jsonFile.readAll(); jsonFile.close(); QJsonParseError jsonParseError; QJsonDocument doc = QJsonDocument::fromJson(bytes, &jsonParseError); if (jsonParseError.error != QJsonParseError::NoError) { qWarning() << jsonFilename << "Unable to open json document" << jsonParseError.errorString(); return; } QJsonObject json = doc.object(); m_version = json.value(_versionJsonKey).toString(); qDebug() << "command version :" << m_version; m_width = json.value(_WidthJsonKey).toInt(); m_height = json.value(_HeightJsonKey).toInt(); m_spacing = json.value(_spacingJsonKey).toInt(); QJsonValue jsonValue = json.value(_ButtonsJsonKey); if (!jsonValue.isArray()) { qWarning() << jsonFilename << "_CommandButtonJsonKey not array"; return; } CMD_infoArray = jsonValue.toArray(); for(QJsonValue info: CMD_infoArray) { if (!info.isObject()) { qWarning() << jsonFilename << "mavCmdArray should contain objects"; return; } } } void CommandUI::setSecondConfirm(QVariant value) { if(value.toBool() == true) { for(int i = 0;i < ui->tabWidget_Command->count();i++) { QObjectList list = ui->tabWidget_Command->widget(i)->children(); foreach (QObject *obj, list) { CommandButton *btn = qobject_cast(obj); if(btn) { if((btn->text() == m_name) &&(btn->Command() == m_command) &&(btn->Confirm() == m_confirm) &&(btn->Param1() == m_param1) &&(btn->Param2() == m_param2) &&(btn->Param3() == m_param3) &&(btn->Param4() == m_param4) &&(btn->Param5() == m_param5) &&(btn->Param6() == m_param6) &&(btn->Param7() == m_param7)) { btn->setProperty("state",state::failure); btn->style()->unpolish(btn); btn->style()->polish(btn); emit showMessage(tr("发送指令 %1 %2").arg(btn->Command().toString()).arg(btn->text())); } } } } emit cmd_long(m_param1.toDouble(), m_param2.toDouble(), m_param3.toDouble(), m_param4.toDouble(), m_param5.toDouble(), m_param6.toDouble(), m_param7.toDouble(), m_command,m_confirm); } else { m_param1 = 0; m_param2 = 0; m_param3 = 0; m_param4 = 0; m_param5 = 0; m_param6 = 0; m_param7 = 0; m_command = 0; m_confirm = 0; } } void CommandUI::setSecondValue(QVariant value) { for(int i = 0;i < ui->tabWidget_Command->count();i++) { QObjectList list = ui->tabWidget_Command->widget(i)->children(); foreach (QObject *obj, list) { CommandButton *btn = qobject_cast(obj); if(btn) { if((btn->text() == m_name) &&(btn->Command() == m_command) &&(btn->Confirm() == m_confirm) &&(btn->Param1() == m_param1) &&(btn->Param2() == m_param2) &&(btn->Param3() == m_param3) &&(btn->Param4() == m_param4) &&(btn->Param5() == m_param5) &&(btn->Param6() == m_param6) &&(btn->Param7() == m_param7)) { btn->setProperty("state",state::failure); btn->style()->unpolish(btn); btn->style()->polish(btn); emit showMessage(tr("发送指令 %1 %2").arg(btn->Command().toString()).arg(btn->text())); } } } } //找到要编辑的,然后把数字赋值过去 bool flag = false; m_param1.toDouble(&flag); if(!flag) { m_param1 = value; } m_param2.toDouble(&flag); if(!flag) { m_param2 = value; } m_param3.toDouble(&flag); if(!flag) { m_param3 = value; } m_param4.toDouble(&flag); if(!flag) { m_param4 = value; } m_param5.toDouble(&flag); if(!flag) { m_param5 = value; } m_param6.toDouble(&flag); if(!flag) { m_param6 = value; } m_param7.toDouble(&flag); if(!flag) { m_param7 = value; } emit cmd_long(m_param1.toDouble(), m_param2.toDouble(), m_param3.toDouble(), m_param4.toDouble(), m_param5.toDouble(), m_param6.toDouble(), m_param7.toDouble(), m_command,m_confirm); } void CommandUI::setParamValue(QVariant value) { emit showMessage(tr("无人机 %1 %2 %3 %4") .arg(QString::number(m_currentSys)) .arg(m_name) .arg(m_param1.toString()) .arg(value.toString())); //找到要编辑的,然后把数字赋值过去 /* bool flag = false; m_param3.toDouble(&flag); if(!flag) { m_param3 = value; } */ m_param3 = value; emit WriteParam(m_currentSys,1,m_param1,m_param2,m_param3); } void CommandUI::on_commandClicked() { //找到消息的发送者 CommandButton *btn = qobject_cast(sender()); m_name = btn->text(); m_command = btn->Command().toInt(); m_confirm = btn->Confirm().toInt(); m_param1 = btn->Param1(); m_param2 = btn->Param2(); m_param3 = btn->Param3(); m_param4 = btn->Param4(); m_param5 = btn->Param5(); m_param6 = btn->Param6(); m_param7 = btn->Param7(); int32_t dialogValue = btn->Dialog().toInt(); switch (dialogValue) { case -1://param { //value 确定要输入的参数 Inputter *inputter = new Inputter(this); inputter->setGeometry(0,0,this->width(),this->height()); //设置警告界面 QString str; str.append(tr("set parameter \n")); str.append(m_param1.toString()); str.append(" "); str.append(m_param2.toString()); str.append("\n"); str.append(btn->Notice().toString()); inputter->setLabel(str); connect(inputter,SIGNAL(confirmValue(QVariant)), this,SLOT(setParamValue(QVariant))); inputter->show(); }break; case 0:{ btn->setProperty("state",state::failure); btn->style()->unpolish(btn); btn->style()->polish(btn); emit cmd_long(m_param1.toDouble(), m_param2.toDouble(), m_param3.toDouble(), m_param4.toDouble(), m_param5.toDouble(), m_param6.toDouble(), m_param7.toDouble(), m_command,m_confirm); }break; case 1:{ //根据这个comfirm 确定要输入的参数 Confirm *confirmor = new Confirm(this); confirmor->setGeometry(0,0,this->width(),this->height()); //设置警告界面 confirmor->setNotice(btn->Notice().toString()); connect(confirmor,SIGNAL(confirmValue(QVariant)), this,SLOT(setSecondConfirm(QVariant))); confirmor->show(); }break; case 2:{ //value 确定要输入的参数 Inputter *inputter = new Inputter(this); inputter->setGeometry(0,0,this->width(),this->height()); //设置警告界面 inputter->setLabel(btn->Notice()); connect(inputter,SIGNAL(confirmValue(QVariant)), this,SLOT(setSecondValue(QVariant))); inputter->show(); }break; default:{ btn->setProperty("state",state::failure); btn->style()->unpolish(btn); btn->style()->polish(btn); emit cmd_long(m_param1.toDouble(), m_param2.toDouble(), m_param3.toDouble(), m_param4.toDouble(), m_param5.toDouble(), m_param6.toDouble(), m_param7.toDouble(), m_command,m_confirm); }break; } } void CommandUI::commandAccepted(bool flag,uint16_t command,uint8_t result) { bool isFundSender = false; QString string; //找到消息的发送者 qDebug() << "command accepte" << command; for(int i = 0;i < ui->tabWidget_Command->count();i++) { if(!ui->tabWidget_Command->widget(i)) { continue; } qDebug() << "tab index" << i; QObjectList list = ui->tabWidget_Command->widget(i)->children(); if(command == m_command) { foreach (QObject *obj, list) { CommandButton *btn = qobject_cast(obj); if(btn) { QString btn_name = btn->text(); if(btn_name == m_name)//查到正在等待回复的按键 { isFundSender = true; if(flag) { btn->setProperty("state",state::success); } else { btn->setProperty("state",state::failure); } switch (result) { case MAV_RESULT_ACCEPTED: string.append(tr("%1,%2 accepted and executed").arg(command).arg(btn_name)); break; case MAV_RESULT_TEMPORARILY_REJECTED: string.append(tr("%1,%2 rejected").arg(command).arg(btn_name)); break; case MAV_RESULT_DENIED: string.append(tr("%1,%2 permanently denied").arg(command).arg(btn_name)); break; case MAV_RESULT_UNSUPPORTED: string.append(tr("%1,%2 unsupported").arg(command).arg(btn_name)); break; case MAV_RESULT_FAILED: string.append(tr("%1,%2 failed").arg(command).arg(btn_name)); break; case MAV_RESULT_IN_PROGRESS: string.append(tr("%1,%2 being executed").arg(command).arg(btn_name)); break; case 0xFE: string.append(tr("%1,%2 time out").arg(command).arg(btn_name)); break; case 0xFF: string.append(tr("Please do not operate too fast, the last instruction has not been sent")); break; default: break; } btn->style()->unpolish(btn); btn->style()->polish(btn); m_command = 0; m_name.clear(); } } } } } if(isFundSender == false) { emit Accepted(flag,command,result); } else { if(string.size() > 0) { /* QVariant flag; Config *cfg = new Config(); cfg->getTTS(&flag); if(flag.toBool()) { QTextToSpeech *tts = new QTextToSpeech(); tts->say(string); tts->deleteLater(); } */ say(string); emit showMessage(string); } } } //专门为航点设计的一个确认窗口 void CommandUI::setCurrent(QVariant value) { if(value.toBool() == true) { emit showMessage(tr("设置航点 %1").arg(m_currentSeq)); emit SetCurrentPoint(m_currentSeq); } } void CommandUI::missionConfirm(int group, int seq) { m_currentSeq = seq; //根据这个comfirm 确定要输入的参数 Confirm *confirmor = new Confirm(this); confirmor->setGeometry(0,0,this->width(),this->height()); //设置警告界面 confirmor->setNotice(tr("click confirm to set Point %1 as current point").arg(seq)); connect(confirmor,SIGNAL(confirmValue(QVariant)), this,SLOT(setCurrent(QVariant))); confirmor->show(); //输入 1~4 /* if(isCofirmShow == true) { } else { m_currentSeq = seq; //根据这个comfirm 确定要输入的参数 Confirm *confirmor = new Confirm(this); confirmor->setGeometry(0,0,this->width(),this->height()); //设置警告界面 confirmor->setNotice(tr("click confirm to set group %1 Point %2 as current point").arg(group).arg(seq)); connect(confirmor,&Confirm::confirmValue,[=](QVariant value){ if(value.toBool() == true) { emit showMessage(tr("set mission :group %1,point %2").arg(group).arg(seq)); //emit SetCurrentPoint(seq); //发送修改航线的指令 emit cmd_long(group, seq, 0, 0, 0, 0, 0, 20011, 0); } isCofirmShow = false; }); connect(confirmor,&Confirm::destroyed,[=](){ isCofirmShow = false; }); confirmor->show(); isCofirmShow = true; } */ } void CommandUI::DoubleClickTimeout() { DoubleClickTimer->stop(); } void CommandUI::addVehicles(int sysid, int compid) { Q_UNUSED(compid) if(ui->comboBox_uav->findData(sysid) == -1) { ui->comboBox_uav->addItem(QString::number(sysid),sysid); ui->comboBox_uav->setCurrentIndex(0); } } void CommandUI::CurrentUAV(int id,int comp) { m_currentSys = id; m_currentComp = comp; } void CommandUI::setPointCount(QList nums) { ui->comboBox_seq->clear(); foreach (int i, nums) { ui->comboBox_seq->addItem(QString::number(i),i); } ui->comboBox_seq->setCurrentIndex(0); } void CommandUI::on_groupBox_Command_clicked() { qDebug() << "clicked"; if(DoubleClickTimer) { if(DoubleClickTimer->isActive()) { //双击后的效果 qDebug() << "double"; DoubleClickTimer->stop(); } else { qDebug() << "clicked"; DoubleClickTimer->start(); } } } void CommandUI::on_pushButton_setSeq_clicked() { if(ui->comboBox_seq->currentIndex() != -1) { int group = 1;//ui->comboBox_group->currentData().toInt() - 2; int seq = ui->comboBox_seq->currentData(Qt::UserRole).toInt(); missionConfirm(group,seq); } } void CommandUI::on_pushButton_setUAV_clicked() { if(ui->comboBox_uav->currentIndex() != -1) { int seq = ui->comboBox_uav->currentData(Qt::UserRole).toInt(); //根据这个comfirm 确定要输入的参数 Confirm *confirmor = new Confirm(this); confirmor->setGeometry(0,0,this->width(),this->height()); //设置警告界面 confirmor->setNotice(tr("click confirm to set uav %1 as current").arg(seq)); connect(confirmor,SIGNAL(confirmValue(QVariant)), this,SLOT(setCurrent(QVariant))); confirmor->show(); } }