LayoutController.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include "../model/State.h"
28 
29 #include <QList>
30 #include <QObject>
31 #include <QTimer>
32 
33 #include <map>
34 #include <memory>
35 #include <utility>
36 #include <string>
37 
38 namespace armarx
39 {
40 
41  typedef std::shared_ptr<StateModelLayoutMediator>MediatorPtr;
42  using MediatorMap = std::map<size_t, MediatorPtr>;
43  using MediatorLayoutOptionPair = std::pair<size_t, bool>;
44 
45  class LayoutController : public QObject
46  {
47  Q_OBJECT
48 
49  public:
50  LayoutController(armarx::statechartmodel::StatePtr topState, bool startEnabled = true);
51 
52  /**
53  * @brief setTopState Resets all workers i.e. deletes all old workers and sets topState and its
54  * worker as only member of workers.
55  * @param topState The new top state.
56  */
58 
59  /**
60  * @brief createAllWorkers Creates workers for all mediators
61  */
62  void createAllWorkers();
63 
65 
66  signals:
67  /**
68  * @brief createWorker Tells the LayoutWorkerCreator to create a new worker for this mediator.
69  * @param mediator Manages the communication between the new worker and the associated state.
70  * @param id The mediator's and worker's id.
71  * @param name Name of mediator's state.
72  */
73  void createWorker(MediatorPtr mediator, size_t id, QString name);
74 
75  /**
76  * @brief reset Notifies the LayoutWorkerCreator that the topState has been reset.
77  */
78  void reset();
79 
80  public slots:
81  void enableLayouting(bool enable = true);
82 
83  /**
84  * @brief scheduleMediator Put the mediator in the layoutQueue.
85  * @param mediatorId ID of the StateModelLayoutMediator that should be scheduled.
86  * @param layoutAll Indicates whether nodes and edges (true) or only edges shall be layouted (false).
87  */
88  void scheduleMediator(size_t mediatorId, bool layoutAll);
89  bool layoutNow(size_t mediatorId, bool layoutAll);
90 
91 
92  /**
93  * @brief startNextLayouting The last worker that layouted is finished and the next should be
94  * started.
95  */
96  void startNextLayouting();
97 
98  /**
99  * @brief deleteMediator Call the destructor of mediator and tell LayoutWorkerCreator to delete
100  * the corresponding worker.
101  * @param mediatorId The id of the mediator that needs to be deleted.
102  */
103  void deleteMediator(size_t mediatorId);
104 
105  /**
106  * @brief potentialStateAdded If a substate is added it might mean that there is a new state
107  * needing a LayoutWorker. Creates a new LayoutWorker if necessary.
108  * @param substate The substate that was changed.
109  * @param signalType Type of change to the substate.
110  */
112 
113  private:
114  /**
115  * @brief layoutQueue Queue of all workers that need to layout their graph.
116  */
117  QList<MediatorLayoutOptionPair> layoutQueue;
118 
119  /**
120  * @brief workers List of all LayoutWorkers.
121  */
122  MediatorMap mediators;
123  /**
124  * @brief idCounter Value is the next unused ID.
125  */
126  size_t idCounter;
127 
128  /**
129  * @brief states List of all states that already have a LayoutWorker.
130  */
131  QList<statechartmodel::StatePtr> states;
132 
133  /**
134  * @brief timer Timeouts are used when the layoutQueue is empty in order to start layouting when
135  * a mediator is scheduled. Prevents starvation.
136  */
137  QTimer timer;
138 
139 
140  bool layoutingDisabled;
141  /**
142  * @brief createMediator Create mediator and connect it to state via signals and slots.
143  * @param state The state for which a StateModelLayoutMediator is to be created.
144  */
145  void createMediator(statechartmodel::StatePtr state);
146  };
147 } //namespace armarx
armarx::MediatorLayoutOptionPair
std::pair< size_t, bool > MediatorLayoutOptionPair
Definition: LayoutController.h:43
armarx::LayoutController::potentialStateAdded
void potentialStateAdded(statechartmodel::StateInstancePtr substate, statechartmodel::SignalType signalType)
potentialStateAdded If a substate is added it might mean that there is a new state needing a LayoutWo...
Definition: LayoutController.cpp:202
armarx::LayoutController::LayoutController
LayoutController(armarx::statechartmodel::StatePtr topState, bool startEnabled=true)
Definition: LayoutController.cpp:34
armarx::LayoutController::createWorker
void createWorker(MediatorPtr mediator, size_t id, QString name)
createWorker Tells the LayoutWorkerCreator to create a new worker for this mediator.
armarx::MediatorMap
std::map< size_t, MediatorPtr > MediatorMap
Definition: LayoutController.h:42
armarx::LayoutController
Definition: LayoutController.h:45
armarx::LayoutController::setTopState
void setTopState(statechartmodel::StatePtr topState)
setTopState Resets all workers i.e.
Definition: LayoutController.cpp:53
StateModelLayoutMediator.h
armarx::statechartmodel::StateInstancePtr
std::shared_ptr< StateInstance > StateInstancePtr
Definition: StateInstance.h:138
armarx::LayoutController::startNextLayouting
void startNextLayouting()
startNextLayouting The last worker that layouted is finished and the next should be started.
Definition: LayoutController.cpp:137
armarx::LayoutController::reset
void reset()
reset Notifies the LayoutWorkerCreator that the topState has been reset.
armarx::LayoutController::createAllWorkers
void createAllWorkers()
createAllWorkers Creates workers for all mediators
Definition: LayoutController.cpp:68
armarx::LayoutController::scheduleMediator
void scheduleMediator(size_t mediatorId, bool layoutAll)
scheduleMediator Put the mediator in the layoutQueue.
Definition: LayoutController.cpp:94
armarx::LayoutController::getStateId
size_t getStateId(armarx::statechartmodel::StatePtr state) const
Definition: LayoutController.cpp:76
armarx::LayoutController::layoutNow
bool layoutNow(size_t mediatorId, bool layoutAll)
Definition: LayoutController.cpp:116
armarx::MediatorPtr
std::shared_ptr< StateModelLayoutMediator > MediatorPtr
Definition: LayoutController.h:41
armarx::statechartmodel::SignalType
SignalType
The SignalType enum.
Definition: SignalType.h:33
armarx::statechartmodel::StatePtr
std::shared_ptr< State > StatePtr
Definition: State.h:46
armarx::LayoutController::deleteMediator
void deleteMediator(size_t mediatorId)
deleteMediator Call the destructor of mediator and tell LayoutWorkerCreator to delete the correspondi...
Definition: LayoutController.cpp:164
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::LayoutController::enableLayouting
void enableLayouting(bool enable=true)
Definition: LayoutController.cpp:89