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 "StyleSheets.h"
25 
26 #include <QCheckBox>
27 
28 namespace 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 
49  {
50  delete ui;
51  }
52 
53  void SensorDevicesWidget::sensorDeviceStatusChanged(const SensorDeviceStatusSeq& allStatus)
54  {
55  std::unique_lock<std::recursive_timed_mutex> guard {mutex, std::defer_lock};
56  if (!guard.try_lock_for(std::chrono::microseconds(100)))
57  {
58  return;
59  }
60  for (const auto& status : allStatus)
61  {
62  if (statusUpdates[status.deviceName].timestampUSec < status.timestampUSec)
63  {
64  statusUpdates[status.deviceName] = status;
65  if (doMetaCall)
66  {
67  QMetaObject::invokeMethod(this, "updateContent", Qt::QueuedConnection);
68  }
69  }
70  }
71  }
72 
74  {
75  entries.clear();
76  statusUpdates.clear();
77  resetData.clear();
78  }
79 
81  {
82  for (const auto& pair : statusUpdates)
83  {
84  if (!entries.count(pair.second.deviceName))
85  {
86  add(robotUnit->getSensorDeviceDescription(pair.second.deviceName));
87  }
88  entries.at(pair.second.deviceName)->update(pair.second);
89  }
90  statusUpdates.clear();
91  }
92 
94  {
95  auto temp = robotUnit->getSensorDeviceDescriptions();
96  {
97  std::unique_lock<std::recursive_timed_mutex> guard {mutex};
98  resetData = std::move(temp);
99  }
100  }
101 
103  {
104  if (resetData.empty())
105  {
106  return true;
107  }
108  add(resetData.back());
109  resetData.pop_back();
110  return false;
111  }
112 
113  void SensorDevicesWidget::add(const SensorDeviceDescription& desc)
114  {
115  std::unique_lock<std::recursive_timed_mutex> guard {mutex};
116  if (!entries.count(desc.deviceName))
117  {
118  entries[desc.deviceName] = new SensorDevicesWidgetEntry(*this, *(ui->treeWidget), desc);
119  }
120  }
121 
122  SensorDevicesWidgetEntry::SensorDevicesWidgetEntry(SensorDevicesWidget& parent, QTreeWidget& treeWidget, const SensorDeviceDescription& desc):
123  QObject {&parent}
124  {
125  header = new QTreeWidgetItem;
126  treeWidget.addTopLevelItem(header);
127  header->setText(SensorDevicesWidget::idxName, QString::fromStdString(desc.deviceName));
128  //tags
129  {
130  QCheckBox* boxTags = new QCheckBox{QString::number(desc.tags.size()) + " Tags"};
131  treeWidget.setItemWidget(header, SensorDevicesWidget::idxTags, boxTags);
132  boxTags->setStyleSheet(checkboxStyleSheet());
133  boxTags->setChecked(false);
134  connect(boxTags, SIGNAL(clicked(bool)), this, SLOT(hideTagList(bool)));
135  for (const auto& tag : desc.tags)
136  {
137  QTreeWidgetItem* child = new QTreeWidgetItem {{QString::fromStdString(tag)}};
138  treeWidget.addTopLevelItem(child);
139  tags.emplace_back(child);
140  }
141  }
142  header->setText(SensorDevicesWidget::idxValType, QString::fromStdString(desc.sensorValueType));
143  value = new VariantWidget;
144  QTreeWidgetItem* valchild = new QTreeWidgetItem;
145  header->addChild(valchild);
146  treeWidget.setItemWidget(valchild, SensorDevicesWidget::idxValue, value);
147  }
148 
149  void SensorDevicesWidgetEntry::update(const SensorDeviceStatus& status)
150  {
151  value->update(status.sensorValue);
152  value->layout()->setContentsMargins(0, 0, 0, 0);
153  }
154 
155  bool SensorDevicesWidgetEntry::matchName(const QString& name)
156  {
157  return header->text(SensorDevicesWidget::idxName).contains(name, Qt::CaseInsensitive);
158  }
159 
160  bool SensorDevicesWidgetEntry::matchTag(const QString& tag)
161  {
162  for (const auto& tagit : tags)
163  {
164  if (tagit->text(SensorDevicesWidget::idxTags).contains(tag, Qt::CaseInsensitive))
165  {
166  return true;
167  }
168  }
169  return false;
170  }
171 
173  {
174  return header->text(SensorDevicesWidget::idxValType).contains(type, Qt::CaseInsensitive);
175  }
176 
178  {
179  if (!vis)
180  {
181  hideTagList(true);
182  }
183  header->setHidden(!vis);
184  }
185 
186  void SensorDevicesWidgetEntry::hideTagList(bool hide)
187  {
188  for (QTreeWidgetItem* tag : tags)
189  {
190  tag->setHidden(hide);
191  }
192  }
193 
194 }
armarx::SensorDevicesWidget::clearAll
void clearAll() override
Definition: SensorDevicesWidget.cpp:73
armarx::checkboxStyleSheet
QString checkboxStyleSheet()
Definition: StyleSheets.h:30
armarx::VariantWidget::update
void update(const VariantBasePtr &v)
Definition: VariantWidget.cpp:423
armarx::SensorDevicesWidget::getResetData
void getResetData() override
Definition: SensorDevicesWidget.cpp:93
armarx::SensorDevicesWidget
Definition: SensorDevicesWidget.h:49
armarx::SensorDevicesWidget::idxValue
static constexpr int idxValue
Definition: SensorDevicesWidget.h:72
armarx::SensorDevicesWidgetEntry::setVisible
void setVisible(bool vis)
Definition: SensorDevicesWidget.cpp:177
armarx::SensorDevicesWidget::~SensorDevicesWidget
~SensorDevicesWidget() override
Definition: SensorDevicesWidget.cpp:48
armarx::SensorDevicesWidget::addOneFromResetData
bool addOneFromResetData() override
Definition: SensorDevicesWidget.cpp:102
armarx::SensorDevicesWidgetEntry::update
void update(const SensorDeviceStatus &status)
Definition: SensorDevicesWidget.cpp:149
armarx::RobotUnitWidgetTemplateBase< Ui::SensorDevicesWidget >::ui
Ui::SensorDevicesWidget * ui
Definition: RobotUnitWidgetBase.h:117
armarx::SensorDevicesWidget::SensorDevicesWidget
SensorDevicesWidget(QWidget *parent=0)
Definition: SensorDevicesWidget.cpp:30
armarx::RobotUnitWidgetBase::doMetaCall
std::atomic_bool doMetaCall
Definition: RobotUnitWidgetBase.h:87
armarx::status
status
Definition: FiniteStateMachine.h:259
armarx::SensorDevicesWidget::idxValType
static constexpr int idxValType
Definition: SensorDevicesWidget.h:71
armarx::SensorDevicesWidgetEntry::matchTag
bool matchTag(const QString &tag)
Definition: SensorDevicesWidget.cpp:160
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::RobotUnitWidgetBase::robotUnit
RobotUnitInterfacePrx robotUnit
Definition: RobotUnitWidgetBase.h:81
armarx::SensorDevicesWidgetEntry::matchName
bool matchName(const QString &name)
Definition: SensorDevicesWidget.cpp:155
armarx::RobotUnitWidgetBase::mutex
std::recursive_timed_mutex mutex
Definition: RobotUnitWidgetBase.h:82
armarx::RobotUnitWidgetTemplateBase
Definition: RobotUnitWidgetBase.h:93
armarx::SensorDevicesWidget::doContentUpdate
void doContentUpdate() override
Definition: SensorDevicesWidget.cpp:80
armarx::SensorDevicesWidget::idxName
static constexpr int idxName
Definition: SensorDevicesWidget.h:69
SensorDevicesWidget.h
armarx::SensorDevicesWidget::sensorDeviceStatusChanged
void sensorDeviceStatusChanged(const SensorDeviceStatusSeq &allStatus)
Definition: SensorDevicesWidget.cpp:53
armarx::SensorDevicesWidgetEntry::matchValueType
bool matchValueType(const QString &type)
Definition: SensorDevicesWidget.cpp:172
armarx::SensorDevicesWidgetEntry::SensorDevicesWidgetEntry
SensorDevicesWidgetEntry(SensorDevicesWidget &parent, QTreeWidget &treeWidget, const SensorDeviceDescription &desc)
Definition: SensorDevicesWidget.cpp:122
StyleSheets.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::SensorDevicesWidget::idxTags
static constexpr int idxTags
Definition: SensorDevicesWidget.h:70