30#include <QApplication>
34#include <QtDesigner/abstractpropertyeditor.h>
36#include <SimoxUtility/json.h>
46controller::Controller::Controller() : QObject(), lastSelected(NULL)
48 qRegisterMetaType<OperationPtrListPtr>(
"OperationPtrListPtr");
71 QMetaObject::invokeMethod(
74 blocking ? (QApplication::instance()->thread() == QThread::currentThread() ||
75 QApplication::instance()->thread() == NULL
76 ? Qt::DirectConnection
77 : Qt::BlockingQueuedConnection)
78 : Qt::QueuedConnection,
86 std::unique_lock lock(execute_mutex);
89 std::vector<controller::OperationPtr>::size_type i = 0;
91 for (; i != operations->size(); i++)
93 if (!operations->at(i)->isExecuteable())
95 std::unique_lock lock(queue_mutex);
96 queuedOperations.push_back(std::make_pair(flags, operations->at(i)));
102 std::string tmp = operations->at(i)->getObjectId();
106 op->executeOnWorkingMemory();
108 catch (std::exception& e)
110 ARMARX_ERROR_S <<
"executeOnWorkingMemory failed: " << op->getObjectId()
121 if (tmp != operations->at(i)->getObjectId())
123 if (getScene()->getObjectManager()->getObjectById(operations->at(i)->getObjectId()))
126 throw std::runtime_error(
"New id is already used in local scene.");
129 std::string
newId = operations->at(i)->getObjectId();
130 undoRedoStack->updateObjectId(tmp,
newId);
133 if (operations->at(i)->getObjectId() ==
newId)
135 for (
auto ito = operations->begin(); ito != operations->end(); ito++)
137 if (ito->get()->getObjectId() == tmp)
139 ito->get()->setObjectId(
newId);
141 else if (ito->get()->getObjectId() ==
newId)
143 ito->get()->setObjectId(tmp);
150 operations->at(i)->setObjectId(
newId);
156 std::string tmp = operations->at(i)->getObjectId();
160 op->executeOnScene();
162 catch (std::exception& e)
164 ARMARX_ERROR_S <<
"executeOnScene failed: " << op->getObjectId() << e.what();
174 if (tmp != operations->at(i)->getObjectId())
176 throw std::runtime_error(
"ID changed while execution on local Scene.");
180 objectIds.push_back(operations->at(i)->getObjectId());
185 std::cerr <<
"Could not execute operations. Trying to revert." << std::endl;
200 inverse->executeOnScene();
204 std::cerr <<
"Could not revert operation on the local scene. Trying to "
205 "revert the other operations."
214 inverse->executeOnWorkingMemory();
218 std::cerr <<
"Could not revert operation on WorkingMemory. Trying to "
219 "revert the other operations."
228 undoRedoStack->push(operations);
231 std::vector<scene3D::SceneObjectPtr> allSelected =
232 scene->getSelectionManager()->getAllSelected();
234 allSelected.size() == 0 ? NULL : allSelected[allSelected.size() - 1];
236 if (newSelected != lastSelected)
238 lastSelected = newSelected;
239 triggerSceneObjectSelected(newSelected);
242 qRegisterMetaType<controller::vector_string>(
"controller::vector_string");
243 emit operationExecuted(objectIds);
250 std::unique_lock lock(queue_mutex);
252 for (
auto it = queuedOperations.begin(); it != queuedOperations.end(); ++it)
255 new std::vector<controller::OperationPtr>);
256 queuedOperationsTmp->push_back((*it).second);
257 this->
execute((*it).first, queuedOperationsTmp, blocking);
260 queuedOperations.clear();
267 if (!memoryXController->getWorkingMemoryController())
269 throw std::runtime_error(
"Not initialized");
273 std::vector<scene3D::SceneObjectPtr> currentLocalObjects =
274 scene->getObjectManager()->getAllObjects();
278 scene->getObjectManager()->removeObject(
object);
282 undoRedoStack->clear();
284 memoryXController->getWorkingMemoryController()->addAllInstancesToLocalScene();
288const std::shared_ptr<scene3D::Scene>
294const std::shared_ptr<memoryxcontroller::MemoryXController>
297 return memoryXController;
300const std::shared_ptr<gui::ShortcutController>
303 return shortcutController;
310 undoRedoStack->undo());
317 undoRedoStack->redo());
322 const std::string& collection)
348 std::shared_ptr<std::vector<controller::OperationPtr>> operations(
349 new std::vector<controller::OperationPtr>());
350 std::vector<scene3D::SceneObjectPtr> selectedObjects =
351 getScene()->getSelectionManager()->getAllSelected();
353 for (
auto it = selectedObjects.rbegin(); it != selectedObjects.rend(); ++it)
357 operations->push_back(operation);
360 std::vector<scene3D::SceneObjectPtr> allObjects =
361 getScene()->getObjectManager()->getAllObjects();
362 std::vector<scene3D::SceneGroupPtr> groups =
getScene()->getGroupManager()->getAllGroups();
364 for (
auto itG = groups.begin(); itG != groups.end(); ++itG)
366 std::vector<scene3D::SceneObjectPtr> groupObjects = (*itG)->getAllObjects();
368 for (
auto it = groupObjects.rbegin(); it != groupObjects.rend(); ++it)
373 (*itG)->getGroupId(),
374 (*it)->getObjectId()));
375 operations->push_back(removeFromGroupOperation);
380 operations->push_back(deleteGroupOperation);
383 for (
auto it = allObjects.rbegin(); it != allObjects.rend(); ++it)
387 operations->push_back(operation);
398 std::shared_ptr<scene3D::Scene> scene =
getScene();
400 std::vector<scene3D::SceneObjectPtr> objects = objectManager->getAllObjects();
407 SbVec3f translation =
object->getTranslation();
408 objData.
position.x() = translation[0];
409 objData.
position.y() = translation[1];
410 objData.
position.z() = translation[2];
413 SbRotation rotation =
object->getRotation();
421 objData.
className =
object->getClassId();
423 nlohmann::json j =
data;
426 const std::string filename = snapshotName +
".json";
429 nlohmann::write_json(filename, j, 2);
430 ARMARX_INFO <<
"Saved snapshot as JSON at \n" << std::filesystem::absolute(filename);
432 catch (
const std::ios_base::failure& e)
435 << std::filesystem::absolute(filename) <<
":\n"
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.
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.
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.
static const int EXECUTE_ON_SCENE
A flag to execute operations on the Scene.
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.
A operation to delete a group.
A operation to deselect a object.
A operation to removes a object from a existing group.
A Operation to remove a object in the scene and the WorkingMemory.
A Stack to save a history of Actions.
The class ShortcutController manages all shortcuts that can be set in the widget.
static ScenePtr create(controller::ControllerPtr controller)
Creates a new instance of the Scene.
#define ARMARX_INFO
The normal logging level.
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
std::vector< std::string > vector_string
std::shared_ptr< Controller > ControllerPtr
std::shared_ptr< std::vector< OperationPtr > > OperationPtrListPtr
std::shared_ptr< Operation > OperationPtr
std::shared_ptr< SceneObjectManager > SceneObjectManagerPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Eigen::Quaternionf orientation