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 namespace armarx::plugins
28 {
29  void
32  unsigned long guiTaskPeriod)
33  {
34  createOrUpdateTab("", widget, guiTaskFnc, guiTaskPeriod);
35  }
36 
37  void
39  const RemoteGui::WidgetPtr& widget,
41  unsigned long guiTaskPeriod)
42  {
43  std::lock_guard g{_tabsMutex};
44  if (hasTab(name))
45  {
46  ARMARX_INFO << "Replacing the tab " << tabName(name);
47  }
48  auto& data = _tabs[name];
49  if (data.task)
50  {
51  data.task->stop();
52  data.task = nullptr;
53  }
54  data.fnc = std::move(guiTaskFnc);
55  data.taskPeriod = guiTaskPeriod;
56  data.widget = widget;
57  create(name, data);
58  }
59 
60  void
62  const RemoteGui::WidgetPtr& widget,
63  unsigned long guiTaskPeriod)
64  {
65  std::lock_guard g{_tabsMutex};
66  if (!hasTab(name))
67  {
68  throw armarx::LocalException(
69  "Can only update Tab with a name for an already existing Tab");
70  }
71  auto& data = _tabs[name];
72  if (data.task)
73  {
74  data.task->stop();
75  data.task = nullptr;
76  }
77  data.taskPeriod = guiTaskPeriod;
78  data.widget = widget;
79  create(name, data);
80  }
81 
83  RemoteGuiComponentPlugin::getTab(const std::string& name)
84  {
86  std::lock_guard g{_tabsMutex};
87  return _tabs.at(name).tab;
88  }
89 
90  bool
91  RemoteGuiComponentPlugin::hasTab(const std::string& name)
92  {
93  std::lock_guard g{_tabsMutex};
94  return _tabs.count(name);
95  }
96 
97  void
98  RemoteGuiComponentPlugin::removeTab(const std::string& name)
99  {
100  std::lock_guard g{_tabsMutex};
101  if (_tabs.count(name))
102  {
103  auto& data = _tabs.at(name);
104  remove(name, data);
105  }
106  _tabs.erase(name);
107  }
108 
109  const RemoteGuiInterfacePrx&
111  {
112  return _remoteGui;
113  }
114 
115  void
117  {
118  if (!_remoteGui)
119  {
120  parent<Component>().usingProxyFromProperty(makePropertyName(_propertyName));
121  }
122  }
123 
124  void
126  {
127  if (!_remoteGui)
128  {
129  parent<Component>().getProxyFromProperty(_remoteGui, makePropertyName(_propertyName));
130  }
131  }
132 
133  void
135  {
136  std::lock_guard g{_tabsMutex};
137  for (auto& [name, data] : _tabs)
138  {
139  if (data.task)
140  {
141  continue;
142  }
144  create(name, data);
145  }
146  }
147 
148  void
150  {
151  std::lock_guard g{_tabsMutex};
152  for (auto& [name, data] : _tabs)
153  {
154  if (data.task)
155  {
156  data.task->stop();
157  data.task = nullptr;
158  }
159  remove(name, data);
160  }
161  _remoteGui = nullptr;
162  }
163 
164  void
166  {
167  if (!properties->hasDefinition(makePropertyName(_propertyName)))
168  {
169  properties->defineOptionalProperty<std::string>(makePropertyName(_propertyName),
170  "RemoteGuiProvider",
171  "Name of the remote gui provider");
172  }
173  }
174 
175  std::string
176  RemoteGuiComponentPlugin::tabName(const std::string& name)
177  {
178  return parent<ManagedIceObject>().getName() + (name.empty() ? "" : "_" + name);
179  }
180 
181  void
182  RemoteGuiComponentPlugin::create(const std::string& name, RemoteGuiComponentPlugin::TabData& td)
183  {
184  {
185  const auto state = parent<ManagedIceObject>().getState();
186  if (state != ManagedIceObjectState::eManagedIceObjectStarting &&
187  state != ManagedIceObjectState::eManagedIceObjectStarted)
188  {
189  return;
190  }
191  }
192  ARMARX_CHECK_NOT_NULL(_remoteGui);
193  if (td.task)
194  {
195  td.task->stop();
196  td.task = nullptr;
197  }
198  const auto tabname = tabName(name);
199  td.task = new SimplePeriodicTask<>([&td] { td.fnc(td.tab); }, td.taskPeriod);
200  _remoteGui->createTab(tabname, td.widget);
201  td.tab = RemoteGui::TabProxy(_remoteGui, tabname);
202  td.task->start();
203  }
204 
205  void
206  RemoteGuiComponentPlugin::remove(const std::string& name, RemoteGuiComponentPlugin::TabData& td)
207  {
208  if (td.task)
209  {
210  td.task->stop();
211  td.task = nullptr;
212  }
213  try
214  {
215  const auto tabname = tabName(name);
216  ARMARX_INFO << "Removing tab: " << tabname;
217  _remoteGui->removeTab(tabname);
218  }
219  catch (...)
220  {
221  }
222  }
223 } // namespace armarx::plugins
224 
225 namespace armarx
226 {
228  {
229  addPlugin(_remoteGuiComponentPlugin);
230  }
231 
234  {
235  return getRemoteGuiPlugin().getTab(name);
236  }
237 
238  bool
240  {
241  return getRemoteGuiPlugin().hasTab(name);
242  }
243 
244  void
246  {
248  }
249 
250  const RemoteGuiInterfacePrx&
252  {
253  return getRemoteGuiPlugin().getRemoteGui();
254  }
255 
258  {
259  return *_remoteGuiComponentPlugin;
260  }
261 
264  {
265  return *_remoteGuiComponentPlugin;
266  }
267 } // namespace armarx
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:91
armarx::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
Definition: ManagedIceObject.h:186
armarx::plugins::RemoteGuiComponentPlugin::removeTab
void removeTab(const std::string &name="")
Definition: RemoteGuiComponentPlugin.cpp:98
armarx::plugins::RemoteGuiComponentPlugin::postOnConnectComponent
void postOnConnectComponent() override
Definition: RemoteGuiComponentPlugin.cpp:134
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:165
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:245
armarx::RemoteGuiComponentPluginUser::getRemoteGui
const RemoteGuiInterfacePrx & getRemoteGui() const
Definition: RemoteGuiComponentPlugin.cpp:251
armarx::plugins
This file is part of ArmarX.
Definition: DebugObserverComponentPlugin.cpp:27
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::RemoteGuiComponentPluginUser::getRemoteGuiTab
RemoteGui::TabProxy & getRemoteGuiTab(const std::string &name="")
Definition: RemoteGuiComponentPlugin.cpp:233
armarx::RemoteGuiComponentPluginUser::RemoteGuiComponentPluginUser
RemoteGuiComponentPluginUser()
Definition: RemoteGuiComponentPlugin.cpp:227
armarx::plugins::RemoteGuiComponentPlugin::preOnConnectComponent
void preOnConnectComponent() override
Definition: RemoteGuiComponentPlugin.cpp:125
armarx::plugins::RemoteGuiComponentPlugin::getRemoteGui
const RemoteGuiInterfacePrx & getRemoteGui() const
Definition: RemoteGuiComponentPlugin.cpp:110
armarx::plugins::RemoteGuiComponentPlugin::getTab
RemoteGui::TabProxy & getTab(const std::string &name="")
Definition: RemoteGuiComponentPlugin.cpp:83
armarx::plugins::RemoteGuiComponentPlugin::TabTask
std::function< void(RemoteGui::TabProxy &)> TabTask
Definition: RemoteGuiComponentPlugin.h:57
armarx::RemoteGuiComponentPluginUser::getRemoteGuiPlugin
const armarx::plugins::RemoteGuiComponentPlugin & getRemoteGuiPlugin() const
Definition: RemoteGuiComponentPlugin.cpp:257
Component.h
armarx::plugins::RemoteGuiComponentPlugin::preOnInitComponent
void preOnInitComponent() override
Definition: RemoteGuiComponentPlugin.cpp:116
armarx::WidgetDescription::WidgetPtr
::IceInternal::Handle<::armarx::WidgetDescription::Widget > WidgetPtr
Definition: NJointControllerBase.h:67
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:181
armarx::plugins::RemoteGuiComponentPlugin
Definition: RemoteGuiComponentPlugin.h:51
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::ManagedIceObjectPlugin::makePropertyName
std::string makePropertyName(const std::string &name)
Definition: ManagedIceObjectPlugin.cpp:56
armarx::plugins::RemoteGuiComponentPlugin::postOnDisconnectComponent
void postOnDisconnectComponent() override
Definition: RemoteGuiComponentPlugin.cpp:149
RemoteGuiComponentPlugin.h
armarx::RemoteGuiComponentPluginUser::hasRemoteGuiTab
bool hasRemoteGuiTab(const std::string &name="")
Definition: RemoteGuiComponentPlugin.cpp:239
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27