diff --git a/source/RobotAPI/libraries/armem_gui/instance/InstanceView.cpp b/source/RobotAPI/libraries/armem_gui/instance/InstanceView.cpp index 7c88f03c70ff85262f8c356b93987d03d83f0cfe..362c9c589714146662043ab052aadfb53dc5ef3b 100644 --- a/source/RobotAPI/libraries/armem_gui/instance/InstanceView.cpp +++ b/source/RobotAPI/libraries/armem_gui/instance/InstanceView.cpp @@ -1,6 +1,8 @@ #include "InstanceView.h" #include <QAction> +#include <QApplication> +#include <QClipboard> #include <QGroupBox> #include <QHBoxLayout> #include <QHeaderView> @@ -220,6 +222,14 @@ namespace armarx::armem::gui::instance } + aron::Path InstanceView::getElementPath(QTreeWidgetItem* item) const + { + QStringList qpath = item->data(int(Columns::KEY), Qt::UserRole).toStringList(); + aron::Path path = deserializePath(qpath); + return path; + } + + void InstanceView::prepareTreeContextMenu(const QPoint& pos) { QTreeWidgetItem* item = tree->itemAt(pos); @@ -229,13 +239,6 @@ namespace armarx::armem::gui::instance return; } - auto getPath = [item]() - { - QStringList qpath = item->data(int(Columns::KEY), Qt::UserRole).toStringList(); - aron::Path path = deserializePath(qpath); - return path; - }; - QMenu menu(this); // Type descriptor based actions @@ -244,7 +247,7 @@ namespace armarx::armem::gui::instance { case aron::type::Descriptor::eIVTCByteImage: { - aron::Path path = getPath(); + const aron::Path path = getElementPath(item); QAction* viewAction = new QAction("Show image"); menu.addAction(viewAction); @@ -262,12 +265,18 @@ namespace armarx::armem::gui::instance const std::string typeName = item->text(int(Columns::TYPE)).toStdString(); if (typeName == instance::sanitizedMemoryIDTypeName) { - const aron::Path path = getPath(); + const aron::Path path = getElementPath(item); - QAction* action = makeResolveMemoryIdAction(path); - if (action) + if (std::optional<MemoryID> id = getElementMemoryID(path)) { - menu.addAction(action); + if (QAction* action = makeActionCopyMemoryID(id.value())) + { + menu.addAction(action); + } + if (QAction* action = makeActionResolveMemoryID(id.value())) + { + menu.addAction(action); + } } } @@ -279,7 +288,7 @@ namespace armarx::armem::gui::instance } - QAction* InstanceView::makeResolveMemoryIdAction(const aron::Path& elementPath) + std::optional<MemoryID> InstanceView::getElementMemoryID(const aron::Path& elementPath) { aron::datanavigator::NavigatorPtr element; try @@ -290,12 +299,12 @@ namespace armarx::armem::gui::instance catch (const aron::error::AronException&) { // showErrorMessage(e.what()); - return nullptr; + return std::nullopt; } catch (const armarx::LocalException& e) { showErrorMessage(e.what()); - return nullptr; + return std::nullopt; } std::stringstream couldNotParseMsg; @@ -305,23 +314,29 @@ namespace armarx::armem::gui::instance if (!dictElement) { showErrorMessage(couldNotParseMsg.str() + " (Failed to cast to DictNavigator.)"); - return nullptr; + return std::nullopt; } - MemoryID id; try { arondto::MemoryID dto; dto.fromAron(dictElement); + + MemoryID id; armem::fromAron(dto, id); + return id; } catch (const armarx::aron::error::AronException&) { showErrorMessage(couldNotParseMsg.str()); - return nullptr; + return std::nullopt; } + } - QAction* action = new QAction("Resolve Memory ID"); + + QAction* InstanceView::makeActionResolveMemoryID(const MemoryID& id) + { + QAction* action = new QAction("Resolve memory ID"); if (not(id.hasEntityName() and id.isWellDefined())) { @@ -337,6 +352,23 @@ namespace armarx::armem::gui::instance return action; } + QAction* InstanceView::makeActionCopyMemoryID(const MemoryID& id) + { + QAction* action = new QAction("Copy memory ID to clipboard"); + + connect(action, &QAction::triggered, [/*this,*/ id]() // `this` for ARMARX_IMPORTANT + { + const QString idStr = QString::fromStdString(id.str()); + + // ARMARX_IMPORTANT << "Copy '" << idStr.toStdString() << "' to clipboard."; + QClipboard* clipboard = QApplication::clipboard(); + clipboard->setText(idStr); + QApplication::processEvents(); + }); + + return action; + } + void InstanceView::showImageView(const aron::Path& elementPath) { diff --git a/source/RobotAPI/libraries/armem_gui/instance/InstanceView.h b/source/RobotAPI/libraries/armem_gui/instance/InstanceView.h index b871152562da9432adb5aa1ec52f43c80119a6e0..a643819d90df62292ff28f15b8c0c59a4a222362 100644 --- a/source/RobotAPI/libraries/armem_gui/instance/InstanceView.h +++ b/source/RobotAPI/libraries/armem_gui/instance/InstanceView.h @@ -68,7 +68,11 @@ namespace armarx::armem::gui::instance void showErrorMessage(const std::string& message); - QAction* makeResolveMemoryIdAction(const aron::Path& elementPath); + aron::Path getElementPath(QTreeWidgetItem* item) const; + std::optional<MemoryID> getElementMemoryID(const aron::Path& elementPath); + + QAction* makeActionResolveMemoryID(const MemoryID& id); + QAction* makeActionCopyMemoryID(const MemoryID& id); private: