Skip to content

Commit

Permalink
3 ➡️ 6
Browse files Browse the repository at this point in the history
Signed-off-by: Louise Poubel <louise@openrobotics.org>
  • Loading branch information
chapulina committed Jun 1, 2022
2 parents cce99b5 + 5cd426d commit 5184313
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
6 changes: 6 additions & 0 deletions include/ignition/gui/qml/PluginMenu.qml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ Popup {
IgnSortFilterModel {
id: filteredModel

lessThan: function(left, right) {
var leftStr = left.modelData.toLowerCase();
var rightStr = right.modelData.toLowerCase();
return leftStr < rightStr;
}

filterAcceptsItem: function(item) {
var itemStr = item.modelData.toLowerCase();
var filterStr = searchField.text.toLowerCase();
Expand Down
36 changes: 32 additions & 4 deletions src/Application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#include <queue>

#include <ignition/common/Console.hh>
#include <ignition/common/Filesystem.hh>
#include <ignition/common/SignalHandler.hh>
#include <ignition/common/StringUtils.hh>
#include <ignition/common/SystemPaths.hh>
#include <ignition/common/Util.hh>

Expand Down Expand Up @@ -222,24 +224,50 @@ bool Application::LoadConfig(const std::string &_config)
return false;
}

std::string configFull = _config;

// Check if the passed in config file exists.
// (If the default config path doesn't exist yet, it's expected behavior.
// It will be created the first time the user presses "Save configuration".)
if (!common::exists(configFull) && (configFull != this->DefaultConfigPath()))
{
// If not, then check environment variable
std::string configPathEnv;
common::env("GZ_GUI_RESOURCE_PATH", configPathEnv);

if (!configPathEnv.empty())
{
std::vector<std::string> parentPaths = common::Split(configPathEnv, ':');
for (auto parentPath : parentPaths)
{
std::string tempPath = common::joinPaths(parentPath, configFull);
if (common::exists(tempPath))
{
configFull = tempPath;
break;
}
}
}
}

// Use tinyxml to read config
tinyxml2::XMLDocument doc;
auto success = !doc.LoadFile(_config.c_str());
auto success = !doc.LoadFile(configFull.c_str());
if (!success)
{
// We do not show an error message if the default config path doesn't exist
// yet. It's expected behavior, it will be created the first time the user
// presses "Save configuration".
if (_config != this->DefaultConfigPath())
if (configFull != this->DefaultConfigPath())
{
ignerr << "Failed to load file [" << _config << "]: XMLError"
ignerr << "Failed to load file [" << configFull << "]: XMLError"
<< std::endl;
}

return false;
}

ignmsg << "Loading config [" << _config << "]" << std::endl;
ignmsg << "Loading config [" << configFull << "]" << std::endl;

// Clear all previous plugins
auto plugins = this->dataPtr->mainWin->findChildren<Plugin *>();
Expand Down
22 changes: 22 additions & 0 deletions src/Application_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,28 @@ TEST(ApplicationTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(LoadConfig))
auto testSourcePath = std::string(PROJECT_SOURCE_PATH) + "/test/";
EXPECT_TRUE(app.LoadConfig(testSourcePath + "config/test.config"));
}

// Test environment variable and relative path
{
// Environment variable not set
Application app(g_argc, g_argv);
EXPECT_FALSE(app.LoadConfig("ignore.config"));

// Invalid path
setenv("GZ_GUI_RESOURCE_PATH", "invalidPath", 1);
EXPECT_FALSE(app.LoadConfig("ignore.config"));

// Valid path
setenv("GZ_GUI_RESOURCE_PATH",
(std::string(PROJECT_SOURCE_PATH) + "/test/config").c_str(), 1);
EXPECT_TRUE(app.LoadConfig("ignore.config"));

// Multiple paths, one valid
setenv("GZ_GUI_RESOURCE_PATH",
("banana:" + std::string(PROJECT_SOURCE_PATH) + "/test/config" +
":orange").c_str(), 1);
EXPECT_TRUE(app.LoadConfig("ignore.config"));
}
}

//////////////////////////////////////////////////
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/cmdgui.rb.in
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ COMMANDS = { 'gui' =>
" The default verbosity is 1, use -v without\n"\
" arguments for level 3.\n"\
"\n" +
COMMON_OPTIONS,
COMMON_OPTIONS + "\n\n" +
"Environment variables: \n"\
" GZ_GUI_RESOURCE_PATH Colon separated paths used to locate GUI \n"\
" resources such as configuration files. \n"\
}

#
Expand Down
14 changes: 12 additions & 2 deletions tutorials/07_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@ By default, Ignition GUI will load the config file at
Configuration files can also be loaded from the command line or through the
C++ API.

From the command line, use the `--config` / `-c` option, for example:
From the command line, use the `--config` / `-c` option.
For example, you can specify an absolute path:

`ign gui -c path/to/example.config`
`ign gui -c /absolute/path/to/example.config`

Or a path relative to the current working directory:

`ign gui -c relative/path/to/example.config`

Or a path relative to a custom directory, which you can specify by setting the
environment variable `GZ_GUI_RESOURCE_PATH`, like so:

`GZ_GUI_RESOURCE_PATH=/absolute/path/to/ ign gui --config example.config`

From the C++ API, pass the file path to
[Application::LoadConfig](https://ignitionrobotics.org/api/gui/6.0/classignition_1_1gui_1_1Application.html#a03c4c3a1b1e58cc4bff05658f21fff17).
Expand Down

0 comments on commit 5184313

Please sign in to comment.