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 <mutex>
29#include <vector>
30
31// qt
32#include <QMetaType>
33#include <QObject>
34
35// local
38#include "../scene3D/Scene.h"
40#include "ClassDefinitions.h"
41#include "Operation.h"
42#include "UndoRedoStack.h"
44
45namespace 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,
163 const std::string& collection);
164
165 /**
166 * Triggers signal controller:Controller::sceneObjectSelected with given parameter.
167 *
168 * @param object The object which was selected.
169 */
171
172 /**
173 * Triggers signal controller:Controller::objectsChanged with given parameter.
174 *
175 * @param objectIds The IDs of the changed objects.
176 */
178
179 /**
180 * Triggers signal controller:Controller::minimapClicked.
181 */
183
184 /**
185 * Deletes all objects in local scene and in WorkingMemory and deletes all groups.
186 * The operations are performed using undoable operations.
187 */
188 void clearScene();
189
190 /**
191 * Executes the Operations, which are queued because the object, which would be affected by this was busy.
192 *
193 * @param blocking If true, wait for ending of execution.
194 */
195 void executeQueuedOperations(bool blocking = true);
196
197 void saveSnapshotAsJSON(std::string const& snapshotName);
198
199 Q_SIGNALS:
200
201 /**
202 * A signal which gets triggered, after the scene is reloaded.
203 *
204 * @see controller::Controller::reloadLocalScene
205 */
207
208 /**
209 * A signal which gets triggered, after a vector of operations is executed.
210 *
211 * @param objectIds The IDs of the objects affected by the executed operations.
212 */
214
215 /**
216 * A signal which gets triggered, if objects are moved or rotated.
217 *
218 * @param objectIds The IDs of the changed objects.
219 */
221
222 /**
223 * A signal which gets triggered, after a class gets selected.
224 *
225 * @param objectClass The class of the selected class.
226 * @param collection The collection of the selected class.
227 */
228 void objectClassSelected(const std::string& objectClass, const std::string& collection);
229
230 /**
231 * A signal which gets triggered, after a object gets selected.
232 *
233 * @param object The object which was selected.
234 */
236
237 /**
238 * A signal which gets triggered, after a click was performed on the minimap.
239 */
241
242 private:
243 Controller();
244
245 UndoRedoStackPtr undoRedoStack;
246 std::shared_ptr<scene3D::Scene> scene;
247 std::shared_ptr<memoryxcontroller::MemoryXController> memoryXController;
248 std::shared_ptr<gui::ShortcutController> shortcutController;
249 mutable std::recursive_mutex execute_mutex;
250 mutable std::mutex queue_mutex;
251 scene3D::SceneObjectPtr lastSelected;
252 std::vector<std::pair<int, OperationPtr>> queuedOperations;
253
254 private slots:
255 void executeQtThread(int flags, const OperationPtrListPtr& operations);
256 };
257} // namespace controller
258
259Q_DECLARE_METATYPE(controller::vector_string)
void execute(int flags, const OperationPtrListPtr &operations, bool blocking=true)
Executes multiple operations.
void undo()
Goes one step back in the history and undoes the corresponding Operations.
void triggerSceneObjectSelected(scene3D::SceneObjectPtr object)
Triggers signal controller:Controller::sceneObjectSelected with given parameter.
const std::shared_ptr< memoryxcontroller::MemoryXController > getMemoryXController() const
Returns the MemoryXController.
void triggerMinimapClicked()
Triggers signal controller:Controller::minimapClicked.
void triggerObjectsChanged(controller::vector_string objectIds)
Triggers signal controller:Controller::objectsChanged with given parameter.
void saveSnapshotAsJSON(std::string const &snapshotName)
void objectsChanged(controller::vector_string objectIds)
A signal which gets triggered, if objects are moved or rotated.
static const int UNDOABLE
A flag to save the executed operations to the history.
Definition Controller.h:80
void clearScene()
Deletes all objects in local scene and in WorkingMemory and deletes all groups.
void triggerObjectClassSelected(const std::string &objectClass, const std::string &collection)
Triggers signal controller:Controller::objectClassSelected with given parameter.
void reloadScene()
A signal which gets triggered, after the scene is reloaded.
const std::shared_ptr< gui::ShortcutController > getShortcutController() const
Returns the ShortcutController.
static const int EXECUTE_ON_WM
A Flag to execute operations on the WorkingMemory.
Definition Controller.h:66
const std::shared_ptr< scene3D::Scene > getScene() const
Returns the Scene.
void executeQueuedOperations(bool blocking=true)
Executes the Operations, which are queued because the object, which would be affected by this was bus...
void reloadLocalScene()
Deletes all objects in the local Scene and loads the current objects from the WorkingMemory in the Sc...
void redo()
Goes one step forward in the history and redoes the corresponding Operations.
void operationExecuted(controller::vector_string objectIds)
A signal which gets triggered, after a vector of operations is executed.
static const int EXECUTE_ON_SCENE
A flag to execute operations on the Scene.
Definition Controller.h:73
static ControllerPtr create()
Creates a new instance of the Controller, initializes the Scene and the MemoryXController and returns...
void objectClassSelected(const std::string &objectClass, const std::string &collection)
A signal which gets triggered, after a class gets selected.
void minimapClicked()
A signal which gets triggered, after a click was performed on the minimap.
void sceneObjectSelected(scene3D::SceneObjectPtr object)
A signal which gets triggered, after a object gets selected.
~Controller() override
A destructor.
std::vector< std::string > vector_string
Definition Controller.h:47
std::shared_ptr< Controller > ControllerPtr
std::shared_ptr< std::vector< OperationPtr > > OperationPtrListPtr
std::shared_ptr< UndoRedoStack > UndoRedoStackPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr