camera_focus_frontend.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009, 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 "pr2_interactive_manipulation/camera_focus_frontend.h"
00031 
00032 #include <view_controller_msgs/CameraPlacement.h>
00033 
00034 #include "object_manipulator/tools/camera_configurations.h"
00035 
00036 #include <rviz/window_manager_interface.h>
00037 #include <rviz/display_context.h>
00038 #include <rviz/display.h>
00039 #include <rviz/view_controller.h>
00040 #include <rviz/selection/selection_manager.h>
00041 #include <rviz/properties/property.h>
00042 #include <rviz/properties/ros_topic_property.h>
00043 #include <rviz/load_resource.h>
00044 
00045 #include <QMainWindow>
00046 #include <QToolBar>
00047 #include <QAction>
00048 #include <QPainter>
00049 
00050 namespace pr2_interactive_manipulation {
00051 
00052 CameraFocusFrontend::CameraFocusFrontend() :
00053   root_nh_(""),
00054   toolbar_( 0 )
00055 {
00056   topic_prop_ = new rviz::RosTopicProperty( "Command topic", "/rviz/camera_placement",
00057       QString::fromStdString(ros::message_traits::datatype<view_controller_msgs::CameraPlacement>() ),
00058       "Topic on which to send out camera placement messages.", this , SLOT( updateTopic() ) );
00059 }
00060 
00061 CameraFocusFrontend::~CameraFocusFrontend()
00062 {
00063   delete toolbar_actions_;
00064   delete toolbar_;
00065 }
00066 
00067 void CameraFocusFrontend::addFocusButton( QString id )
00068 {
00069   // load icon
00070   QPixmap icon_fg = rviz::loadPixmap( "package://pr2_interactive_manipulation_frontend/icons/"+id+".png" );
00071   icon_fg = icon_fg.scaled( 32, 32 );
00072 
00073   // load background image
00074   QPixmap icon_bg = rviz::loadPixmap( "package://pr2_interactive_manipulation_frontend/icons/icon_bg.svg" );
00075 
00076   // draw icon on top of bg
00077   QPainter painter( &icon_bg );
00078   painter.drawPixmap( 0, 0, icon_fg );
00079 
00080   // add action button to toolbar
00081   QString name  = icon_bg.isNull() ? id : "";
00082   QAction* action = new QAction( icon_bg, id, toolbar_ );
00083   action->setData( QVariant( id ) ); // store the focus id in the action
00084   toolbar_actions_->addAction( action );
00085   toolbar_->addAction( action );
00086 }
00087 
00088 void CameraFocusFrontend::onDisable()
00089 {
00090   toolbar_->setVisible(false);
00091 }
00092 
00093 void CameraFocusFrontend::onEnable()
00094 {
00095   toolbar_->setVisible(true);
00096 }
00097 
00098 void CameraFocusFrontend::onInitialize()
00099 {
00100   rviz::WindowManagerInterface* window_context = context_->getWindowManager();
00101   QMainWindow* main_win = dynamic_cast<QMainWindow*>( window_context );
00102 
00103   if ( !main_win )
00104   {
00105     setStatusStd( rviz::StatusProperty::Error, "Initialization", "This display needs a QMainWindow as Window Manager." );
00106     return;
00107   }
00108 
00109   toolbar_ = main_win->addToolBar( "CameraFocus" );
00110   toolbar_->setObjectName( "CameraFocus" );
00111   toolbar_->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
00112 
00113   toolbar_actions_ = new QActionGroup( this );
00114   connect( toolbar_actions_, SIGNAL( triggered( QAction* )), this, SLOT( changeView( QAction* )));
00115 
00116   addFocusButton("left");
00117   addFocusButton("right");
00118   addFocusButton("facing");
00119   addFocusButton("top");
00120   //addFocusButton("front");
00121   addFocusButton("overhead");
00122   addFocusButton("kinect");
00123 
00124   updateTopic();
00125 }
00126 
00127 void CameraFocusFrontend::changeView( QAction* action )
00128 {
00129   processButtonClick( action->data().toString().toStdString() );
00130 }
00131 
00132 void CameraFocusFrontend::updateTopic()
00133 {
00134   pub_ = root_nh_.advertise<view_controller_msgs::CameraPlacement>(topic_prop_->getStdString(), 5);
00135 }
00136 
00137 void CameraFocusFrontend::processButtonClick(const std::string &name)
00138 {
00139   view_controller_msgs::CameraPlacement cp;
00140   try
00141   {
00142     if(object_manipulator::cameraConfigurations().get_camera_placement(name.c_str(), cp.eye, cp.focus, cp.up))
00143     {
00144       cp.target_frame = (name == "kinect") ? "head_mount_kinect_rgb_link" : "<Fixed Frame>" ; // special case for kinect...
00145       cp.time_from_start = ros::Duration(0.6);
00146       ROS_DEBUG_STREAM("Publishing camera placement request [" << name << "]: " << cp);
00147       pub_.publish(cp);
00148     }
00149     else
00150     {
00151       ROS_ERROR("There was an error processing pre-defined camera position [%s]", name.c_str());
00152     }
00153   }
00154   catch(...)
00155   {
00156     ROS_ERROR("There was an unhandled exception processing pre-defined camera position [%s]", name.c_str());
00157   }
00158 }
00159 
00160 }  // namespace
00161 
00162 
00163 #include <pluginlib/class_list_macros.h>
00164 PLUGINLIB_EXPORT_CLASS(pr2_interactive_manipulation::CameraFocusFrontend,rviz::Display )


pr2_interactive_manipulation_frontend
Author(s): Jonathan Binney
autogenerated on Mon Oct 6 2014 12:06:28