Skip to content

Commit

Permalink
Added getName and getNamespace to Ros singleton to obtain node name a…
Browse files Browse the repository at this point in the history
…nd namespace.
  • Loading branch information
StefanFabian committed Sep 23, 2021
1 parent fd5ff1b commit b7b105c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions include/qml_ros_plugin/ros.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ Q_OBJECT
//! @copydoc RosQml::setThreads
Q_INVOKABLE void setThreads( int count );

//! Returns the name of the node. Returns empty string before ROS node was initialized.
Q_INVOKABLE QString getName();

//! Returns the namespace of the node. Returns empty string before ROS node was initialized.
Q_INVOKABLE QString getNamespace();

//! @copydoc RosQml::queryTopics
Q_INVOKABLE QStringList queryTopics( const QString &datatype = QString()) const;

Expand Down
18 changes: 15 additions & 3 deletions src/ros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ QStringList RosQml::queryTopics( const QString &datatype ) const
ros::master::getTopics( topic_info );
QStringList result;
std::string std_datatype = datatype.toStdString();
for ( const auto &topic : topic_info )
for ( const auto &topic: topic_info )
{
if ( !std_datatype.empty() && topic.datatype != std_datatype ) continue;
result.append( QString::fromStdString( topic.name ));
Expand All @@ -97,7 +97,7 @@ QList<TopicInfo> RosQml::queryTopicInfo() const
ros::master::V_TopicInfo topic_info;
ros::master::getTopics( topic_info );
QList<TopicInfo> result;
for ( const auto &topic : topic_info )
for ( const auto &topic: topic_info )
{
result.append( { QString::fromStdString( topic.name ), QString::fromStdString( topic.datatype ) } );
}
Expand All @@ -111,7 +111,7 @@ QString RosQml::queryTopicType( const QString &name ) const
ros::master::getTopics( topic_info );
QList<TopicInfo> result;
std::string std_name = name.toStdString();
for ( const auto &topic : topic_info )
for ( const auto &topic: topic_info )
{
if ( std_name != topic.name ) continue;
return QString::fromStdString( topic.datatype );
Expand Down Expand Up @@ -267,6 +267,18 @@ void RosQmlSingletonWrapper::spinOnce() { RosQml::getInstance().spinOnce(); }

void RosQmlSingletonWrapper::setThreads( int count ) { RosQml::getInstance().setThreads( count ); }

QString RosQmlSingletonWrapper::getName()
{
if ( !isInitialized()) return {};
return QString::fromStdString( ros::this_node::getName());
}

QString RosQmlSingletonWrapper::getNamespace()
{
if ( !isInitialized()) return {};
return QString::fromStdString( ros::this_node::getNamespace());
}

QStringList RosQmlSingletonWrapper::queryTopics( const QString &datatype ) const
{
return RosQml::getInstance().queryTopics( datatype );
Expand Down

0 comments on commit b7b105c

Please sign in to comment.