StatechartView.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::
17 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
18 * @date 2014
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "StatechartView.h"
24 
25 #include <QTimer>
26 
28 #include <ArmarXCore/interface/statechart/RemoteStateOffererIce.h>
31 
32 #include <ArmarXGui/gui-plugins/StatechartViewerPlugin/ui_StatechartView.h>
33 
34 #include "../IceStateConverter.h"
35 #include "../StateScene.h"
36 #include "../model/State.h"
37 #include "../model/stateinstance/EndState.h"
38 #include "../model/stateinstance/StateInstance.h"
39 #include "GraphicsViewZoom.h"
40 #include "StateItem.h"
41 
42 namespace armarx
43 {
45  bool enableLayouting,
46  QWidget* parent) :
47  QWidget(parent),
48  ui(new Ui::StatechartView),
49  // layoutController(state, enableLayouting)
50  layoutThread(state, enableLayouting)
51  {
52  // layoutController.enableLayouting(enableLayouting);
53 
54  srand(IceUtil::Time::now().toMilliSeconds());
55  ui->setupUi(this);
56  stateScene = new StateScene(this);
57 
58  if (!state)
59  {
60  state = CreateTestStates();
61  }
62 
63  setState(state);
64 
65  ui->graphicsView->setScene(stateScene);
66  zoomer = new Graphics_view_zoom(ui->graphicsView);
67  zoomer->set_modifiers(Qt::NoModifier);
68 
69  if (state)
70  {
71  layoutThread.run();
72  }
73  else
74  {
75  ARMARX_WARNING_S << "no state set!";
76  }
77 
78  connect(stateScene, SIGNAL(selectionChanged()), this, SLOT(onItemChange()));
79  }
80 
82  {
83  delete ui;
84  }
85 
88  {
89  return stateScene->getStateInstance();
90  }
91 
92  QGraphicsView*
94  {
95  return ui->graphicsView;
96  }
97 
100  {
101  return zoomer;
102  }
103 
106  {
107  return layoutThread.getController();
108  }
109 
110  void
112  {
113  stateScene->setToplevelState(state);
114 
115  if (state)
116  {
117  layoutThread.setState(state);
118  // layoutController.setTopState(state);
119  }
120  }
121 
122  void
124  {
125  ui->graphicsView->setTransform(QTransform());
126  }
127 
128  void
130  {
131  QList<QGraphicsItem*> selection = stateScene->selectedItems();
132  foreach (QGraphicsItem* item, selection)
133  {
134  StateItem* state = dynamic_cast<StateItem*>(item);
135 
136  if (state)
137  {
138  statechartmodel::StatePtr parent = state->getStateInstance()->getParent();
139 
140  if (parent)
141  {
142  if (parent->removeSubstate(state->getStateInstance()->getInstanceName()))
143  {
144  ARMARX_INFO_S << "removed substate";
145  }
146  else
147  {
148  ARMARX_INFO_S << "removing failed";
149  }
150  }
151  }
152  }
153  }
154 
155  void
157  {
158  ui->graphicsView->fitInView(stateScene->itemsBoundingRect(), Qt::KeepAspectRatio);
159  }
160 
161  void
163  {
164  QList<QGraphicsItem*> selection = stateScene->selectedItems();
165 
166  if (selection.size() > 0)
167  {
168  StateItem* state = dynamic_cast<StateItem*>(selection.first());
169 
170  if (state && state->getStateInstance())
171  {
172  emit selectedStateChanged(state->getStateInstance());
173  }
174  }
175  }
176 
177  void
179  {
180  stateScene->getTopLevelStateItem()->setMaxShownSubstateLevel(show ? -1 : 1);
181  stateScene->update();
182  }
183 
186  {
187 
189  topState->setStateName("TopLevelState");
190 
192  sub1->setStateName("sub1");
193 
194  statechartmodel::EventList eventList;
196  e1->name = "Success";
197  eventList.append(e1);
199  e2->name = "Failure";
200  eventList.append(e2);
202  e3->name = "Lolz";
203  eventList.append(e3);
204  sub1->setOutgoingEvents(eventList);
205 
207  sub2->setStateName("sub2");
208 
210  subsub1->setStateName("subsub1");
211 
212 
213  topState->addSubstate(sub1);
214  topState->addSubstate(sub2);
215  topState->addRemoteSubstate(sub1, "ProxyName", "remotesub1-2");
216  statechartmodel::EventList eventList2;
218  e21->name = "Success";
219  eventList.append(e21);
221  e22->name = "Failure";
222  eventList.append(e22);
223  subsub1->setOutgoingEvents(eventList2);
224  sub1->addSubstate(subsub1);
225 
226  subsub1->addEndSubstate("EndState", "Success");
227 
229  t1->eventName = "GoOn";
230  t1->sourceState = topState->getSubstates()["sub1"];
231  t1->destinationState = topState->getSubstates()["sub2"];
232  topState->addTransition(t1);
233 
234  return topState;
235  }
236 
237  QSize
239  {
240  return QSize(200, 200);
241  }
242 } // namespace armarx
armarx::Graphics_view_zoom::set_modifiers
void set_modifiers(Qt::KeyboardModifiers modifiers)
Definition: GraphicsViewZoom.cpp:64
StatechartView.h
armarx::StatechartView::viewAll
void viewAll()
Definition: StatechartView.cpp:156
GraphicsViewZoom.h
armarx::StateItem::getStateInstance
statechartmodel::StateInstancePtr getStateInstance() const
Definition: StateItem.h:66
armarx::statechartmodel::TransitionPtr
std::shared_ptr< Transition > TransitionPtr
Definition: Transition.h:90
StateItem.h
armarx::statechartmodel::EventList
QList< EventPtr > EventList
Definition: XmlWriter.h:47
armarx::LayoutController
Definition: LayoutController.h:45
armarx::StateScene::getStateInstance
statechartmodel::StateInstancePtr getStateInstance() const
Definition: StateScene.cpp:43
armarx::LayoutThread::getController
LayoutController & getController()
Definition: LayoutThread.h:56
armarx::statechartmodel::StateInstancePtr
std::shared_ptr< StateInstance > StateInstancePtr
Definition: StateInstance.h:146
StatechartObjectFactories.h
armarx::StateItem::setMaxShownSubstateLevel
void setMaxShownSubstateLevel(int value)
Definition: StateItem.cpp:676
armarx::StatechartView::setState
void setState(statechartmodel::StatePtr state)
Definition: StatechartView.cpp:111
ObserverObjectFactories.h
armarx::StateScene::getTopLevelStateItem
StateItem * getTopLevelStateItem() const
Definition: StateScene.cpp:138
armarx::StatechartView::getGraphicsView
QGraphicsView * getGraphicsView() const
Definition: StatechartView.cpp:93
armarx::StatechartView
Definition: StatechartView.h:45
armarx::statechartmodel::Transition
Definition: Transition.h:68
armarx::LayoutThread::setState
void setState(armarx::statechartmodel::StatePtr state)
setState Stops the event loop and resets the top state.
Definition: LayoutThread.cpp:47
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:54
ManagedIceObject.h
armarx::LayoutThread::run
void run()
Definition: LayoutThread.cpp:55
armarx::StatechartView::deleteSelectedStates
void deleteSelectedStates()
Definition: StatechartView.cpp:129
armarx::StatechartView::getLayoutController
LayoutController & getLayoutController()
Definition: StatechartView.cpp:105
armarx::StatechartView::getGraphicsViewZoomer
Graphics_view_zoom * getGraphicsViewZoomer() const
Definition: StatechartView.cpp:99
armarx::statechartmodel::State
Definition: State.h:54
armarx::StatechartView::showSubSubstates
void showSubSubstates(bool show=true)
Definition: StatechartView.cpp:178
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:213
armarx::Graphics_view_zoom
Definition: GraphicsViewZoom.h:65
armarx::StateScene::setToplevelState
void setToplevelState(statechartmodel::StatePtr toplevelStateInstance)
Definition: StateScene.cpp:49
armarx::statechartmodel::StatePtr
std::shared_ptr< State > StatePtr
Definition: State.h:48
armarx::StateItem
Definition: StateItem.h:58
armarx::statechartmodel::EventPtr
std::shared_ptr< Event > EventPtr
Definition: XmlWriter.h:46
armarx::StatechartView::~StatechartView
~StatechartView() override
Definition: StatechartView.cpp:81
armarx::StatechartView::getStateInstance
statechartmodel::StateInstancePtr getStateInstance() const
Definition: StatechartView.cpp:87
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:202
armarx::statechartmodel::Event
Definition: Event.h:31
armarx::StatechartView::setOriginalZoom
void setOriginalZoom()
Definition: StatechartView.cpp:123
armarx::StatechartView::CreateTestStates
static statechartmodel::StatePtr CreateTestStates()
Definition: StatechartView.cpp:185
armarx::StateScene
Definition: StateScene.h:52
armarx::StatechartView::StatechartView
StatechartView(statechartmodel::StatePtr state=statechartmodel::StatePtr(new statechartmodel::State()), bool enableLayouting=true, QWidget *parent=0)
Definition: StatechartView.cpp:44
armarx::StatechartView::sizeHint
QSize sizeHint() const override
Definition: StatechartView.cpp:238
armarx::StatechartView::selectedStateChanged
void selectedStateChanged(statechartmodel::StateInstancePtr selectedStateInstance)
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::StatechartView::onItemChange
void onItemChange()
Definition: StatechartView.cpp:162