Controller.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-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 MemoryX::gui-plugins::SceneEditor
19  * @date 2015
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #pragma once
25 
26 // std
27 #include <exception>
28 #include <vector>
29 #include <mutex>
30 
31 // qt
32 #include <QObject>
33 #include <QMetaType>
34 
35 // local
36 #include "ClassDefinitions.h"
37 #include "../scene3D/Scene.h"
38 #include "../scene3D/SceneObject.h"
39 #include "UndoRedoStack.h"
40 #include "../memoryxcontroller/MemoryXController.h"
41 #include "Operation.h"
42 #include "../gui/ShortcutController.h"
44 
45 namespace controller
46 {
47  using vector_string = std::vector<std::string>;
48 
49  /**
50  * A class to execute Operations, maintain the execution history and initialize Scene and MemoryXController.
51  *
52  * @see memoryxcontroller::MemoryXController
53  * @see scene3D::scene
54  */
55  class Controller : public QObject
56  {
57  Q_OBJECT
58 
59  public:
60  /**
61  * A Flag to execute operations on the WorkingMemory.
62  *
63  * @see memoryxcontroller::WorkingMemoryController
64  * @see controller::Controller::execute()
65  */
66  static const int EXECUTE_ON_WM = 1 << 0;
67  /**
68  * A flag to execute operations on the Scene.
69  *
70  * @see scene3D::Scene
71  * @see controller::Controller::execute()
72  */
73  static const int EXECUTE_ON_SCENE = 1 << 1;
74  /**
75  * A flag to save the executed operations to the history
76  *
77  * @see controller::UndoRedoStack
78  * @see controller::Controller::execute()
79  */
80  static const int UNDOABLE = 1 << 2;
81 
82  /**
83  * Creates a new instance of the Controller, initializes the Scene and the MemoryXController and returns the new Controller.
84  *
85  * @return the newly created Controller
86  * @see scene3D::Scene
87  * @see memoryxcontroller::MemoryXController
88  */
89  static ControllerPtr create();
90 
91  /**
92  * A destructor.
93  * Deletes all initialized references.
94  */
95  ~Controller() override;
96 
97  /**
98  * Executes multiple operations.
99  * The Operations will be run depending on the flags:
100  * flags & EXECUTE_ON_WM == 1 => The Operations will be run at the WorkingMemory.
101  * flags & EXECUTE_ON_SCENE == 1 => The Operations will be run at the Scene.
102  * flags & UNDOABLE == 1 => The Operations will be saved to the history.
103  *
104  * @param flags The flags for the execution.
105  * @param operations The operations to execute.
106  * @param blocking If true, wait for ending of execution.
107  *
108  * @see controller::Controller::EXECUTE_ON_WM
109  * @see controller::Controller::EXECUTE_ON_SCENE
110  * @see controller::Controller::UNDOABLE
111  * @see scene3D::Scene
112  * @see memoryxcontroller::WorkingMemoryController
113  */
114  void execute(int flags, const OperationPtrListPtr& operations, bool blocking = true);
115 
116  /**
117  * Deletes all objects in the local Scene and loads the current objects from the WorkingMemory in the Scene.
118  */
119  void reloadLocalScene();
120 
121  /**
122  * Returns the Scene.
123  *
124  * @return the Scene
125  */
126  const std::shared_ptr<scene3D::Scene> getScene() const;
127 
128  /**
129  * Returns the MemoryXController.
130  *
131  * @return the MemoryXController
132  */
133  const std::shared_ptr<memoryxcontroller::MemoryXController> getMemoryXController() const;
134 
135  /**
136  * Returns the ShortcutController.
137  *
138  * @return the ShortcutController
139  */
140  const std::shared_ptr<gui::ShortcutController> getShortcutController() const;
141 
142  /**
143  * Goes one step back in the history and undoes the corresponding Operations.
144  *
145  * @see controller::UndoRedoStack::undo
146  */
147  void undo();
148 
149  /**
150  * Goes one step forward in the history and redoes the corresponding Operations.
151  *
152  * @see controller::UndoRedoStack::redo
153  */
154  void redo();
155 
156  /**
157  * Triggers signal controller:Controller::objectClassSelected with given parameter.
158  *
159  * @param objectClass The class of the selected class.
160  * @param collection The collection of the selected class.
161  */
162  void triggerObjectClassSelected(const std::string& objectClass, const std::string& collection);
163 
164  /**
165  * Triggers signal controller:Controller::sceneObjectSelected with given parameter.
166  *
167  * @param object The object which was selected.
168  */
170 
171  /**
172  * Triggers signal controller:Controller::objectsChanged with given parameter.
173  *
174  * @param objectIds The IDs of the changed objects.
175  */
177 
178  /**
179  * Triggers signal controller:Controller::minimapClicked.
180  */
181  void triggerMinimapClicked();
182 
183  /**
184  * Deletes all objects in local scene and in WorkingMemory and deletes all groups.
185  * The operations are performed using undoable operations.
186  */
187  void clearScene();
188 
189  /**
190  * Executes the Operations, which are queued because the object, which would be affected by this was busy.
191  *
192  * @param blocking If true, wait for ending of execution.
193  */
194  void executeQueuedOperations(bool blocking = true);
195 
196  void saveSnapshotAsJSON(std::string const& snapshotName);
197 
198  Q_SIGNALS:
199 
200  /**
201  * A signal which gets triggered, after the scene is reloaded.
202  *
203  * @see controller::Controller::reloadLocalScene
204  */
205  void reloadScene();
206 
207  /**
208  * A signal which gets triggered, after a vector of operations is executed.
209  *
210  * @param objectIds The IDs of the objects affected by the executed operations.
211  */
213 
214  /**
215  * A signal which gets triggered, if objects are moved or rotated.
216  *
217  * @param objectIds The IDs of the changed objects.
218  */
220 
221  /**
222  * A signal which gets triggered, after a class gets selected.
223  *
224  * @param objectClass The class of the selected class.
225  * @param collection The collection of the selected class.
226  */
227  void objectClassSelected(const std::string& objectClass, const std::string& collection);
228 
229  /**
230  * A signal which gets triggered, after a object gets selected.
231  *
232  * @param object The object which was selected.
233  */
235 
236  /**
237  * A signal which gets triggered, after a click was performed on the minimap.
238  */
239  void minimapClicked();
240 
241  private:
242  Controller();
243 
244  UndoRedoStackPtr undoRedoStack;
245  std::shared_ptr<scene3D::Scene> scene;
246  std::shared_ptr<memoryxcontroller::MemoryXController> memoryXController;
247  std::shared_ptr<gui::ShortcutController> shortcutController;
248  mutable std::recursive_mutex execute_mutex;
249  mutable std::mutex queue_mutex;
250  scene3D::SceneObjectPtr lastSelected;
251  std::vector<std::pair<int, OperationPtr>> queuedOperations;
252 
253  private slots:
254  void executeQtThread(int flags, const OperationPtrListPtr& operations);
255  };
256 }
257 
258 Q_DECLARE_METATYPE(controller::vector_string)
259 
controller::Controller::redo
void redo()
Goes one step forward in the history and redoes the corresponding Operations.
Definition: Controller.cpp:289
controller::Controller::triggerMinimapClicked
void triggerMinimapClicked()
Triggers signal controller:Controller::minimapClicked.
Definition: Controller.cpp:305
controller::Controller::triggerObjectsChanged
void triggerObjectsChanged(controller::vector_string objectIds)
Triggers signal controller:Controller::objectsChanged with given parameter.
Definition: Controller.cpp:310
controller::Controller::getShortcutController
const std::shared_ptr< gui::ShortcutController > getShortcutController() const
Returns the ShortcutController.
Definition: Controller.cpp:279
controller::Controller::create
static ControllerPtr create()
Creates a new instance of the Controller, initializes the Scene and the MemoryXController and returns...
Definition: Controller.cpp:54
controller::Controller::triggerSceneObjectSelected
void triggerSceneObjectSelected(scene3D::SceneObjectPtr object)
Triggers signal controller:Controller::sceneObjectSelected with given parameter.
Definition: Controller.cpp:300
controller::OperationPtrListPtr
std::shared_ptr< std::vector< OperationPtr > > OperationPtrListPtr
Definition: ClassDefinitions.h:55
UndoRedoStack.h
controller::Controller::EXECUTE_ON_SCENE
static const int EXECUTE_ON_SCENE
A flag to execute operations on the Scene.
Definition: Controller.h:73
MongoDBRef.h
controller::Controller::minimapClicked
void minimapClicked()
A signal which gets triggered, after a click was performed on the minimap.
controller::Controller::saveSnapshotAsJSON
void saveSnapshotAsJSON(std::string const &snapshotName)
Definition: Controller.cpp:352
Operation.h
ClassDefinitions.h
controller::Controller::triggerObjectClassSelected
void triggerObjectClassSelected(const std::string &objectClass, const std::string &collection)
Triggers signal controller:Controller::objectClassSelected with given parameter.
Definition: Controller.cpp:295
controller::Controller::getScene
const std::shared_ptr< scene3D::Scene > getScene() const
Returns the Scene.
Definition: Controller.cpp:269
controller::Controller::reloadScene
void reloadScene()
A signal which gets triggered, after the scene is reloaded.
controller::Controller::~Controller
~Controller() override
A destructor.
Definition: Controller.cpp:66
controller::Controller::getMemoryXController
const std::shared_ptr< memoryxcontroller::MemoryXController > getMemoryXController() const
Returns the MemoryXController.
Definition: Controller.cpp:274
controller::Controller::clearScene
void clearScene()
Deletes all objects in local scene and in WorkingMemory and deletes all groups.
Definition: Controller.cpp:315
controller::vector_string
std::vector< std::string > vector_string
Definition: Controller.h:47
controller::Controller::UNDOABLE
static const int UNDOABLE
A flag to save the executed operations to the history.
Definition: Controller.h:80
controller
Definition: AddOperation.h:39
controller::ControllerPtr
std::shared_ptr< Controller > ControllerPtr
Definition: ClassDefinitions.h:41
controller::Controller
A class to execute Operations, maintain the execution history and initialize Scene and MemoryXControl...
Definition: Controller.h:55
controller::Controller::objectClassSelected
void objectClassSelected(const std::string &objectClass, const std::string &collection)
A signal which gets triggered, after a class gets selected.
controller::Controller::reloadLocalScene
void reloadLocalScene()
Deletes all objects in the local Scene and loads the current objects from the WorkingMemory in the Sc...
Definition: Controller.cpp:246
controller::Controller::objectsChanged
void objectsChanged(controller::vector_string objectIds)
A signal which gets triggered, if objects are moved or rotated.
controller::Controller::executeQueuedOperations
void executeQueuedOperations(bool blocking=true)
Executes the Operations, which are queued because the object, which would be affected by this was bus...
Definition: Controller.cpp:230
controller::UndoRedoStackPtr
std::shared_ptr< UndoRedoStack > UndoRedoStackPtr
Definition: ClassDefinitions.h:73
controller::Controller::sceneObjectSelected
void sceneObjectSelected(scene3D::SceneObjectPtr object)
A signal which gets triggered, after a object gets selected.
controller::Controller::undo
void undo()
Goes one step back in the history and undoes the corresponding Operations.
Definition: Controller.cpp:284
scene3D::SceneObjectPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Definition: PointerDefinitions.h:40
controller::Controller::EXECUTE_ON_WM
static const int EXECUTE_ON_WM
A Flag to execute operations on the WorkingMemory.
Definition: Controller.h:66
controller::Controller::operationExecuted
void operationExecuted(controller::vector_string objectIds)
A signal which gets triggered, after a vector of operations is executed.
controller::Controller::execute
void execute(int flags, const OperationPtrListPtr &operations, bool blocking=true)
Executes multiple operations.
Definition: Controller.cpp:70