Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use world name in default topics #104

Merged
merged 3 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/ignition/gui/Helpers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ namespace ignition
std::string uniqueFilePath(const std::string &_pathAndName,
const std::string &_extension);

/// \brief The main window's "worldNames" property may be filled with a list
/// of the names of all worlds currently loaded. This information can be
/// used by plugins to choose which world to work with.
/// This helper function provides a handy access to the world names list.
/// \return List of world names, as stored in the `MainWindow`'s
/// "worldNames" property.
IGNITION_GUI_VISIBLE
QStringList worldNames();

/// \brief Returns the first element on a QList which matches the given
/// property.
/// \param[in] _list The list to search through.
Expand Down
2 changes: 2 additions & 0 deletions include/ignition/gui/qml/IgnCard.qml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Pane {
* Tool bar background color
*/
property string pluginToolBarColor:
typeof MainWindow === "undefined" ||
MainWindow.pluginToolBarColorLight === "" ||
MainWindow.pluginToolBarColorDark === "" ?
Material.accent :
Expand All @@ -125,6 +126,7 @@ Pane {
* Tool bar text color
*/
property string pluginToolBarTextColor:
typeof MainWindow === "undefined" ||
MainWindow.pluginToolBarTextColorLight === "" ||
MainWindow.pluginToolBarTextColorDark === "" ?
Material.background :
Expand Down
16 changes: 16 additions & 0 deletions src/Helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
#include <string>
#include <ignition/math/Helpers.hh>

#include "ignition/gui/Application.hh"
#include "ignition/gui/Enums.hh"
#include "ignition/gui/Helpers.hh"
#include "ignition/gui/MainWindow.hh"

/////////////////////////////////////////////////
std::string ignition::gui::humanReadable(const std::string &_key)
Expand Down Expand Up @@ -164,3 +166,17 @@ std::string ignition::gui::uniqueFilePath(const std::string &_pathAndName,

return result;
}

/////////////////////////////////////////////////
QStringList ignition::gui::worldNames()
{
auto win = App()->findChild<MainWindow *>();
if (nullptr == win)
return {};

auto worldNamesVariant = win->property("worldNames");
if (!worldNamesVariant.isValid())
return {};

return worldNamesVariant.toStringList();
}
30 changes: 30 additions & 0 deletions src/Helpers_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "test_config.h" // NOLINT(build/include)
#include "ignition/gui/Application.hh"
#include "ignition/gui/MainWindow.hh"
#include "ignition/gui/Helpers.hh"

int gg_argc = 1;
Expand Down Expand Up @@ -143,3 +144,32 @@ TEST(HelpersTest, IGN_UTILS_TEST_DISABLED_ON_WIN32(findFirstByProperty))
EXPECT_EQ(findFirstByProperty(list, "banana", 3.0), nullptr);
EXPECT_EQ(findFirstByProperty(list, "acerola", 1.0), nullptr);
}

/////////////////////////////////////////////////
// See https://github.com/ignitionrobotics/ign-gui/issues/75
TEST(HelpersTest, IGN_UTILS_TEST_DISABLED_ON_WIN32(worldNames))
{
// No app, no window, no names
EXPECT_TRUE(worldNames().empty());

Application app(gg_argc, gg_argv);
auto mainWindow = app.findChild<MainWindow *>();
ASSERT_NE(nullptr, mainWindow);

// No names by default
EXPECT_TRUE(worldNames().empty());

QStringList names{"banana", "grape"};
mainWindow->setProperty("worldNames", names);

// Has names
EXPECT_FALSE(worldNames().empty());
ASSERT_EQ(2, worldNames().size());
EXPECT_EQ("banana", worldNames()[0]);
EXPECT_EQ("grape", worldNames()[1]);

mainWindow->setProperty("worldNames", QStringList());

// No more names
EXPECT_TRUE(worldNames().empty());
}
4 changes: 2 additions & 2 deletions src/plugins/topic_echo/TopicEcho.qml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ Rectangle {
}

Rectangle {
width: topicEcho.parent.width - 20
height: topicEcho.parent.height - 200
width: topicEcho.parent !== null ? topicEcho.parent.width - 20 : 50
height: topicEcho.parent !== null ? topicEcho.parent.height - 200 : 50
color: "transparent"

ListView {
Expand Down
33 changes: 29 additions & 4 deletions src/plugins/world_control/WorldControl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
*/

#include <ignition/common/Console.hh>
#include <ignition/plugin/Register.hh>
#include <ignition/common/Time.hh>
#include <ignition/plugin/Register.hh>

#include "ignition/gui/Helpers.hh"

#include "WorldControl.hh"

Expand Down Expand Up @@ -80,18 +82,32 @@ void WorldControl::LoadConfig(const tinyxml2::XMLElement *_pluginElem)
return;
}

// World name from window, to construct default topics and services
std::string worldName;
auto worldNames = gui::worldNames();
if (!worldNames.empty())
worldName = worldNames[0].toStdString();

// For world control requests
auto serviceElem = _pluginElem->FirstChildElement("service");
if (nullptr != serviceElem && nullptr != serviceElem->GetText())
this->dataPtr->controlService = serviceElem->GetText();

if (this->dataPtr->controlService.empty())
{
ignerr << "Must specify a service for world control requests."
<< std::endl;
return;
if (worldName.empty())
{
ignerr << "Must specify a <service> for world control requests, or set "
<< "the MainWindow's [worldNames] property." << std::endl;
return;
}

this->dataPtr->controlService = "/world/" + worldName + "/control";
}

ignmsg << "Using world control service [" << this->dataPtr->controlService
<< "]" << std::endl;

// Play / pause buttons
if (auto playElem = _pluginElem->FirstChildElement("play_pause"))
{
Expand Down Expand Up @@ -128,6 +144,11 @@ void WorldControl::LoadConfig(const tinyxml2::XMLElement *_pluginElem)
if (nullptr != statsTopicElem && nullptr != statsTopicElem->GetText())
statsTopic = statsTopicElem->GetText();

if (statsTopic.empty() && !worldName.empty())
{
statsTopic = "/world/" + worldName + "/stats";
}

if (!statsTopic.empty())
{
// Subscribe to world_stats
Expand All @@ -136,6 +157,10 @@ void WorldControl::LoadConfig(const tinyxml2::XMLElement *_pluginElem)
{
ignerr << "Failed to subscribe to [" << statsTopic << "]" << std::endl;
}
else
{
ignmsg << "Listening to stats on [" << statsTopic << "]" << std::endl;
}
}
}

Expand Down
23 changes: 19 additions & 4 deletions src/plugins/world_stats/WorldStats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
*/

#include <ignition/common/Console.hh>
#include <ignition/plugin/Register.hh>
#include <ignition/common/Time.hh>
#include <ignition/plugin/Register.hh>

#include "ignition/gui/Helpers.hh"

#include "WorldStats.hh"

Expand Down Expand Up @@ -83,6 +85,12 @@ void WorldStats::LoadConfig(const tinyxml2::XMLElement *_pluginElem)
return;
}

// World name from window, to construct default topics and services
std::string worldName;
auto worldNames = gui::worldNames();
if (!worldNames.empty())
worldName = worldNames[0].toStdString();

// Subscribe
std::string topic;
auto topicElem = _pluginElem->FirstChildElement("topic");
Expand All @@ -91,9 +99,14 @@ void WorldStats::LoadConfig(const tinyxml2::XMLElement *_pluginElem)

if (topic.empty())
{
ignerr << "Must specify a topic to subscribe to world statistics."
<< std::endl;
return;
if (worldName.empty())
{
ignerr << "Must specify a <topic> to subscribe to world statistics, or "
<< "set the MainWindow's [worldNames] property." << std::endl;
return;
}

topic = "/world/" + worldName + "/stats";
}

if (!this->dataPtr->node.Subscribe(topic, &WorldStats::OnWorldStatsMsg,
Expand All @@ -103,6 +116,8 @@ void WorldStats::LoadConfig(const tinyxml2::XMLElement *_pluginElem)
return;
}

ignmsg << "Listening to stats on [" << topic << "]" << std::endl;

// Sim time
if (auto simTimeElem = _pluginElem->FirstChildElement("sim_time"))
{
Expand Down