28 #include <QtDesigner/abstractpropertyeditor.h>
30 #include <QMetaObject>
33 #include <QApplication>
35 #include <SimoxUtility/json.h>
47 controller::Controller::Controller() :
51 qRegisterMetaType<OperationPtrListPtr>(
"OperationPtrListPtr");
72 QMetaObject::invokeMethod(
this,
"executeQtThread", blocking ? (QApplication::instance()->thread() == QThread::currentThread() || QApplication::instance()->thread() == NULL ? Qt::DirectConnection : Qt::BlockingQueuedConnection) : Qt::QueuedConnection, Q_ARG(
int, flags), Q_ARG(
OperationPtrListPtr, operations));
75 void controller::Controller::executeQtThread(
int flags,
const OperationPtrListPtr& operations)
77 std::unique_lock lock(execute_mutex);
80 std::vector<controller::OperationPtr>::size_type i = 0;
82 for (; i != operations->size(); i++)
84 if (!operations->at(i)->isExecuteable())
86 std::unique_lock lock(queue_mutex);
87 queuedOperations.push_back(std::make_pair(flags, operations->at(i)));
93 std::string tmp = operations->at(i)->getObjectId();
97 op->executeOnWorkingMemory();
99 catch (std::exception& e)
101 ARMARX_ERROR_S <<
"executeOnWorkingMemory failed: " << op->getObjectId() << e.what();
111 if (tmp != operations->at(i)->getObjectId())
113 if (getScene()->getObjectManager()->getObjectById(operations->at(i)->getObjectId()))
116 throw std::runtime_error(
"New id is already used in local scene.");
119 std::string
newId = operations->at(i)->getObjectId();
120 undoRedoStack->updateObjectId(tmp,
newId);
123 if (operations->at(i)->getObjectId() ==
newId)
125 for (
auto ito = operations->begin(); ito != operations->end(); ito++)
127 if (ito->get()->getObjectId() == tmp)
129 ito->get()->setObjectId(
newId);
131 else if (ito->get()->getObjectId() ==
newId)
133 ito->get()->setObjectId(tmp);
140 operations->at(i)->setObjectId(
newId);
146 std::string tmp = operations->at(i)->getObjectId();
150 op->executeOnScene();
152 catch (std::exception& e)
154 ARMARX_ERROR_S <<
"executeOnScene failed: " << op->getObjectId() << e.what();
164 if (tmp != operations->at(i)->getObjectId())
166 throw std::runtime_error(
"ID changed while execution on local Scene.");
170 objectIds.push_back(operations->at(i)->getObjectId());
175 std::cerr <<
"Could not execute operations. Trying to revert." << std::endl;
190 inverse->executeOnScene();
194 std::cerr <<
"Could not revert operation on the local scene. Trying to revert the other operations." << std::endl;
202 inverse->executeOnWorkingMemory();
206 std::cerr <<
"Could not revert operation on WorkingMemory. Trying to revert the other operations." << std::endl;
214 undoRedoStack->push(operations);
217 std::vector<scene3D::SceneObjectPtr> allSelected = scene->getSelectionManager()->getAllSelected();
220 if (newSelected != lastSelected)
222 lastSelected = newSelected;
223 triggerSceneObjectSelected(newSelected);
226 qRegisterMetaType<controller::vector_string>(
"controller::vector_string");
227 emit operationExecuted(objectIds);
233 std::unique_lock lock(queue_mutex);
235 for (
auto it = queuedOperations.begin(); it != queuedOperations.end(); ++it)
238 queuedOperationsTmp->push_back((*it).second);
239 this->
execute((*it).first, queuedOperationsTmp, blocking);
242 queuedOperations.clear();
248 if (!memoryXController->getWorkingMemoryController())
250 throw std::runtime_error(
"Not initialized");
254 std::vector<scene3D::SceneObjectPtr> currentLocalObjects = scene->getObjectManager()->getAllObjects();
258 scene->getObjectManager()->removeObject(
object);
262 undoRedoStack->clear();
264 memoryXController->getWorkingMemoryController()->addAllInstancesToLocalScene();
276 return memoryXController;
281 return shortcutController;
297 emit objectClassSelected(objectClass, collection);
302 emit sceneObjectSelected(
object);
307 emit minimapClicked();
312 emit objectsChanged(objectIds);
317 std::shared_ptr<std::vector<controller::OperationPtr> > operations(
new std::vector<controller::OperationPtr>());
318 std::vector<scene3D::SceneObjectPtr> selectedObjects = getScene()->getSelectionManager()->getAllSelected();
320 for (
auto it = selectedObjects.rbegin(); it != selectedObjects.rend(); ++it)
323 operations->push_back(operation);
326 std::vector<scene3D::SceneObjectPtr> allObjects = getScene()->getObjectManager()->getAllObjects();
327 std::vector<scene3D::SceneGroupPtr> groups = getScene()->getGroupManager()->getAllGroups();
329 for (
auto itG = groups.begin(); itG != groups.end(); ++itG)
331 std::vector<scene3D::SceneObjectPtr> groupObjects = (*itG)->getAllObjects();
333 for (
auto it = groupObjects.rbegin(); it != groupObjects.rend(); ++it)
336 operations->push_back(removeFromGroupOperation);
340 operations->push_back(deleteGroupOperation);
343 for (
auto it = allObjects.rbegin(); it != allObjects.rend(); ++it)
346 operations->push_back(operation);
354 std::shared_ptr<scene3D::Scene> scene = getScene();
356 std::vector<scene3D::SceneObjectPtr> objects = objectManager->getAllObjects();
363 SbVec3f translation =
object->getTranslation();
364 objData.
position.x() = translation[0];
365 objData.
position.y() = translation[1];
366 objData.
position.z() = translation[2];
369 SbRotation rotation =
object->getRotation();
377 objData.
className =
object->getClassId();
379 nlohmann::json j =
data;
382 const std::string
filename = snapshotName +
".json";
385 nlohmann::write_json(
filename, j, 2);
388 catch (
const std::ios_base::failure& e)
391 << std::filesystem::absolute(
filename) <<
":\n" << e.what();