ConditionViewerWidget.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "ConditionViewerWidget.h"
25 
26 #include "../ConditionViewerWidgetController.h"
27 #include "../ConditionTreeFactory.h"
28 
32 
33 #include <QList>
34 
35 namespace armarx
36 {
38  : QWidget(0), timerId(0)
39  {
40  this->controller = controller;
41 
42  ui.setupUi(this);
43 
44  // setup scene
45  scene = new TermTreeGraphicsScene();
46  ui.graphicsView->setScene(scene);
47 
48  // modes for conditions
49  activeConditionsModel = new ConditionItemModel();
50  pastConditionsModel = new ConditionItemModel();
51 
52  ui.activeConditionsTableView->setModel(activeConditionsModel);
53  ui.pastConditionsTableView->setModel(pastConditionsModel);
54 
55  // condition checks table
56  QStringList checksHeader;
57  checksHeader << "datafield" << "checktype" << "parameters" << "state" << "current value";
58  ui.checksTableWidget->setColumnCount(5);
59  ui.checksTableWidget->setHorizontalHeaderLabels(checksHeader);
60  ui.checksTableWidget->update();
61 
62  // item selection
63  QItemSelectionModel* activeSelectionModel = ui.activeConditionsTableView->selectionModel();
64  QItemSelectionModel* pastSelectionModel = ui.pastConditionsTableView->selectionModel();
65 
66  // connections
67  connect(activeSelectionModel, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(activeConditionItemSelected(const QItemSelection&, const QItemSelection&)));
68  connect(pastSelectionModel, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(pastConditionItemSelected(const QItemSelection&, const QItemSelection&)));
69  connect(scene, SIGNAL(graphicsSceneClicked()), ui.checksTableWidget, SLOT(clearSelection()));
70  }
71 
73  {
74  if (updateTask)
75  {
76  updateTask->stop();
77  }
78  }
79 
81  {
82  //clear scene and checks
83  scene->clear();
84  root.reset();
85  ui.checksTableWidget->clearContents();
86  ui.checksTableWidget->setRowCount(0);
87 
88  //reset the models
89  activeConditionsModel->reset();
90  pastConditionsModel->reset();
91 
92  // resize tables with active and past conditions
93  ui.activeConditionsTableView->resizeColumnsToContents();
94  ui.pastConditionsTableView->resizeColumnsToContents();
95 
96  // starting timer for periodic task
97  while (timerId == 0)
98  {
99  timerId = startTimer(500);
100  }
101 
102  updateTask = new PeriodicTask<ConditionViewerWidget>(this, &ConditionViewerWidget::updateLiterals, 200, false, "LiteralsUpdater");
103  updateTask->start();
104  }
105 
107  {
108  killTimer(timerId);
109  timerId = 0;
110  if (updateTask)
111  {
112  updateTask->stop();
113  }
114  }
115 
116  // ****************************************************************
117  // slots
118  // ****************************************************************
119 
120 
121  void ConditionViewerWidget::activeConditionItemSelected(const QItemSelection& selected, const QItemSelection& deselected)
122  {
123  std::unique_lock lock(dataMutex);
124 
125  if (selected.indexes().size() == 0)
126  {
127  return;
128  }
129 
130  QModelIndexList list = selected.indexes();
131  QStandardItem* i = activeConditionsModel->itemFromIndex(list.at(0));
132 
133 
134  ConditionRootPtr condition = conditionFromItem(i, activeConditions);
135 
136  if (condition)
137  {
138  updateCondition(i->data(CONDITION_ITEM_ID).toInt(), condition);
139  }
140  }
141 
142  void ConditionViewerWidget::pastConditionItemSelected(const QItemSelection& selected, const QItemSelection& deselected)
143  {
144  std::unique_lock lock(dataMutex);
145 
146  if (selected.indexes().size() == 0)
147  {
148  return;
149  }
150 
151  QModelIndexList list = selected.indexes();
152  QStandardItem* i = pastConditionsModel->itemFromIndex(list.at(0));
153 
154  ConditionRootPtr condition = conditionFromItem(i, pastConditions);
155 
156  if (condition)
157  {
158  updateCondition(i->data(CONDITION_ITEM_ID).toInt(), condition);
159  }
160  }
161 
162  ConditionRootPtr ConditionViewerWidget::conditionFromItem(QStandardItem* selectedItem, const ConditionRegistry& registry)
163  {
164  ConditionRegistry::const_iterator iter = registry.find(selectedItem->data(CONDITION_ITEM_ID).toInt());
165 
166  if (iter != registry.end())
167  {
168  ConditionRootPtr conditionRoot = ConditionRootPtr::dynamicCast(iter->second);
169  return conditionRoot;
170  }
171 
172  return ConditionRootPtr();
173  }
174 
175  void ConditionViewerWidget::updateCondition(int conditionId, ConditionRootPtr& condition)
176  {
177  std::unique_lock lock(dataMutex);
178  scene->clear();
179  ui.checksTableWidget->clearContents();
180  ui.checksTableWidget->setRowCount(0);
181  if (controller->handler)
182  {
183  root = ConditionTreeFactory::createConditionTree(scene, ui.checksTableWidget, condition, conditionId, controller->getIceManager(), controller->handler);
184  root->update();
185  }
186 
187 
188  }
189 
190  void ConditionViewerWidget::updateLiterals()
191  {
192  if (!root)
193  {
194  return;
195  }
196  std::vector<TreeNodePtr> tempChildNodes;
197  std::vector<TreeNodePtr> qtChildren;
198  int conditionId = -1;
199  {
200  std::unique_lock lock(dataMutex);
201 
202  qtChildren.push_back(root);
203  tempChildNodes = root->getChildren();
204  conditionId = root->conditionId;
205  qtChildren.insert(qtChildren.end(), tempChildNodes.begin(), tempChildNodes.end());
206  auto result = controller->handler->getCondition(conditionId);
207  if (!result)
208  {
209  return;
210  }
211  ConditionRootPtr newRoot = ConditionRootPtr::dynamicCast(result);
212  TermImplSequence children;
213  children.push_back(newRoot);
214  auto temp2 = newRoot->getChilds();
215  children.insert(children.end(), temp2.begin(), temp2.end());
216 
217  while (qtChildren.size() > 0)
218  {
219  TreeNodePtr node = *qtChildren.rbegin();
220  TermImplPtr term = TermImplPtr::dynamicCast(*children.rbegin());
221  qtChildren.pop_back();
222  children.pop_back();
223  auto newQtChildren = node->getChildren();
224  qtChildren.insert(qtChildren.end(), newQtChildren.begin(), newQtChildren.end());
225  auto newChildren = term->getChilds();
226  children.insert(children.end(), newChildren.begin(), newChildren.end());
227 
228  if (children.size() != qtChildren.size())
229  {
230  ARMARX_WARNING_S << "Size discrepancy while updating term";
231  return;
232  }
233 
234  TermNodePtr termNode = std::dynamic_pointer_cast<TermNode>(node);
235 
236  if (termNode && term)
237  {
238  emit termNode->newLiteralValue(term->getValue());
239  termNode->timerEventRun();
240  }
241  }
242  }
243 
244  }
245 
246  void ConditionViewerWidget::timerEvent(QTimerEvent* event)
247  {
248  if (event->timerId() == timerId)
249  {
250  std::unique_lock lock(dataMutex);
251  // retrieve conditions
252  activeConditions = controller->getActiveConditions();
253  pastConditions = controller->getPastConditions();
254 
255  // update model
256  activeConditionsModel->update(activeConditions);
257  pastConditionsModel->update(pastConditions);
258 
259  // resize view
260  ui.activeConditionsTableView->resizeColumnsToContents();
261  ui.pastConditionsTableView->resizeColumnsToContents();
262  }
263  }
264 }
LiteralImpl.h
armarx::ConditionRootPtr
IceInternal::Handle< ConditionRoot > ConditionRootPtr
Typedef of ConditionRootPtr as IceInternal::Handle<ConditionRoot> for convenience.
Definition: ConditionRoot.h:49
armarx::TermNodePtr
std::shared_ptr< TermNode > TermNodePtr
Definition: TermNode.h:42
list
list(APPEND SOURCES ${QT_RESOURCES}) set(COMPONENT_LIBS ArmarXGui ArmarXCoreObservers ArmarXCoreEigen3Variants PlotterController $
Definition: CMakeLists.txt:49
armarx::TreeNodePtr
std::shared_ptr< TreeNode > TreeNodePtr
Definition: TreeNode.h:40
armarx::ConditionViewerWidget::ConditionViewerWidget
ConditionViewerWidget(ConditionViewerWidgetController *controller)
Definition: ConditionViewerWidget.cpp:37
armarx::ConditionItemModel::reset
void reset()
Definition: ConditionItemModel.cpp:51
Term.h
armarx::ConditionViewerWidget::~ConditionViewerWidget
~ConditionViewerWidget() override
Definition: ConditionViewerWidget.cpp:72
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::ConditionItemModel
Definition: ConditionItemModel.h:36
armarx::ConditionViewerWidget::onConnect
void onConnect()
Definition: ConditionViewerWidget.cpp:80
controller
Definition: AddOperation.h:39
ConditionViewerWidget.h
armarx::ConditionViewerWidget::activeConditionItemSelected
void activeConditionItemSelected(const QItemSelection &, const QItemSelection &)
Definition: ConditionViewerWidget.cpp:121
armarx::TermTreeGraphicsScene
Definition: TermTreeGraphicsScene.h:32
armarx::ConditionViewerWidget::onDisconnect
void onDisconnect()
Definition: ConditionViewerWidget.cpp:106
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
armarx::ConditionViewerWidget::timerEvent
void timerEvent(QTimerEvent *event) override
Definition: ConditionViewerWidget.cpp:246
ConditionRoot.h
armarx::ConditionTreeFactory::createConditionTree
static TermNodePtr createConditionTree(TermTreeGraphicsScene *scene, QTableWidget *checksTable, const TermImplPtr &root, int conditionId, IceManagerPtr manager, ConditionHandlerInterfacePrx handler)
Definition: ConditionTreeFactory.h:37
armarx::PeriodicTask
Definition: ArmarXManager.h:70
CONDITION_ITEM_ID
#define CONDITION_ITEM_ID
Definition: ConditionItemModel.h:32
armarx::ConditionViewerWidgetController
With this widget conditions can be visualized.
Definition: ConditionViewerWidgetController.h:54
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ConditionViewerWidget::pastConditionItemSelected
void pastConditionItemSelected(const QItemSelection &, const QItemSelection &)
Definition: ConditionViewerWidget.cpp:142
armarx::TermImplPtr
IceInternal::Handle< TermImpl > TermImplPtr
Typedef of TermImplPtr as IceInternal::Handle<TermImpl> for convenience.
Definition: TermImpl.h:41
armarx::ConditionItemModel::update
void update(const ConditionRegistry &registry)
Definition: ConditionItemModel.cpp:33