249 lines
7.1 KiB
C++
249 lines
7.1 KiB
C++
#include "CommandUI.h"
|
|
#include "ui_CommandUI.h"
|
|
|
|
|
|
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::_TextJsonKey = "Text";
|
|
const char* CommandUI::_rowJsonKey = "row";
|
|
const char* CommandUI::_columnJsonKey = "column";
|
|
|
|
|
|
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文件夹,如果不存在,那么就新建一个,这里存着所有的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(":/json/Command.json");
|
|
loadCommandJson("./commands/Command.json");
|
|
|
|
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()));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CommandUI::~CommandUI()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
/*
|
|
if (version != 1) {
|
|
qWarning() << jsonFilename << "Invalid version" << version;
|
|
return;
|
|
}
|
|
*/
|
|
|
|
CMDwidth = json.value(_WidthJsonKey).toInt();
|
|
CMDheight = json.value(_HeightJsonKey).toInt();
|
|
|
|
|
|
QJsonValue jsonValue = json.value(_CommandButtonJsonKey);
|
|
|
|
if (!jsonValue.isArray()) {
|
|
qWarning() << jsonFilename << "_CommandButtonJsonKey not array";
|
|
return;
|
|
}
|
|
|
|
// Iterate over MissionCommandUIInfo objects
|
|
CMD_infoArray = jsonValue.toArray();
|
|
|
|
//qDebug() << CMD_infoArray;
|
|
|
|
for(QJsonValue info: CMD_infoArray) {
|
|
if (!info.isObject()) {
|
|
qWarning() << jsonFilename << "mavCmdArray should contain objects";
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
float param1 = info.toObject().find(_param1JsonKey).value().toDouble();
|
|
float param2 = info.toObject().find(_param2JsonKey).value().toDouble();
|
|
float param3 = info.toObject().find(_param3JsonKey).value().toDouble();
|
|
float param4 = info.toObject().find(_param4JsonKey).value().toDouble();
|
|
float param5 = info.toObject().find(_param5JsonKey).value().toDouble();
|
|
float param6 = info.toObject().find(_param6JsonKey).value().toDouble();
|
|
float param7 = info.toObject().find(_param7JsonKey).value().toDouble();
|
|
uint16_t command = info.toObject().find(_commandJsonKey).value().toInt();
|
|
uint8_t confirm = info.toObject().find(_confirmJsonKey).value().toInt();
|
|
|
|
|
|
emit cmd_long(param1,param2, param3,param4,param5,param6,param7,command,confirm);
|
|
|
|
qDebug() << tr("clicked") << btn->text()
|
|
<< param1
|
|
<< param2
|
|
<< param3
|
|
<< param4
|
|
<< param5
|
|
<< param6
|
|
<< param7
|
|
<< command
|
|
<< confirm;
|
|
|
|
btn->setProperty("state",state::failure);
|
|
btn->style()->unpolish(btn);
|
|
btn->style()->polish(btn);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void CommandUI::commandAccepted(bool flag,uint16_t command,uint8_t result)
|
|
{
|
|
//找到消息的发送者
|
|
QObjectList list = ui->groupBox_Command->children();
|
|
|
|
for(QJsonValue info: CMD_infoArray) {
|
|
if (!info.isObject()) {
|
|
return;
|
|
}
|
|
|
|
if(command == info.toObject().find(_commandJsonKey).value().toInt())
|
|
{
|
|
foreach (QObject *obj, list)
|
|
{
|
|
QPushButton *btn = qobject_cast<QPushButton *>(obj);
|
|
if(btn)
|
|
{
|
|
if(btn->text() == info.toObject().find(_TextJsonKey).value().toString())
|
|
{
|
|
if(flag)
|
|
{
|
|
btn->setProperty("state",state::success);
|
|
}
|
|
else
|
|
{
|
|
btn->setProperty("state",state::failure);
|
|
}
|
|
btn->style()->unpolish(btn);
|
|
btn->style()->polish(btn);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|