根据界面QComboBox切换调整需要获取信息类型,按确定后将界面内容整理为json的全流程。
定义需要获取的信息类型,避免字符串匹配:
enum class EnvInfo
{
none = 0,
Platform = 1,
Object = 2,
Env = 3
};在EditPlatformAttributeWindow类中维护AttributeWindow的vector指针列表,动态维护AttributeWindow个数:
std::vector<AttributeWindow*> m_attribute_windows_;初始化comboBox,并设置槽函数onComboBoxIndexChanged,注意用QVariant绑定,方便后期获取index转为EnvInfo:
void AttributeWindow::InitCombox() {
ui->comboBox_model_4->clear();
ui->comboBox_model_4->addItem("请选择属性", QVariant::fromValue(static_cast<int>(EnvInfo::none)));
ui->comboBox_model_4->addItem("平台", QVariant::fromValue(static_cast<int>(EnvInfo::Platform)));
ui->comboBox_model_4->addItem("目标", QVariant::fromValue(static_cast<int>(EnvInfo::Object)));
ui->comboBox_model_4->addItem("环境", QVariant::fromValue(static_cast<int>(EnvInfo::Env)));
connect(ui->comboBox_model_4, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &AttributeWindow::onComboBoxIndexChanged);
}ComboBox切换后调整需要获取的信息:
void AttributeWindow::onComboBoxIndexChanged(int index)
{
QVariant data_ = ui->comboBox_model_4->itemData(index);
m_env_type = static_cast<EnvInfo>(data_.toInt());
if (m_env_type == EnvInfo::none) {
}
if (m_env_type == EnvInfo::Platform)
{
adjustLabels(true);
ui->label_2->setText("id");
ui->label_4->setText("silent_control");
ui->label_5->setText("dof.\nheading");
ui->label_9->setText("coordinate.\nlatitude");
// ...
}
if (m_env_type == EnvInfo::Object)
{
adjustLabels(true);
ui->label_2->setText("id");
ui->label_3->setText("device_type");
// ...
}
if (m_env_type == EnvInfo::Env)
{
adjustLabels(false);
ui->label->setText("wind_speed");
ui->label_2->setText("ship_density");
// ...
}
}根据需要获取的信息,调整label显隐,因为代码过于冗余,单独拆分为函数:
void AttributeWindow::adjustLabels(bool is_show) {
if (!is_show) {
ui->label_3->hide();
ui->textEdit_3->hide();
} else {
ui->label_3->show();
ui->textEdit_3->show();
}
}获取内容:
UIContext AttributeWindow::GetContext()
{
int index_ = ui->comboBox_model_4->currentIndex();
QVariant data_ = ui->comboBox_model_4->itemData(index_);
m_env_type = static_cast<EnvInfo>(data_.toInt());
if (m_env_type==EnvInfo::none)
return ui_context_;
if (m_env_type == EnvInfo::Platform) {
ui_context_.env_type = EnvInfo::Platform;
ui_context_.id = ui->textEdit_2->toPlainText();
ui_context_.device_type = ui->textEdit_3->toPlainText();
ui_context_.dof_heading = ui->textEdit_5->toPlainText();
ui_context_.coordinate_latitude = ui->textEdit_8->toPlainText();
// ...
return ui_context_;
}
if (m_env_type == EnvInfo::Object) {
ui_context_.env_type = EnvInfo::Object;
ui_context_.name = ui->textEdit->toPlainText();
ui_context_.id = ui->textEdit_2->toPlainText();
// ...
return ui_context_;
}
if (m_env_type == EnvInfo::Env) {
ui_context_.env_type = EnvInfo::Env;
ui_context_.wind_speed = ui->textEdit->toPlainText();
ui_context_.ship_density = ui->textEdit_2->toPlainText();
// ...
return ui_context_;
}
}按确定按钮获取内容:
void EditPlatformAttributeWindow::on_pushButton_Confirm_clicked() {
this->close();
QJsonObject obj;
QJsonArray ObjectAttribute_s;
// 环境参数(self、object、env)
for (const auto& it : m_attribute_windows_) {
if (it->m_env_type == EnvInfo::none) {
QMessageBox msgBox;
msgBox.setStyleSheet(MYSTYLE::messageBoxStyle);
msgBox.setWindowTitle("提示信息");
QString msg_ = "请配置场景属性!!";
msgBox.setText(msg_);
msgBox.setWindowFlags(msgBox.windowFlags() | Qt::WindowStaysOnTopHint);
QPushButton* closeButton =
msgBox.addButton("关闭", QMessageBox::RejectRole);
msgBox.exec();
if (msgBox.clickedButton() == closeButton) return;
return;
}
if (it->m_env_type == EnvInfo::Platform) {
UIContext content_ = it->GetContext();
QJsonObject SelfAttribute;
SelfAttribute["id"] = content_.id;
SelfAttribute["device_type"] = content_.device_type;
SelfAttribute["silent_control"] = content_.silent_control;
QJsonObject DOF_SELF;
DOF_SELF["heading"] = content_.dof_heading;
// ...
SelfAttribute["dof"] = DOF_SELF;
// ...
obj["self_attribute"] = SelfAttribute;
}
if (it->m_env_type == EnvInfo::Object) {
UIContext content_ = it->GetContext();
QJsonObject ObjectAttribute;
ObjectAttribute["name"] = content_.name;
ObjectAttribute["id"] = content_.id;
ObjectAttribute["device_type"] = content_.device_type;
ObjectAttribute["silent_control"] = content_.silent_control;
QJsonObject DOF_OBJECT;
DOF_OBJECT["heading"] = content_.dof_heading;
ObjectAttribute["dof"] = DOF_OBJECT;
// ...
ObjectAttribute_s.append(ObjectAttribute);
}
if (it->m_env_type == EnvInfo::Env) {
UIContext content_ = it->GetContext();
QJsonObject env;
env["wind_speed"] = content_.wind_speed;
// ...
obj["environment"] = env;
}
}
obj["objects_attribute"] = ObjectAttribute_s;
// 分析模式 20/11/25 yilin
run_type = ui->comboBox_model->currentIndex();
m_run_type = static_cast<RunModel>(run_type);
DataManager::getInstance().SetRunMode(m_run_type);
// 仿真模式 20/11/25 yilin
simulation_type = ui->comboBox_model_4->currentIndex();
m_simulation_type = static_cast<SinulationMode>(simulation_type);
}