InstanceView.cpp
Go to the documentation of this file.
1 #include "InstanceView.h"
2 #include <new>
3 
4 #include <QAction>
5 #include <QApplication>
6 #include <QClipboard>
7 #include <QGroupBox>
8 #include <QHeaderView>
9 #include <QImage>
10 #include <QLabel>
11 #include <QLayout>
12 #include <QMenu>
13 #include <QSplitter>
14 #include <QTreeWidget>
15 #include <QVBoxLayout>
16 
17 #include <SimoxUtility/algorithm/string.h>
18 
20 
23 
24 #include <RobotAPI/libraries/armem/aron/MemoryID.aron.generated.h>
26 
33 
34 #include "MemoryIDTreeWidgetItem.h"
35 #include "WidgetsWithToolbar.h"
36 
37 
39 {
40 
42  {
43  Logging::setTag("InstanceView");
44 
45  treeItemInstanceID = new MemoryIDTreeWidgetItem({"Instance ID"});
46  treeItemInstanceID->addKeyChildren();
47 
48  treeItemMetadata = new QTreeWidgetItem({"Metadata"});
49  treeItemMetadata->addChild(new QTreeWidgetItem({"Confidence"}));
50  treeItemMetadata->addChild(new QTreeWidgetItem({"Time Created"}));
51  treeItemMetadata->addChild(new QTreeWidgetItem({"Time Sent"}));
52  treeItemMetadata->addChild(new QTreeWidgetItem({"Time Arrived"}));
53  treeItemMetadata->addChild(new QTreeWidgetItem({"Time Last Accessed"}));
54 
55  QList<QTreeWidgetItem*> items = {treeItemInstanceID, treeItemMetadata};
56  tree->insertTopLevelItems(0, items);
57 
58  treeItemInstanceID->setExpanded(true);
59  treeItemMetadata->setExpanded(false);
60  }
61 
63  {
64  aron::type::ObjectPtr aronType = nullptr;
65  const armem::wm::EntityInstance* instance = nullptr;
66  try
67  {
68  instance = &memory.getInstance(id);
69  if (useTypeInfo)
70  {
71  aronType = memory.getProviderSegment(id).aronType();
72  }
73  }
74  catch (const armem::error::ArMemError& e)
75  {
76  showErrorMessage(e.what());
77  }
78  if (instance)
79  {
80  update(*instance, aronType);
81  }
82  }
83 
85  {
86  currentInstance = instance;
87  currentAronType = aronType;
88  update();
89  }
90 
92  {
93  if (currentInstance)
94  {
95  updateInstanceID(currentInstance->id());
96  updateMetaData(currentInstance->metadata());
97  updateData(currentInstance->data(), currentAronType);
98  updateImageView(currentInstance->data());
99 
100  emit updated();
101  }
102  }
103 
104  void InstanceView::updateInstanceID(const MemoryID& id)
105  {
106  treeItemInstanceID->setInstanceID(id, int(Columns::VALUE));
107  }
108 
109  void InstanceView::updateMetaData(const wm::EntityInstanceMetadata& metadata)
110  {
111  std::vector<std::string> items =
112  {
113  std::to_string(metadata.confidence),
117  armem::toDateTimeMilliSeconds(metadata.lastAccessedTime) + " (" + std::to_string(metadata.numAccessed) + " times total)"
118  };
119  ARMARX_CHECK_EQUAL(static_cast<size_t>(treeItemMetadata->childCount()), items.size());
120  int i = 0;
121  for (const std::string& item : items)
122  {
123  treeItemMetadata->child(i++)->setText(int(Columns::VALUE), QString::fromStdString(item));
124  }
125  }
126 
127  QMenu* InstanceView::buildActionsMenu(const QPoint& pos)
128  {
129  auto* parentMenu = DataView::buildActionsMenu(pos);
130 
131  const QTreeWidgetItem* item = tree->itemAt(pos);
132  if (item == this->treeItemInstanceID && currentInstance.has_value())
133  {
134  if (QAction* action = makeActionCopyMemoryID(currentInstance->id()))
135  {
136  parentMenu->addAction(action);
137  }
138  }
139 
140  return parentMenu;
141  }
142 
143  void InstanceView::prepareTreeContextMenu(const QPoint& pos)
144  {
145  if (currentInstance.has_value())
146  {
147  auto* menu = buildActionsMenu(pos);
148 
149  emit actionsMenuRequested(currentInstance->id(), this, tree->mapToGlobal(pos),
150  menu->actions().isEmpty() ? nullptr : menu);
151  }
152  }
153 
154  aron::data::DictPtr InstanceView::getData()
155  {
156  if (currentInstance)
157  {
158  return currentInstance->data();
159  }
160  return nullptr;
161  }
162 
163 } // namespace armarx::armem::gui::instance
164 
sanitize_typename.h
WidgetsWithToolbar.h
armarx::armem::base::EntityInstanceMetadata::numAccessed
unsigned long numAccessed
A counter how often the instance has been accessed.
Definition: EntityInstanceBase.h:60
TypedDataTreeBuilder.h
armarx::armem::gui::instance::DataView::useTypeInfo
bool useTypeInfo
Definition: DataView.h:97
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::armem::gui::instance::InstanceView::InstanceView
InstanceView()
Definition: InstanceView.cpp:41
armarx::armem::gui::instance::DataView::updated
void updated()
armarx::armem::error::ArMemError
Base class for all exceptions thrown by the armem library.
Definition: ArMemError.h:18
armarx::armem::gui::instance::DataView::tree
QTreeWidget * tree
Definition: DataView.h:101
armarx::armem::gui::instance::DataView::actionsMenuRequested
void actionsMenuRequested(const MemoryID &memoryID, QWidget *parent, const QPoint &pos, QMenu *menu)
armarx::memory
Brief description of class memory.
Definition: memory.h:39
NLohmannJSONConverter.h
armarx::armem::gui::instance::MemoryIDTreeWidgetItem::addKeyChildren
void addKeyChildren()
Definition: MemoryIDTreeWidgetItem.cpp:11
Object.h
DataTreeBuilder.h
armarx::armem::server::wm::Memory
Definition: memory_definitions.h:136
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::gui::instance
Definition: AronDataView.cpp:2
armarx::armem::base::EntityInstanceMetadata::sentTime
Time sentTime
Time when this value was sent to the memory.
Definition: EntityInstanceBase.h:49
armarx::armem::toDateTimeMilliSeconds
std::string toDateTimeMilliSeconds(const Time &time, int decimals=6)
Returns timeas e.g.
Definition: Time.cpp:35
armarx::armem::gui::instance::InstanceView::update
void update() override
Definition: InstanceView.cpp:91
TreeTypedJSONConverter.h
InstanceView.h
armarx::armem::gui::instance::DataView::updateData
virtual void updateData(const aron::data::DictPtr &data, aron::type::ObjectPtr aronType=nullptr)
Definition: DataView.cpp:101
armarx::armem::gui::instance::DataView::buildActionsMenu
virtual QMenu * buildActionsMenu(const QPoint &pos)
Definition: DataView.cpp:343
armarx::armem::base::EntityInstanceMetadata::arrivedTime
Time arrivedTime
Time when this value has arrived at the memory.
Definition: EntityInstanceBase.h:51
armarx::armem::gui::instance::MemoryIDTreeWidgetItem::setInstanceID
void setInstanceID(const MemoryID &id, int valueColumn=1)
Definition: MemoryIDTreeWidgetItem.cpp:22
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
aron_conversions.h
ExpressionException.h
armarx::armem::base::EntityInstanceMetadata::referencedTime
Time referencedTime
Time this instance refers to.
Definition: EntityInstanceBase.h:46
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::armem::gui::instance::MemoryIDTreeWidgetItem
Definition: MemoryIDTreeWidgetItem.h:14
armarx::armem::gui::instance::DataView::showErrorMessage
void showErrorMessage(const std::string &message)
Definition: DataView.cpp:133
armarx::armem::base::EntityInstanceMetadata
Metadata of an entity instance.
Definition: EntityInstanceBase.h:34
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
armarx::Logging::setTag
void setTag(const LogTag &tag)
Definition: Logging.cpp:55
armarx::armem::gui::instance::DataView::currentAronType
aron::type::ObjectPtr currentAronType
Definition: DataView.h:96
armarx::armem::base::EntityInstanceMetadata::lastAccessedTime
Time lastAccessedTime
An optional value indicating the time of last access.
Definition: EntityInstanceBase.h:57
armarx::armem::base::EntityInstanceMetadata::confidence
float confidence
An optional confidence, may be used for things like decay.
Definition: EntityInstanceBase.h:54
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::armem::gui::instance::DataView::Columns::VALUE
@ VALUE
MemoryIDTreeWidgetItem.h
armarx::armem::gui::instance::DataView::updateImageView
void updateImageView(const aron::data::DictPtr &data)
Definition: DataView.cpp:576
gui_utils.h
serialize_path.h
armarx::armem::gui::instance::DataView::makeActionCopyMemoryID
QAction * makeActionCopyMemoryID(const MemoryID &id)
Definition: DataView.cpp:232