overlay_text_display.cpp
Go to the documentation of this file.
00001 // -*- mode: c++; -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2014, JSK Lab
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/o2r other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Willow Garage nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 
00036 #include "overlay_text_display.h"
00037 #include <OGRE/OgreMaterialManager.h>
00038 #include <rviz/uniform_string_stream.h>
00039 #include <OGRE/OgreTexture.h>
00040 #include <OGRE/OgreHardwarePixelBuffer.h>
00041 #include <QPainter>
00042 
00043 namespace jsk_rviz_plugin
00044 {
00045   OverlayTextDisplay::OverlayTextDisplay() : Display(),
00046                                              texture_width_(0), texture_height_(0),
00047                                              text_size_(14),
00048                                              line_width_(2),
00049                                              text_(""), font_(""),
00050                                              bg_color_(0, 0, 0, 0),
00051                                              fg_color_(255, 255, 255, 255.0),
00052                                              require_update_texture_(false)
00053   {
00054     update_topic_property_ = new rviz::RosTopicProperty(
00055       "Topic", "",
00056       ros::message_traits::datatype<jsk_rviz_plugins::OverlayText>(),
00057       "jsk_rviz_plugins::OverlayText topic to subscribe to.",
00058       this, SLOT( updateTopic() ));
00059   }
00060 
00061   
00062   OverlayTextDisplay::~OverlayTextDisplay()
00063   {
00064     onDisable();
00065     //delete overlay_;
00066     delete update_topic_property_;
00067   }
00068 
00069   void OverlayTextDisplay::onEnable()
00070   {
00071     if (overlay_) {
00072       overlay_->show();
00073     }
00074     subscribe();
00075   }
00076 
00077   void OverlayTextDisplay::onDisable()
00078   {
00079     if (overlay_) {
00080       overlay_->hide();
00081     }
00082     unsubscribe();
00083   }
00084   
00085   void OverlayTextDisplay::unsubscribe()
00086   {
00087     sub_.shutdown();
00088   }
00089 
00090   void OverlayTextDisplay::subscribe()
00091   {
00092     std::string topic_name = update_topic_property_->getTopicStd();
00093     if (topic_name.length() > 0 && topic_name != "/") {
00094       sub_ = ros::NodeHandle().subscribe(topic_name, 1, &OverlayTextDisplay::processMessage, this);
00095     }
00096   }
00097   
00098   void OverlayTextDisplay::updateTopic()
00099   {
00100     unsubscribe();
00101     subscribe();
00102   }
00103     
00104   // only the first time
00105   void OverlayTextDisplay::onInitialize()
00106   {
00107     onEnable();
00108     updateTopic();
00109     require_update_texture_ = true;
00110   }
00111   
00112   void OverlayTextDisplay::update(float wall_dt, float ros_dt)
00113   {
00114     if (!require_update_texture_) {
00115       return;
00116     }
00117     if (!isEnabled()) {
00118       return;
00119     }
00120     if (!overlay_) {
00121       return;
00122     }
00123     overlay_->updateTextureSize(texture_width_, texture_height_);
00124     {
00125       ScopedPixelBuffer buffer = overlay_->getBuffer();
00126       QImage Hud = buffer.getQImage(*overlay_, bg_color_);
00127       QPainter painter( &Hud );
00128       painter.setRenderHint(QPainter::Antialiasing, true);
00129       painter.setPen(QPen(fg_color_, line_width_ || 1, Qt::SolidLine));
00130       uint16_t w = overlay_->getTextureWidth();
00131       uint16_t h = overlay_->getTextureHeight();
00132 
00133       // font
00134       if (text_size_ != 0) {
00135         //QFont font = painter.font();
00136         QFont font(font_.length() > 0 ? font_.c_str(): "Arial");
00137         font.setPointSize(text_size_);
00138         font.setBold(true);
00139         painter.setFont(font);
00140       }
00141       if (text_.length() > 0) {
00142         //painter.drawText(0, 0, w, h, Qt::TextWordWrap | Qt::AlignLeft,
00143         painter.drawText(0, 0, w, h,
00144                          Qt::TextWordWrap | Qt::AlignLeft | Qt::AlignTop,
00145                          text_.c_str());
00146       }
00147       painter.end();
00148     }
00149     overlay_->setDimensions(overlay_->getTextureWidth(), overlay_->getTextureHeight());
00150     require_update_texture_ = false;
00151   }
00152   
00153   void OverlayTextDisplay::processMessage
00154   (const jsk_rviz_plugins::OverlayText::ConstPtr& msg)
00155   {
00156     if (!isEnabled()) {
00157       return;
00158     }
00159     if (!overlay_) {
00160       static int count = 0;
00161       rviz::UniformStringStream ss;
00162       ss << "OverlayTextDisplayObject" << count++;
00163       overlay_.reset(new OverlayObject(ss.str()));
00164       overlay_->show();
00165     }
00166     if (overlay_) {
00167       if (msg->action == jsk_rviz_plugins::OverlayText::DELETE) {
00168         overlay_->hide();
00169       }
00170       else if (msg->action == jsk_rviz_plugins::OverlayText::ADD) {
00171         overlay_->show();
00172       }
00173     }
00174     texture_width_ = msg->width;
00175     texture_height_ = msg->height;
00176     if (overlay_) {
00177       overlay_->setPosition(msg->left, msg->top);
00178     }
00179     // store message for update method
00180     text_ = msg->text;
00181     font_ = msg->font;
00182     bg_color_ = QColor(msg->bg_color.r * 255.0,
00183                        msg->bg_color.g * 255.0,
00184                        msg->bg_color.b * 255.0,
00185                        msg->bg_color.a * 255.0);
00186     fg_color_ = QColor(msg->fg_color.r * 255.0,
00187                        msg->fg_color.g * 255.0,
00188                        msg->fg_color.b * 255.0,
00189                        msg->fg_color.a * 255.0);
00190     text_size_ = msg->text_size;
00191     line_width_ = msg->line_width;
00192     require_update_texture_ = true;
00193   }
00194   
00195 }
00196 
00197 #include <pluginlib/class_list_macros.h>
00198 PLUGINLIB_EXPORT_CLASS( jsk_rviz_plugin::OverlayTextDisplay, rviz::Display )


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