Files
gcs-nf/App/CommandUI/CommandUI.cpp
T
2020-08-26 22:10:44 +08:00

567 lines
16 KiB
C++

#include "CommandUI.h"
#include "ui_CommandUI.h"
#include "QtTextToSpeech/QtTextToSpeech"
//逻辑搞清楚,需要优化
const char* CommandUI::_versionJsonKey = "version";
const char* CommandUI::_commentJsonKey = "comment";
const char* CommandUI::_CommandButtonJsonKey = "CommandButton";
const char* CommandUI::_WidthJsonKey = "Width";
const char* CommandUI::_HeightJsonKey = "Height";
const char* CommandUI::_dialogJsonKey = "Dialog";
const char* CommandUI::_TextJsonKey = "Text";
const char* CommandUI::_rowJsonKey = "row";
const char* CommandUI::_columnJsonKey = "column";
const char* CommandUI::_noticeJsonKey = "Notice";
const char* CommandUI::_param1JsonKey = "param1";
const char* CommandUI::_param2JsonKey = "param2";
const char* CommandUI::_param3JsonKey = "param3";
const char* CommandUI::_param4JsonKey = "param4";
const char* CommandUI::_param5JsonKey = "param5";
const char* CommandUI::_param6JsonKey = "param6";
const char* CommandUI::_param7JsonKey = "param7";
const char* CommandUI::_commandJsonKey = "command";
const char* CommandUI::_confirmJsonKey = "confirm";
CommandUI::CommandUI(QWidget *parent) :
QWidget(parent),
ui(new Ui::CommandUI)
{
ui->setupUi(this);
//检测文件夹,如果不存在,那么就新建一个,这里存着所有的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();
//载入json
loadCommandJson(CommandFile);
ui->groupBox_Command->setTitle(tr("Command Version:") + QString::number(CMD_version));
for(QJsonValue info: CMD_infoArray) {
if (!info.isObject()) {
return;
}
QPushButton *btn = new QPushButton(ui->groupBox_Command);
btn->setText(info.toObject().find(_TextJsonKey).value().toString());
//btn->setMinimumSize(CMDwidth,CMDheight);
//btn->resize(CMDwidth,CMDheight);
btn->setFixedSize(CMDwidth,CMDheight);
ui->gridLayout_Command->addWidget(btn,
info.toObject().find(_rowJsonKey).value().toInt(),
info.toObject().find(_columnJsonKey).value().toInt());
btn->setProperty("state",state::success);
btn->style()->unpolish(btn);
btn->style()->polish(btn);
connect(btn,SIGNAL(clicked(bool)),
this,SLOT(on_commandClicked()));
}
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)
{
QObjectList list = ui->groupBox_Command->children();
foreach (QObject *obj, list) {
QPushButton *btn = qobject_cast<QPushButton *>(obj);
if(btn)
{
qreal width = btn->width();
qreal height = btn->height();
if(e->delta() > 0)
{
btn->resize(width - 1,height - 1);
}
else if(e->delta() < 0)
{
btn->resize(width + 1,height + 1);
}
}
}
}
void CommandUI::mousePressEvent(QMouseEvent *event)
{
qDebug() << "clicked";
if (event->button() == Qt::MiddleButton)
{
QObjectList olist = ui->groupBox_Command->children();
foreach (QObject *obj, olist) {
QPushButton *btn = qobject_cast<QPushButton *>(obj);
if(btn)
{
disconnect(btn,SIGNAL(clicked(bool)),
this,SLOT(on_commandClicked()));
delete btn;
}
QLabel *lab = qobject_cast<QLabel *>(obj);
if(lab)
{
delete lab;
}
}
loadCommandJson(CommandFile);
ui->groupBox_Command->setTitle(tr("Command Version:") + QString::number(CMD_version));
for(QJsonValue info: CMD_infoArray) {
if (!info.isObject()) {
return;
}
QPushButton *btn = new QPushButton(ui->groupBox_Command);
btn->setText(info.toObject().find(_TextJsonKey).value().toString());
//btn->setMinimumSize(CMDwidth,CMDheight);
//btn->resize(CMDwidth,CMDheight);
btn->setFixedSize(CMDwidth,CMDheight);
ui->gridLayout_Command->addWidget(btn,
info.toObject().find(_rowJsonKey).value().toInt(),
info.toObject().find(_columnJsonKey).value().toInt());
btn->setProperty("state",state::success);
btn->style()->unpolish(btn);
btn->style()->polish(btn);
connect(btn,SIGNAL(clicked(bool)),
this,SLOT(on_commandClicked()));
}
}
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::setCommandFile(QString File)
{
CommandFile = File;
QObjectList olist = ui->groupBox_Command->children();
foreach (QObject *obj, olist) {
QPushButton *btn = qobject_cast<QPushButton *>(obj);
if(btn)
{
disconnect(btn,SIGNAL(clicked(bool)),
this,SLOT(on_commandClicked()));
delete btn;
}
QLabel *lab = qobject_cast<QLabel *>(obj);
if(lab)
{
delete lab;
}
}
loadCommandJson(CommandFile);
ui->groupBox_Command->setTitle(tr("Command Version:") + QString::number(CMD_version));
for(QJsonValue info: CMD_infoArray) {
if (!info.isObject()) {
return;
}
QPushButton *btn = new QPushButton(ui->groupBox_Command);
btn->setText(info.toObject().find(_TextJsonKey).value().toString());
btn->setFixedSize(CMDwidth,CMDheight);
ui->gridLayout_Command->addWidget(btn,
info.toObject().find(_rowJsonKey).value().toInt(),
info.toObject().find(_columnJsonKey).value().toInt());
btn->setProperty("state",state::success);
btn->style()->unpolish(btn);
btn->style()->polish(btn);
connect(btn,SIGNAL(clicked(bool)),
this,SLOT(on_commandClicked()));
}
}
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();
CMD_version = json.value(_versionJsonKey).toInt();
qDebug() << "command version :" << CMD_version;
CMDwidth = json.value(_WidthJsonKey).toInt();
CMDheight = json.value(_HeightJsonKey).toInt();
QJsonValue jsonValue = json.value(_CommandButtonJsonKey);
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)
{
QObjectList list = ui->groupBox_Command->children();
foreach (QObject *obj, list) {
QPushButton *btn = qobject_cast<QPushButton *>(obj);
if(btn)
{
if(btn->text() == m_name)
{
btn->setProperty("state",state::failure);
btn->style()->unpolish(btn);
btn->style()->polish(btn);
}
}
}
emit cmd_long(m_param1,m_param2,m_param3,m_param4,m_param5,m_param6,m_param7,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::on_commandClicked()
{
//找到消息的发送者
QPushButton *btn = qobject_cast<QPushButton *>(sender());
for(QJsonValue info: CMD_infoArray) {
if (!info.isObject()) {
return;
}
//找到id或者名字一样的按键,找到参数,发出去
QString _name = info.toObject().find(_TextJsonKey).value().toString();
QString _text = btn->text();
if(_name == _text)
{
m_name = _name;
m_param1 = info.toObject().find(_param1JsonKey).value().toDouble();
m_param2 = info.toObject().find(_param2JsonKey).value().toDouble();
m_param3 = info.toObject().find(_param3JsonKey).value().toDouble();
m_param4 = info.toObject().find(_param4JsonKey).value().toDouble();
m_param5 = info.toObject().find(_param5JsonKey).value().toDouble();
m_param6 = info.toObject().find(_param6JsonKey).value().toDouble();
m_param7 = info.toObject().find(_param7JsonKey).value().toDouble();
m_command = info.toObject().find(_commandJsonKey).value().toInt();
m_confirm = info.toObject().find(_confirmJsonKey).value().toInt();
//如果confirm 不为0,那么弹出确认对话框
if(info.toObject().find(_dialogJsonKey).value().toInt() != 0)
{
//根据这个comfirm 确定要输入的参数
Confirm *confirmor = new Confirm(this);
confirmor->setGeometry(0,0,this->width(),this->height());
//设置警告界面
confirmor->setNotice(info.toObject().find(_noticeJsonKey).value().toString());
connect(confirmor,SIGNAL(confirmValue(QVariant)),
this,SLOT(setSecondConfirm(QVariant)));
confirmor->show();
}
else
{
btn->setProperty("state",state::failure);
btn->style()->unpolish(btn);
btn->style()->polish(btn);
emit cmd_long(m_param1,m_param2,m_param3,m_param4,m_param5,m_param6,m_param7,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;
//找到消息的发送者
QObjectList list = ui->groupBox_Command->children();
if(command == m_command)
{
foreach (QObject *obj, list)
{
QPushButton *btn = qobject_cast<QPushButton *>(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;
default:
break;
}
btn->style()->unpolish(btn);
btn->style()->polish(btn);
}
}
}
}
if(isFundSender == false)
{
switch (result) {
case MAV_RESULT_ACCEPTED:
string.append(tr("%1 accepted and executed").arg(command));
break;
case MAV_RESULT_TEMPORARILY_REJECTED:
string.append(tr("%1 rejected").arg(command));
break;
case MAV_RESULT_DENIED:
string.append(tr("%1 permanently denied").arg(command));
break;
case MAV_RESULT_UNSUPPORTED:
string.append(tr("%1 unsupported").arg(command));
break;
case MAV_RESULT_FAILED:
string.append(tr("%1 failed").arg(command));
break;
case MAV_RESULT_IN_PROGRESS:
string.append(tr("%1 being executed").arg(command));
break;
default:
break;
}
}
QTextToSpeech *tts = new QTextToSpeech();
tts->say(string);
}
//专门为航点设计的一个确认窗口
void CommandUI::setCurrent(QVariant value)
{
if(value.toBool() == true)
{
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::on_groupBox_Command_clicked()
{
qDebug() << "clicked";
if(DoubleClickTimer)
{
if(DoubleClickTimer->isActive())
{
//双击后的效果
qDebug() << "double";
DoubleClickTimer->stop();
}
else
{
qDebug() << "clicked";
DoubleClickTimer->start();
}
}
}