Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <sstream>
00031
00032 #include "rviz/properties/ros_topic_tree.h"
00033 #include "ros/master.h"
00034
00035 #include <QTimer>
00036
00037 namespace rviz
00038 {
00039
00040 class TopicItem: public QTreeWidgetItem
00041 {
00042 public:
00043 TopicItem( QTreeWidgetItem* parent = 0 )
00044 : QTreeWidgetItem( parent )
00045 {}
00046
00047 void setSelectable( bool selectable )
00048 {
00049 setFlags( selectable ? (flags() | Qt::ItemIsSelectable) : (flags() & ~Qt::ItemIsSelectable) );
00050 }
00051
00052 std::string full_name;
00053 };
00054
00055 RosTopicTree::RosTopicTree( const std::string& message_type, QWidget* parent )
00056 : QTreeWidget( parent )
00057 , message_type_( message_type )
00058 {
00059 setHeaderHidden( true );
00060 connect( this, SIGNAL( itemSelectionChanged() ), this, SLOT( onSelectionChanged() ));
00061 connect( this, SIGNAL( itemActivated( QTreeWidgetItem*, int )),
00062 this, SLOT( onItemActivated( QTreeWidgetItem*, int )));
00063 refreshTopics();
00064 }
00065
00066 void RosTopicTree::refreshTopics()
00067 {
00068 QFont bold_font = font();
00069 bold_font.setBold( true );
00070
00071 ros::master::V_TopicInfo topics;
00072 ros::master::getTopics(topics);
00073
00074
00075 ros::master::V_TopicInfo::iterator it = topics.begin();
00076 ros::master::V_TopicInfo::iterator end = topics.end();
00077 for (; it != end; ++it)
00078 {
00079 const ros::master::TopicInfo& topic = *it;
00080
00081
00082 if (!message_type_.empty() && topic.datatype != message_type_)
00083 {
00084 continue;
00085 }
00086
00087
00088 std::istringstream iss( topic.name );
00089 std::string token;
00090
00091 QTreeWidgetItem* item = invisibleRootItem();
00092
00093 while( std::getline( iss, token, '/' ))
00094 {
00095 if (!token.empty())
00096 {
00097 QTreeWidgetItem* child = 0;
00098 bool exists = false;
00099
00100 for( int child_index = 0; child_index < item->childCount(); child_index++ )
00101 {
00102 child = item->child( child_index );
00103
00104 if( child->text( 0 ) == QString::fromStdString( token ))
00105 {
00106 exists = true;
00107 break;
00108 }
00109 }
00110
00111 if( exists )
00112 {
00113 item = child;
00114 }
00115 else
00116 {
00117 TopicItem* topic_item = new TopicItem( item );
00118 topic_item->setSelectable( false );
00119 item = topic_item;
00120 item->setText( 0, QString::fromStdString( token ));
00121 }
00122 }
00123 }
00124
00125 TopicItem* topic_item = dynamic_cast<TopicItem*>( item );
00126 if( topic_item )
00127 {
00128 topic_item->full_name = topic.name;
00129 topic_item->setSelectable( true );
00130 }
00131 item->setText( 0, QString::fromStdString( token ) + " (" + QString::fromStdString( topic.datatype ) + ")" );
00132 item->setFont( 0, bold_font );
00133 }
00134 }
00135
00136 void RosTopicTree::onSelectionChanged()
00137 {
00138 QList<QTreeWidgetItem *> selection = selectedItems();
00139 if( selection.empty() )
00140 {
00141 selected_topic_ = "";
00142 }
00143 else
00144 {
00145 TopicItem* topic_item = dynamic_cast<TopicItem*>( selection.first() );
00146 if( topic_item )
00147 {
00148 selected_topic_ = topic_item->full_name;
00149 }
00150 else
00151 {
00152 selected_topic_ = "";
00153 }
00154 }
00155 Q_EMIT selectedTopicChanged( selected_topic_ );
00156 }
00157
00158 void RosTopicTree::onItemActivated( QTreeWidgetItem* item, int column )
00159 {
00160 TopicItem* topic_item = dynamic_cast<TopicItem*>( item );
00161 if( topic_item )
00162 {
00163 selected_topic_ = topic_item->full_name;
00164 if( selected_topic_ != "" )
00165 {
00166 Q_EMIT topicActivated( selected_topic_ );
00167 }
00168 }
00169 }
00170
00171 }