$search
00001 /* 00002 * Copyright (c) 2011, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 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 // Loop through all published topics 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 // If we are filtering by type, skip topics whose type does not match. 00082 if (!message_type_.empty() && topic.datatype != message_type_) 00083 { 00084 continue; 00085 } 00086 00087 // Topic not in cache yet. Find right place to put it in the tree 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 } // end namespace rviz