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 #include "TextOutDisplay.h"
00023
00024 #include <QLabel>
00025 #include <QTimer>
00026 #include <QVBoxLayout>
00027
00028 #include <string>
00029
00030 TextOutDisplay::TextOutDisplay(int min_height, int font_size, bool user_input, QWidget* parent) :
00031 QWidget( parent )
00032 {
00033 user_input_ = user_input;
00034
00035 QVBoxLayout* layout = new QVBoxLayout( this );
00036
00037 smileys_.push_back( ">:" );
00038 smileys_.push_back( ":)" );
00039 smileys_.push_back( ":(" );
00040 smileys_.push_back( ":O" );
00041 smileys_.push_back( ":o" );
00042 smileys_.push_back( ":!" );
00043 smileys_.push_back( ":&" );
00044
00045
00046 font_.setPixelSize(font_size);
00047 font_.setBold(true);
00048
00049 text_out_label_ = new QLabel( this );
00050 text_out_label_->setFont( font_ );
00051 text_out_label_->setMinimumSize( 100, min_height );
00052 text_out_label_->setAlignment ( Qt::AlignCenter );
00053 text_out_label_->setWordWrap ( true );
00054
00055 layout->addWidget( text_out_label_ );
00056
00057 setLayout( layout );
00058
00059 reset_timer_ = new QTimer ( this );
00060 connect ( reset_timer_, SIGNAL ( timeout() ), SLOT ( clearText() ) );
00061 reset_timer_->start ( 1000 / 25 );
00062
00063 setVisible( false );
00064
00065 text_ = "";
00066 }
00067
00068
00069 TextOutDisplay::~TextOutDisplay()
00070 {
00071 }
00072
00073 void TextOutDisplay::clearText()
00074 {
00075 if( text_ != "" )
00076 {
00077 setText( text_ );
00078 text_ = "";
00079 }
00080 else
00081 {
00082 setText( "" );
00083 text_out_label_->setText( "" );
00084 setVisible( false );
00085 }
00086 }
00087
00088 void TextOutDisplay::setText( std::string text )
00089 {
00090 if ( text == "" )
00091 {
00092 reset_timer_->start ( 1000 / 25 );
00093 }
00094 else
00095 {
00096 if( user_input_ )
00097 {
00098 font_.setPixelSize( 20 );
00099 text_out_label_->setFont(font_);
00100 setVisible( true );
00101 text_out_label_->setText( text.c_str() );
00102 reset_timer_->start( 10000 );
00103 }
00104 else
00105 {
00106 if( text.length() >= 130 )
00107 {
00108 font_.setPixelSize( 18 );
00109 text_out_label_->setFont(font_);
00110 }
00111 else
00112 {
00113 font_.setPixelSize( 27 );
00114 text_out_label_->setFont(font_);
00115 }
00116 setVisible( true );
00117 text_out_label_->setText( text.c_str() );
00118 reset_timer_->start ( 100000 );
00119 }
00120 }
00121 }
00122
00123 void TextOutDisplay::subscribeWithNodeHandle( ros::NodeHandle node_handle )
00124 {
00125 if( user_input_ )
00126 {
00127 user_input_subscriber_ = node_handle.subscribe( "robot_face/user_input", 1, &TextOutDisplay::callbackText, this );
00128 }
00129 else
00130 {
00131 text_out_subscriber_ = node_handle.subscribe( "robot_face/text_out", 1, &TextOutDisplay::callbackText, this );
00132 talking_finished_subscriber_ = node_handle.subscribe( "robot_face/talking_finished", 1, &TextOutDisplay::callbackTalkingFinished, this );
00133 }
00134 }
00135
00136 void TextOutDisplay::callbackText( const std_msgs::String::ConstPtr& msg )
00137 {
00138 text_ = prepareText( msg->data );
00139 if( user_input_ )
00140 {
00141 reset_timer_->start( text_.length() * 10 );
00142 }
00143 }
00144
00145 void TextOutDisplay::callbackTalkingFinished( const std_msgs::String::ConstPtr& msg )
00146 {
00147 reset_timer_->start( text_.length() * 10 );
00148 }
00149
00150 std::string TextOutDisplay::prepareText( std::string text )
00151 {
00152 std::string tmp_text = text;
00153
00154 tmp_text = trimSpaces(tmp_text);
00155 tmp_text = clearSmileys(tmp_text);
00156
00157 tmp_text = trimSpaces( tmp_text );
00158
00159 if( tmp_text == ".")
00160 {
00161 tmp_text = "";
00162 }
00163
00164 return tmp_text;
00165 }
00166
00167 std::string TextOutDisplay::clearSmileys( std::string text )
00168 {
00169 std::string tmp_text = text;
00170
00171 size_t i_smiley = std::string::npos;
00172
00173 for( unsigned int j = 0; j < smileys_.size(); j++ )
00174 {
00175 for( unsigned int i = 0; i <= tmp_text.length(); i++ )
00176 {
00177 i_smiley = tmp_text.find( smileys_.at( j ), 0 );
00178 if( i_smiley != std::string::npos )
00179 {
00180 tmp_text.erase(i_smiley, smileys_.at( j ).length());
00181 }
00182 }
00183 }
00184
00185 return tmp_text;
00186 }
00187
00188 std::string TextOutDisplay::trimSpaces( std::string text )
00189 {
00190 std::string tmp_text = text;
00191
00192 size_t startpos = tmp_text.find_first_not_of(" \t");
00193 size_t endpos = tmp_text.find_last_not_of(" \t");
00194
00195 if(( std::string::npos == startpos ) || ( std::string::npos == endpos))
00196 {
00197 tmp_text = "";
00198 }
00199 else
00200 {
00201 tmp_text = tmp_text.substr( startpos, endpos-startpos+1 );
00202 }
00203
00204 return tmp_text;
00205 }