ObserverItemModel.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::Gui
17 * @author Kai Welke (welke@kit.edu)
18 * @copyright 2012 Humanoids Group, IAIM, IFA
19 * @license http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 
24 
25 #include "ObserverItemModel.h"
26 
30 
31 #include <ArmarXCore/interface/observers/ConditionHandlerInterface.h>
37 
38 namespace armarx
39 {
41  : QStandardItemModel(0, 1),
42  iceManager(iceManager),
43  info(info)
44 
45  {
46 
48  }
49 
50  void ObserverItemModel::updateModel(const std::string& observerName, const ChannelRegistry& channels, const StringConditionCheckMap& checks)
51  {
52  // update data
53  channelRegistry[observerName] = channels;
54  availableChecks[observerName] = checks;
55  // activeConditions[observerName] = conditions;
56 
57  // mark all elements for deletion
58  markAllForDelete(invisibleRootItem());
59 
60  // insert observer
61  QStandardItem* observerItem = updateObserver(observerName);
62 
63  // update channels
64  updateChannels(getChildByName(observerItem, "channels"), channels);
65 
66  // update checks
67  updateChecks(getChildByName(observerItem, "checks"), checks);
68 
69  // update conditions
70  // updateConditions(observerItem->child(2), conditions);
71 
72  // delete not updated items
73  deleteUnusedItems(observerItem);
74  }
75 
76  QWidget* ObserverItemModel::getPropertiesWidget(const QModelIndex& index, QWidget* parent)
77  {
78  QStandardItem* item = itemFromIndex(index);
79  if (!item)
80  {
81  ARMARX_WARNING << "Could not find widget at column " << index.column() << " and row " << index.row();
82  return NULL;
83  }
84  QVariant id = item->data(OBSERVER_ITEM_ID);
85 
86  QWidget* widget;
87 
88  switch (item->data(OBSERVER_ITEM_TYPE).toInt())
89  {
90  // properties for channel
91  case eChannelItem:
92  {
93  // retrieve registry entry
94  QString channelName = id.toString();
95  QString observerName = item->parent()->parent()->data(OBSERVER_ITEM_ID).toString();
96  ChannelRegistryEntry& entry = channelRegistry[observerName.toLatin1().data()][channelName.toLatin1().data()];
97 
98  // create widget
99  widget = new ChannelPropertiesWidget(channelName.toLatin1().data(), observerName.toLatin1().data(), entry.description, parent);
100  break;
101  }
102 
103  // properties for datafields
104  case eDataFieldItem:
105  {
106  // retrieve registry entry
107  const DataFieldIdentifier dataFieldIdentifier(id.toString().toLatin1().data());
108 
110  channelRegistry.count(dataFieldIdentifier.getObserverName())) <<
111  "No channel registry for name '" << dataFieldIdentifier.getObserverName() << "' "
112  << "Available names:\n" << getMapKeys(channelRegistry);
113  ChannelRegistry& chanReg = channelRegistry.at(dataFieldIdentifier.getObserverName());
114 
116  chanReg.count(dataFieldIdentifier.getChannelName())) <<
117  "Channel registry '" << dataFieldIdentifier.getObserverName()
118  << "' has no channel '" << dataFieldIdentifier.getChannelName() << "' "
119  << "Available channels:\n" << getMapKeys(chanReg);
120  ChannelRegistryEntry& chanRegEntry = chanReg.at(dataFieldIdentifier.getChannelName());
121 
122  ARMARX_CHECK_EXPRESSION(chanRegEntry.dataFields.count(dataFieldIdentifier.getDataFieldName()))
123  << "Channel '" << dataFieldIdentifier.getObserverName() << "." << dataFieldIdentifier.getChannelName()
124  << "' has no datafield '" << dataFieldIdentifier.getDataFieldName()
125  << "'. Available datafields:\n" << getMapKeys(chanRegEntry.dataFields);
126  DataFieldRegistryEntry& entry = chanRegEntry.dataFields.at(dataFieldIdentifier.getDataFieldName());
127 
128  // create widget
129  DatafieldRefPtr ref = DatafieldRefPtr::dynamicCast(entry.identifier);
130  if (info && (!entry.value->data || entry.value->data->ice_id() == "::armarx::VariantData"))
131  {
132  ARMARX_INFO << "Variant Factory for " << entry.typeName << " is missing - trying to load lib now";
133  auto lib = info->loadLibraryOfVariant(entry.typeName);
134  if (!lib)
135  {
136  widget = new QWidget(parent);
137  break;
138  }
139  ArmarXManager::RegisterKnownObjectFactoriesWithIce(iceManager->getCommunicator());
140  entry.value = ref->getDataField();
141  }
142  widget = new DataFieldPropertiesWidget(entry.description, ref, VariantPtr::dynamicCast(entry.value), iceManager, parent);
143  break;
144  }
145 
146  case eCheckItem:
147  {
148  // retrieve check
149  QString checkName = id.toString();
150  QString observerName = item->parent()->parent()->data(OBSERVER_ITEM_ID).toString();
151  ConditionCheckBasePtr check = availableChecks[observerName.toLatin1().data()][checkName.toLatin1().data()];
152 
153  // create widget
154  widget = new ElementaryCheckPropertiesWidget(check, checkName.toLatin1().data(), true, false, false, parent);
155 
156  break;
157  }
158 
159  /* case eConditionItem:
160  {
161  // retrieve check
162  int conditionIndex = id.toInt();
163  QString observerName = item->parent()->parent()->data(OBSERVER_ITEM_ID).toString();
164  ConditionCheckBasePtr condition = activeConditions[observerName.toLatin1().data()][conditionIndex];
165 
166  // create widget
167  widget = new ConditionPropertiesWidget(condition, parent);
168 
169  break;
170  }
171 
172  case eElementaryConditionItem:
173  {
174  // retrieve check
175  int elementId = id.toInt();
176  int conditionIndex = item->parent()->data(OBSERVER_ITEM_ID).toInt();
177  QString observerName = item->parent()->parent()->parent()->data(OBSERVER_ITEM_ID).toString();
178  ConditionCheckBasePtr check = activeConditions[observerName.toLatin1().data()][conditionIndex];
179  ElementaryConditionCheckBasePtr element = check->elementaryChecks[elementId];
180 
181  // create widget
182  widget = new ElementaryCheckPropertiesWidget(element, element->condition->checkName, false, true, true, parent);
183 
184  break;
185  }*/
186 
187  default:
188  widget = new QWidget(parent);
189  break;
190  }
191 
192  return widget;
193  }
194 
196  {
198 
199  if (!iceManager)
200  {
201  return list;
202  }
203 
204  try
205  {
206  ConditionHandlerInterfacePrx handler = iceManager->getProxy<ConditionHandlerInterfacePrx>("ConditionHandler");
207 
208  if (handler)
209  {
210  ObserverList registeredObs = handler->getObserverNames();
211  list = registeredObs;
212  }
213  }
214  catch (...)
215  {
216 
217  }
218 
219 
220  ObserverList globalList = iceManager->getIceGridSession()->getRegisteredObjectNames<ObserverInterfacePrx>("*Observer");
221  list.insert(list.end(), globalList.begin(), globalList.end());
222  std::sort(list.begin(), list.end());
223  ObserverList::iterator it = std::unique(list.begin(), list.end());
224  list.resize(std::distance(list.begin(), it));
225  return list;
226  }
227 
229  {
230  if (!iceManager)
231  {
232  ARMARX_WARNING_S << "No IceManager set";
233  return;
234  }
235 
236  ObserverList observerList = getObservers();
237  ObserverList::iterator iter = observerList.begin();
238 
239  while (iter != observerList.end())
240  {
241  ObserverInterfacePrx proxy;
242  try
243  {
244  proxy = iceManager->getProxy<ObserverInterfacePrx>(*iter);
245  updateModel(*iter, proxy->getAvailableChannels(true), proxy->getAvailableChecks()); //, component->getActiveConditionChecks(*iter));
246  }
247  catch (...)
248  {
249  if (proxy)
250  {
251  iceManager->removeProxyFromCache(proxy);
252  }
253  }
254 
255  iter++;
256  }
257  }
258 
259  QStandardItem* ObserverItemModel::updateObserver(std::string observerName)
260  {
261  // create observer item
262  QStandardItem* observerItem = new QStandardItem(QString(observerName.c_str()));
263  observerItem->setData(QVariant(eObserverItem), OBSERVER_ITEM_TYPE);
264  observerItem->setData(QVariant(observerName.c_str()), OBSERVER_ITEM_ID);
265  observerItem = updateOrInsertItem(invisibleRootItem(), observerItem);
266 
267  // add channels
268  QStandardItem* channelsItem = new QStandardItem(QString("channels"));
269  channelsItem->setData(QVariant(eChannelsItem), OBSERVER_ITEM_TYPE);
270  channelsItem->setData(QVariant("channels"), OBSERVER_ITEM_ID);
271  updateOrInsertItem(observerItem, channelsItem);
272 
273  // add checks
274  QStandardItem* checksItem = new QStandardItem(QString("checks"));
275  checksItem->setData(QVariant(eChecksItem), OBSERVER_ITEM_TYPE);
276  checksItem->setData(QVariant("checks"), OBSERVER_ITEM_ID);
277  updateOrInsertItem(observerItem, checksItem);
278 
279  // add conditions
280  /* QStandardItem *conditionsItem = new QStandardItem( QString("conditions") );
281  conditionsItem->setData(QVariant(eConditionsItem), OBSERVER_ITEM_TYPE);
282  conditionsItem->setData(QVariant("conditions"), OBSERVER_ITEM_ID);
283  updateOrInsertItem(observerItem, conditionsItem);*/
284 
285  return observerItem;
286  }
287 
288  void ObserverItemModel::updateChannels(QStandardItem* channelsItem, const ChannelRegistry& channels)
289  {
290  if (!channelsItem)
291  {
292  return;
293  }
294 
295  // loop through channelsobser
296  ChannelRegistry::const_iterator iterChannels = channels.begin();
297 
298  while (iterChannels != channels.end())
299  {
300  // create item for channels
301  QStandardItem* channelItem = new QStandardItem(QString(iterChannels->second.name.c_str()));
302 
303  channelItem->setData(QVariant(eChannelItem), OBSERVER_ITEM_TYPE);
304  channelItem->setData(QVariant(iterChannels->second.name.c_str()), OBSERVER_ITEM_ID);
305 
306  // insert channel's item
307  channelItem = updateOrInsertItem(channelsItem, channelItem);
308 
309  // loop through datafields
310  DataFieldRegistry::const_iterator iterDataFields = iterChannels->second.dataFields.begin();
311 
312  while (iterDataFields != iterChannels->second.dataFields.end())
313  {
314  // create new datafield item
315  std::string datafieldName = iterDataFields->second.identifier->channelRef->observerName + "." + iterDataFields->second.identifier->channelRef->channelName + "." + iterDataFields->second.identifier->datafieldName;
316  QStandardItem* dataFieldItem = new QStandardItem(QString(iterDataFields->second.identifier->datafieldName.c_str()));
317  dataFieldItem->setData(QVariant(eDataFieldItem), OBSERVER_ITEM_TYPE);
318  dataFieldItem->setData(QVariant(datafieldName.c_str()), OBSERVER_ITEM_ID);
319 
320  // insert item
321  updateOrInsertItem(channelItem, dataFieldItem);
322  iterDataFields++;
323  }
324 
325  iterChannels++;
326  }
327  }
328 
329  void ObserverItemModel::updateChecks(QStandardItem* checksItem, const StringConditionCheckMap& checks)
330  {
331  if (!checksItem)
332  {
333  return;
334  }
335 
336  StringConditionCheckMap::const_iterator iterChecks = checks.begin();
337 
338  while (iterChecks != checks.end())
339  {
340  QStandardItem* checkItem = new QStandardItem(QString(iterChecks->first.c_str()));
341  checkItem->setData(QVariant(eCheckItem), OBSERVER_ITEM_TYPE);
342  checkItem->setData(QVariant(iterChecks->first.c_str()), OBSERVER_ITEM_ID);
343 
344  updateOrInsertItem(checksItem, checkItem);
345 
346  iterChecks++;
347  }
348  }
349  /*
350  void ObserverItemModel::updateConditions(QStandardItem* conditionsItem, const ConditionCheckMap& conditions)
351  {
352  ConditionCheckMap::const_iterator iterConditions = conditions.begin();
353  while(iterConditions != conditions.end())
354  {
355  // update condition
356  QString name = QString("%1").arg(iterConditions->second->info.identifier.uniqueId);
357 
358  QStandardItem* conditionItem = new QStandardItem( name );
359  conditionItem->setData(QVariant(eConditionItem), OBSERVER_ITEM_TYPE);
360  conditionItem->setData(QVariant(name), OBSERVER_ITEM_ID);
361 
362  conditionItem = updateOrInsertItem(conditionsItem, conditionItem);
363 
364  // loop through elementary conditions
365  IntElementaryConditionCheckMap::iterator iterElements = iterConditions->second->elementaryChecks.begin();
366 
367  while (iterElements != iterConditions->second->elementaryChecks.end())
368  {
369  // update elementary condition
370  QStandardItem* elementItem = new QStandardItem(iterElements->second->condition->checkName.c_str());
371  elementItem->setData(QVariant(eElementaryConditionItem), OBSERVER_ITEM_TYPE);
372  elementItem->setData(QVariant(iterElements->first), OBSERVER_ITEM_ID);
373 
374  updateOrInsertItem(conditionItem, elementItem);
375 
376  iterElements++;
377  }
378 
379  iterConditions++;
380  }
381  }
382  */
383  QStandardItem* ObserverItemModel::updateOrInsertItem(QStandardItem* parent, QStandardItem* insert)
384  {
385  // loop through childs and compare data
386  for (int r = 0 ; r < parent->rowCount() ; r++)
387  {
388  QStandardItem* item = parent->child(r);
389 
390  if (item->data(OBSERVER_ITEM_ID) == insert->data(OBSERVER_ITEM_ID))
391  {
392  delete insert;
393  item->setData(QVariant(false), OBSERVER_ITEM_DELETE);
394  return item;
395  }
396  }
397 
398  parent->appendRow(insert);
399 
400  insert->setData(QVariant(false), OBSERVER_ITEM_DELETE);
401  return insert;
402  }
403 
404  QStandardItem* ObserverItemModel::getChildByName(QStandardItem* item, const QString& name)
405  {
406  for (int r = 0 ; r < item->rowCount() ; r++)
407  {
408  if (item->child(r)->data(OBSERVER_ITEM_ID).toString() == name)
409  {
410  return item->child(r);
411  }
412  }
413 
414  return 0;
415 
416  }
417 
418  // mark all elements for deletion
419  void ObserverItemModel::markAllForDelete(QStandardItem* observerItem)
420  {
421  int nCount = 0;
422  std::list<QStandardItem*> markList;
423  markList.push_back(observerItem);
424 
425  while (markList.size() > 0)
426  {
427  QStandardItem* current = markList.front();
428  markList.pop_front();
429  current->setData(QVariant(true), OBSERVER_ITEM_DELETE);
430  nCount++;
431 
432  if (current->hasChildren())
433  {
434  for (int r = 0 ; r < current->rowCount() ; r++)
435  {
436  markList.push_back(current->child(r));
437  }
438  }
439  }
440  }
441 
442  void ObserverItemModel::deleteUnusedItems(QStandardItem* observerItem)
443  {
444  std::list<QStandardItem*> searchList;
445  std::vector<int> removeRows;
446 
447  searchList.push_back(observerItem);
448 
449  while (searchList.size() > 0)
450  {
451  QStandardItem* current = searchList.back();
452  searchList.pop_back();
453 
454  if (!current)
455  {
456  continue;
457  }
458 
459  // ARMARX_INFO_S << "CurrentItem: " << current->data(OBSERVER_ITEM_ID).toString().toStdString() << " num: " << current->rowCount();
460  if (current->hasChildren())
461  {
462  removeRows.clear();
463 
464  for (int r = 0 ; r < current->rowCount() ; r++)
465  {
466  if (current->child(r)->data(OBSERVER_ITEM_DELETE).toBool())
467  {
468  // ARMARX_INFO_S << "Removing row: " << current->child(r)->data(OBSERVER_ITEM_ID).toString().toStdString();
469  removeRows.push_back(r);
470  }
471  else
472  {
473  // ARMARX_INFO_S << "Insert row: " << current->child(r)->data(OBSERVER_ITEM_ID).toString().toStdString();
474  searchList.push_back(current->child(r));
475  }
476  }
477 
478  // remove unused rows
479  std::vector<int>::reverse_iterator iter = removeRows.rbegin();
480 
481  while (iter != removeRows.rend())
482  {
483  current->removeRow(*iter);
484  iter++;
485  }
486  }
487  }
488  }
489 }
algorithm.h
index
uint8_t index
Definition: EtherCATFrame.h:59
ArmarXManager.h
ElementaryCheckPropertiesWidget.h
list
list(APPEND SOURCES ${QT_RESOURCES}) set(COMPONENT_LIBS ArmarXGui ArmarXCoreObservers ArmarXCoreEigen3Variants PlotterController $
Definition: CMakeLists.txt:49
OBSERVER_ITEM_TYPE
#define OBSERVER_ITEM_TYPE
Definition: ObserverItemModel.h:39
armarx::DataFieldIdentifier::getObserverName
std::string getObserverName() const
Retrieve observer name.
Definition: DataFieldIdentifier.h:101
armarx::eChannelItem
@ eChannelItem
Definition: ObserverItemModel.h:54
armarx::ObserverItemModel::getPropertiesWidget
QWidget * getPropertiesWidget(const QModelIndex &index, QWidget *parent)
Definition: ObserverItemModel.cpp:76
armarx::eChecksItem
@ eChecksItem
Definition: ObserverItemModel.h:56
DataFieldPropertiesWidget.h
OBSERVER_ITEM_ID
#define OBSERVER_ITEM_ID
Definition: ObserverItemModel.h:40
ObserverItemModel.h
IceInternal::Handle< DatafieldRef >
armarx::ObserverItemModel::updateModel
void updateModel(const std::string &observerName, const ChannelRegistry &channels, const StringConditionCheckMap &checks)
Definition: ObserverItemModel.cpp:50
armarx::DataFieldIdentifier::getChannelName
std::string getChannelName() const
Retrieve channel name.
Definition: DataFieldIdentifier.h:111
DatafieldRef.h
armarx::ChannelPropertiesWidget
Definition: ChannelPropertiesWidget.h:37
ChannelPropertiesWidget.h
armarx::DataFieldIdentifier::getDataFieldName
std::string getDataFieldName() const
Retrieve datafield name.
Definition: DataFieldIdentifier.h:121
armarx::ObserverItemModel::ObserverItemModel
ObserverItemModel(IceManagerPtr iceManager, VariantInfoPtr info=NULL)
Definition: ObserverItemModel.cpp:40
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::ObserverItemModel::updateObservers
void updateObservers()
Definition: ObserverItemModel.cpp:228
armarx::eObserverItem
@ eObserverItem
Definition: ObserverItemModel.h:52
armarx::eCheckItem
@ eCheckItem
Definition: ObserverItemModel.h:57
IceGridAdmin.h
armarx::armem::server::ltm::util::mongodb::detail::insert
bool insert(mongocxx::collection &coll, const nlohmann::json &value)
Definition: mongodb.cpp:43
armarx::ObserverItemModel::getObservers
ObserverList getObservers()
Definition: ObserverItemModel.cpp:195
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
ExpressionException.h
armarx::ObserverList
std::vector< std::string > ObserverList
Definition: ObserverItemModel.h:45
armarx::ElementaryCheckPropertiesWidget
Definition: ElementaryCheckPropertiesWidget.h:37
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
OBSERVER_ITEM_DELETE
#define OBSERVER_ITEM_DELETE
Definition: ObserverItemModel.h:41
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< IceManager >
armarx::eDataFieldItem
@ eDataFieldItem
Definition: ObserverItemModel.h:55
armarx::VariantInfoPtr
std::shared_ptr< VariantInfo > VariantInfoPtr
Definition: VariantInfo.h:39
armarx::viz::toString
const char * toString(InteractionFeedbackType type)
Definition: Interaction.h:27
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::DataFieldPropertiesWidget
Definition: DataFieldPropertiesWidget.h:41
armarx::eChannelsItem
@ eChannelsItem
Definition: ObserverItemModel.h:53
armarx::getMapKeys
void getMapKeys(const MapType &map, OutputIteratorType it)
Definition: algorithm.h:157
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ArmarXManager::RegisterKnownObjectFactoriesWithIce
static void RegisterKnownObjectFactoriesWithIce(const Ice::CommunicatorPtr &ic)
Registers all object factories that are known with Ice.
Definition: ArmarXManager.cpp:1204
armarx::DataFieldIdentifier
DataFieldIdentifier provide the basis to identify data field within a distributed ArmarX scenario.
Definition: DataFieldIdentifier.h:48