StateScene.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 "StateScene.h"
24 
25 #include "view/StateItem.h"
27 #include "model/State.h"
29 #include <QMenu>
30 #include <QPainter>
31 #include <QtSvg/QSvgGenerator>
32 
33 
34 namespace armarx
35 {
36  StateScene::StateScene(QObject* parent) :
37  QGraphicsScene(parent)
38  {
39  setItemIndexMethod(QGraphicsScene::ItemIndexMethod::NoIndex);
40  }
41 
43  {
44  return toplevelStateInstance;
45  }
46 
48  {
49  clear();
50  statechartmodel::StateInstancePtr instance(new statechartmodel::LocalState(toplevelState, ""));
51  this->toplevelStateInstance = instance;
52  StateItem* toplevelItem = new StateItem(instance);
53  addItem(toplevelItem);
54  topLevelStateItem = toplevelItem;
55  }
56 
57  void StateScene::saveSceneToSVG(QString path, int width)
58  {
59  QImage img(width, width * sceneRect().height() / sceneRect().width(), QImage::Format_ARGB32);
60  img.fill(Qt::transparent);
61 
62 
63  QPainter painter(&img);
64  painter.setRenderHint(QPainter::Antialiasing);
65  render(&painter);
66  img.save(path);
67  }
68 
70  {
71  std::function<void(StateItem*)> clearRecursive;
72  clearRecursive = [&](StateItem * item)
73  {
74  if (!item)
75  {
76  return;
77  }
78  for (auto substateItem : item->getSubstateItems())
79  {
80  clearRecursive(substateItem);
81  }
82  item->clearActiveSubstate();
83  };
84 
85  if (topLevelStateItem)
86  {
87  clearRecursive(topLevelStateItem);
88  }
89  }
90 
91  QMap<QString, StateInstanceData> StateScene::getStateInstanceData() const
92  {
93  QMap<QString, StateInstanceData> stateInstanceData;
94 
95  std::function<QMap<QString, StateInstanceData>(StateItem*)> getChildren;
96  getChildren = [&](StateItem * stateItem)
97  {
98  QMap<QString, StateInstanceData> stateInstanceData;
99  for (StateItem* substateItem : stateItem->getSubstateItems())
100  {
101  auto fullpath = substateItem->getFullStatePath();
103  data.active = substateItem->isActive();
104  data.fullStatePath = fullpath;
105  data.stateInstance = substateItem->getStateInstance();
106  stateInstanceData[fullpath] = data;
107  auto map = getChildren(substateItem);
108  for (auto& key : map.keys())
109  {
110  stateInstanceData[key] = map[key];
111  }
112  }
113  return stateInstanceData;
114  };
115 
116  if (topLevelStateItem)
117  {
118  stateInstanceData = getChildren(topLevelStateItem);
119  auto fullpath = topLevelStateItem->getFullStatePath();
121  data.active = topLevelStateItem->isActive();
122  data.fullStatePath = fullpath;
123  data.stateInstance = topLevelStateItem->getStateInstance();
124  stateInstanceData[fullpath] = data;
125  }
126 
127  return stateInstanceData;
128  }
129 
131  {
132  return topLevelStateItem;
133  }
134 
135 }
armarx::StateScene::saveSceneToSVG
void saveSceneToSVG(QString path="/tmp/statechart.png", int width=6000)
Definition: StateScene.cpp:57
armarx::StateScene::getStateInstanceData
QMap< QString, StateInstanceData > getStateInstanceData() const
Definition: StateScene.cpp:91
armarx::StateScene::clearActiveSubstates
void clearActiveSubstates()
Definition: StateScene.cpp:69
armarx::StateItem::getStateInstance
statechartmodel::StateInstancePtr getStateInstance() const
Definition: StateItem.h:70
StateItem.h
armarx::StateScene::getStateInstance
statechartmodel::StateInstancePtr getStateInstance() const
Definition: StateScene.cpp:42
armarx::statechartmodel::StateInstancePtr
std::shared_ptr< StateInstance > StateInstancePtr
Definition: StateInstance.h:138
armarx::StateItem::getFullStatePath
QString getFullStatePath() const
Definition: StateItem.cpp:664
armarx::StateScene::getTopLevelStateItem
StateItem * getTopLevelStateItem() const
Definition: StateScene.cpp:130
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::StateScene::StateScene
StateScene(QObject *parent=0)
Definition: StateScene.cpp:36
LocalState.h
StateScene.h
ArmarXWidgetController.h
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::LocalState
Definition: LocalState.h:31
armarx::transparent
QColor transparent()
Definition: StyleSheets.h:84
State.h
armarx::StateInstanceData
Definition: StateScene.h:46
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::StateItem::getSubstateItems
QVector< StateItem * > getSubstateItems() const
Definition: StateItem.cpp:682