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