StateDialog.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "StateDialog.h"
25 #include <ArmarXGui/gui-plugins/StatechartEditorPlugin/view/dialogs/ui_StateDialog.h>
26 
27 #include "../../../StatechartViewerPlugin/model/stateinstance/RemoteState.h"
29 #include <QFileDialog>
30 #include <QInputDialog>
31 #include <QtGui>
32 #include <QDesktopWidget>
33 #include <QMessageBox>
34 
35 #include <iterator>
36 
38 
40 
42 
43 namespace armarx
44 {
45 
46  QMap<QString, DynamicLibraryPtr> StateDialog::Libraries = QMap<QString, DynamicLibraryPtr>();
47 
48  StateDialog::StateDialog(statechartmodel::StateInstancePtr stateInstance, Ice::CommunicatorPtr communicator, VariantInfoPtr variantInfo, StatechartProfilePtr currentProfile, bool locked, bool readOnly, QPointer<TipDialog> tipDialog, QWidget* parent) :
49  QDialog(parent),
50  ui(new Ui::StateDialog),
51  stateInstance(stateInstance),
52  communicator(communicator),
53  variantInfo(variantInfo),
54  currentProfile(currentProfile),
55  tipDialog(tipDialog),
56  readOnly(readOnly)
57  {
58  ui->setupUi(this);
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);
66  ui->tableLocal->hideColumn(StatechartEditorParameterEditor::eOptional);
67  ui->tableLocal->hideValueColumns();
68  ui->tableOutput->hideValueColumns();
69  // ui->tableLocal->hideColumn(StatechartEditorParameterEditor::eProvideDefaultValue);
70  // ui->tableLocal->hideColumn(StatechartEditorParameterEditor::eEditableValue);
71  // ui->tableOutput->hideColumn(StatechartEditorParameterEditor::eProvideDefaultValue);
72  // ui->tableOutput->hideColumn(StatechartEditorParameterEditor::eEditableValue);
73  ui->tableInput->setVariantInfo(variantInfo);
74  ui->tableLocal->setVariantInfo(variantInfo);
75  ui->tableOutput->setVariantInfo(variantInfo);
76 
77  if (stateInstance)
78  {
79  statechartmodel::StatePtr state = stateInstance->getStateClass();
80 
81  if (!stateInstance->getParent())
82  {
83  // toplevel state-> instance name is not real anyway
84  ui->editInstanceName->setEnabled(false);
85  }
86 
87  ui->editInstanceName->setText(stateInstance->getInstanceName());
88  ui->editInstanceName->setValidator(new QRegExpValidator(QRegExp("([a-zA-Z][a-zA-Z0-9_]*)"), this));
89 
90  if (state)
91  {
92  ui->editStateName->setText(state->getStateName());
93  ui->stateDocuEditor->setPlainText(state->getDescription());
94  statechartmodel::EventList outgoingEvents = state->getOutgoingEvents();
95  foreach (statechartmodel::EventPtr e, outgoingEvents)
96  {
97  ui->cbOutgoingEvents->addItem(e->name);
98  eventDescriptionMap[e->name] = e->description;
99  }
100 
101  if (!outgoingEvents.isEmpty())
102  {
103  eventNameComboboxChanged(ui->cbOutgoingEvents->currentText());
104  }
105 
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());
112 
113  connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(setBlackLists()));
114  setBlackLists();
115 
116 
117 
118  }
119  else
120  {
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);
128  }
129 
130  statechartmodel::RemoteStatePtr remoteState = std::dynamic_pointer_cast<statechartmodel::RemoteState>(stateInstance);
131 
132  if (remoteState)
133  {
134  ui->editProxyName->setText(remoteState->proxyName);
135  }
136  else
137  {
138  ui->editProxyName->setEnabled(false);
139  }
140 
141  statechartmodel::EndStatePtr endstate = std::dynamic_pointer_cast<statechartmodel::EndState>(stateInstance);
142 
143  if (endstate)
144  {
145  ui->labelInstanceName->setText("State/Event name");
146  }
147  else
148  {
149  ui->labelInstanceName->setText("State Instance name");
150  }
151  }
152 
153  fileDialog = new QFileDialog(this);
154  fileDialog->setModal(true);
155  fileDialog->setFileMode(QFileDialog::ExistingFiles);
156  fileDialog->setOption(QFileDialog::HideNameFilterDetails, false);
157  QList<QUrl> urls;
158  urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::HomeLocation))
159  << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
160 
162  {
163  urls << QUrl::fromLocalFile(QString::fromStdString(ArmarXDataPath::getHomePath()));
164  }
165 
166  fileDialog->setSidebarUrls(urls);
167  QStringList fileTypes;
168  fileTypes << tr("Libraries (*.so)")
169  << tr("All Files (*.*)");
170  fileDialog->setNameFilters(fileTypes);
171  // connect(ui->btnLoadLib, SIGNAL(clicked()), fileDialog, SLOT(show()));
172  connect(fileDialog, SIGNAL(accepted()), this, SLOT(loadLibrary()));
173 
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)));
182 
183  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!readOnly);
184 
185 
186  if (locked)
187  {
188  setLockStatus(false);
189  }
190 
191  if (readOnly || !locked)
192  {
193  ui->btnLock->hide();
194  }
195 
196  QRect rec = QApplication::desktop()->screenGeometry();
197  this->resize(rec.width() * 0.9, this->height());
198 
199  updateLinkHint(0, 0);
200 
201  }
202 
204  {
205  delete ui;
206  }
207 
209  {
211 
212  if (stateInstance && (state = stateInstance->getStateClass()))
213  {
214  return getUpdatedParameters(state->getInputParameters(), *ui->tableInput);
215  }
216 
217  throw new LocalException("Could not get state parameters; invalid state?");
219  }
220 
222  {
224 
225  if (stateInstance && (state = stateInstance->getStateClass()))
226  {
227  return getUpdatedParameters(state->getLocalParameters(), *ui->tableLocal);
228  }
229 
230  throw new LocalException("Could not get state parameters; invalid state?");
232  }
233 
235  {
237 
238  if (stateInstance && (state = stateInstance->getStateClass()))
239  {
240  return getUpdatedParameters(state->getOutputParameters(), *ui->tableOutput);
241  }
242 
243  throw new LocalException("Could not get state parameters; invalid state?");
245  }
246 
248  {
249  return ui->editInstanceName->text();
250  }
251 
253  {
254  return ui->editStateName->text();
255  }
256 
258  {
259  return ui->editProxyName->text();
260  }
261 
263  {
264  return ui->stateDocuEditor->toPlainText();
265  }
266 
268  {
269  statechartmodel::EventList eventList;
270 
271  for (int i = 0; i < ui->cbOutgoingEvents->count(); ++i)
272  {
274  event->name = ui->cbOutgoingEvents->itemText(i);
275  event->description = eventDescriptionMap[event->name];
276  eventList.append(event);
277  }
278 
279  return eventList;
280  }
281 
282  void StateDialog::eventNameComboboxChanged(const QString& eventName)
283  {
284  ui->editEventDescription->setEnabled(true);
285  ui->editEventDescription->setPlainText(eventDescriptionMap[eventName]);
286  ui->btnDeleteEvent->setEnabled(true);
287  }
288 
289  void StateDialog::eventButtonAdd()
290  {
291  RegExpValidatedInputDialog d("New Event Name", "Event name",
292  QRegExp("^([a-zA-Z_]{1})([a-zA-Z_0-9]+)$"), this);
293 
294  if (d.exec() == QDialog::Accepted)
295  {
296  auto eventName = d.getTextValue();
297  if (!eventName.isEmpty())
298  {
299  ui->cbOutgoingEvents->addItem(eventName);
300  }
301  // No need to create entry in event description map (automatically done by operator[] on access)
302  }
303  }
304 
305  void StateDialog::eventButtonDelete()
306  {
307  if (ui->cbOutgoingEvents->currentText() != "Failure")
308  {
309  ui->cbOutgoingEvents->removeItem(ui->cbOutgoingEvents->currentIndex());
310  }
311  else
312  {
313  QMessageBox::warning(0, "Deletion failed", "You cannot delete the failure event. This event is needed to handle internal statechart failures.");
314  }
315  }
316 
317  void StateDialog::eventDescriptionChanged()
318  {
319  eventDescriptionMap[ui->cbOutgoingEvents->currentText()] = ui->editEventDescription->toPlainText();
320  }
321 
322  void StateDialog::loadLibrary()
323  {
324  ARMARX_INFO_S << "loading libs";
325  QStringList files = fileDialog->selectedFiles();
326  foreach (QString file, files)
327  {
328  ARMARX_INFO_S << "loading " << file.toStdString();
329 
330  if (Libraries.find(file) != Libraries.end())
331  {
332  continue;
333  }
334 
335  DynamicLibraryPtr lib(new DynamicLibrary());
336  lib->load(file.toStdString());
337  Libraries[file] = lib;
338  }
340  ui->tableInput->refreshVariantTypes();
341  ui->tableLocal->refreshVariantTypes();
342  ui->tableOutput->refreshVariantTypes();
343  }
344 
345  void StateDialog::setBlackLists()
346  {
347  ui->tableInput->setKeyBlackList(ui->tableLocal->getKeys());
348  ui->tableLocal->setKeyBlackList(ui->tableInput->getKeys());
349  }
350 
351  void StateDialog::setLockStatus(bool unlock)
352  {
353  if (unlock && tipDialog)
354  {
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();
359  tipDialog->raise();
360  }
361 
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);
368  }
369 
370  void StateDialog::updateLinkHint(int, QString)
371  {
372  if (!variantInfo)
373  {
374  return;
375  }
376 
377  QSet<QString> vars;
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)
383  {
384  auto type = VariantContainerType::GetInnerType(var.toStdString());
385  if (!type.empty())
386  {
387  types.push_back(type);
388  }
389  }
390  QStringList libs;
391  for (std::string& lib : variantInfo->findLibNames(types))
392  {
393  libs << QString::fromStdString(lib);
394  }
395  ui->editDependencies->setText(libs.join(" "));
396  }
397 
398 
399  statechartmodel::StateParameterMap StateDialog::getUpdatedParameters(const statechartmodel::StateParameterMap& source, const StatechartEditorParameterEditor& parameterEditor) const
400  {
401  statechartmodel::StateParameterMap newValues = parameterEditor.getStateParameters();
402 
403  QSet<QString> profiles;
404  StatechartProfilePtr p = currentProfile;
405 
406  while (p)
407  {
408  profiles.insert(QString::fromUtf8(p->getName().data()));
409  p = p->getParent();
410  }
411 
412  for (const auto& oldParamEntry : source.toStdMap())
413  {
414  QString key = oldParamEntry.first;
415 
416  // copy default values for other profiles if the parameter still exists && the type is unchanged:
417  if (newValues.contains(key) && newValues[key]->type == oldParamEntry.second->type)
418  {
419  for (const auto& pdvEntry : oldParamEntry.second->profileDefaultValues.toStdMap())
420  {
421  if (!profiles.contains(pdvEntry.first))
422  {
423  newValues[key]->profileDefaultValues.insert(pdvEntry.first, pdvEntry.second);
424  }
425  }
426  }
427  }
428 
429  return newValues;
430  }
431 
432  void StateDialog::showEvent(QShowEvent*)
433  {
434  if (readOnly && tipDialog)
435  {
436 
437  tipDialog->showMessage("This state is read-only. Thus, changes cannot be saved.", "Read-only state");
438  tipDialog->raise();
439  tipDialog->activateWindow();
440  tipDialog->exec();
441 
442  }
443  }
444 }
files
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation files(the "Software")
armarx::DynamicLibraryPtr
std::shared_ptr< DynamicLibrary > DynamicLibraryPtr
Definition: DynamicLibrary.h:123
armarx::StateDialog::StateDialog
StateDialog(statechartmodel::StateInstancePtr stateInstance, Ice::CommunicatorPtr communicator, VariantInfoPtr variantInfo, StatechartProfilePtr currentProfile, bool locked, bool readOnly, QPointer< TipDialog > tipDialog, QWidget *parent=0)
Definition: StateDialog.cpp:48
armarx::VariantContainerType::GetInnerType
static std::string GetInnerType(const std::string &typeStr)
Definition: VariantContainer.cpp:281
ArmarXManager.h
armarx::StateDialog::getProxyName
QString getProxyName() const
Definition: StateDialog.cpp:257
VariantContainer.h
RegExpValidatedInputDialog.h
armarx::ArmarXDataPath::getHomePath
static std::string getHomePath()
Definition: ArmarXDataPath.cpp:583
armarx::statechartmodel::EndStatePtr
std::shared_ptr< EndState > EndStatePtr
Definition: EndState.h:44
armarx::StateDialog
Definition: StateDialog.h:53
armarx::statechartmodel::EventList
QList< EventPtr > EventList
Definition: XmlWriter.h:47
armarx::StatechartProfilePtr
std::shared_ptr< class StatechartProfile > StatechartProfilePtr
Definition: StatechartContext.h:52
StateDialog.h
armarx::statechartmodel::StateInstancePtr
std::shared_ptr< StateInstance > StateInstancePtr
Definition: StateInstance.h:138
cxxopts::empty
bool empty(const std::string &s)
Definition: cxxopts.hpp:255
IceInternal::Handle< ::Ice::Communicator >
armarx::StateDialog::getInputParams
statechartmodel::StateParameterMap getInputParams() const
Definition: StateDialog.cpp:208
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:54
armarx::statechartmodel::StateParameterMap
QMap< QString, StateParameterPtr > StateParameterMap
Definition: StateParameter.h:46
armarx::StateDialog::~StateDialog
~StateDialog() override
Definition: StateDialog.cpp:203
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
armarx::StateDialog::showEvent
void showEvent(QShowEvent *) override
Definition: StateDialog.cpp:432
armarx::StateDialog::getStateInstanceName
QString getStateInstanceName() const
Definition: StateDialog.cpp:247
armarx::StateDialog::getDescription
QString getDescription() const
Definition: StateDialog.cpp:262
armarx::StateDialog::getLocalParams
statechartmodel::StateParameterMap getLocalParams() const
Definition: StateDialog.cpp:221
armarx::StatechartEditorParameterEditor::eOptional
@ eOptional
Definition: StatechartEditorParameterEditor.h:80
armarx::VariantInfoPtr
std::shared_ptr< VariantInfo > VariantInfoPtr
Definition: VariantInfo.h:39
armarx::statechartmodel::StatePtr
std::shared_ptr< State > StatePtr
Definition: State.h:46
TipDialog.h
armarx::statechartmodel::EventPtr
std::shared_ptr< Event > EventPtr
Definition: XmlWriter.h:46
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
armarx::StateDialog::getStateName
QString getStateName() const
Definition: StateDialog.cpp:252
armarx::statechartmodel::RemoteStatePtr
std::shared_ptr< RemoteState > RemoteStatePtr
Definition: RemoteState.h:43
armarx::statechartmodel::Event
Definition: Event.h:30
armarx::StateDialog::getOutgoingEvents
statechartmodel::EventList getOutgoingEvents() const
Definition: StateDialog.cpp:267
armarx::StateDialog::getOutputParams
statechartmodel::StateParameterMap getOutputParams() const
Definition: StateDialog.cpp:234
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ArmarXManager::RegisterKnownObjectFactoriesWithIce
static void RegisterKnownObjectFactoriesWithIce(const Ice::CommunicatorPtr &ic)
Registers all object factories that are known with Ice.
Definition: ArmarXManager.cpp:1204