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 <map>
27 #include <memory>
28 #include <string>
29 #include <utility>
30 
31 #include <QList>
32 #include <QObject>
33 #include <QTimer>
34 
35 #include "../model/State.h"
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  statechartmodel::SignalType signalType);
113 
114  private:
115  /**
116  * @brief layoutQueue Queue of all workers that need to layout their graph.
117  */
118  QList<MediatorLayoutOptionPair> layoutQueue;
119 
120  /**
121  * @brief workers List of all LayoutWorkers.
122  */
123  MediatorMap mediators;
124  /**
125  * @brief idCounter Value is the next unused ID.
126  */
127  size_t idCounter;
128 
129  /**
130  * @brief states List of all states that already have a LayoutWorker.
131  */
132  QList<statechartmodel::StatePtr> states;
133 
134  /**
135  * @brief timer Timeouts are used when the layoutQueue is empty in order to start layouting when
136  * a mediator is scheduled. Prevents starvation.
137  */
138  QTimer timer;
139 
140 
141  bool layoutingDisabled;
142  /**
143  * @brief createMediator Create mediator and connect it to state via signals and slots.
144  * @param state The state for which a StateModelLayoutMediator is to be created.
145  */
146  void createMediator(statechartmodel::StatePtr state);
147  };
148 } //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:210
armarx::LayoutController::LayoutController
LayoutController(armarx::statechartmodel::StatePtr topState, bool startEnabled=true)
Definition: LayoutController.cpp:35
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:54
StateModelLayoutMediator.h
armarx::statechartmodel::StateInstancePtr
std::shared_ptr< StateInstance > StateInstancePtr
Definition: StateInstance.h:146
armarx::LayoutController::startNextLayouting
void startNextLayouting()
startNextLayouting The last worker that layouted is finished and the next should be started.
Definition: LayoutController.cpp:144
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:70
armarx::LayoutController::scheduleMediator
void scheduleMediator(size_t mediatorId, bool layoutAll)
scheduleMediator Put the mediator in the layoutQueue.
Definition: LayoutController.cpp:99
armarx::LayoutController::getStateId
size_t getStateId(armarx::statechartmodel::StatePtr state) const
Definition: LayoutController.cpp:79
armarx::LayoutController::layoutNow
bool layoutNow(size_t mediatorId, bool layoutAll)
Definition: LayoutController.cpp:122
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:48
armarx::LayoutController::deleteMediator
void deleteMediator(size_t mediatorId)
deleteMediator Call the destructor of mediator and tell LayoutWorkerCreator to delete the correspondi...
Definition: LayoutController.cpp:171
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::LayoutController::enableLayouting
void enableLayouting(bool enable=true)
Definition: LayoutController.cpp:93