RobotUnitWidgetBase.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 "RobotUnitWidgetBase.h"
23 
24 #include <thread>
25 
28 
29 namespace armarx
30 {
31  RobotUnitWidgetBase::RobotUnitWidgetBase(QString name, QWidget* parent) : QWidget(parent)
32  {
33  setObjectName(name);
34  }
35 
37  {
38  while (_resetThread.joinable() &&
39  !_resetThread.try_join_for(boost::chrono::milliseconds{100}))
40  {
41  ARMARX_INFO << "Joining reset thread";
42  }
43  }
44 
45  void
47  {
48  if (isResetting)
49  {
50  return;
51  }
52  std::unique_lock<std::recursive_timed_mutex> guard{mutex};
53  resetCount = 0;
54  getResettigLabel().setText("Resetting... " + QString::number(resetCount));
55  getStackedWidget().setCurrentIndex(0);
56  if (!robotUnit)
57  {
58  return;
59  }
61  }
62 
63  void
65  {
66  isResetting = true;
67  gotResetData = false;
68  std::unique_lock<std::recursive_timed_mutex> guard{mutex};
69  robotUnit = ru;
70  QMetaObject::invokeMethod(this, "doReset", Qt::QueuedConnection);
71  }
72 
73  void
75  {
76  doMetaCall = visible;
77  if (visible)
78  {
79  QMetaObject::invokeMethod(this, "updateContent", Qt::QueuedConnection);
80  }
81  QWidget::setVisible(visible);
82  }
83 
84  void
85  RobotUnitWidgetBase::doReset()
86  {
87  std::unique_lock<std::recursive_timed_mutex> guard{mutex};
88  clearAll();
89  getTreeWidget().clear();
90  addFilter();
91  if (robotUnit)
92  {
93  if (!_resetThread.joinable() ||
94  _resetThread.try_join_for(boost::chrono::milliseconds{10}))
95  {
96  resetTimerId = startTimer(0);
97  getStackedWidget().setCurrentIndex(1);
98  _resetThread = boost::thread{
99  [&]
100  {
101  ARMARX_INFO << "Resetting of widget '" << objectName().toStdString()
102  << "' started";
103  try
104  {
105  getResetData();
106  }
107  catch (...)
108  {
109  ARMARX_WARNING << "Resetting of widget '" << objectName().toStdString()
110  << "' failed: " << armarx::GetHandledExceptionString();
111  }
112  gotResetData = true;
113  if (doMetaCall)
114  {
115  QMetaObject::invokeMethod(this, "updateContent", Qt::QueuedConnection);
116  }
117  ARMARX_INFO << "Resetting of widget '" << objectName().toStdString()
118  << "' done!";
119  }};
120  }
121  }
122  else
123  {
124  gotResetData = true;
125  if (doMetaCall)
126  {
127  QMetaObject::invokeMethod(this, "updateContent", Qt::QueuedConnection);
128  }
129  }
130  }
131 
132  void
134  {
135  if (isResetting && gotResetData)
136  {
137  getResettigLabel().setText("Resetting... " + QString::number(++resetCount));
139  if (!isResetting)
140  {
141  killTimer(resetTimerId);
142  resetTimerId = 0;
143  if (doMetaCall)
144  {
145  QMetaObject::invokeMethod(this, "updateContent", Qt::QueuedConnection);
146  }
147  }
148  }
149  }
150 } // namespace armarx
RobotUnitWidgetBase.h
armarx::RobotUnitWidgetBase::doContentUpdate
virtual void doContentUpdate()=0
armarx::RobotUnitWidgetBase::addFilter
virtual void addFilter()
Definition: RobotUnitWidgetBase.h:78
armarx::RobotUnitWidgetBase::addOneFromResetData
virtual bool addOneFromResetData()=0
armarx::RobotUnitWidgetBase::~RobotUnitWidgetBase
~RobotUnitWidgetBase()
Definition: RobotUnitWidgetBase.cpp:36
armarx::RobotUnitWidgetBase::doMetaCall
std::atomic_bool doMetaCall
Definition: RobotUnitWidgetBase.h:99
armarx::GetHandledExceptionString
std::string GetHandledExceptionString()
Definition: Exception.cpp:165
armarx::RobotUnitWidgetBase::resetTimerId
int resetTimerId
Definition: RobotUnitWidgetBase.h:96
armarx::RobotUnitWidgetBase::robotUnit
RobotUnitInterfacePrx robotUnit
Definition: RobotUnitWidgetBase.h:93
armarx::RobotUnitWidgetBase::updateContent
void updateContent()
Definition: RobotUnitWidgetBase.cpp:46
armarx::RobotUnitWidgetBase::gotResetData
std::atomic_bool gotResetData
Definition: RobotUnitWidgetBase.h:95
armarx::RobotUnitWidgetBase::mutex
std::recursive_timed_mutex mutex
Definition: RobotUnitWidgetBase.h:94
armarx::RobotUnitWidgetBase::getStackedWidget
virtual QStackedWidget & getStackedWidget()=0
armarx::RobotUnitWidgetBase::timerEvent
void timerEvent(QTimerEvent *) override
Definition: RobotUnitWidgetBase.cpp:133
armarx::RobotUnitWidgetBase::RobotUnitWidgetBase
RobotUnitWidgetBase(QString name, QWidget *parent)
Definition: RobotUnitWidgetBase.cpp:31
armarx::RobotUnitWidgetBase::getResettigLabel
virtual QLabel & getResettigLabel()=0
armarx::RobotUnitWidgetBase::setVisible
void setVisible(bool visible) override
Definition: RobotUnitWidgetBase.cpp:74
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::RobotUnitWidgetBase::getTreeWidget
virtual QTreeWidget & getTreeWidget()=0
IceInternal::ProxyHandle<::IceProxy::armarx::RobotUnitInterface >
armarx::RobotUnitWidgetBase::isResetting
std::atomic_bool isResetting
Definition: RobotUnitWidgetBase.h:98
armarx::RobotUnitWidgetBase::clearAll
virtual void clearAll()=0
armarx::RobotUnitWidgetBase::reset
void reset(RobotUnitInterfacePrx ru)
Definition: RobotUnitWidgetBase.cpp:64
Logging.h
armarx::RobotUnitWidgetBase::resetCount
int resetCount
Definition: RobotUnitWidgetBase.h:97
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::RobotUnitWidgetBase::getResetData
virtual void getResetData()=0
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
Exception.h