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
35namespace 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
QVector< StateItem * > getSubstateItems() const
void saveSceneToSVG(QString path="/tmp/statechart.png", int width=6000)
QMap< QString, StateInstanceData > getStateInstanceData() const
statechartmodel::StateInstancePtr getStateInstance() const
StateScene(QObject *parent=0)
StateItem * getTopLevelStateItem() const
void setToplevelState(statechartmodel::StatePtr toplevelStateInstance)
void clearActiveSubstates()
std::shared_ptr< State > StatePtr
Definition State.h:48
std::shared_ptr< StateInstance > StateInstancePtr
This file offers overloads of toIce() and fromIce() functions for STL container types.