25 #include <ArmarXGui/gui-plugins/StatechartEditorPlugin/view/dialogs/ui_StateDialog.h>
27 #include "../../../StatechartViewerPlugin/model/stateinstance/RemoteState.h"
29 #include <QFileDialog>
30 #include <QInputDialog>
32 #include <QDesktopWidget>
33 #include <QMessageBox>
46 QMap<QString, DynamicLibraryPtr> StateDialog::Libraries = QMap<QString, DynamicLibraryPtr>();
51 stateInstance(stateInstance),
52 communicator(communicator),
53 variantInfo(variantInfo),
54 currentProfile(currentProfile),
59 setWindowTitle(
"State " + stateInstance->getInstanceName() +
" Dialog");
60 ui->tableInput->setCommunicator(communicator);
61 ui->tableLocal->setCommunicator(communicator);
62 ui->tableOutput->setCommunicator(communicator);
63 ui->tableInput->setCurrentProfile(currentProfile);
64 ui->tableLocal->setCurrentProfile(currentProfile);
65 ui->tableOutput->setCurrentProfile(currentProfile);
67 ui->tableLocal->hideValueColumns();
68 ui->tableOutput->hideValueColumns();
73 ui->tableInput->setVariantInfo(variantInfo);
74 ui->tableLocal->setVariantInfo(variantInfo);
75 ui->tableOutput->setVariantInfo(variantInfo);
81 if (!stateInstance->getParent())
84 ui->editInstanceName->setEnabled(
false);
87 ui->editInstanceName->setText(stateInstance->getInstanceName());
88 ui->editInstanceName->setValidator(
new QRegExpValidator(QRegExp(
"([a-zA-Z][a-zA-Z0-9_]*)"),
this));
92 ui->editStateName->setText(state->getStateName());
93 ui->stateDocuEditor->setPlainText(state->getDescription());
97 ui->cbOutgoingEvents->addItem(e->name);
98 eventDescriptionMap[e->name] = e->description;
101 if (!outgoingEvents.isEmpty())
103 eventNameComboboxChanged(ui->cbOutgoingEvents->currentText());
106 ui->tableInput->setStateInstance(stateInstance);
107 ui->tableInput->buildFromMap(state->getInputParameters());
108 ui->tableLocal->setStateInstance(stateInstance);
109 ui->tableLocal->buildFromMap(state->getLocalParameters());
110 ui->tableOutput->setStateInstance(stateInstance);
111 ui->tableOutput->buildFromMap(state->getOutputParameters());
113 connect(ui->tabWidget, SIGNAL(currentChanged(
int)),
this, SLOT(setBlackLists()));
121 ui->editStateName->setEnabled(
false);
122 ui->stateDocuEditor->setEnabled(
false);
123 ui->cbOutgoingEvents->setEnabled(
false);
124 ui->btnAddEvent->setEnabled(
false);
125 ui->tabWidget->setTabEnabled(1,
false);
126 ui->tabWidget->setTabEnabled(2,
false);
127 ui->tabWidget->setTabEnabled(3,
false);
134 ui->editProxyName->setText(remoteState->proxyName);
138 ui->editProxyName->setEnabled(
false);
145 ui->labelInstanceName->setText(
"State/Event name");
149 ui->labelInstanceName->setText(
"State Instance name");
153 fileDialog =
new QFileDialog(
this);
154 fileDialog->setModal(
true);
155 fileDialog->setFileMode(QFileDialog::ExistingFiles);
156 fileDialog->setOption(QFileDialog::HideNameFilterDetails,
false);
158 urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::HomeLocation))
159 << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
166 fileDialog->setSidebarUrls(urls);
167 QStringList fileTypes;
168 fileTypes << tr(
"Libraries (*.so)")
169 << tr(
"All Files (*.*)");
170 fileDialog->setNameFilters(fileTypes);
172 connect(fileDialog, SIGNAL(accepted()),
this, SLOT(loadLibrary()));
174 connect(ui->cbOutgoingEvents, SIGNAL(currentIndexChanged(
const QString&)),
this, SLOT(eventNameComboboxChanged(
const QString&)));
175 connect(ui->btnAddEvent, SIGNAL(clicked()),
this, SLOT(eventButtonAdd()));
176 connect(ui->btnDeleteEvent, SIGNAL(clicked()),
this, SLOT(eventButtonDelete()));
177 connect(ui->editEventDescription, SIGNAL(textChanged()),
this, SLOT(eventDescriptionChanged()));
178 connect(ui->btnLock, SIGNAL(toggled(
bool)),
this, SLOT(setLockStatus(
bool)));
179 connect(ui->tableInput, SIGNAL(typeChanged(
int, QString)),
this, SLOT(updateLinkHint(
int, QString)));
180 connect(ui->tableLocal, SIGNAL(typeChanged(
int, QString)),
this, SLOT(updateLinkHint(
int, QString)));
181 connect(ui->tableOutput, SIGNAL(typeChanged(
int, QString)),
this, SLOT(updateLinkHint(
int, QString)));
183 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!readOnly);
188 setLockStatus(
false);
191 if (readOnly || !locked)
196 QRect rec = QApplication::desktop()->screenGeometry();
197 this->resize(rec.width() * 0.9, this->height());
199 updateLinkHint(0, 0);
212 if (stateInstance && (state = stateInstance->getStateClass()))
214 return getUpdatedParameters(state->getInputParameters(), *ui->tableInput);
217 throw new LocalException(
"Could not get state parameters; invalid state?");
225 if (stateInstance && (state = stateInstance->getStateClass()))
227 return getUpdatedParameters(state->getLocalParameters(), *ui->tableLocal);
230 throw new LocalException(
"Could not get state parameters; invalid state?");
238 if (stateInstance && (state = stateInstance->getStateClass()))
240 return getUpdatedParameters(state->getOutputParameters(), *ui->tableOutput);
243 throw new LocalException(
"Could not get state parameters; invalid state?");
249 return ui->editInstanceName->text();
254 return ui->editStateName->text();
259 return ui->editProxyName->text();
264 return ui->stateDocuEditor->toPlainText();
271 for (
int i = 0; i < ui->cbOutgoingEvents->count(); ++i)
274 event->name = ui->cbOutgoingEvents->itemText(i);
275 event->description = eventDescriptionMap[
event->name];
276 eventList.append(event);
282 void StateDialog::eventNameComboboxChanged(
const QString& eventName)
284 ui->editEventDescription->setEnabled(
true);
285 ui->editEventDescription->setPlainText(eventDescriptionMap[eventName]);
286 ui->btnDeleteEvent->setEnabled(
true);
289 void StateDialog::eventButtonAdd()
291 RegExpValidatedInputDialog d(
"New Event Name",
"Event name",
292 QRegExp(
"^([a-zA-Z_]{1})([a-zA-Z_0-9]+)$"),
this);
294 if (d.exec() == QDialog::Accepted)
296 auto eventName = d.getTextValue();
297 if (!eventName.isEmpty())
299 ui->cbOutgoingEvents->addItem(eventName);
305 void StateDialog::eventButtonDelete()
307 if (ui->cbOutgoingEvents->currentText() !=
"Failure")
309 ui->cbOutgoingEvents->removeItem(ui->cbOutgoingEvents->currentIndex());
313 QMessageBox::warning(0,
"Deletion failed",
"You cannot delete the failure event. This event is needed to handle internal statechart failures.");
317 void StateDialog::eventDescriptionChanged()
319 eventDescriptionMap[ui->cbOutgoingEvents->currentText()] = ui->editEventDescription->toPlainText();
322 void StateDialog::loadLibrary()
325 QStringList
files = fileDialog->selectedFiles();
326 foreach (QString file,
files)
330 if (Libraries.find(file) != Libraries.end())
336 lib->load(file.toStdString());
337 Libraries[file] = lib;
340 ui->tableInput->refreshVariantTypes();
341 ui->tableLocal->refreshVariantTypes();
342 ui->tableOutput->refreshVariantTypes();
345 void StateDialog::setBlackLists()
347 ui->tableInput->setKeyBlackList(ui->tableLocal->getKeys());
348 ui->tableLocal->setKeyBlackList(ui->tableInput->getKeys());
351 void StateDialog::setLockStatus(
bool unlock)
353 if (unlock && tipDialog)
355 tipDialog->setModal(
true);
356 tipDialog->showMessage(
"You are unlocking the editing of the state class. Editing a state class will change all occurrences of this state and not only this instance. To use custom parameters for this instance use the transition mapping.",
"State class unlocking");
357 tipDialog->resize(500, 150);
358 tipDialog->activateWindow();
362 ui->tableInput->setEnabled(unlock);
363 ui->tableOutput->setEnabled(unlock);
364 ui->tableLocal->setEnabled(unlock);
365 ui->cbOutgoingEvents->setEnabled(unlock);
366 ui->stateDocuEditor->setEnabled(unlock);
367 ui->editEventDescription->setEnabled(unlock);
370 void StateDialog::updateLinkHint(
int, QString)
378 vars.unite(ui->tableInput->getTypes())
379 .unite(ui->tableLocal->getTypes())
380 .unite(ui->tableOutput->getTypes());
381 Ice::StringSeq types;
382 for (
auto& var : vars)
387 types.push_back(type);
391 for (std::string& lib : variantInfo->findLibNames(types))
393 libs << QString::fromStdString(lib);
395 ui->editDependencies->setText(libs.join(
" "));
403 QSet<QString> profiles;
408 profiles.insert(QString::fromUtf8(p->getName().data()));
412 for (
const auto& oldParamEntry :
source.toStdMap())
414 QString key = oldParamEntry.first;
417 if (newValues.contains(key) && newValues[key]->type == oldParamEntry.second->type)
419 for (
const auto& pdvEntry : oldParamEntry.second->profileDefaultValues.toStdMap())
421 if (!profiles.contains(pdvEntry.first))
423 newValues[key]->profileDefaultValues.insert(pdvEntry.first, pdvEntry.second);
434 if (readOnly && tipDialog)
437 tipDialog->showMessage(
"This state is read-only. Thus, changes cannot be saved.",
"Read-only state");
439 tipDialog->activateWindow();