24 #include <ArmarXGui/gui-plugins/StatechartEditorPlugin/ui_ProfileDefaultValueEditWidget.h>
26 #include <QFileDialog>
47 typeString(typeString),
49 communicator(communicator),
54 QLineEdit* lineEdit =
new QLineEdit;
55 ui->value->insertWidget(eLineEdit, lineEdit);
57 QPushButton* editButton =
new QPushButton(
"Edit");
58 ui->value->insertWidget(eEditButton, editButton);
59 connect(editButton, SIGNAL(clicked()),
this, SLOT(editDefaultButtonClicked()));
61 QPushButton* setButton =
new QPushButton(
"Click to set");
62 setButton->setStyleSheet(
"color: #888;");
63 setButton->setFlat(
true);
64 ui->value->insertWidget(eSetButton, setButton);
65 connect(setButton, SIGNAL(clicked()),
this, SLOT(setButtonClicked()));
71 lineEdit->setValidator(NULL);
77 QValidator* validator =
new QRegExpValidator(rx,
this);
78 lineEdit->setValidator(validator);
82 QRegExp rx(
"(true|false)");
83 QValidator* validator =
new QRegExpValidator(rx,
this);
84 lineEdit->setValidator(validator);
90 QRegExp rx(
"-?\\d+(\\.\\d+)?([eE][+-]?\\d+)?");
91 QValidator* validator =
new QRegExpValidator(rx,
this);
92 lineEdit->setValidator(validator);
95 if (jsonValue.isEmpty())
97 lineEdit->setText(
"");
102 std::string compressedValue = nav.
isValue() ? nav.
asValue()->rawValue() : nav.
getData()->toJsonString(0);
103 lineEdit->setText(QString::fromUtf8(compressedValue.c_str()));
108 connect(ui->pushButtonDelete, SIGNAL(clicked()),
this, SLOT(resetValue()));
110 if (jsonValue.size() == 0)
132 this->editable = editable;
133 ui->value->widget(eLineEdit)->setEnabled(editable);
138 if (ui->value->currentIndex() == Widget::eLineEdit)
140 return qobject_cast<QLineEdit*>(ui->value->currentWidget())->text();
142 else if (ui->value->currentIndex() == Widget::eEditButton)
153 int index = ui->value->currentIndex();
155 if (
index == Widget::eEditButton)
159 else if (
index == Widget::eLineEdit)
161 QLineEdit* valueEdit = qobject_cast<QLineEdit*>(ui->value->currentWidget());
163 return QString::fromUtf8(verboseJson.c_str());
171 VariantContainerBasePtr result;
173 if (ui->value->currentIndex() == eLineEdit)
175 QLineEdit* valueEdit = qobject_cast<QLineEdit*>(ui->value->currentWidget());
181 variant.
setInt(valueEdit->text().toInt());
185 variant.
setLong(valueEdit->text().toLong());
189 variant.
setBool(valueEdit->text().compare(
"true", Qt::CaseInsensitive) == 0 || valueEdit->text() ==
"1");
193 variant.
setFloat(valueEdit->text().toFloat());
197 variant.
setDouble(valueEdit->text().toDouble());
201 variant.
setString(valueEdit->text().trimmed().toStdString());
206 else if (ui->value->currentIndex() == eEditButton)
208 if (jsonValue.isEmpty())
215 jsonObject->fromString(jsonValue.toUtf8().data());
219 SerializablePtr obj = jsonObject->deserializeIceObject();
220 result = VariantContainerBasePtr::dynamicCast(obj);
228 catch (std::exception& e)
230 ARMARX_WARNING_S <<
"JSON string for type " << typeString.toStdString() <<
" could not be deserialized: " << e.what();
238 void ProfileDefaultValueEditWidget::resetValue()
242 ui->value->setCurrentIndex(eSetButton);
243 ui->pushButtonDelete->setEnabled(
false);
244 ui->pushButtonDelete->hide();
247 auto lineEdit = qobject_cast<QLineEdit*>(ui->value->widget(eLineEdit));
248 lineEdit->setText(
"");
251 void ProfileDefaultValueEditWidget::setButtonClicked()
253 ui->pushButtonDelete->setEnabled(
true);
254 ui->pushButtonDelete->show();
259 ui->value->setCurrentIndex(eLineEdit);
264 ui->value->setCurrentIndex(eEditButton);
268 void ProfileDefaultValueEditWidget::initEditButton()
272 if (jsonValue.isEmpty())
280 Ice::ValueFactoryPtr factory = communicator->getValueFactoryManager()->find(variantContainerType->typeId);
284 ARMARX_WARNING <<
"Could not get ObjectFactory for Variant " << variantContainerType->typeId;
289 Ice::ValuePtr valPtr = factory->create(variantContainerType->typeId);
290 ARMARX_INFO_S << valPtr.get() <<
" " << SerializablePtr::dynamicCast(valPtr).get();
292 VariantContainerBasePtr container = VariantContainerBasePtr::dynamicCast(valPtr);
298 Ice::ValueFactoryPtr subfactory = communicator->getValueFactoryManager()->find(variantContainerType->subType->typeId);
303 subfactory = IceInternal::factoryTable->getValueFactory(variantContainerType->subType->typeId);
308 Ice::ValuePtr subObj = subfactory->create(variantContainerType->subType->typeId);
309 VariantDataPtr var = VariantDataPtr::dynamicCast(subObj);
322 map->addVariant(
"mykey",
Variant(var));
335 jsonObject->serializeIceObject(container);
338 jsonValue = QString::fromStdString(jsonObject->asString(
true));
344 ui->value->widget(eEditButton)->setEnabled(
false);
348 void ProfileDefaultValueEditWidget::editDefaultButtonClicked()
351 if (d.exec() == QDialog::Accepted)
353 jsonValue = d.getJson();