Files
gcs-nf/App/CommandUI/CommandUI.cpp
T
2020-12-15 13:47:14 +08:00

773 lines
23 KiB
C++

#include "CommandUI.h"
#include "ui_CommandUI.h"
#include "QtTextToSpeech/QtTextToSpeech"
CommandUI::CommandUI(QWidget *parent) :
QWidget(parent),
ui(new Ui::CommandUI)
{
ui->setupUi(this);
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()));
}
CommandUI::~CommandUI()
{
delete ui;
}
void CommandUI::wheelEvent(QWheelEvent *e)
{
}
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();
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()));
/*
QVariant notice = binfo.toObject().find(_noticeJsonKey).value();
QVariant row = binfo.toObject().find(_colunmJsonKey).value();
QVariant column = binfo.toObject().find(_param7JsonKey).value();
QVariant notice = binfo.toObject().find(_param7JsonKey).value();
QVariant command= binfo.toObject().find(_param7JsonKey).value();
QVariant confirm= binfo.toObject().find(_param7JsonKey).value();
QVariant page = binfo.toObject().find(_param7JsonKey).value();
QVariant dialog = binfo.toObject().find(_param7JsonKey).value();
QVariant param1 = binfo.toObject().find(_param7JsonKey).value();
QVariant param2 = binfo.toObject().find(_param7JsonKey).value();
QVariant param3 = binfo.toObject().find(_param7JsonKey).value();
QVariant param4 = binfo.toObject().find(_param7JsonKey).value();
QVariant param5 = binfo.toObject().find(_param7JsonKey).value();
QVariant param6 = binfo.toObject().find(_param7JsonKey).value();
QVariant param7 = binfo.toObject().find(_param7JsonKey).value();
*/
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->setParameters(binfo.toObject().find(_rowJsonKey).value().toInt(),
binfo.toObject().find(_colunmJsonKey).value().toInt(),
binfo.toObject().find(_noticeJsonKey).value().toInt(),
binfo.toObject().find(_commandJsonKey).value().toInt(),
binfo.toObject().find(_confirmJsonKey).value().toInt(),
info.toObject().find(_PageJsonKey).value().toInt(),
binfo.toObject().find(_dialogJsonKey).value().toInt(),
binfo.toObject().find(_param1JsonKey).value().toInt(),
binfo.toObject().find(_param2JsonKey).value().toInt(),
binfo.toObject().find(_param3JsonKey).value().toInt(),
binfo.toObject().find(_param4JsonKey).value().toInt(),
binfo.toObject().find(_param5JsonKey).value().toInt(),
binfo.toObject().find(_param6JsonKey).value().toInt(),
binfo.toObject().find(_param7JsonKey).value().toInt());
*/
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<CommandButton *>(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<CommandButton *>(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<CommandButton *>(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;
}
qDebug() << tr("clicked") << m_name
<< 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;
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::on_commandClicked()
{
//qDebug() << "clicked";
/*
if(m_command == 0)
{
QMessageBox *msg = new QMessageBox();
msg->setWindowTitle(tr("Notice"));
msg->setText(tr("please set command first ,command :%1").arg(m_command));
msg->show();
return;
}
*/
//找到消息的发送者
CommandButton *btn = qobject_cast<CommandButton *>(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();
uint32_t dialogValue = btn->Dialog().toInt();
if(dialogValue != 0)
{
if(dialogValue == 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();
}
else if(dialogValue == 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();
}
//qDebug() << "Notice:" << btn->Notice();
}
else
{
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);
}
qDebug() << tr("clicked") << btn->text()
<< m_param1
<< m_param2
<< m_param3
<< m_param4
<< m_param5
<< m_param6
<< m_param7
<< m_command
<< m_confirm;
}
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<CommandButton *>(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)
{
QTextToSpeech *tts = new QTextToSpeech();
tts->say(string);
tts->deleteLater();
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 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();
}
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::setPointCount(QList<int> 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 seq = ui->comboBox_seq->currentData(Qt::UserRole).toInt();
missionConfirm(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();
}
}