RemoteGuiComponentPlugin.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 ArmarXGui::ArmarXObjects::ArmarXGuiComponentPlugins
17  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * @date 2019
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
26 
27 
28 namespace armarx::plugins
29 {
31  const RemoteGui::WidgetPtr& widget,
33  unsigned long guiTaskPeriod)
34  {
35  createOrUpdateTab("", widget, guiTaskFnc, guiTaskPeriod);
36  }
37 
39  const std::string& name,
40  const RemoteGui::WidgetPtr& widget,
42  unsigned long guiTaskPeriod)
43  {
44  std::lock_guard g{_tabsMutex};
45  if (hasTab(name))
46  {
47  ARMARX_INFO << "Replacing the tab " << tabName(name);
48  }
49  auto& data = _tabs[name];
50  if (data.task)
51  {
52  data.task->stop();
53  data.task = nullptr;
54  }
55  data.fnc = std::move(guiTaskFnc);
56  data.taskPeriod = guiTaskPeriod;
57  data.widget = widget;
58  create(name, data);
59  }
60 
61  void RemoteGuiComponentPlugin::createOrUpdateTab(const std::string& name, const RemoteGui::WidgetPtr& widget,
62  unsigned long guiTaskPeriod)
63  {
64  std::lock_guard g{_tabsMutex};
65  if (!hasTab(name))
66  {
67  throw armarx::LocalException("Can only update Tab with a name for an already existing Tab");
68  }
69  auto& data = _tabs[name];
70  if (data.task)
71  {
72  data.task->stop();
73  data.task = nullptr;
74  }
75  data.taskPeriod = guiTaskPeriod;
76  data.widget = widget;
77  create(name, data);
78  }
79 
81  {
83  std::lock_guard g{_tabsMutex};
84  return _tabs.at(name).tab;
85  }
86 
87  bool RemoteGuiComponentPlugin::hasTab(const std::string& name)
88  {
89  std::lock_guard g{_tabsMutex};
90  return _tabs.count(name);
91  }
92 
93  void RemoteGuiComponentPlugin::removeTab(const std::string& name)
94  {
95  std::lock_guard g{_tabsMutex};
96  if (_tabs.count(name))
97  {
98  auto& data = _tabs.at(name);
99  remove(name, data);
100  }
101  _tabs.erase(name);
102  }
103 
104  const RemoteGuiInterfacePrx& RemoteGuiComponentPlugin::getRemoteGui() const
105  {
106  return _remoteGui;
107  }
108 
110  {
111  if (!_remoteGui)
112  {
113  parent<Component>().usingProxyFromProperty(makePropertyName(_propertyName));
114  }
115  }
116 
118  {
119  if (!_remoteGui)
120  {
121  parent<Component>().getProxyFromProperty(_remoteGui, makePropertyName(_propertyName));
122  }
123  }
125  {
126  std::lock_guard g{_tabsMutex};
127  for (auto& [name, data] : _tabs)
128  {
129  if (data.task)
130  {
131  continue;
132  }
134  create(name, data);
135  }
136  }
137 
139  {
140  std::lock_guard g{_tabsMutex};
141  for (auto& [name, data] : _tabs)
142  {
143  if (data.task)
144  {
145  data.task->stop();
146  data.task = nullptr;
147  }
148  remove(name, data);
149  }
150  _remoteGui = nullptr;
151  }
152 
154  {
155  if (!properties->hasDefinition(makePropertyName(_propertyName)))
156  {
157  properties->defineOptionalProperty<std::string>(
158  makePropertyName(_propertyName),
159  "RemoteGuiProvider",
160  "Name of the remote gui provider");
161  }
162  }
163 
164  std::string RemoteGuiComponentPlugin::tabName(const std::string& name)
165  {
166  return parent<ManagedIceObject>().getName() + (name.empty() ? "" : "_" + name);
167  }
168 
169  void RemoteGuiComponentPlugin::create(const std::string& name, RemoteGuiComponentPlugin::TabData& td)
170  {
171  {
172  const auto state = parent<ManagedIceObject>().getState();
173  if (
174  state != ManagedIceObjectState::eManagedIceObjectStarting &&
175  state != ManagedIceObjectState::eManagedIceObjectStarted
176  )
177  {
178  return;
179  }
180  }
181  ARMARX_CHECK_NOT_NULL(_remoteGui);
182  if (td.task)
183  {
184  td.task->stop();
185  td.task = nullptr;
186  }
187  const auto tabname = tabName(name);
188  td.task = new SimplePeriodicTask<>([&td] {td.fnc(td.tab);}, td.taskPeriod);
189  _remoteGui->createTab(tabname, td.widget);
190  td.tab = RemoteGui::TabProxy(_remoteGui, tabname);
191  td.task->start();
192  }
193  void RemoteGuiComponentPlugin::remove(const std::string& name, RemoteGuiComponentPlugin::TabData& td)
194  {
195  if (td.task)
196  {
197  td.task->stop();
198  td.task = nullptr;
199  }
200  try
201  {
202  const auto tabname = tabName(name);
203  ARMARX_INFO << "Removing tab: " << tabname;
204  _remoteGui->removeTab(tabname);
205  }
206  catch (...) {}
207  }
208 }
209 
210 namespace armarx
211 {
213  {
214  addPlugin(_remoteGuiComponentPlugin);
215  }
216 
218  {
219  return getRemoteGuiPlugin().getTab(name);
220  }
221 
222  bool RemoteGuiComponentPluginUser::hasRemoteGuiTab(const std::string& name)
223  {
224  return getRemoteGuiPlugin().hasTab(name);
225  }
226 
228  {
230  }
231 
232  const RemoteGuiInterfacePrx& RemoteGuiComponentPluginUser::getRemoteGui() const
233  {
234  return getRemoteGuiPlugin().getRemoteGui();
235  }
237  {
238  return *_remoteGuiComponentPlugin;
239  }
241  {
242  return *_remoteGuiComponentPlugin;
243  }
244 }
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::plugins::RemoteGuiComponentPlugin::hasTab
bool hasTab(const std::string &name="")
Definition: RemoteGuiComponentPlugin.cpp:87
armarx::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObject.h:182
armarx::plugins::RemoteGuiComponentPlugin::removeTab
void removeTab(const std::string &name="")
Definition: RemoteGuiComponentPlugin.cpp:93
armarx::plugins::RemoteGuiComponentPlugin::postOnConnectComponent
void postOnConnectComponent() override
Definition: RemoteGuiComponentPlugin.cpp:124
armarx::RemoteGui::TabProxy
Definition: WidgetProxy.h:17
armarx::plugins::RemoteGuiComponentPlugin::createOrUpdateTab
void createOrUpdateTab(const RemoteGui::WidgetPtr &widget, TabTask guiTaskFnc, unsigned long guiTaskPeriod=10)
Definition: RemoteGuiComponentPlugin.cpp:30
armarx::plugins::RemoteGuiComponentPlugin::postCreatePropertyDefinitions
void postCreatePropertyDefinitions(PropertyDefinitionsPtr &properties) override
Definition: RemoteGuiComponentPlugin.cpp:153
ARMARX_CHECK_IS_NULL
#define ARMARX_CHECK_IS_NULL(ptr)
This macro evaluates whether ptr is null and if it turns out to be false it will throw an ExpressionE...
Definition: ExpressionException.h:194
armarx::RemoteGuiComponentPluginUser::removeRemoteGuiTab
void removeRemoteGuiTab(const std::string &name="")
Definition: RemoteGuiComponentPlugin.cpp:227
armarx::RemoteGuiComponentPluginUser::getRemoteGui
const RemoteGuiInterfacePrx & getRemoteGui() const
Definition: RemoteGuiComponentPlugin.cpp:232
armarx::plugins
This file is part of ArmarX.
Definition: DebugObserverComponentPlugin.cpp:28
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::RemoteGuiComponentPluginUser::getRemoteGuiTab
RemoteGui::TabProxy & getRemoteGuiTab(const std::string &name="")
Definition: RemoteGuiComponentPlugin.cpp:217
armarx::RemoteGuiComponentPluginUser::RemoteGuiComponentPluginUser
RemoteGuiComponentPluginUser()
Definition: RemoteGuiComponentPlugin.cpp:212
armarx::plugins::RemoteGuiComponentPlugin::preOnConnectComponent
void preOnConnectComponent() override
Definition: RemoteGuiComponentPlugin.cpp:117
armarx::plugins::RemoteGuiComponentPlugin::getRemoteGui
const RemoteGuiInterfacePrx & getRemoteGui() const
Definition: RemoteGuiComponentPlugin.cpp:104
armarx::plugins::RemoteGuiComponentPlugin::getTab
RemoteGui::TabProxy & getTab(const std::string &name="")
Definition: RemoteGuiComponentPlugin.cpp:80
armarx::plugins::RemoteGuiComponentPlugin::TabTask
std::function< void(RemoteGui::TabProxy &)> TabTask
Definition: RemoteGuiComponentPlugin.h:59
armarx::RemoteGuiComponentPluginUser::getRemoteGuiPlugin
const armarx::plugins::RemoteGuiComponentPlugin & getRemoteGuiPlugin() const
Definition: RemoteGuiComponentPlugin.cpp:236
Component.h
armarx::plugins::RemoteGuiComponentPlugin::preOnInitComponent
void preOnInitComponent() override
Definition: RemoteGuiComponentPlugin.cpp:109
armarx::WidgetDescription::WidgetPtr
::IceInternal::Handle<::armarx::WidgetDescription::Widget > WidgetPtr
Definition: NJointControllerBase.h:66
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::plugins::RemoteGuiComponentPlugin
Definition: RemoteGuiComponentPlugin.h:52
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::ManagedIceObjectPlugin::makePropertyName
std::string makePropertyName(const std::string &name)
Definition: ManagedIceObjectPlugin.cpp:53
armarx::plugins::RemoteGuiComponentPlugin::postOnDisconnectComponent
void postOnDisconnectComponent() override
Definition: RemoteGuiComponentPlugin.cpp:138
RemoteGuiComponentPlugin.h
armarx::RemoteGuiComponentPluginUser::hasRemoteGuiTab
bool hasRemoteGuiTab(const std::string &name="")
Definition: RemoteGuiComponentPlugin.cpp:222
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28