-
Notifications
You must be signed in to change notification settings - Fork 269
/
Gui.cc
481 lines (419 loc) · 15.4 KB
/
Gui.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* Copyright (C) 2020 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <QScreen>
#include <ignition/common/Console.hh>
#include <ignition/common/SignalHandler.hh>
#include <ignition/common/Filesystem.hh>
#include <ignition/gui/Application.hh>
#include <ignition/gui/MainWindow.hh>
#include <ignition/gui/Plugin.hh>
#include <ignition/gui/Dialog.hh>
#include "ignition/gazebo/Util.hh"
#include "ignition/gazebo/config.hh"
#include "ignition/gazebo/gui/Gui.hh"
#include "AboutDialogHandler.hh"
#include "GuiFileHandler.hh"
#include "GuiRunner.hh"
#include "PathManager.hh"
#include "QuickStartHandler.hh"
namespace ignition
{
namespace gazebo
{
// Inline bracket to help doxygen filtering.
inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
namespace gui
{
/// \brief Get the path to the default config file. If the file doesn't exist
/// yet, this function will copy the installed file into its location.
/// \param[in] _isPlayback True if playing back a log file
/// \param[in] _customDefaultConfig A default config passed by the CLI or
/// another caller.
/// \return Path to the default config file.
std::string defaultGuiConfigFile(bool _isPlayback,
const char *_customDefaultConfig)
{
std::string defaultConfig;
std::string defaultGuiConfigName = "gui.config";
if (nullptr == _customDefaultConfig)
{
// The playback flag (and not the gui-config flag) was
// specified from the command line
if (_isPlayback)
{
defaultGuiConfigName = "playback_gui.config";
}
common::env(IGN_HOMEDIR, defaultConfig);
// TODO(chapulina) Update to .gz/sim when merging forward to Garden
defaultConfig = common::joinPaths(defaultConfig, ".ignition",
"gazebo", IGNITION_GAZEBO_MAJOR_VERSION_STR,
defaultGuiConfigName);
}
else
{
// Downstream applications can override the default path
defaultConfig = _customDefaultConfig;
}
// Check if the default config file exists. If it doesn't, copy the installed
// file there first.
if (!common::exists(defaultConfig))
{
auto defaultConfigFolder = common::parentPath(defaultConfig);
if (!ignition::common::exists(defaultConfigFolder))
{
if (!ignition::common::createDirectories(defaultConfigFolder))
{
ignerr << "Failed to create the default config folder ["
<< defaultConfigFolder << "]\n";
return nullptr;
}
}
auto installedConfig = common::joinPaths(
IGNITION_GAZEBO_GUI_CONFIG_PATH, defaultGuiConfigName);
if (!common::copyFile(installedConfig, defaultConfig))
{
ignerr << "Failed to copy installed config [" << installedConfig
<< "] to default config [" << defaultConfig << "]."
<< std::endl;
return nullptr;
}
else
{
ignmsg << "Copied installed config [" << installedConfig
<< "] to default config [" << defaultConfig << "]."
<< std::endl;
}
}
return defaultConfig;
}
//////////////////////////////////////////////////
/// \brief Launch the quick start dialog
/// \param[in] _argc Number of command line arguments.
/// \param[in] _argv Command line arguments.
/// \param[in] _defaultConfig Path to the default configuration file.
/// \param[in] _configInUse The config that the user chose to load. If the user
/// didn't pass one, this will be equal to _defaultConfig
/// \return The path to the starting world or an empty string if none was
/// chosen.
std::string launchQuickStart(int &_argc, char **_argv,
const std::string &_defaultConfig,
const std::string &_configInUse)
{
ignmsg << "Gazebo Sim Quick start dialog" << std::endl;
// Gui application in dialog mode
auto app = std::make_unique<ignition::gui::Application>(
_argc, _argv, ignition::gui::WindowType::kDialog);
app->SetDefaultConfigPath(_defaultConfig);
auto quickStartHandler = new gui::QuickStartHandler();
quickStartHandler->setParent(app->Engine());
auto dialog = new ignition::gui::Dialog();
dialog->setObjectName("quick_start");
igndbg << "Reading Quick start menu config." << std::endl;
auto showDialog = dialog->ReadConfigAttribute(_configInUse, "show_again");
if (showDialog == "false")
{
ignmsg << "Not showing Quick start menu." << std::endl;
return "";
}
// This is the fixed window size for the quick start dialog
QSize winSize(960, 540);
dialog->QuickWindow()->resize(winSize);
dialog->QuickWindow()->setMaximumSize(dialog->QuickWindow()->size());
dialog->QuickWindow()->setTitle("Gazebo quick start");
// Position the quick start in the center of the screen
QSize screenSize = dialog->QuickWindow()->screen()->size();
screenSize /= 2.0;
screenSize -= winSize / 2.0;
dialog->QuickWindow()->setPosition(screenSize.width(), screenSize.height());
auto context = new QQmlContext(app->Engine()->rootContext());
context->setContextProperty("QuickStartHandler", quickStartHandler);
std::string qmlFile("qrc:/Gazebo/QuickStart.qml");
QQmlComponent dialogComponent(ignition::gui::App()->Engine(),
QString(QString::fromStdString(qmlFile)));
auto dialogItem = qobject_cast<QQuickItem *>(dialogComponent.create(context));
dialogItem->setParentItem(dialog->RootItem());
// Run qt application and show quick dialog
if (nullptr != app)
{
app->exec();
igndbg << "Shutting quick setup dialog" << std::endl;
}
// Update dialog config
dialog->UpdateConfigAttribute(_configInUse, "show_again",
quickStartHandler->ShowAgain());
return quickStartHandler->StartingWorld();
}
//////////////////////////////////////////////////
std::unique_ptr<ignition::gui::Application> createGui(
int &_argc, char **_argv, const char *_guiConfig,
const char *_defaultGuiConfig, bool _loadPluginsFromSdf,
const char *_renderEngine)
{
return createGui(_argc, _argv, _guiConfig, _defaultGuiConfig,
_loadPluginsFromSdf, nullptr, 0, _renderEngine);
}
//////////////////////////////////////////////////
std::unique_ptr<ignition::gui::Application> createGui(
int &_argc, char **_argv, const char *_guiConfig,
const char *_defaultGuiConfig, bool _loadPluginsFromSdf,
const char *_sdfFile, int _waitGui, const char *_renderEngine)
{
ignition::common::SignalHandler sigHandler;
bool sigKilled = false;
sigHandler.AddCallback([&](const int /*_sig*/)
{
sigKilled = true;
});
ignmsg << "Ignition Gazebo GUI v" << IGNITION_GAZEBO_VERSION_FULL
<< std::endl;
// Set auto scaling factor for HiDPI displays
if (QString::fromLocal8Bit(qgetenv("QT_AUTO_SCREEN_SCALE_FACTOR")).isEmpty())
{
qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1");
}
bool isPlayback = (nullptr != _guiConfig &&
std::string(_guiConfig) == "_playback_");
auto defaultConfig = defaultGuiConfigFile(isPlayback, _defaultGuiConfig);
bool hasSdfFile = (nullptr != _sdfFile && strlen(_sdfFile) != 0);
bool configFromCli = (nullptr != _guiConfig && std::strlen(_guiConfig) > 0 &&
std::string(_guiConfig) != "_playback_");
transport::Node node;
// Quick start dialog if no specific SDF file was passed and it's not playback
std::string startingWorld;
if (!hasSdfFile && _waitGui && !isPlayback)
{
std::string configInUse = configFromCli ? _guiConfig : defaultConfig;
startingWorld = launchQuickStart(_argc, _argv, defaultConfig, configInUse);
}
else if (hasSdfFile)
{
startingWorld = _sdfFile;
}
if (sigKilled)
{
igndbg << "Received kill signal. Not starting main window." << std::endl;
return nullptr;
}
// Publish starting world even if it's empty. The server is blocking waiting
// for it.
if (_waitGui)
{
std::string topic{"/gazebo/starting_world"};
auto startingWorldPub = node.Advertise<msgs::StringMsg>(topic);
msgs::StringMsg msg;
msg.set_data(startingWorld);
// Wait for the server to be listening, so we're sure it receives the
// message.
igndbg << "Waiting for subscribers to [" << topic << "]..." << std::endl;
for (int sleep = 0; sleep < 100 && !startingWorldPub.HasConnections();
++sleep)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (!startingWorldPub.HasConnections())
{
ignwarn << "Waited for 10s for a subscriber to [" << topic
<< "] and got none." << std::endl;
}
startingWorldPub.Publish(msg);
}
// Launch main window
auto app = std::make_unique<ignition::gui::Application>(
_argc, _argv, ignition::gui::WindowType::kMainWindow);
app->AddPluginPath(IGN_GAZEBO_GUI_PLUGIN_INSTALL_DIR);
auto aboutDialogHandler = new ignition::gazebo::gui::AboutDialogHandler();
aboutDialogHandler->setParent(app->Engine());
auto guiFileHandler = new ignition::gazebo::gui::GuiFileHandler();
guiFileHandler->setParent(app->Engine());
auto pathManager = new ignition::gazebo::gui::PathManager();
pathManager->setParent(app->Engine());
// add import path so we can load custom modules
app->Engine()->addImportPath(IGN_GAZEBO_GUI_PLUGIN_INSTALL_DIR);
app->SetDefaultConfigPath(defaultConfig);
// Customize window
auto mainWin = app->findChild<ignition::gui::MainWindow *>();
if (_renderEngine != nullptr)
{
mainWin->SetRenderEngine(_renderEngine);
}
auto win = mainWin->QuickWindow();
win->setProperty("title", "Gazebo");
// Let QML files use C++ functions and properties
auto context = new QQmlContext(app->Engine()->rootContext());
context->setContextProperty("AboutDialogHandler", aboutDialogHandler);
context->setContextProperty("GuiFileHandler", guiFileHandler);
// Instantiate GazeboDrawer.qml file into a component
QQmlComponent component(app->Engine(), ":/Gazebo/GazeboDrawer.qml");
auto gzDrawerItem = qobject_cast<QQuickItem *>(component.create(context));
if (gzDrawerItem)
{
// C++ ownership
QQmlEngine::setObjectOwnership(gzDrawerItem, QQmlEngine::CppOwnership);
// Add to main window
auto parentDrawerItem = win->findChild<QQuickItem *>("sideDrawer");
gzDrawerItem->setParentItem(parentDrawerItem);
gzDrawerItem->setParent(app->Engine());
}
else
{
ignerr << "Failed to instantiate custom drawer, drawer will be empty"
<< std::endl;
}
// Get list of worlds
bool executed{false};
bool result{false};
unsigned int timeout{5000};
std::string service{"/gazebo/worlds"};
msgs::StringMsg_V worldsMsg;
// This loop is here to allow the server time to download resources.
// \todo(nkoenig) Async resource download. Search for "Async resource
// download in `src/Server.cc` for corresponding todo item. This todo is
// resolved when this while loop can be removed.
while (!sigKilled && !executed)
{
igndbg << "GUI requesting list of world names. The server may be busy "
<< "downloading resources. Please be patient." << std::endl;
executed = node.Request(service, timeout, worldsMsg, result);
}
// Only print error message if a sigkill was not received.
if (!sigKilled)
{
if (!executed)
ignerr << "Timed out when getting world names." << std::endl;
else if (!result)
ignerr << "Failed to get world names." << std::endl;
}
if (!executed || !result || worldsMsg.data().empty())
return nullptr;
std::size_t runnerCount = 0;
// Configuration file from command line
if (configFromCli)
{
// Use the first world name with the config file
// TODO(anyone) Most of ign-gazebo's transport API includes the world name,
// which makes it complicated to mix configurations across worlds.
// We could have a way to use world-agnostic topics like Gazebo-classic's ~
auto runner = new ignition::gazebo::GuiRunner(worldsMsg.data(0));
++runnerCount;
runner->setParent(ignition::gui::App());
// Load plugins after runner is up
if (!app->LoadConfig(_guiConfig))
{
ignwarn << "Failed to load config file[" << _guiConfig << "]."
<< std::endl;
}
}
// GUI configuration from SDF (request to server)
else
{
// TODO(anyone) Parallelize this if multiple worlds becomes an important use
// case.
for (int w = 0; w < worldsMsg.data_size(); ++w)
{
const auto &worldName = worldsMsg.data(w);
// Request GUI info for each world
result = false;
ignition::msgs::GUI res;
service = transport::TopicUtils::AsValidTopic("/world/" + worldName +
"/gui/info");
if (service.empty())
{
ignerr << "Failed to generate valid service for world [" << worldName
<< "]" << std::endl;
}
else
{
igndbg << "Requesting GUI from [" << service << "]..." << std::endl;
// Request and block
executed = node.Request(service, timeout, res, result);
if (!executed)
{
ignerr << "Service call timed out for [" << service << "]"
<< std::endl;
}
else if (!result)
{
ignerr << "Service call failed for [" << service << "]" << std::endl;
}
}
// GUI runner
auto runner = new ignition::gazebo::GuiRunner(worldName);
runner->setParent(ignition::gui::App());
++runnerCount;
// Load plugins after creating GuiRunner, so they can access worldName
if (_loadPluginsFromSdf)
{
for (int p = 0; p < res.plugin_size(); ++p)
{
const auto &plugin = res.plugin(p);
const auto &fileName = plugin.filename();
std::string pluginStr = "<plugin filename='" + fileName + "'>" +
plugin.innerxml() + "</plugin>";
tinyxml2::XMLDocument pluginDoc;
pluginDoc.Parse(pluginStr.c_str());
app->LoadPlugin(fileName,
pluginDoc.FirstChildElement("plugin"));
}
}
}
mainWin->configChanged();
}
if (0 == runnerCount)
{
ignerr << "Failed to start a GUI runner." << std::endl;
return nullptr;
}
// If no plugins have been added, load default config file
auto plugins = mainWin->findChildren<ignition::gui::Plugin *>();
if (plugins.empty())
{
if (!app->LoadConfig(defaultConfig))
{
ignerr << "Failed to load config file[" << defaultConfig << "]."
<< std::endl;
return nullptr;
}
}
return app;
}
//////////////////////////////////////////////////
int runGui(int &_argc, char **_argv, const char *_guiConfig,
const char *_renderEngine)
{
return runGui(_argc, _argv, _guiConfig, nullptr, 0, _renderEngine);
}
//////////////////////////////////////////////////
int runGui(int &_argc, char **_argv,
const char *_guiConfig, const char *_sdfFile, int _waitGui,
const char *_renderEngine)
{
auto app = gazebo::gui::createGui(_argc, _argv, _guiConfig, nullptr, true,
_sdfFile, _waitGui, _renderEngine);
if (nullptr != app)
{
// Run main window.
// This blocks until the window is closed or we receive a SIGINT
app->exec();
igndbg << "Shutting down ign-gazebo-gui" << std::endl;
return 0;
}
return -1;
}
} // namespace gui
} // namespace IGNITION_GAZEBO_VERSION_NAMESPACE
} // namespace gazebo
} // namespace ignition