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 <QTimer>
24 
25 #include "StatechartView.h"
26 #include <ArmarXGui/gui-plugins/StatechartViewerPlugin/ui_StatechartView.h>
27 
28 #include "StateItem.h"
29 #include "GraphicsViewZoom.h"
30 
31 #include "../IceStateConverter.h"
32 #include "../StateScene.h"
33 #include "../model/State.h"
34 #include "../model/stateinstance/StateInstance.h"
35 #include "../model/stateinstance/EndState.h"
36 
40 #include <ArmarXCore/interface/statechart/RemoteStateOffererIce.h>
41 
42 
43 
44 namespace armarx
45 {
46  StatechartView::StatechartView(statechartmodel::StatePtr state, bool enableLayouting, 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 
87  {
88  return stateScene->getStateInstance();
89  }
90 
91  QGraphicsView* StatechartView::getGraphicsView() const
92  {
93  return ui->graphicsView;
94  }
95 
97  {
98  return zoomer;
99  }
100 
102  {
103  return layoutThread.getController();
104  }
105 
107  {
108  stateScene->setToplevelState(state);
109 
110  if (state)
111  {
112  layoutThread.setState(state);
113  // layoutController.setTopState(state);
114  }
115  }
116 
118  {
119  ui->graphicsView->setTransform(QTransform());
120  }
121 
123  {
124  QList<QGraphicsItem*> selection = stateScene->selectedItems();
125  foreach (QGraphicsItem* item, selection)
126  {
127  StateItem* state = dynamic_cast<StateItem*>(item);
128 
129  if (state)
130  {
131  statechartmodel::StatePtr parent = state->getStateInstance()->getParent();
132 
133  if (parent)
134  {
135  if (parent->removeSubstate(state->getStateInstance()->getInstanceName()))
136  {
137  ARMARX_INFO_S << "removed substate";
138  }
139  else
140  {
141  ARMARX_INFO_S << "removing failed";
142  }
143 
144  }
145  }
146  }
147  }
148 
150  {
151  ui->graphicsView->fitInView(stateScene->itemsBoundingRect(), Qt::KeepAspectRatio);
152  }
153 
155  {
156  QList<QGraphicsItem*> selection = stateScene->selectedItems();
157 
158  if (selection.size() > 0)
159  {
160  StateItem* state = dynamic_cast<StateItem*>(selection.first());
161 
162  if (state && state->getStateInstance())
163  {
164  emit selectedStateChanged(state->getStateInstance());
165  }
166  }
167  }
168 
170  {
171  stateScene->getTopLevelStateItem()->setMaxShownSubstateLevel(show ? -1 : 1);
172  stateScene->update();
173  }
174 
176  {
177 
179  topState->setStateName("TopLevelState");
180 
182  sub1->setStateName("sub1");
183 
184  statechartmodel::EventList eventList;
186  e1->name = "Success";
187  eventList.append(e1);
189  e2->name = "Failure";
190  eventList.append(e2);
192  e3->name = "Lolz";
193  eventList.append(e3);
194  sub1->setOutgoingEvents(eventList);
195 
197  sub2->setStateName("sub2");
198 
200  subsub1->setStateName("subsub1");
201 
202 
203  topState->addSubstate(sub1);
204  topState->addSubstate(sub2);
205  topState->addRemoteSubstate(sub1, "ProxyName", "remotesub1-2");
206  statechartmodel::EventList eventList2;
208  e21->name = "Success";
209  eventList.append(e21);
211  e22->name = "Failure";
212  eventList.append(e22);
213  subsub1->setOutgoingEvents(eventList2);
214  sub1->addSubstate(subsub1);
215 
216  subsub1->addEndSubstate("EndState", "Success");
217 
219  t1->eventName = "GoOn";
220  t1->sourceState = topState->getSubstates()["sub1"];
221  t1->destinationState = topState->getSubstates()["sub2"];
222  topState->addTransition(t1);
223 
224  return topState;
225 
226  }
227 
229  {
230  return QSize(200, 200);
231  }
232 }
armarx::Graphics_view_zoom::set_modifiers
void set_modifiers(Qt::KeyboardModifiers modifiers)
Definition: GraphicsViewZoom.cpp:62
StatechartView.h
armarx::StatechartView::viewAll
void viewAll()
Definition: StatechartView.cpp:149
GraphicsViewZoom.h
armarx::StateItem::getStateInstance
statechartmodel::StateInstancePtr getStateInstance() const
Definition: StateItem.h:70
armarx::statechartmodel::TransitionPtr
std::shared_ptr< Transition > TransitionPtr
Definition: Transition.h:93
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:42
armarx::LayoutThread::getController
LayoutController & getController()
Definition: LayoutThread.h:53
armarx::statechartmodel::StateInstancePtr
std::shared_ptr< StateInstance > StateInstancePtr
Definition: StateInstance.h:138
StatechartObjectFactories.h
armarx::StateItem::setMaxShownSubstateLevel
void setMaxShownSubstateLevel(int value)
Definition: StateItem.cpp:632
armarx::StatechartView::setState
void setState(statechartmodel::StatePtr state)
Definition: StatechartView.cpp:106
ObserverObjectFactories.h
armarx::StateScene::getTopLevelStateItem
StateItem * getTopLevelStateItem() const
Definition: StateScene.cpp:130
armarx::StatechartView::getGraphicsView
QGraphicsView * getGraphicsView() const
Definition: StatechartView.cpp:91
armarx::StatechartView
Definition: StatechartView.h:44
armarx::statechartmodel::Transition
Definition: Transition.h:73
armarx::LayoutThread::setState
void setState(armarx::statechartmodel::StatePtr state)
setState Stops the event loop and resets the top state.
Definition: LayoutThread.cpp:46
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:58
ManagedIceObject.h
armarx::LayoutThread::run
void run()
Definition: LayoutThread.cpp:53
armarx::StatechartView::deleteSelectedStates
void deleteSelectedStates()
Definition: StatechartView.cpp:122
armarx::StatechartView::getLayoutController
LayoutController & getLayoutController()
Definition: StatechartView.cpp:101
armarx::StatechartView::getGraphicsViewZoomer
Graphics_view_zoom * getGraphicsViewZoomer() const
Definition: StatechartView.cpp:96
armarx::statechartmodel::State
Definition: State.h:52
armarx::StatechartView::showSubSubstates
void showSubSubstates(bool show=true)
Definition: StatechartView.cpp:169
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
armarx::Graphics_view_zoom
Definition: GraphicsViewZoom.h:64
armarx::StateScene::setToplevelState
void setToplevelState(statechartmodel::StatePtr toplevelStateInstance)
Definition: StateScene.cpp:47
armarx::statechartmodel::StatePtr
std::shared_ptr< State > StatePtr
Definition: State.h:46
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:86
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
armarx::statechartmodel::Event
Definition: Event.h:30
armarx::StatechartView::setOriginalZoom
void setOriginalZoom()
Definition: StatechartView.cpp:117
armarx::StatechartView::CreateTestStates
static statechartmodel::StatePtr CreateTestStates()
Definition: StatechartView.cpp:175
armarx::StateScene
Definition: StateScene.h:53
armarx::StatechartView::StatechartView
StatechartView(statechartmodel::StatePtr state=statechartmodel::StatePtr(new statechartmodel::State()), bool enableLayouting=true, QWidget *parent=0)
Definition: StatechartView.cpp:46
armarx::StatechartView::sizeHint
QSize sizeHint() const override
Definition: StatechartView.cpp:228
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:28
armarx::StatechartView::onItemChange
void onItemChange()
Definition: StatechartView.cpp:154