30 #include <QVBoxLayout>
40 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
42 _layout =
new QVBoxLayout(
this);
43 _layout->setContentsMargins(5, 5, 0, 0);
44 _layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
46 _combobox =
new LabelComboBoxWidget(label);
48 _layout->addWidget(_combobox);
50 connect(_combobox->widgetComboBox(), SIGNAL(currentTextChanged(
const QString&)),
this, SLOT(selectParameter(
const QString&)));
53 OneOfParamWidget::~OneOfParamWidget() {}
55 QSize OneOfParamWidget::sizeHint()
const {
return QSize(200, 50); }
57 void OneOfParamWidget::registerParameter(
const QString& label,
const MessageParser::FieldInformation&
info)
59 _message =
info.message_raw;
60 _fields.insert(label,
info.field_raw);
64 bool selected_field = (
info.field_raw ==
info.oneof_selected_field) || !
info.oneof_selected_field;
66 if (selected_field) _selected_item_parsed =
true;
68 _combobox->widgetComboBox()->addItem(label);
76 _combobox->widgetComboBox()->setCurrentText(label);
80 void OneOfParamWidget::selectParameter(
const QString& label)
83 if (!_selected_item_parsed)
return;
86 addParametersToCache();
87 _selected_item = label;
91 _layout->removeWidget(_param_widget);
93 _param_widget =
nullptr;
96 restoreParametersFromCache();
98 auto field = _fields.find(label);
99 if (field != _fields.end())
107 _layout->addWidget(params);
108 _param_widget = params;
109 params->connect(params, &ParameterWidget::signalUpdateRequested, [
this]() { emit signalUpdateRequested(); });
110 params->connect(params, &ParameterWidget::updatedOneOfField, [
this](
const QString& name) { emit currentParameterChanged(name); });
115 _param_widget =
nullptr;
118 emit currentParameterChanged(label);
122 void OneOfParamWidget::addParametersToCache()
124 if (!_param_cache || _selected_item.isEmpty())
return;
127 QList<OneOfParamWidget*> widgets = _param_widget->findChildren<
OneOfParamWidget*>(
"", Qt::FindDirectChildrenOnly);
131 std::string param_path = ParameterCache::nestedNameListToKey(_nested_field_name);
133 param_path +=
"/" + _selected_item.toStdString();
136 _param_cache->toCache(param_path, *_message);
139 bool OneOfParamWidget::restoreParametersFromCache()
141 std::string param_path = ParameterCache::nestedNameListToKey(_nested_field_name);
143 param_path +=
"/" + _selected_item.toStdString();
145 std::unique_ptr<google::protobuf::Message> cache = _param_cache->fromCache(param_path);
146 if (!cache)
return false;
148 const google::protobuf::FieldDescriptor* current_field = _fields[_selected_item];
151 _message->GetReflection()->SwapFields(_message, cache.get(), {current_field});
156 std::string OneOfParamWidget::nestedFieldNameUnrolled(
const std::string& delimiter)
158 auto it = _nested_field_name.begin();
159 std::string nnames = *it;
160 for (std::advance(it, 1); it != _nested_field_name.end(); ++it) nnames += delimiter + *it;
164 void OneOfParamWidget::addDescription(
const QString& description)
166 _layout->addWidget(
new QLabel(description));