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 */
25
26#include <QList>
27
31
34
35namespace 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"
58 << "checktype"
59 << "parameters"
60 << "state"
61 << "current value";
62 ui.checksTableWidget->setColumnCount(5);
63 ui.checksTableWidget->setHorizontalHeaderLabels(checksHeader);
64 ui.checksTableWidget->update();
65
66 // item selection
67 QItemSelectionModel* activeSelectionModel = ui.activeConditionsTableView->selectionModel();
68 QItemSelectionModel* pastSelectionModel = ui.pastConditionsTableView->selectionModel();
69
70 // connections
71 connect(activeSelectionModel,
72 SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
73 this,
74 SLOT(activeConditionItemSelected(const QItemSelection&, const QItemSelection&)));
75 connect(pastSelectionModel,
76 SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
77 this,
78 SLOT(pastConditionItemSelected(const QItemSelection&, const QItemSelection&)));
79 connect(
80 scene, SIGNAL(graphicsSceneClicked()), ui.checksTableWidget, SLOT(clearSelection()));
81 }
82
84 {
85 if (updateTask)
86 {
87 updateTask->stop();
88 }
89 }
90
91 void
93 {
94 //clear scene and checks
95 scene->clear();
96 root.reset();
97 ui.checksTableWidget->clearContents();
98 ui.checksTableWidget->setRowCount(0);
99
100 //reset the models
101 activeConditionsModel->reset();
102 pastConditionsModel->reset();
103
104 // resize tables with active and past conditions
105 ui.activeConditionsTableView->resizeColumnsToContents();
106 ui.pastConditionsTableView->resizeColumnsToContents();
107
108 // starting timer for periodic task
109 while (timerId == 0)
110 {
111 timerId = startTimer(500);
112 }
113
115 this, &ConditionViewerWidget::updateLiterals, 200, false, "LiteralsUpdater");
116 updateTask->start();
117 }
118
119 void
121 {
122 killTimer(timerId);
123 timerId = 0;
124 if (updateTask)
125 {
126 updateTask->stop();
127 }
128 }
129
130 // ****************************************************************
131 // slots
132 // ****************************************************************
133
134
135 void
137 const QItemSelection& deselected)
138 {
139 std::unique_lock lock(dataMutex);
140
141 if (selected.indexes().size() == 0)
142 {
143 return;
144 }
145
146 QModelIndexList list = selected.indexes();
147 QStandardItem* i = activeConditionsModel->itemFromIndex(list.at(0));
148
149
150 ConditionRootPtr condition = conditionFromItem(i, activeConditions);
151
152 if (condition)
153 {
154 updateCondition(i->data(CONDITION_ITEM_ID).toInt(), condition);
155 }
156 }
157
158 void
160 const QItemSelection& deselected)
161 {
162 std::unique_lock lock(dataMutex);
163
164 if (selected.indexes().size() == 0)
165 {
166 return;
167 }
168
169 QModelIndexList list = selected.indexes();
170 QStandardItem* i = pastConditionsModel->itemFromIndex(list.at(0));
171
172 ConditionRootPtr condition = conditionFromItem(i, pastConditions);
173
174 if (condition)
175 {
176 updateCondition(i->data(CONDITION_ITEM_ID).toInt(), condition);
177 }
178 }
179
181 ConditionViewerWidget::conditionFromItem(QStandardItem* selectedItem,
182 const ConditionRegistry& registry)
183 {
184 ConditionRegistry::const_iterator iter =
185 registry.find(selectedItem->data(CONDITION_ITEM_ID).toInt());
186
187 if (iter != registry.end())
188 {
189 ConditionRootPtr conditionRoot = ConditionRootPtr::dynamicCast(iter->second);
190 return conditionRoot;
191 }
192
193 return ConditionRootPtr();
194 }
195
196 void
197 ConditionViewerWidget::updateCondition(int conditionId, ConditionRootPtr& condition)
198 {
199 std::unique_lock lock(dataMutex);
200 scene->clear();
201 ui.checksTableWidget->clearContents();
202 ui.checksTableWidget->setRowCount(0);
203 if (controller->handler)
204 {
206 ui.checksTableWidget,
207 condition,
208 conditionId,
209 controller->getIceManager(),
210 controller->handler);
211 root->update();
212 }
213 }
214
215 void
216 ConditionViewerWidget::updateLiterals()
217 {
218 if (!root)
219 {
220 return;
221 }
222 std::vector<TreeNodePtr> tempChildNodes;
223 std::vector<TreeNodePtr> qtChildren;
224 int conditionId = -1;
225 {
226 std::unique_lock lock(dataMutex);
227
228 qtChildren.push_back(root);
229 tempChildNodes = root->getChildren();
230 conditionId = root->conditionId;
231 qtChildren.insert(qtChildren.end(), tempChildNodes.begin(), tempChildNodes.end());
232 auto result = controller->handler->getCondition(conditionId);
233 if (!result)
234 {
235 return;
236 }
237 ConditionRootPtr newRoot = ConditionRootPtr::dynamicCast(result);
238 TermImplSequence children;
239 children.push_back(newRoot);
240 auto temp2 = newRoot->getChilds();
241 children.insert(children.end(), temp2.begin(), temp2.end());
242
243 while (qtChildren.size() > 0)
244 {
245 TreeNodePtr node = *qtChildren.rbegin();
246 TermImplPtr term = TermImplPtr::dynamicCast(*children.rbegin());
247 qtChildren.pop_back();
248 children.pop_back();
249 auto newQtChildren = node->getChildren();
250 qtChildren.insert(qtChildren.end(), newQtChildren.begin(), newQtChildren.end());
251 auto newChildren = term->getChilds();
252 children.insert(children.end(), newChildren.begin(), newChildren.end());
253
254 if (children.size() != qtChildren.size())
255 {
256 ARMARX_WARNING_S << "Size discrepancy while updating term";
257 return;
258 }
259
260 TermNodePtr termNode = std::dynamic_pointer_cast<TermNode>(node);
261
262 if (termNode && term)
263 {
264 emit termNode->newLiteralValue(term->getValue());
265 termNode->timerEventRun();
266 }
267 }
268 }
269 }
270
271 void
273 {
274 if (event->timerId() == timerId)
275 {
276 std::unique_lock lock(dataMutex);
277 // retrieve conditions
278 activeConditions = controller->getActiveConditions();
279 pastConditions = controller->getPastConditions();
280
281 // update model
282 activeConditionsModel->update(activeConditions);
283 pastConditionsModel->update(pastConditions);
284
285 // resize view
286 ui.activeConditionsTableView->resizeColumnsToContents();
287 ui.pastConditionsTableView->resizeColumnsToContents();
288 }
289 }
290} // namespace armarx
#define CONDITION_ITEM_ID
static TermNodePtr createConditionTree(TermTreeGraphicsScene *scene, QTableWidget *checksTable, const TermImplPtr &root, int conditionId, IceManagerPtr manager, ConditionHandlerInterfacePrx handler)
With this widget conditions can be visualized.
void pastConditionItemSelected(const QItemSelection &, const QItemSelection &)
void timerEvent(QTimerEvent *event) override
ConditionViewerWidget(ConditionViewerWidgetController *controller)
void activeConditionItemSelected(const QItemSelection &, const QItemSelection &)
The periodic task executes one thread method repeatedly using the time period specified in the constr...
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< TreeNode > TreeNodePtr
Definition TreeNode.h:40
std::shared_ptr< TermNode > TermNodePtr
Definition TermNode.h:42
IceInternal::Handle< TermImpl > TermImplPtr
Typedef of TermImplPtr as IceInternal::Handle<TermImpl> for convenience.
Definition TermImpl.h:41
IceInternal::Handle< ConditionRoot > ConditionRootPtr
Typedef of ConditionRootPtr as IceInternal::Handle<ConditionRoot> for convenience.