TextOutDisplay.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  TalkingHead - A talking head for robots
00003  *  Copyright (C) 2012 AG Aktives Sehen <agas@uni-koblenz.de>
00004  *                     Universitaet Koblenz-Landau
00005  *
00006  *  This program is free software: you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation, either version 3 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014  *  Library General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Library General Public
00017  *  License along with this library; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00019  *  MA 02110-1301  USA or see <http://www.gnu.org/licenses/>.
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   // Text output
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 );  // create internal timer
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 }


robot_face
Author(s): AGAS, Julian Giesen, David Gossow
autogenerated on Mon Jan 6 2014 11:41:09