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
27namespace armarx::plugins
28{
29 void
30 RemoteGuiComponentPlugin::createOrUpdateTab(const RemoteGui::WidgetPtr& widget,
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
225namespace 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&
255
258 {
259 return *_remoteGuiComponentPlugin;
260 }
261
264 {
265 return *_remoteGuiComponentPlugin;
266 }
267} // namespace armarx
std::string makePropertyName(const std::string &name)
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
std::string getName() const
Retrieve name of object.
int getState() const
Retrieve current state of the ManagedIceObject.
bool hasRemoteGuiTab(const std::string &name="")
void removeRemoteGuiTab(const std::string &name="")
const armarx::plugins::RemoteGuiComponentPlugin & getRemoteGuiPlugin() const
RemoteGui::TabProxy & getRemoteGuiTab(const std::string &name="")
const RemoteGuiInterfacePrx & getRemoteGui() const
void postCreatePropertyDefinitions(PropertyDefinitionsPtr &properties) override
const RemoteGuiInterfacePrx & getRemoteGui() const
std::function< void(RemoteGui::TabProxy &)> TabTask
void createOrUpdateTab(const RemoteGui::WidgetPtr &widget, TabTask guiTaskFnc, unsigned long guiTaskPeriod=10)
RemoteGui::TabProxy & getTab(const std::string &name="")
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#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...
#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...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
This file is part of ArmarX.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
SimplePeriodicTask(Ts...) -> SimplePeriodicTask< std::function< void(void)> >