27 #include <MemoryX/gui-plugins/PriorMemoryEditor/ui_PriorEditorConfigDialog.h>
30 #include <MemoryX/gui-plugins/PriorMemoryEditor/ui_CollectionCreateDialog.h>
34 #include <ArmarXCore/interface/core/Log.h>
43 #include <QPushButton>
44 #include <QStringList>
48 #include <QMessageBox>
49 #include <QStandardItemModel>
52 #include <Inventor/SoDB.h>
53 #include <Inventor/Qt/SoQt.h>
54 #include <Inventor/nodes/SoUnits.h>
68 #include <VirtualRobot/XML/RobotIO.h>
69 #include <VirtualRobot/XML/ObjectIO.h>
70 #include <VirtualRobot/ManipulationObject.h>
71 #include <VirtualRobot/Grasping/GraspSet.h>
72 #include <VirtualRobot/Grasping/Grasp.h>
73 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
86 namespace fs = std::filesystem;
88 #define DEFAULT_SETTINGS_PLUGIN_NAME "PriorMemoryEditorPlugin"
89 #define DEFAULT_SETTINGS_PRIORMEMORY_NAME "PriorKnowledge"
103 addWidget<PriorEditorController>();
117 collCreateDialog->setCollectionPrefix(memoryx::PRIOR_COLLECTION_PREFIX);
126 visu =
new SoSeparator();
131 ARMARX_INFO <<
"PriorMemory: " << settings_priorMemory;
135 if (!settings_robotStateComponentProxyName.empty())
137 usingProxy(settings_robotStateComponentProxyName);
144 memoryPrx = getProxy<PriorKnowledgeInterfacePrx>(settings_priorMemory);
145 classesSegmentPrx = memoryPrx->getObjectClassesSegment();
146 databasePrx = memoryPrx->getCommonStorage();
147 if (!settings_robotStateComponentProxyName.empty())
149 getProxy(robotStateComponent, settings_robotStateComponentProxyName);
154 ui.editHost->setText(QString::fromStdString(databasePrx->getMongoHostAndPort()));
156 connected = databasePrx->isConnected();
159 refreshCollectionList();
160 setCurrentCollection(classesSegmentPrx->getWriteCollectionNS());
168 collCreateDialog->close();
194 return qobject_cast<PriorEditorConfigDialog*>(dialog);
200 settings_mongoHost = dialog->ui->editMongoHost->text().toStdString();
201 settings_mongoUser = dialog->ui->editMongoUser->text().toStdString();
202 settings_mongoPass = dialog->ui->editMongoPass->text().toStdString();
203 settings_priorMemory = dialog->ui->editPriorMemoryName->text().toStdString();
205 settings_robotStateComponentProxyName = dialog->proxyFinder->getSelectedProxyName().toStdString();
232 std::cout <<
"returning scene=" <<
visu->getName() << std::endl;
241 connect(
ui.btnReconnect, SIGNAL(clicked()),
this, SLOT(
reconnect()));
243 connect(
ui.btnAddCollection, SIGNAL(clicked()),
this, SLOT(
addCollection()));
244 connect(
ui.btnRefetch, SIGNAL(clicked()),
this, SLOT(
refetchData()));
246 connect(
ui.cbCollection, SIGNAL(currentIndexChanged(
int)),
257 connect(
ui.treeWidgetEntities, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
259 connect(
ui.treeWidgetEntities, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,
int)),
267 connected = databasePrx->reconnect(
ui.editHost->text().toStdString(), settings_mongoUser, settings_mongoPass);
273 msgBox.setText(
"Error connecting to MongoDB! \nCheck if mongod is running on " +
ui.editHost->text());
274 msgBox.setIcon(QMessageBox::Critical);
278 refreshCollectionList();
281 void PriorEditorController::setCurrentCollection(
const std::string& collNS)
284 ui.cbCollection->setCurrentIndex(
ui.cbCollection->findText(QString::fromStdString(collNS)));
290 const std::string collName =
ui.cbCollection->currentText().toStdString();
292 if (!collName.empty())
294 CollectionInterfacePrx coll = databasePrx->requestCollection(collName);
295 classesSegmentPrx->setWriteCollection(coll);
303 const NameList dbNames = databasePrx->getDBNames();
304 collCreateDialog->setDatabases(dbNames);
305 int result = collCreateDialog->exec();
307 if (result == QDialog::Accepted)
309 const std::string collectionNS = collCreateDialog->getDBName() +
"." + collCreateDialog->getCollectionName();
310 ui.cbCollection->addItem(QString::fromStdString(collectionNS));
311 setCurrentCollection(collectionNS);
318 const std::string collName =
ui.cbCollection->currentText().toStdString();
319 ARMARX_INFO <<
"Refetching data from DB from collection '" << collName <<
"'";
320 std::unique_lock lock(mutexEntities);
323 auto collection = databasePrx->requestCollection(collName);
324 auto ids = collection->findAllIds();
325 ARMARX_INFO <<
"Found " << ids.size() <<
" entities";
328 for (
const auto&
id : ids)
330 EntityBasePtr entity = classesSegmentPrx->getEntityById(
id);
334 ARMARX_WARNING <<
"Could not find entity with id " <<
id <<
" in collection " << collName;
337 const ObjectClassPtr objClass = ObjectClassPtr::dynamicCast(entity);
345 ARMARX_WARNING <<
"Failed to cast to ObjectClass: " << entity->getName() <<
", " << entity->getId() <<
", " << entity->ice_id();
350 ui.treeWidgetEntities->sortByColumn(0, Qt::AscendingOrder);
355 void PriorEditorController::refreshCollectionList()
358 ui.cbCollection->clear();
365 auto activeReadCollectionNames = memoryPrx->getObjectClassesSegment()->getReadCollectionsNS();
366 const NameList dbNames = databasePrx->getDBNames();
367 int firstActiveCollection = -1;
369 for (std::string
const& dbName : dbNames)
371 const NameList collNames = databasePrx->getCollectionNames(dbName);
373 for (std::string
const& collName : collNames)
375 std::string fullCollName = dbName +
"." + collName;
376 if (memoryPrx->isPriorCollection(fullCollName))
378 if (std::find(activeReadCollectionNames.begin(), activeReadCollectionNames.end(), fullCollName) != activeReadCollectionNames.end())
380 ui.cbCollection->addItem(QString::fromStdString(fullCollName));
382 if (firstActiveCollection == -1)
384 firstActiveCollection =
ui.cbCollection->count() - 1;
389 ui.cbCollection->addItem(QString::fromStdString(collName +
"(not selected in PriorKnowledge config)"));
390 const QStandardItemModel* model = qobject_cast<const QStandardItemModel*>(
ui.cbCollection->model());
392 QStandardItem* item = model->item(model->rowCount() - 1);
395 item->setFlags(item->flags() & ~(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
398 item->setData(
ui.cbCollection->palette().color(QPalette::Disabled, QPalette::Text)
406 if (
ui.cbCollection->currentIndex() >= 0)
408 const QStandardItemModel* model = qobject_cast<const QStandardItemModel*>(
ui.cbCollection->model());
410 QStandardItem* item = model->item(
ui.cbCollection->currentIndex());
413 if (!(item->flags() & Qt::ItemIsEnabled))
415 ui.cbCollection->setCurrentIndex(firstActiveCollection);
420 ui.cbCollection->setCurrentIndex(firstActiveCollection);
437 void PriorEditorController::doEditClass(
bool isNew)
443 const std::string clsId = getSelectedClassId();
472 int result = classEditDialog->exec();
474 if (result == QDialog::Accepted)
476 ARMARX_INFO <<
"Saving changes to object class...";
479 classEditDialog->
updateEntity(objectClass, getFilesDBName());
484 const std::string
id = classesSegmentPrx->addEntity(objectClass);
485 objectClass->setId(
id);
490 classesSegmentPrx->updateEntity(objectClass->getId(), objectClass);
493 updateObjectInTree(objectClass);
497 delete classEditDialog;
503 const std::string clsName = getSelectedClassName();
504 const std::string clsId = getSelectedClassId();
512 msgBox.setText(
"Delete object class " + QString(clsName.c_str()) +
"?");
513 msgBox.setIcon(QMessageBox::Question);
515 int ret = msgBox.exec();
527 classesSegmentPrx->removeEntity(clsId);
528 QTreeWidgetItem* selItem =
ui.treeWidgetEntities->currentItem();
532 delete ui.treeWidgetEntities->takeTopLevelItem(
ui.treeWidgetEntities->indexOfTopLevelItem(selItem));
539 std::string PriorEditorController::getSelectedClassId()
542 QTreeWidgetItem* selItem =
ui.treeWidgetEntities->currentItem();
546 return selItem->text(1).toStdString();
554 std::string PriorEditorController::getSelectedClassName()
557 QTreeWidgetItem* selItem =
ui.treeWidgetEntities->currentItem();
561 return selItem->text(0).toStdString();
569 void PriorEditorController::refreshControls()
572 ui.lblConnected->setText(QString::fromStdString(connected ?
"Connected" :
"Disconnected"));
573 ui.lblConnected->setStyleSheet(QString::fromStdString(connected ?
"color: rgb(0, 170, 0);" :
"color: rgb(170, 0, 0);"));
575 ui.btnAddCollection->setEnabled(connected);
576 ui.btnRefetch->setEnabled(connected);
578 QTreeWidgetItem* cur =
ui.treeWidgetEntities->currentItem();
580 ui.btnAdd->setEnabled(connected);
581 ui.btnEdit->setEnabled(clsSelected);
582 ui.btnRemove->setEnabled(clsSelected);
583 ui.btnShowVisu->setEnabled(clsSelected);
584 ui.btnShowColl->setEnabled(clsSelected);
598 ARMARX_INFO <<
" Selected object: " << item->text(1).toStdString();
599 showObjectClassModel(item->text(1).toStdString(), SceneObject::Full);
603 std::string PriorEditorController::getFilesDBName()
606 const std::string ns = classesSegmentPrx->getWriteCollectionNS();
607 const size_t found = ns.find_first_of(
'.');
608 return (found != std::string::npos) ? ns.substr(0, found) :
"";
611 void PriorEditorController::updateObjectInTree(
ObjectClassPtr obj)
614 QString objName(obj->getName().c_str());
615 QString objId(obj->getId().c_str());
617 QList<QTreeWidgetItem*> oldItems =
ui.treeWidgetEntities->findItems(objId, Qt::MatchFixedString, 1);
619 QTreeWidgetItem* objItem = (oldItems.size() == 0) ?
621 objItem->setText(0, objName);
622 objItem->setText(1, objId);
656 std::unique_lock lock(mutexEntities);
658 const std::string
id = objClass->getId();
663 ARMARX_INFO <<
"Building new entry for object class " << objClass->getName();
674 updateObjectInTree(objClass);
677 void PriorEditorController::showObjectClassModel(
const std::string& objectId, SceneObject::VisualizationType visuType)
680 std::unique_lock lock(mutexEntities);
694 auto mo = simoxWrapper->getManipulationObject();
697 mo->setGlobalPose(m);
698 mo->showCoordinateSystem(
true);
701 VisualizationNodePtr visNode;
703 if (visuType == SceneObject::Full)
706 visNode = simoxWrapper->getVisualization();
708 else if (visuType == SceneObject::Collision && simoxWrapper->getCollisionModel())
711 visNode = simoxWrapper->getCollisionModel()->getVisualization();
717 visu->addChild(VirtualRobot::CoinVisualizationFactory::getCoinVisualization(visNode));
724 showObjectClassModel(getSelectedClassId(), SceneObject::Full);
730 showObjectClassModel(getSelectedClassId(), SceneObject::Collision);
736 std::unique_lock lock(mutexEntities);
742 ui.treeWidgetEntities->clear();
748 visu->removeAllChildren();