SensorDevicesWidget.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 RobotAPI::gui-plugins::RobotUnitPlugin
17 * \author Raphael Grimm ( raphael dot grimm at kit dot edu )
18 * \date 2017
19 * \copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22#include "SensorDevicesWidget.h"
23
24#include <QCheckBox>
25
26#include "StyleSheets.h"
27
28namespace armarx
29{
31 RobotUnitWidgetTemplateBase("SensorDevicesWidget", parent)
32 {
33 ui->treeWidget->setColumnCount(4);
34 QTreeWidgetItem* head = ui->treeWidget->headerItem();
35 head->setText(idxName, "Name");
36 head->setText(idxTags, "Tags");
37 head->setText(idxValType, "Value Type");
38 head->setText(idxValue, "Value");
39 head->setToolTip(idxName, "The control device's name");
40 head->setToolTip(idxTags, "The control device's tags");
41 head->setToolTip(idxValType, "The sensor value's' type");
42 head->setToolTip(idxValue, "The sensor value");
43 ui->treeWidget->setColumnWidth(idxName, 150);
44 ui->treeWidget->setColumnWidth(idxTags, 100);
45 ui->treeWidget->setColumnWidth(idxValType, 450);
46 }
47
52
53 void
54 SensorDevicesWidget::sensorDeviceStatusChanged(const SensorDeviceStatusSeq& allStatus)
55 {
56 std::unique_lock<std::recursive_timed_mutex> guard{mutex, std::defer_lock};
57 if (!guard.try_lock_for(std::chrono::microseconds(100)))
58 {
59 return;
60 }
61 for (const auto& status : allStatus)
62 {
63 if (statusUpdates[status.deviceName].timestampUSec < status.timestampUSec)
64 {
65 statusUpdates[status.deviceName] = status;
66 if (doMetaCall)
67 {
68 QMetaObject::invokeMethod(this, "updateContent", Qt::QueuedConnection);
69 }
70 }
71 }
72 }
73
74 void
76 {
77 entries.clear();
78 statusUpdates.clear();
79 resetData.clear();
80 }
81
82 void
84 {
85 for (const auto& pair : statusUpdates)
86 {
87 if (!entries.count(pair.second.deviceName))
88 {
89 add(robotUnit->getSensorDeviceDescription(pair.second.deviceName));
90 }
91 entries.at(pair.second.deviceName)->update(pair.second);
92 }
93 statusUpdates.clear();
94 }
95
96 void
98 {
99 auto temp = robotUnit->getSensorDeviceDescriptions();
100 {
101 std::unique_lock<std::recursive_timed_mutex> guard{mutex};
102 resetData = std::move(temp);
103 }
104 }
105
106 bool
108 {
109 if (resetData.empty())
110 {
111 return true;
112 }
113 add(resetData.back());
114 resetData.pop_back();
115 return false;
116 }
117
118 void
119 SensorDevicesWidget::add(const SensorDeviceDescription& desc)
120 {
121 std::unique_lock<std::recursive_timed_mutex> guard{mutex};
122 if (!entries.count(desc.deviceName))
123 {
124 entries[desc.deviceName] = new SensorDevicesWidgetEntry(*this, *(ui->treeWidget), desc);
125 }
126 }
127
129 QTreeWidget& treeWidget,
130 const SensorDeviceDescription& desc) :
131 QObject{&parent}
132 {
133 header = new QTreeWidgetItem;
134 treeWidget.addTopLevelItem(header);
135 header->setText(SensorDevicesWidget::idxName, QString::fromStdString(desc.deviceName));
136 //tags
137 {
138 QCheckBox* boxTags = new QCheckBox{QString::number(desc.tags.size()) + " Tags"};
139 treeWidget.setItemWidget(header, SensorDevicesWidget::idxTags, boxTags);
140 boxTags->setStyleSheet(checkboxStyleSheet());
141 boxTags->setChecked(false);
142 connect(boxTags, SIGNAL(clicked(bool)), this, SLOT(hideTagList(bool)));
143 for (const auto& tag : desc.tags)
144 {
145 QTreeWidgetItem* child = new QTreeWidgetItem{{QString::fromStdString(tag)}};
146 treeWidget.addTopLevelItem(child);
147 tags.emplace_back(child);
148 }
149 }
150 header->setText(SensorDevicesWidget::idxValType,
151 QString::fromStdString(desc.sensorValueType));
152 value = new VariantWidget;
153 QTreeWidgetItem* valchild = new QTreeWidgetItem;
154 header->addChild(valchild);
155 treeWidget.setItemWidget(valchild, SensorDevicesWidget::idxValue, value);
156 }
157
158 void
159 SensorDevicesWidgetEntry::update(const SensorDeviceStatus& status)
160 {
161 value->update(status.sensorValue);
162 value->layout()->setContentsMargins(0, 0, 0, 0);
163 }
164
165 bool
167 {
168 return header->text(SensorDevicesWidget::idxName).contains(name, Qt::CaseInsensitive);
169 }
170
171 bool
173 {
174 for (const auto& tagit : tags)
175 {
176 if (tagit->text(SensorDevicesWidget::idxTags).contains(tag, Qt::CaseInsensitive))
177 {
178 return true;
179 }
180 }
181 return false;
182 }
183
184 bool
186 {
187 return header->text(SensorDevicesWidget::idxValType).contains(type, Qt::CaseInsensitive);
188 }
189
190 void
192 {
193 if (!vis)
194 {
195 hideTagList(true);
196 }
197 header->setHidden(!vis);
198 }
199
200 void
201 SensorDevicesWidgetEntry::hideTagList(bool hide)
202 {
203 for (QTreeWidgetItem* tag : tags)
204 {
205 tag->setHidden(hide);
206 }
207 }
208
209} // namespace armarx
RobotUnitInterfacePrx robotUnit
std::recursive_timed_mutex mutex
bool matchValueType(const QString &type)
SensorDevicesWidgetEntry(SensorDevicesWidget &parent, QTreeWidget &treeWidget, const SensorDeviceDescription &desc)
void update(const SensorDeviceStatus &status)
void sensorDeviceStatusChanged(const SensorDeviceStatusSeq &allStatus)
static constexpr int idxValType
This file offers overloads of toIce() and fromIce() functions for STL container types.
QString checkboxStyleSheet()
Definition StyleSheets.h:31