StateEditorController.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 "StateEditorController.h"
25 #include "StateTreeController.h"
31 #include "../StatechartViewerPlugin/view/StatechartView.h"
32 #include "../StatechartViewerPlugin/model/stateinstance/RemoteState.h"
33 #include "../StatechartViewerPlugin/model/stateinstance/DynamicRemoteState.h"
34 #include <QMenu>
35 #include <QMessageBox>
37 
38 namespace armarx
39 {
40 
41  StateEditorController::StateEditorController(StatechartEditorMainWindow* mainWindow, StateTreeControllerPtr treeController, Ice::CommunicatorPtr communicator, VariantInfoPtr variantInfo, StatechartProfilePtr currentProfile, QPointer<TipDialog> tipDialog, QObject* parent) :
42  QObject(parent),
43  mainWindow(mainWindow),
44  treeController(treeController),
45  communicator(communicator),
46  variantInfo(variantInfo),
47  currentProfile(currentProfile),
48  tipDialog(tipDialog)
49  {
50  if (mainWindow)
51  {
52  connect(mainWindow->getStateTabWidget(), SIGNAL(currentChanged(int)), this, SLOT(connectToStateTab(int)));
53  }
54  else
55  {
56  ARMARX_ERROR_S << "mainwindow ptr must not be NULL";
57  }
58 
59  editState = menu.addAction(QIcon(":/icons/accessories-text-editor-6.ico"), "");
60  deleteState = menu.addAction(QIcon(":/icons/delete.ico"), "Delete State");
61  layoutState = menu.addAction(QIcon(":/icons/magic-wand-icon.png"), "Layout State");
62  setstartstate = menu.addAction("Set as initial State");
63 
64  newEndstate = menu.addAction(QIcon(":/statechart-editor/new-endstate.svg"), "Insert new Endstate");
65  replaceState = menu.addAction("Replace State");
66  openCode = menu.addAction(QIcon(":/icons/cpp.svg"), "Open C++ Code");
67  findStateInTree = menu.addAction(QIcon(":/icons/search.svg"), "Find State in Tree");
68  for (QAction* a : menu.actions())
69  {
70  a->setIconVisibleInMenu(true);
71  }
72  connect(mainWindow->ui->actionEdit_State_Properties, SIGNAL(triggered(bool)), this, SLOT(editSelectedState()));
73  }
74 
75 
77  {
78  statechartmodel::StatePtr state = stateInstance->getStateClass();
79 
80  if (stateInstance->getParent())
81  {
82  stateInstance->getParent()->renameSubstate(stateInstance->getInstanceName(), d.getStateInstanceName());
83  }
84 
85  if (state)
86  {
87  // ARMARX_INFO_S << "setting class params";
88  // state->setStateName(d.getStateName());
89 
90  state->setDescription(d.getDescription());
91  state->setOutgoingEvents(d.getOutgoingEvents());
92  state->setInputParameters(d.getInputParams());
93  state->setLocalParameters(d.getLocalParams());
94  state->setOutputParameters(d.getOutputParams());
95  }
96 
97  statechartmodel::RemoteStatePtr remoteState = std::dynamic_pointer_cast<statechartmodel::RemoteState>(stateInstance);
98 
99  if (remoteState)
100  {
101  remoteState->proxyName = d.getProxyName();
102  }
103 
104  statechartmodel::EndStatePtr endstate = std::dynamic_pointer_cast<statechartmodel::EndState>(stateInstance);
105 
106  if (endstate)
107  {
108  endstate->setEventName(d.getStateInstanceName());
109  }
110  }
111 
112  void StateEditorController::showStateContextMenu(statechartmodel::StateInstancePtr stateInstance, QPoint mouseScreenPos, QPointF mouseItemPos)
113  {
114  if (!stateInstance)
115  {
116  return;
117  }
118  bool editable = true;
119  if (stateInstance &&
120  stateInstance->getStateClass() &&
121  !stateInstance->getStateClass()->isEditable())
122  {
123  editable = false;
124  }
125 
126 
127 
128  statechartmodel::StatePtr parentState = stateInstance->getParent();
129  StateTreeNodePtr node = treeController->getNodeByState(stateInstance->getStateClass());
130  editState->setText("Edit State " + stateInstance->getInstanceName());
131  deleteState->setEnabled(!!parentState && parentState->isEditable());
132  setstartstate->setEnabled(!!parentState && parentState->isEditable());
133 
134  replaceState->setEnabled(editable);
135 
136 
137  openCode->setEnabled(!(!stateInstance->getStateClass() || (node && !node->getCppExists())));
138 
139 
140  newEndstate->setEnabled(!(stateInstance->getType() == eFinalState || stateInstance->getType() == eDynamicRemoteState)
141  && stateInstance->getStateClass()
142  && stateInstance->getStateClass()->isEditable());
143 
144 
145  layoutState->setEnabled(!(stateInstance->getType() == eFinalState
146  || stateInstance->getType() == eDynamicRemoteState
147  || !stateInstance->getStateClass()
148  || stateInstance->getStateClass()->getSubstates().size() <= 1));
149 
150 
151  replaceState->setEnabled(false);
152 
153  QAction* result = menu.exec(mouseScreenPos);
154 
155  if (result == editState)
156  {
157  StateDialog d(stateInstance, communicator, variantInfo, currentProfile, lockRemoteStates && stateInstance->getType() == eRemoteState ? true : false, !editable, tipDialog);
158 
159  if (d.exec() == QDialog::Accepted)
160  {
161  stateDialogAccepted(stateInstance, d);
162  }
163  }
164  else if (result == deleteState)
165  {
166  if (QMessageBox::question(0, "State deletion", "Do you really want to delete state '" + stateInstance->getInstanceName() + "'?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
167  {
168  parentState->removeSubstate(stateInstance);
169  }
170  }
171  else if (result == layoutState)
172  {
174  }
175  else if (result == setstartstate)
176  {
177  parentState->setStartState(stateInstance);
178  }
179  else if (result == newEndstate)
180  {
181  ARMARX_INFO_S << "Inserting endstate";
182  statechartmodel::StatePtr state = stateInstance->getStateClass();
183  int i = 2;
184  const QString newStateNameBase = "MyEndState";
185  QString newStateName = newStateNameBase;
186 
187  while (!state->addEndSubstate(newStateName, newStateName, mouseItemPos))
188  {
189  newStateName = newStateNameBase + "_" + QString::number(i);
190  i++;
191  }
192 
193  }
194  else if (result == openCode && stateInstance->getStateClass())
195  {
196  StateTreeNodePtr node = treeController->getNodeByState(stateInstance->getStateClass());
197  treeController->openStateCPP(node);
198  }
199  else if (result == findStateInTree && stateInstance->getStateClass())
200  {
201  treeController->selectNodeByState(stateInstance->getStateClass());
202  }
203 
204  }
205 
206  void StateEditorController::showTransitionContextMenu(statechartmodel::TransitionCPtr transition, statechartmodel::StatePtr state, QPoint mouseScreenPos, QPointF mouseItemPos)
207  {
208  ARMARX_DEBUG_S << "pos: " << mouseItemPos;
209  QMenu menu;
210  QAction* editTransition = menu.addAction("Edit Transition " + transition->eventName);
211  QAction* enableTransitionCode = menu.addAction("Enable Transition Hook for " + transition->eventName);
212  enableTransitionCode->setCheckable(true);
213  enableTransitionCode->setChecked(transition->transitionUserCode);
214 
215  StateTreeNodePtr node = treeController->getNodeByState(state);
216 
217 
218  if (!transition->destinationState || !transition->destinationState->getStateClass()
219  || !node || !node->getCppExists() || !transition->sourceState)
220  {
221  if (!node->getCppExists())
222  {
223  enableTransitionCode->setText(enableTransitionCode->text() + " (no parent cpp)");
224  }
225  else if (!transition->sourceState)
226  {
227  enableTransitionCode->setText("Transition code unavailable for initial transition");
228  }
229  else if (transition->destinationState && !transition->destinationState->getStateClass())
230  {
231  enableTransitionCode->setText(enableTransitionCode->text() + " (final or unloaded destination state)");
232  }
233  enableTransitionCode->setEnabled(false);
234  }
235 
236  QAction* detach = menu.addAction("Detach Transition");
237  //QAction* insertPoint = menu.addAction("Insert Support Point");
238  //insertPoint->setEnabled(false);
239 
240  if (!transition->sourceState || !transition->destinationState)
241  {
242  detach->setEnabled(false);
243  }
244 
245  QAction* setSupportPoint {nullptr};
246  std::map<QAction*, int> bendMap;
247 
248  if (transition->sourceState && transition->destinationState)
249  {
250  menu.addSeparator();
251  bendMap.insert(std::make_pair(menu.addAction("Straighten"), 0));
252  QMenu* bend = menu.addMenu("Bend");
253  bendMap.insert(std::make_pair(bend->addAction("Bend Left 10%"), 10));
254  bendMap.insert(std::make_pair(bend->addAction("Bend Left 25%"), 25));
255  bendMap.insert(std::make_pair(bend->addAction("Bend Left 50%"), 50));
256  bendMap.insert(std::make_pair(bend->addAction("Bend Left 100%"), 100));
257  bendMap.insert(std::make_pair(bend->addAction("Bend Right 10%"), -10));
258  bendMap.insert(std::make_pair(bend->addAction("Bend Right 25%"), -25));
259  bendMap.insert(std::make_pair(bend->addAction("Bend Right 50%"), -50));
260  bendMap.insert(std::make_pair(bend->addAction("Bend Right 100%"), -100));
261  setSupportPoint = menu.addAction("Set Support Point");
262  }
263 
264  QAction* result = menu.exec(mouseScreenPos);
265 
266 
267 
268 
269  if (result == editTransition)
270  {
271  if (transition->destinationState &&
272  transition->destinationState->getType() != eFinalState &&
273  !transition->destinationState->getStateClass())
274  {
275  QMessageBox::warning(mainWindow, "Transition Editing Error", "Transition editing not possible because destination state is not loaded - load statechart group of destination state.");
276  }
277  else
278  {
279 
281 
282  if (d.exec() == QDialog::Accepted)
283  {
284  state->setTransitionMapping(transition, d.getMappingToNextStateInput(),
287  }
288  }
289  }
290  else if (result == detach)
291  {
292  state->detachTransitionDestination(transition);
293  }
294  // else if(result == insertPoint)
295  // {
296  // state->addSupportPoint(transition, mouseItemPos);
297  // }
298  else if (bendMap.find(result) != bendMap.end())
299  {
300  state->bendTransition(transition, 50, bendMap[result]);
301  }
302  else if (result == setSupportPoint)
303  {
305 
306  if (d.exec() == QDialog::Accepted)
307  {
308  state->bendTransition(transition, d.getU(), d.getV());
309  }
310  }
311  else if (result == enableTransitionCode)
312  {
313  state->setTransitionUserCodeEnabled(transition, result->isChecked());
314  if (result->isChecked())
315  {
316  tipDialog->showMessage("Transition functions enable you to execute user code during a transition. This is designated for parameter conversion or minor calculations. Do NOT execute long running operations in transition functions.\n"
317  "On next saving of the statecharts a pure virtual function will be generated in the state of this transition. You need to implement this function. Otherwise a compilation error will occur.", "Transition Function", "TransitionFunction");
318  }
319  }
320  }
321 
323  {
324  if (!currentProfile)
325  {
326  return QList<QString>();
327  }
328 
329  QList<QString> result {QString::fromUtf8(currentProfile->getName().c_str())};
330  auto profile = currentProfile;
331 
332  while ((profile = profile->getParent()))
333  {
334  result.push_back(QString::fromUtf8(profile->getName().c_str()));
335  }
336 
337  return result;
338  }
339 
341  {
344  && mainWindow->getStateTabWidget()->currentStateview()->getScene()->selectedItems().size() > 0)
345  {
346  StateItem* item = dynamic_cast<StateItem*>(*mainWindow->getStateTabWidget()->currentStateview()->getScene()->selectedItems().begin());
347 
348  if (item)
349  {
350  StateDialog d(item->getStateInstance(), communicator, variantInfo, currentProfile, item->getStateInstance()->getType() == eRemoteState ? true : false, !item->isEditable(), tipDialog);
351  if (d.exec() == QDialog::Accepted)
352  {
354  }
355 
356  }
357  else
358  {
359  TransitionItem* transitionItem = dynamic_cast<TransitionItem*>(*mainWindow->getStateTabWidget()->currentStateview()->getScene()->selectedItems().begin());
360  item = dynamic_cast<StateItem*>(transitionItem->parentItem());
361  if (transitionItem && item)
362  {
363  TransitionDialog d(transitionItem->getTransition(), item->getStateInstance()->getStateClass(),
365  if (d.exec() == QDialog::Accepted)
366  {
367  item->getStateInstance()->getStateClass()->setTransitionMapping(transitionItem->getTransition(), d.getMappingToNextStateInput(),
370  }
371  }
372  }
373 
374  }
375  }
376 
377 
379  {
380  lockRemoteStates = lock;
381  }
382 
383 
384  void StateEditorController::connectToStateTab(int index)
385  {
387  {
388  connect(mainWindow->getStateTabWidget()->currentStateview()->getScene(), SIGNAL(stateContextMenuRequested(statechartmodel::StateInstancePtr, QPoint, QPointF)), this, SLOT(showStateContextMenu(statechartmodel::StateInstancePtr, QPoint, QPointF)), Qt::UniqueConnection);
389  connect(mainWindow->getStateTabWidget()->currentStateview()->getScene(), SIGNAL(transitionContextMenuRequested(statechartmodel::TransitionCPtr, statechartmodel::StatePtr, QPoint, QPointF)), this, SLOT(showTransitionContextMenu(statechartmodel::TransitionCPtr, statechartmodel::StatePtr, QPoint, QPointF)), Qt::UniqueConnection);
390  connect(mainWindow->ui->actionShow_Subsubstates,
391  SIGNAL(toggled(bool)),
393  SLOT(showSubSubstates(bool)));
394 
395  }
396  }
397 
398 }
armarx::StatechartView::getScene
StateScene * getScene() const
Definition: StatechartView.h:54
armarx::StateEditorController::editState
QAction * editState
Definition: StateEditorController.h:83
armarx::StateTabWidget::currentStateview
StatechartView * currentStateview() const
Definition: StateTabWidget.cpp:47
armarx::TransitionDialog::getMappingToParentStateLocal
statechartmodel::ParameterMappingList getMappingToParentStateLocal() const
Definition: TransitionDialog.cpp:61
armarx::StateEditorController::editSelectedState
void editSelectedState()
Definition: StateEditorController.cpp:340
armarx::StateEditorController::replaceState
QAction * replaceState
Definition: StateEditorController.h:89
armarx::SetSupportPointDialog::getV
int getV()
Definition: SetSupportPointDialog.cpp:51
armarx::StateEditorController::getRelevantProfiles
QList< QString > getRelevantProfiles() const
Definition: StateEditorController.cpp:322
armarx::StateTreeNodePtr
std::shared_ptr< StateTreeNode > StateTreeNodePtr
Definition: StatechartGroupDefs.h:31
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::StateDialog::getProxyName
QString getProxyName() const
Definition: StateDialog.cpp:257
armarx::StateEditorController::communicator
Ice::CommunicatorPtr communicator
Definition: StateEditorController.h:77
armarx::statechartmodel::EndStatePtr
std::shared_ptr< EndState > EndStatePtr
Definition: EndState.h:44
armarx::StateItem::getStateInstance
statechartmodel::StateInstancePtr getStateInstance() const
Definition: StateItem.h:70
armarx::StateEditorController::tipDialog
QPointer< TipDialog > tipDialog
Definition: StateEditorController.h:80
StateItem.h
armarx::StateEditorController::StateEditorController
StateEditorController(StatechartEditorMainWindow *mainWindow, StateTreeControllerPtr treeController, Ice::CommunicatorPtr communicator, VariantInfoPtr variantInfo, StatechartProfilePtr currentProfile, QPointer< TipDialog > tipDialog, QObject *parent=0)
Definition: StateEditorController.cpp:41
armarx::StateDialog
Definition: StateDialog.h:53
StateEditorController.h
armarx::StatechartProfilePtr
std::shared_ptr< class StatechartProfile > StatechartProfilePtr
Definition: StatechartContext.h:52
StateDialog.h
armarx::StateEditorController::openCode
QAction * openCode
Definition: StateEditorController.h:90
armarx::statechartmodel::StateInstancePtr
std::shared_ptr< StateInstance > StateInstancePtr
Definition: StateInstance.h:138
armarx::StateEditorController::treeController
StateTreeControllerPtr treeController
Definition: StateEditorController.h:75
IceInternal::Handle< ::Ice::Communicator >
armarx::StateDialog::getInputParams
statechartmodel::StateParameterMap getInputParams() const
Definition: StateDialog.cpp:208
armarx::StateEditorController::menu
QMenu menu
Definition: StateEditorController.h:82
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:198
armarx::TransitionDialog
Definition: TransitionDialog.h:40
StateTreeController.h
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
armarx::StateEditorController::currentProfile
StatechartProfilePtr currentProfile
Definition: StateEditorController.h:79
armarx::StateEditorController::deleteState
QAction * deleteState
Definition: StateEditorController.h:84
armarx::StateEditorController::findStateInTree
QAction * findStateInTree
Definition: StateEditorController.h:91
armarx::StateEditorController::variantInfo
VariantInfoPtr variantInfo
Definition: StateEditorController.h:78
armarx::StateEditorController::stateDialogAccepted
void stateDialogAccepted(statechartmodel::StateInstancePtr stateInstance, const armarx::StateDialog &d)
Definition: StateEditorController.cpp:76
armarx::StateEditorController::setstartstate
QAction * setstartstate
Definition: StateEditorController.h:86
armarx::StatechartEditorMainWindow::getStateTabWidget
StateTabWidget * getStateTabWidget() const
Definition: StatechartEditorMainWindow.cpp:60
armarx::TransitionItem::getTransition
statechartmodel::TransitionCPtr getTransition() const
Definition: TransitionItem.cpp:565
armarx::TransitionItem
Definition: TransitionItem.h:44
TransitionItem.h
armarx::StateDialog::getStateInstanceName
QString getStateInstanceName() const
Definition: StateDialog.cpp:247
armarx::StateDialog::getDescription
QString getDescription() const
Definition: StateDialog.cpp:262
armarx::StatechartEditorMainWindow
Definition: StatechartEditorMainWindow.h:46
armarx::StateEditorController::newEndstate
QAction * newEndstate
Definition: StateEditorController.h:88
No
Introduction Thank you for taking interest in our work and downloading this software This library implements the algorithm described in the paper R R R Klein Efficient RANSAC for Point Cloud Shape in Computer Graphics No
Definition: ReadMe.txt:21
armarx::TransitionDialog::getMappingToNextStateInput
statechartmodel::ParameterMappingList getMappingToNextStateInput() const
Definition: TransitionDialog.cpp:56
armarx::TransitionDialog::getMappingToParentStateOutput
statechartmodel::ParameterMappingList getMappingToParentStateOutput() const
Definition: TransitionDialog.cpp:66
armarx::SetSupportPointDialog
Definition: SetSupportPointDialog.h:39
armarx::StateDialog::getLocalParams
statechartmodel::StateParameterMap getLocalParams() const
Definition: StateDialog.cpp:221
armarx::StateEditorController::mainWindow
StatechartEditorMainWindow * mainWindow
Definition: StateEditorController.h:74
StateTabWidget.h
armarx::VariantInfoPtr
std::shared_ptr< VariantInfo > VariantInfoPtr
Definition: VariantInfo.h:39
armarx::statechartmodel::TransitionCPtr
std::shared_ptr< const Transition > TransitionCPtr
Definition: Transition.h:94
armarx::statechartmodel::StatePtr
std::shared_ptr< State > StatePtr
Definition: State.h:46
armarx::StateItem
Definition: StateItem.h:58
armarx::SetSupportPointDialog::getU
int getU()
Definition: SetSupportPointDialog.cpp:46
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
SetSupportPointDialog.h
armarx::statechartmodel::RemoteStatePtr
std::shared_ptr< RemoteState > RemoteStatePtr
Definition: RemoteState.h:43
armarx::StateDialog::getOutgoingEvents
statechartmodel::EventList getOutgoingEvents() const
Definition: StateDialog.cpp:267
armarx::StateEditorController::showTransitionContextMenu
void showTransitionContextMenu(statechartmodel::TransitionCPtr transition, statechartmodel::StatePtr state, QPoint mouseScreenPos, QPointF mouseItemPos)
Definition: StateEditorController.cpp:206
TransitionDialog.h
armarx::StatechartEditorMainWindow::layoutState
void layoutState(statechartmodel::StateInstancePtr instance=statechartmodel::StateInstancePtr())
Definition: StatechartEditorMainWindow.cpp:110
armarx::StateEditorController::setLockRemoteStatesByDefault
void setLockRemoteStatesByDefault(bool lock)
Definition: StateEditorController.cpp:378
armarx::StateDialog::getOutputParams
statechartmodel::StateParameterMap getOutputParams() const
Definition: StateDialog.cpp:234
armarx::StateEditorController::layoutState
QAction * layoutState
Definition: StateEditorController.h:85
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::StateEditorController::lockRemoteStates
bool lockRemoteStates
Definition: StateEditorController.h:81
armarx::StateEditorController::showStateContextMenu
void showStateContextMenu(statechartmodel::StateInstancePtr stateInstance, QPoint mouseScreenPos, QPointF mouseItemPos)
Definition: StateEditorController.cpp:112
RoundRectItem::isEditable
bool isEditable() const
Definition: RoundRectItem.cpp:478
armarx::StateTreeControllerPtr
std::shared_ptr< StateTreeController > StateTreeControllerPtr
Definition: StateEditorController.h:46
GfxTL::Yes
OnOff< true > Yes
Definition: OnOff.h:12