TermNode.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::TermNode
17 * @author Kai Welke ( welke at kit dot edu)
18 * @date
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "TermNode.h"
25 
29 
30 
31 namespace armarx
32 {
33  const QSize TermNode::LiteralNodeSize(50, 50);
34  const QSize TermNode::ExpressionNodeSize(30, 30);
35  const QSize TermNode::ConditionRootNodeSize(50, 50);
36 
37  TermNode::TermNode(TermTreeGraphicsScene* scene, QTableWidget* checksTable, TermImplPtr term, int conditionId, IceManagerPtr manager, ConditionHandlerInterfacePrx handler)
38  : TreeNode(scene),
39  conditionId(conditionId),
40  manager(manager),
41  handler(handler)
42  {
43  this->term = term;
44  this->checksTable = checksTable;
45  this->scene = scene;
46 
47  graphicsItem = new TermNodeGraphicsItem(scene, this);
48 
49  // set size
50  switch (term->getType())
51  {
52  case eConditionRoot:
53  {
54  setSize(ConditionRootNodeSize);
55  graphicsItem->setPixmap(QPixmap(":/icons/arrow-right.png"));
56 
57  break;
58  }
59 
60  case eOperation:
61  {
62  setSize(ExpressionNodeSize);
63  graphicsItem->setText(OperationPtr::dynamicCast(term)->getOperationString().c_str());
64  break;
65  }
66 
67  case eLiteral:
68  {
69  setSize(LiteralNodeSize);
70  graphicsItem->setPixmap(QPixmap(":/icons/wave.png"));
71 
72  break;
73  }
74 
75  default:
76  break;
77  }
78 
79  // set tooltip
80  std::stringstream ss;
81  ss << term;
82  graphicsItem->setToolTip(ss.str().c_str());
83 
84  // set color
85  if (term->getValue())
86  {
87  graphicsItem->setColor(Qt::green);
88  }
89  else
90  {
91  graphicsItem->setColor(Qt::red);
92  }
93 
94 
95  // add to scene
96  scene->addItem(graphicsItem);
97  graphicsItem->hide();
98 
99  // update checks table for each literal
100  if (checksTable && (term->getType() == eLiteral))
101  {
102  updateChecksTable(term);
103  }
104 
105  connect(this, SIGNAL(newValueString(QString)), this, SLOT(updateValueString(QString)));
106  connect(this, SIGNAL(newLiteralValue(bool)), this, SLOT(setTermEvaluationState(bool)));
107 
108 
109  }
110 
112  {
113  }
114 
115  void TermNode::drawNode(QRectF boundingBox)
116  {
117  graphicsItem->setRect(boundingBox);
118  graphicsItem->show();
119  }
120 
121  void TermNode::drawEdge(QLineF line)
122  {
123  TreeNode::drawEdge(line);
124  }
125 
127  {
128  std::unique_lock lock(dataMutex);
129 
130  try
131  {
132 
133  VariantPtr var = VariantPtr::dynamicCast(curDatafield->getDataField());
134  auto valueString = var->getOutputValueOnly();
135  return QString::fromStdString(valueString);
136  }
137  catch (std::exception& e)
138  {
139  }
140 
141  return "Retrieval Failed";
142 
143  }
144 
146  {
147  setTermEvaluationState(term->getValue());
148  }
149 
151  {
152  std::unique_lock lock(dataMutex);
153 
154  if (truthy)
155  {
156  QTableWidgetItem* item = new QTableWidgetItem("true");
157  item->setBackground(Qt::green);
158  checksTable->setItem(literalRow, eStateCol, item);
159  graphicsItem->setColor(Qt::green);
160  }
161  else
162  {
163  QTableWidgetItem* item = new QTableWidgetItem("false");
164  item->setBackground(Qt::red);
165  checksTable->setItem(literalRow, eStateCol, item);
166  graphicsItem->setColor(Qt::red);
167  }
168 
169  }
170 
171 
172  void TermNode::updateChecksTable(TermImplPtr term)
173  {
174  std::unique_lock lock(dataMutex);
175 
176  LiteralImplPtr literal = LiteralImplPtr::dynamicCast(term);
177  CheckConfiguration config = literal->getCheckConfiguration();
178 
179  // insert new row
180  checksTable->insertRow(checksTable->rowCount());
181  literalRow = checksTable->rowCount() - 1;
182 
183  // set datafield value
184  DataFieldIdentifierPtr dataFieldIdentifier = DataFieldIdentifierPtr::dynamicCast(config.dataFieldIdentifier);
185 
186  if (dataFieldIdentifier)
187  {
188  literaldatafieldName = dataFieldIdentifier->getIdentifierStr().c_str();
189  checksTable->setItem(literalRow, eDataFieldCol, new QTableWidgetItem(dataFieldIdentifier->getIdentifierStr().c_str()));
190  ObserverInterfacePrx prx = manager->getProxy<ObserverInterfacePrx>(dataFieldIdentifier->getObserverName());
191 
192  try
193  {
194  if (prx->existsChannel(dataFieldIdentifier->channelName))
195  {
196  curDatafield = new DatafieldRef(prx, dataFieldIdentifier->channelName, dataFieldIdentifier->datafieldName);
197  }
198  }
199  catch (std::exception& e)
200  {
201  ARMARX_INFO << e.what();
202  }
203  }
204  else
205  {
206  curDatafield = NULL;
207  checksTable->setItem(literalRow, eDataFieldCol, new QTableWidgetItem("NULL"));
208  literaldatafieldName = "";
209  }
210 
211  ARMARX_INFO << "updating " << dataFieldIdentifier->getIdentifierStr();
212 
213  // set check type value
214  checksTable->setItem(literalRow, eCheckTypeCol, new QTableWidgetItem(config.checkName.c_str()));
215 
216  // set check parameters
217  ParameterList::iterator iter = config.checkParameters.begin();
218  std::stringstream parameters;
219 
220  while (iter != config.checkParameters.end())
221  {
222  VariantPtr var = VariantPtr::dynamicCast(*iter);
223  parameters << var << ", ";
224  iter++;
225  }
226 
227  checksTable->setItem(literalRow, eParametersCol, new QTableWidgetItem(parameters.str().c_str()));
228 
229  // set check state
231 
232  // connections
233  // from graphicsitem to table
234  connect(graphicsItem, SIGNAL(nodeActivated()), this, SLOT(graphicsItemClicked()));
235  connect(this, SIGNAL(literalClicked(int)), checksTable, SLOT(selectRow(int)));
236 
237  // from table to graphicsitem
238  connect(checksTable, SIGNAL(itemSelectionChanged()), this, SLOT(checksTableSelectionChanged()));
239 
240  // proper size
241  checksTable->resizeColumnsToContents();
242  }
243 
245  {
246  bool truthy = false;
247  {
248  std::unique_lock lock(dataMutex);
249  truthy = curDatafield && literaldatafieldName == curDatafield->getDataFieldIdentifier()->getIdentifierStr();
250  }
251 
252  if (truthy)
253  {
254  auto valueString = getDatafieldValue();
255  emit newValueString(valueString);
256  }
257  }
258 
260  {
261  if (term->getType() == eLiteral)
262  {
263  emit literalClicked(literalRow);
264  }
265  }
266 
268  {
269  QList<QTableWidgetItem*> selectedItems = checksTable->selectedItems();
270 
271  int size = selectedItems.size();
272 
273  if (size > 0)
274  {
275  QTableWidgetItem* widget = selectedItems.front();
276  int row = checksTable->row(widget);
277 
278  if (row == literalRow)
279  {
280  scene->resetActivation();
281  graphicsItem->activateNodeSilent();
282  }
283  }
284  }
285 
286  void TermNode::updateValueString(const QString& valueString)
287  {
288  std::unique_lock lock(dataMutex);
289  checksTable->setItem(literalRow, eCurrentValueCol, new QTableWidgetItem(valueString));
290  }
291 
292 }
LiteralImpl.h
armarx::TermNode::literalClicked
void literalClicked(int)
armarx::TermNode::eDataFieldCol
@ eDataFieldCol
Definition: TermNode.h:60
TermNode.h
armarx::TermNode::updateValueString
void updateValueString(const QString &valueString)
Definition: TermNode.cpp:286
armarx::TermNode::setTermEvaluationState
void setTermEvaluationState(TermImplPtr term)
Definition: TermNode.cpp:145
armarx::TermNodeGraphicsItem
Definition: TermNodeGraphicsItem.h:33
armarx::TermNode::eStateCol
@ eStateCol
Definition: TermNode.h:63
armarx::TermNodeGraphicsItem::activateNodeSilent
void activateNodeSilent()
Definition: TermNodeGraphicsItem.cpp:123
armarx::TermTreeGraphicsScene::resetActivation
void resetActivation()
Definition: TermTreeGraphicsScene.h:41
armarx::TermNodeGraphicsItem::setPixmap
void setPixmap(const QPixmap &pixmap)
Definition: TermNodeGraphicsItem.cpp:60
armarx::TermNode::timerEventRun
void timerEventRun()
Definition: TermNode.cpp:244
IceInternal::Handle< TermImpl >
armarx::TermNode::drawEdge
void drawEdge(QLineF line) override
Draws an edge to this node.
Definition: TermNode.cpp:121
armarx::TermNode::drawNode
void drawNode(QRectF boundingBox) override
Draws the node.
Definition: TermNode.cpp:115
armarx::VariantPtr
IceInternal::Handle< Variant > VariantPtr
Definition: Variant.h:42
armarx::TermNode::~TermNode
~TermNode() override
Definition: TermNode.cpp:111
armarx::TermNode::checksTableSelectionChanged
void checksTableSelectionChanged()
Definition: TermNode.cpp:267
armarx::TermNode::eParametersCol
@ eParametersCol
Definition: TermNode.h:62
armarx::TermTreeGraphicsScene
Definition: TermTreeGraphicsScene.h:32
armarx::TermNode::graphicsItemClicked
void graphicsItemClicked()
Definition: TermNode.cpp:259
armarx::TermNode::eCurrentValueCol
@ eCurrentValueCol
Definition: TermNode.h:64
armarx::TermNodeGraphicsItem::setText
void setText(const QString &text)
Definition: TermNodeGraphicsItem.cpp:46
armarx::red
QColor red()
Definition: StyleSheets.h:76
ConditionRoot.h
armarx::TreeNode::drawEdge
virtual void drawEdge(QLineF line)
Draws an edge to this node.
Definition: TreeNode.cpp:234
armarx::VariantType::DatafieldRef
const VariantTypeId DatafieldRef
Definition: DatafieldRef.h:169
Operations.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::TermNodeGraphicsItem::setColor
void setColor(const QColor &penColor)
Definition: TermNodeGraphicsItem.cpp:53
armarx::TermNode::TermNode
TermNode(TermTreeGraphicsScene *scene, QTableWidget *checksTable, TermImplPtr term, int conditionId, IceManagerPtr manager, ConditionHandlerInterfacePrx handler)
Definition: TermNode.cpp:37
IceUtil::Handle< IceManager >
TermNodeGraphicsItem.h
armarx::TermNode::newValueString
void newValueString(QString newValue)
armarx::TermNode::getDatafieldValue
QString getDatafieldValue()
Definition: TermNode.cpp:126
armarx::TreeNode::setSize
void setSize(QSize nodeSize)
Sets size of node.
Definition: TreeNode.h:107
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::TermNodeGraphicsItem::setRect
void setRect(QRectF boundingBox)
Definition: TermNodeGraphicsItem.cpp:38
armarx::green
QColor green()
Definition: StyleSheets.h:72
armarx::TermNode::newLiteralValue
void newLiteralValue(bool truthy)
armarx::TreeNode
Definition: TreeNode.h:43
armarx::TermNode::eCheckTypeCol
@ eCheckTypeCol
Definition: TermNode.h:61