cancel_action.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include <QPainter>
00004 #include <QLineEdit>
00005 #include <QPushButton>
00006 #include <QVBoxLayout>
00007 #include <QHBoxLayout>
00008 #include <QLabel>
00009 #include <QTimer>
00010 
00011 #include <std_msgs/Empty.h>
00012 #include <actionlib_msgs/GoalID.h>
00013 
00014 #include "cancel_action.h"
00015 
00016 namespace jsk_rviz_plugin
00017 {
00018 
00019   CancelAction::CancelAction( QWidget* parent )
00020     : rviz::Panel( parent )
00021   {
00022     layout = new QVBoxLayout;
00023 
00024     //Text Box and Add Button to add new topic
00025     QHBoxLayout* topic_layout = new QHBoxLayout;
00026     output_topic_editor_ = new QLineEdit;
00027     //topic_layout->addWidget( output_topic_editor_ );
00028 
00029     add_topic_box_ = new QComboBox;
00030     initComboBox();
00031     topic_layout->addWidget( add_topic_box_ );
00032 
00033     QPushButton* add_topic_button_ = new QPushButton("Add Action");
00034     topic_layout->addWidget( add_topic_button_ );
00035 
00036     layout->addLayout( topic_layout );
00037     //End of Text Box and Add Button
00038 
00039     m_sigmap = new QSignalMapper(this);
00040 
00041     connect(m_sigmap, SIGNAL(mapped(int)),this, SLOT(OnClickDeleteButton(int)));
00042 
00043     //Button to send cancel topic
00044     QPushButton* send_topic_button_ = new QPushButton("Cancel Action");
00045     layout->addWidget( send_topic_button_ );
00046 
00047     setLayout( layout );
00048 
00049     connect( send_topic_button_, SIGNAL( clicked() ), this, SLOT( sendTopic ()));
00050     connect( add_topic_button_, SIGNAL( clicked() ), this, SLOT( addTopic() ));
00051   }
00052 
00053   void CancelAction::initComboBox(){
00054     add_topic_box_->addItem("");
00055     ros::master::V_TopicInfo topics;
00056     ros::master::getTopics (topics);
00057     ros::master::V_TopicInfo::iterator it = topics.begin();
00058     while( it != topics.end()){
00059       if(it->datatype == "actionlib_msgs/GoalID"){
00060         std::string action_name = it->name;
00061         std::string delete_string = "/cancel";
00062         std::string::size_type index = action_name.find_last_of(delete_string);
00063         if(index != std::string::npos){
00064           action_name.erase(index - delete_string.length() + 1);
00065           add_topic_box_->addItem(action_name.c_str());
00066         }
00067       }
00068       it ++;
00069     }
00070   }
00071 
00072 
00073   void CancelAction::OnClickDeleteButton(int id){
00074     std::vector<topicListLayout>::iterator it = topic_list_layouts_.begin();
00075     while( it != topic_list_layouts_.end()){
00076       if(it->id == id){
00077         it->topic_name_->hide();
00078         delete it->topic_name_;
00079 
00080         it->remove_button_->hide();
00081         delete it->remove_button_;
00082 
00083         delete it->layout_;
00084         it->publisher_.shutdown();
00085         it = topic_list_layouts_.erase( it );
00086         Q_EMIT configChanged();
00087       }else{
00088         ++it;
00089       }
00090     }
00091   }
00092 
00093   void CancelAction::addTopic()
00094   {
00095     //output_topic_ = output_topic_editor_->text();
00096     output_topic_ = add_topic_box_->currentText();
00097     if( output_topic_ != "" ){
00098       add_topic_box_->setCurrentIndex( 0 );
00099       addTopicList(output_topic_.toStdString());
00100       //output_topic_editor_->setText("");
00101     }
00102     Q_EMIT configChanged();
00103   }
00104 
00105   void CancelAction::addTopicList(std::string topic_name){
00106     topicListLayout tll;
00107 
00108     if(!topic_list_layouts_.empty()){
00109       topicListLayout lastTll = topic_list_layouts_.back();
00110       tll.id = lastTll.id + 1;
00111     }else{
00112       tll.id = 0;
00113     }
00114 
00115     tll.layout_ = new QHBoxLayout;
00116 
00117     tll.topic_name_ = new QLabel( topic_name.c_str() );
00118     tll.layout_->addWidget( tll.topic_name_ );
00119 
00120     tll.remove_button_ = new QPushButton("Delete");
00121     tll.layout_->addWidget( tll.remove_button_ );
00122 
00123     layout->addLayout(tll.layout_);
00124 
00125     tll.publisher_ = nh_.advertise<actionlib_msgs::GoalID>( topic_name + "/cancel", 1 );
00126     
00127     topic_list_layouts_.push_back(tll);
00128 
00129     connect(tll.remove_button_, SIGNAL(clicked()), m_sigmap, SLOT(map()));
00130     m_sigmap->setMapping(tll.remove_button_, tll.id);
00131 
00132   }
00133 
00134   void CancelAction::sendTopic(){
00135     std::vector<topicListLayout>::iterator it = topic_list_layouts_.begin();
00136     while( it != topic_list_layouts_.end()){
00137       actionlib_msgs::GoalID msg;
00138       it->publisher_.publish(msg);
00139       it++;
00140     }
00141   }
00142 
00143   void CancelAction::save( rviz::Config config ) const
00144   {
00145     rviz::Panel::save( config );
00146 
00147     rviz::Config topic_list_config = config.mapMakeChild( "topics" );
00148 
00149     std::vector<topicListLayout>::const_iterator it = topic_list_layouts_.begin();
00150     while( it != topic_list_layouts_.end()){
00151       topic_list_config.listAppendNew().setValue( it->topic_name_->text() );
00152       it ++;
00153     }
00154     config.mapSetValue( "Topic", output_topic_ );
00155   }
00156 
00157   // Load all configuration data for this panel from the given Config object.
00158   void CancelAction::load( const rviz::Config& config )
00159   {
00160     rviz::Panel::load( config );
00161     rviz::Config topic_list_config = config.mapGetChild( "topics" );
00162     int num_topics = topic_list_config.listLength();
00163 
00164     for( int i = 0; i < num_topics; i++ ) {
00165       addTopicList(topic_list_config.listChildAt( i ).getValue().toString().toStdString());
00166     }
00167   }
00168 
00169 }
00170 
00171 #include <pluginlib/class_list_macros.h>
00172 PLUGINLIB_EXPORT_CLASS(jsk_rviz_plugin::CancelAction, rviz::Panel )


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Mon Oct 6 2014 01:18:44