GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/gui/settings.cc Lines: 0 396 0.0 %
Date: 2024-04-14 11:13:22 Branches: 0 1944 0.0 %

Line Branch Exec Source
1
// Copyright (c) 2015, Joseph Mirabel
2
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3
//
4
// This file is part of gepetto-viewer.
5
// gepetto-viewer is free software: you can redistribute it
6
// and/or modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation, either version
8
// 3 of the License, or (at your option) any later version.
9
//
10
// gepetto-viewer is distributed in the hope that it will be
11
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
// General Lesser Public License for more details.  You should have
14
// received a copy of the GNU Lesser General Public License along with
15
// gepetto-viewer. If not, see <http://www.gnu.org/licenses/>.
16
17
#include <QDir>
18
#include <QSettings>
19
#include <QStyleFactory>
20
#include <QtDebug>
21
#include <gepetto/gui/dialog/dialogloadenvironment.hh>
22
#include <gepetto/gui/dialog/dialogloadrobot.hh>
23
#include <gepetto/gui/mainwindow.hh>
24
#include <gepetto/gui/settings.hh>
25
#include <osg/ArgumentParser>
26
#include <osg/DisplaySettings>
27
28
#if GEPETTO_GUI_HAS_PYTHONQT
29
#include <gepetto/gui/pythonwidget.hh>
30
#endif
31
32
#include "../log.hh"
33
34
namespace gepetto {
35
namespace gui {
36
Settings::Settings(const char* installDir)
37
    : configurationFile("settings"),
38
      predifinedRobotConf("robots"),
39
      predifinedEnvConf("environments"),
40
      stateConf(".state"),
41
      verbose(false),
42
      noPlugin(false),
43
      useNameService(false),
44
      refreshRate(30),
45
      captureDirectory(),
46
      captureFilename("screenshot"),
47
      captureExtension("png"),
48
      ffmpeg(FFMPEG),
49
      installDirectory(installDir)
50
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
51
      ,
52
      appStyle("cleanlooks")
53
#else
54
      ,
55
      appStyle("fusion")
56
#endif
57
58
      ,
59
      mw(0) {
60
  QDir user(QDir::home());
61
  const char path[] = "Pictures/gepetto-gui";
62
  user.mkpath(path);
63
  user.cd(path);
64
  captureDirectory = user.absolutePath().toStdString();
65
66
  ffmpegInputOptions << "-r" << "25";
67
  ffmpegOutputOptions << "-vf" << "scale=trunc(iw/2)*2:trunc(ih/2)*2" << "-r"
68
                      << "25" << "-vcodec" << "libx264";
69
}
70
71
void Settings::setupPaths() const {
72
  QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
73
  if (env.contains("GEPETTO_GUI_SETTINGS_DIR")) {
74
    QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope,
75
                       env.value("GEPETTO_GUI_SETTINGS_DIR"));
76
    QSettings::setPath(QSettings::NativeFormat, QSettings::SystemScope,
77
                       env.value("GEPETTO_GUI_SETTINGS_DIR"));
78
  } else {
79
    QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope,
80
                       installDirectory + "/etc");
81
    QSettings::setPath(QSettings::NativeFormat, QSettings::SystemScope,
82
                       installDirectory + "/etc");
83
  }
84
85
  if (env.contains("GEPETTO_GUI_PLUGIN_DIRS")) {
86
    foreach (QString p, env.value("GEPETTO_GUI_PLUGIN_DIRS").split(':')) {
87
      PluginManager::addPluginDir(p + "/gepetto-gui-plugins");
88
    }
89
  }
90
  foreach (QString p, env.value("LD_LIBRARY_PATH").split(':')) {
91
    PluginManager::addPluginDir(p + "/gepetto-gui-plugins");
92
  }
93
  PluginManager::addPluginDir(installDirectory + "/lib/gepetto-gui-plugins");
94
}
95
96
int Settings::initSettings(int argc, char* argv[]) {
97
  // 1. Declare the supported options.
98
  osg::ArgumentParser arguments(&argc, argv);
99
  osg::ApplicationUsage* au(arguments.getApplicationUsage());
100
  au->setApplicationName(arguments.getApplicationName());
101
  au->setCommandLineUsage(arguments.getApplicationName() + " [options]");
102
103
  au->addCommandLineOption("-v or --verbose", "Activate verbose output");
104
  au->addCommandLineOption("-g or --generate-config-files",
105
                           "generate configuration files in " +
106
                               installDirectory.toStdString() +
107
                               "/etc and quit");
108
  au->addCommandLineOption("-c or --config-file",
109
                           "set the configuration file (do not include .conf)",
110
                           configurationFile);
111
  au->addCommandLineOption("--log", "set the file where logs are written",
112
                           logFile);
113
  au->addCommandLineOption(
114
      "--predefined-robots",
115
      "set the predefined robots configuration file (do not include .conf)",
116
      predifinedRobotConf);
117
  au->addCommandLineOption("--predefined-environments",
118
                           "set the predefined environments configuration file "
119
                           "(do not include .conf)",
120
                           predifinedEnvConf);
121
  au->addCommandLineOption("--add-robot",
122
                           "Add a robot (a list of comma sperated string)");
123
  au->addCommandLineOption(
124
      "--add-env", "Add an environment (a list of comma sperated string)");
125
  au->addCommandLineOption("-p or --load-plugin", "load the plugin");
126
#if GEPETTO_GUI_HAS_PYTHONQT
127
  au->addCommandLineOption("-q or --load-pyplugin",
128
                           "load the PythonQt module as a plugin");
129
  au->addCommandLineOption("-x or --run-pyscript",
130
                           "run a script into the PythonQt console");
131
#endif
132
  au->addCommandLineOption("-P or --no-plugin", "do not load any plugin");
133
  au->addCommandLineOption("-w or --auto-write-settings",
134
                           "write the settings in the configuration file");
135
  au->addCommandLineOption("--no-viewer-server",
136
                           "do not start the Gepetto Viewer server");
137
  au->addCommandLineOption(
138
      "--use-nameservice",
139
      "The server will be registered to the Omni NameService");
140
141
  // 2. Read configuration files
142
  if (arguments.read("-c", configurationFile) ||
143
      arguments.read("--config-file", configurationFile)) {
144
  }
145
  if (arguments.read("--predefined-robots", predifinedRobotConf)) {
146
  }
147
  if (arguments.read("--predefined-environments", predifinedEnvConf)) {
148
  }
149
  fromFiles();
150
  arguments.read("--log", logFile);
151
152
  // 3. Read other command line arguments
153
  bool genAndQuit = false;
154
  int retVal = 0;
155
156
  // Declare the supported options.
157
  osg::ApplicationUsage::Type help(arguments.readHelpType());
158
159
  osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
160
  ds->readCommandLine(arguments);  // populate the help message.
161
162
  verbose = (arguments.read("-v") || arguments.read("--verbose"));
163
  genAndQuit =
164
      (arguments.read("-g") || arguments.read("--generate-config-files"));
165
  noPlugin = (arguments.read("-P") || arguments.read("--no-plugin"));
166
  autoWriteSettings =
167
      (arguments.read("-w") || arguments.read("--auto-write-settings"));
168
  bool startViewerServer = (!arguments.read("--no-viewer-server"));
169
  while (arguments.read("--use-nameservice", useNameService)) {
170
  }
171
172
  std::string opt;
173
  while (arguments.read("--add-robot", opt)) addRobotFromString(opt);
174
  while (arguments.read("--add-env", opt)) addEnvFromString(opt);
175
  if (startViewerServer) {
176
    // TODO omniorbserver.so should be the first plugin
177
    // because of ORB::init...
178
    // addPlugin ("omniorbserver.so", true);
179
    pluginsToInit_.prepend("omniorbserver.so");
180
    pluginManager_.declarePlugin("omniorbserver.so");
181
  }
182
  while (arguments.read("-p", opt) || arguments.read("--load-plugin", opt))
183
    addPlugin(QString::fromStdString(opt), !noPlugin);
184
  while (arguments.read("-q", opt) || arguments.read("--load-pyplugin", opt))
185
    addPyPlugin(QString::fromStdString(opt), !noPlugin);
186
  while (arguments.read("-x", opt) || arguments.read("--run-pyscript", opt))
187
    addPyScript(QString::fromStdString(opt));
188
189
  for (int i = 0; i < arguments.argc() - 1;) {
190
    if (strncmp(arguments.argv()[i], "-ORB", 4) == 0) {
191
      addOmniORB(arguments.argv()[i], arguments.argv()[i + 1]);
192
      arguments.remove(i, 2);
193
    } else
194
      ++i;
195
  }
196
197
  arguments.reportRemainingOptionsAsUnrecognized(osg::ArgumentParser::BENIGN);
198
  if (arguments.errors(osg::ArgumentParser::CRITICAL)) {
199
    arguments.writeErrorMessages(std::cout);
200
    retVal = 2;
201
  } else if (arguments.errors(osg::ArgumentParser::BENIGN)) {
202
    arguments.writeErrorMessages(std::cout);
203
  }
204
205
  if (!omniORBargv_.contains("-ORBendPoint"))
206
    addOmniORB("-ORBendPoint", ":::12321");
207
208
  if (genAndQuit && retVal < 1) retVal = 1;
209
210
  if (help != osg::ApplicationUsage::NO_HELP) {
211
    arguments.getApplicationUsage()->write(std::cout, help, 80, true);
212
    if (help & osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE) {
213
      const std::string tab("  ");
214
      const char nl = '\n';
215
      std::cout << "---------- gepetto-gui specific variables ---------" << nl
216
                << nl << tab << "GEPETTO_GUI_SETTINGS_DIR" << nl << nl << tab
217
                << tab << "Configure the system configuration directory." << nl
218
                << nl << tab << "GEPETTO_GUI_PLUGIN_DIRS" << nl << nl << tab
219
                << tab << "Configure the plugin search directories."
220
                << std::endl;
221
    }
222
    if (retVal < 1) retVal = 1;
223
  }
224
  if (verbose) print(std::cout) << std::endl;
225
  if (genAndQuit) writeSettings();
226
227
  if (!logFile.empty()) setLogFile(logFile.c_str());
228
229
  return retVal;
230
}
231
232
void Settings::fromFiles() {
233
  readRobotFile();
234
  readEnvFile();
235
  readSettingFile();
236
}
237
238
void Settings::writeSettings() {
239
  writeRobotFile();
240
  writeEnvFile();
241
  writeSettingFile();
242
}
243
244
void Settings::initPlugins() {
245
  foreach (QString name, pluginsToInit_) {
246
    pluginManager_.loadPlugin(name);
247
    pluginManager_.initPlugin(name);
248
  }
249
  foreach (QString name, pyplugins_) {
250
    pluginManager_.loadPyPlugin(name);
251
  }
252
#if GEPETTO_GUI_HAS_PYTHONQT
253
  PythonWidget* pw = mw->pythonWidget();
254
  // TODO Wouldn't it be better to do this later ?
255
  foreach (QString fileName, pyscripts_) {
256
    pw->runScript(fileName);
257
  }
258
#else
259
  foreach (QString fileName, pyscripts_) {
260
    logError(
261
        "gepetto-viewer was compiled without GEPETTO_GUI_HAS_"
262
        "PYTHONQT flag. Cannot not load Python script " +
263
        fileName);
264
  }
265
#endif
266
}
267
268
void Settings::restoreState() const {
269
  QSettings settings(QSettings::SystemScope,
270
                     QCoreApplication::organizationName(),
271
                     getQSettingsFileName(stateConf));
272
  if (settings.status() != QSettings::NoError) {
273
    qDebug() << "Could not restore the window state from"
274
             << settings.fileName();
275
  } else {
276
    settings.beginGroup("mainWindow");
277
    mw->restoreGeometry(settings.value("geometry").toByteArray());
278
    mw->restoreState(settings.value("state").toByteArray());
279
    mw->centralWidget()->setVisible(
280
        settings.value("centralWidgetVisibility", true).toBool());
281
    settings.endGroup();
282
#if GEPETTO_GUI_HAS_PYTHONQT
283
    mw->pythonWidget()->restoreHistory(settings);
284
#endif
285
  }
286
}
287
288
void Settings::restoreDockWidgetsState() const {
289
  QSettings settings(QSettings::SystemScope,
290
                     QCoreApplication::organizationName(),
291
                     getQSettingsFileName(stateConf));
292
  if (settings.status() != QSettings::NoError) {
293
    qDebug() << "Could not restore the dock widget state from"
294
             << settings.fileName();
295
  } else {
296
    settings.beginGroup("mainWindow");
297
    mw->restoreState(settings.value("state").toByteArray());
298
    settings.endGroup();
299
  }
300
}
301
302
void Settings::saveState() const {
303
  QSettings settings(QSettings::SystemScope,
304
                     QCoreApplication::organizationName(),
305
                     getQSettingsFileName(stateConf));
306
  if (settings.status() != QSettings::NoError) {
307
    qDebug() << "Could not save the window state to" << settings.fileName();
308
  } else {
309
    settings.beginGroup("mainWindow");
310
    settings.setValue("geometry", mw->saveGeometry());
311
    settings.setValue("state", mw->saveState());
312
    settings.setValue("centralWidgetVisibility",
313
                      mw->centralWidget()->isVisible());
314
    settings.endGroup();
315
#if GEPETTO_GUI_HAS_PYTHONQT
316
    mw->pythonWidget()->saveHistory(settings);
317
#endif
318
  }
319
}
320
321
void Settings::setMainWindow(gepetto::gui::MainWindow* main) { mw = main; }
322
323
std::ostream& Settings::print(std::ostream& os) {
324
  const char tab = '\t';
325
  const char nl = '\n';
326
  os << nl << "Configuration:" << nl << tab
327
     << "Configuration file:              " << tab << configurationFile << nl
328
     << tab << "Log file:                        " << tab
329
     << (logFile.empty() ? "standard output" : logFile) << nl << tab
330
     << "Predefined robots:               " << tab << predifinedRobotConf << nl
331
     << tab << "Predefined environments:         " << tab << predifinedEnvConf
332
     << nl << tab << "User configuration search path:  " << tab
333
     << QFileInfo(QSettings(QSettings::UserScope,
334
                            QCoreApplication::organizationName(), " ")
335
                      .fileName())
336
            .absolutePath()
337
            .toStdString()
338
     << nl << tab << "System configuration search path:" << tab
339
     << QFileInfo(QSettings(QSettings::SystemScope,
340
                            QCoreApplication::organizationName(), " ")
341
                      .fileName())
342
            .absolutePath()
343
            .toStdString()
344
345
     << nl << nl << "Options:" << nl << tab
346
     << "Verbose:                          " << tab << verbose << nl << tab
347
     << "No plugin:                        " << tab << noPlugin << nl << tab
348
     << "Use omni name service:            " << tab << useNameService << nl
349
     << tab << "Refresh rate:                     " << tab << refreshRate
350
351
     << nl << nl << "Screen capture options:" << nl << tab
352
     << "Directory:                        " << tab << captureDirectory << nl
353
     << tab << "Filename:                         " << tab << captureFilename
354
     << nl << tab << "Extension:                        " << tab
355
     << captureExtension << nl << tab
356
     << "ffmpeg command:                   " << tab << ffmpeg.toStdString()
357
     << nl << tab << "ffmpeg input options:             " << tab
358
     << ffmpegInputOptions.join(" ").toStdString() << nl << tab
359
     << "ffmpeg output options:            " << tab
360
     << ffmpegOutputOptions.join(" ").toStdString()
361
362
     << nl << nl << "omniORB configuration";
363
  for (int i = 1; i < omniORBargv_.size(); i += 2)
364
    os << nl << tab << omniORBargv_[i - 1].toStdString() << " = "
365
       << omniORBargv_[i].toStdString();
366
  return os;
367
}
368
369
QString Settings::getQSettingsFileName(const std::string& settingsName) const {
370
  QString name(QString::fromStdString(settingsName));
371
  QString ext(".conf");
372
  // Remove extension
373
  if (name.endsWith(ext)) name = name.left(name.size() - ext.size());
374
  return name;
375
}
376
377
void Settings::readRobotFile() {
378
  QSettings robot(QSettings::SystemScope, QCoreApplication::organizationName(),
379
                  getQSettingsFileName(predifinedRobotConf));
380
  if (robot.status() != QSettings::NoError) {
381
    logError(QString("Unable to open configuration file ") + robot.fileName());
382
  } else {
383
    foreach (QString name, robot.childGroups()) {
384
      robot.beginGroup(name);
385
      QString robotName = robot.value("RobotName", name).toString();
386
      DialogLoadRobot::addRobotDefinition(
387
          name, robotName, robot.value("RootJointType", "freeflyer").toString(),
388
          robot.value("ModelName", robotName).toString(),
389
          robot.value("Package", "").toString(),
390
          robot.value("URDFSuffix", "").toString(),
391
          robot.value("SRDFSuffix", "").toString());
392
      robot.endGroup();
393
    }
394
    log(QString("Read configuration file ") + robot.fileName());
395
  }
396
}
397
398
void Settings::readEnvFile() {
399
  QSettings env(QSettings::SystemScope, QCoreApplication::organizationName(),
400
                getQSettingsFileName(predifinedEnvConf));
401
  if (env.status() != QSettings::NoError) {
402
    logError(QString("Unable to open configuration file ") + env.fileName());
403
  } else {
404
    foreach (QString name, env.childGroups()) {
405
      env.beginGroup(name);
406
      QString envName = env.value("EnvironmentName", name).toString();
407
      DialogLoadEnvironment::addEnvironmentDefinition(
408
          name, envName, env.value("Package", "").toString(),
409
          env.value("URDFFilename").toString(),
410
          env.value("URDFSuffix").toString(),
411
          env.value("SRDFSuffix").toString());
412
      env.endGroup();
413
    }
414
    log(QString("Read configuration file ") + env.fileName());
415
  }
416
}
417
418
#define GET_PARAM(what, type, variantMethod)                        \
419
  {                                                                 \
420
    QVariant param(env.value(#what, what));                         \
421
    if (param.canConvert<type>())                                   \
422
      what = param.variantMethod();                                 \
423
    else                                                            \
424
      logError("Setting viewer/" #what " should be an " #type "."); \
425
  }
426
427
void Settings::readSettingFile() {
428
  QSettings env(QSettings::SystemScope, QCoreApplication::organizationName(),
429
                getQSettingsFileName(configurationFile));
430
  if (env.status() != QSettings::NoError) {
431
    logError(QString("Unable to open configuration file ") + env.fileName());
432
  } else {
433
    osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
434
    env.beginGroup("viewer");
435
    GET_PARAM(refreshRate, int, toInt);
436
    int nbMultiSamples = 4;
437
    GET_PARAM(nbMultiSamples, int, toInt);
438
    ds->setNumMultiSamples(nbMultiSamples);
439
440
    QString appStyle;
441
    GET_PARAM(appStyle, QString, toString);
442
    if (!appStyle.isNull()) {
443
      if (QStyleFactory::keys().contains(appStyle, Qt::CaseInsensitive))
444
        this->appStyle = appStyle;
445
      else {
446
        logError("Available application styles are " +
447
                 QStyleFactory::keys().join(", "));
448
        logError("Requested value is " + appStyle);
449
        logError("Current value is " + this->appStyle);
450
      }
451
    }
452
453
    GET_PARAM(useNameService, bool, toBool);
454
    env.endGroup();
455
456
    env.beginGroup("plugins");
457
    foreach (QString name, env.childKeys()) {
458
      addPlugin(name, (noPlugin) ? false : env.value(name, true).toBool());
459
    }
460
    env.endGroup();
461
462
    env.beginGroup("pyplugins");
463
    foreach (QString name, env.childKeys()) {
464
      addPyPlugin(name, (noPlugin) ? false : env.value(name, true).toBool());
465
    }
466
    env.endGroup();
467
468
    env.beginGroup("omniORB");
469
    foreach (QString name, env.childKeys()) {
470
      addOmniORB("-ORB" + name, env.value(name).toString());
471
    }
472
    env.endGroup();
473
474
    env.beginGroup("ffmpeg");
475
    ffmpeg = env.value("command", ffmpeg).toString();
476
    ffmpegInputOptions =
477
        env.value("input_options", ffmpegInputOptions).toStringList();
478
    ffmpegOutputOptions =
479
        env.value("output_options", ffmpegOutputOptions).toStringList();
480
    env.endGroup();
481
482
    env.beginGroup("settings");
483
    logFile = env.value("log", QString::fromStdString(logFile))
484
                  .toString()
485
                  .toStdString();
486
    env.endGroup();
487
    log(QString("Read configuration file ") + env.fileName());
488
  }
489
}
490
491
void Settings::writeRobotFile() {
492
  QSettings robot(QSettings::SystemScope, QCoreApplication::organizationName(),
493
                  getQSettingsFileName(predifinedRobotConf));
494
  if (!robot.isWritable()) {
495
    log(QString("Configuration file ") + robot.fileName() +
496
        QString(" is not writable."));
497
    return;
498
  }
499
  foreach (DialogLoadRobot::RobotDefinition rd,
500
           DialogLoadRobot::getRobotDefinitions()) {
501
    if (rd.name_.isEmpty()) continue;
502
    robot.beginGroup(rd.name_);
503
    robot.setValue("RobotName", rd.robotName_);
504
    robot.setValue("ModelName", rd.modelName_);
505
    robot.setValue("RootJointType", rd.rootJointType_);
506
    robot.setValue("Package", rd.package_);
507
    robot.setValue("URDFSuffix", rd.urdfSuf_);
508
    robot.setValue("SRDFSuffix", rd.srdfSuf_);
509
    robot.endGroup();
510
  }
511
  log(QString("Wrote configuration file ") + robot.fileName());
512
}
513
514
void Settings::writeEnvFile() {
515
  QSettings env(QSettings::SystemScope, QCoreApplication::organizationName(),
516
                getQSettingsFileName(predifinedEnvConf));
517
  if (!env.isWritable()) {
518
    logError(QString("Configuration file") + env.fileName() +
519
             QString("is not writable."));
520
    return;
521
  }
522
  foreach (DialogLoadEnvironment::EnvironmentDefinition ed,
523
           DialogLoadEnvironment::getEnvironmentDefinitions()) {
524
    if (ed.name_.isEmpty()) continue;
525
    env.beginGroup(ed.name_);
526
    env.setValue("RobotName", ed.envName_);
527
    env.setValue("Package", ed.package_);
528
    env.setValue("URDFFilename", ed.urdfFilename_);
529
    env.endGroup();
530
  }
531
  log(QString("Wrote configuration file ") + env.fileName());
532
}
533
534
void Settings::writeSettingFile() {
535
  QSettings env(QSettings::SystemScope, QCoreApplication::organizationName(),
536
                getQSettingsFileName(configurationFile));
537
  if (!env.isWritable()) {
538
    logError(QString("Configuration file") + env.fileName() +
539
             QString("is not writable."));
540
    return;
541
  }
542
543
  osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
544
  env.beginGroup("viewer");
545
  env.setValue("refreshRate", refreshRate);
546
  env.setValue("nbMultiSamples", ds->getNumMultiSamples());
547
  env.setValue("useNameService", useNameService);
548
  env.setValue("appStyle", appStyle);
549
  env.endGroup();
550
551
  env.beginGroup("plugins");
552
  for (PluginManager::Map::const_iterator p =
553
           pluginManager_.plugins().constBegin();
554
       p != pluginManager_.plugins().constEnd(); p++) {
555
    env.setValue(p.key(), (noPlugin) ? false : p.value()->isLoaded());
556
  }
557
  env.endGroup();
558
559
  env.beginGroup("pyplugins");
560
  for (PluginManager::PyMap::const_iterator p =
561
           pluginManager_.pyplugins().constBegin();
562
       p != pluginManager_.pyplugins().constEnd(); p++) {
563
    env.setValue(p.key(), (noPlugin)
564
                              ? false
565
                              : pluginManager_.isPyPluginLoaded(p.key()) ||
566
                                    pyplugins_.contains(p.key()));
567
  }
568
  env.endGroup();
569
570
  env.beginGroup("omniORB");
571
  for (int i = 1; i < omniORBargv_.size(); i += 2)
572
    env.setValue(omniORBargv_[i - 1].mid(4), omniORBargv_[i]);
573
  env.endGroup();
574
575
  env.beginGroup("ffmpeg");
576
  env.setValue("command", ffmpeg);
577
  env.setValue("input_options", ffmpegInputOptions);
578
  env.setValue("output_options", ffmpegInputOptions);
579
  env.endGroup();
580
581
  env.beginGroup("settings");
582
  env.setValue("log", QString::fromStdString(logFile));
583
  env.endGroup();
584
  log(QString("Wrote configuration file ") + env.fileName());
585
}
586
587
const char** Settings::makeOmniORBargs(int& argc) {
588
  argc = omniORBargv_.size();
589
  const char** argv = new const char*[argc];
590
  for (int i = 0; i < argc; ++i) {
591
    const QString& v = omniORBargv_[i];
592
    argv[i] = strdup(v.toLocal8Bit().constData());
593
  }
594
  return argv;
595
}
596
597
QVariant Settings::getSetting(const QString& key,
598
                              const QVariant& defaultValue) {
599
  QSettings env(QSettings::SystemScope, QCoreApplication::organizationName(),
600
                getQSettingsFileName(configurationFile));
601
  if (env.status() == QSettings::NoError) {
602
    return env.value(key, defaultValue);
603
  }
604
  return defaultValue;
605
}
606
607
void Settings::addRobotFromString(const std::string& rbtStr) {
608
  QString rbt = QString::fromStdString(rbtStr);
609
  QStringList split = rbt.split(",", QString::KeepEmptyParts);
610
  if (split.size() != 7) {
611
    logError("Robot string is not of length 7");
612
    logError(rbt);
613
    return;
614
  }
615
  DialogLoadRobot::addRobotDefinition(split[0], split[1], split[2].toLower(),
616
                                      split[3], split[4], split[5], split[6]);
617
}
618
619
void Settings::addEnvFromString(const std::string& envStr) {
620
  QString env = QString::fromStdString(envStr);
621
  QStringList split = env.split(",", QString::KeepEmptyParts);
622
  if (split.size() != 6) {
623
    logError("Environment string is not of length 6");
624
    logError(env);
625
    return;
626
  }
627
  DialogLoadEnvironment::addEnvironmentDefinition(split[0], split[1], split[2],
628
                                                  split[3], split[4], split[5]);
629
}
630
631
void Settings::addPlugin(const QString& plg, bool init) {
632
  if (init) pluginsToInit_.append(plg);
633
  pluginManager_.declarePlugin(plg);
634
}
635
636
void Settings::addPyPlugin(const QString& plg, bool init) {
637
  if (init) pyplugins_.append(plg);
638
  pluginManager_.declarePyPlugin(plg);
639
}
640
641
void Settings::addPyScript(const QString& fileName) {
642
  pyscripts_.append(fileName);
643
}
644
645
void Settings::addOmniORB(const QString& arg, const QString& value) {
646
  int i = omniORBargv_.indexOf(arg);
647
  if (i == -1)
648
    omniORBargv_ << arg << value;
649
  else
650
    omniORBargv_[i + 1] = value;
651
}
652
653
void Settings::log(const QString& t) {
654
  if (!verbose) return;
655
  if (mw)
656
    mw->log(t);
657
  else
658
    gepetto::log() << t.toLocal8Bit().constData() << std::endl;
659
}
660
661
void Settings::logError(const QString& t) {
662
  if (mw)
663
    mw->logError(t);
664
  else
665
    qWarning() << t;
666
}
667
}  // namespace gui
668
}  // namespace gepetto