view_controller.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, 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 <QColor>
00031 #include <QFont>
00032 #include <QKeyEvent>
00033 
00034 #include <OGRE/OgreCamera.h>
00035 #include <OGRE/OgreSceneManager.h>
00036 #include <OGRE/OgreSceneNode.h>
00037 
00038 #include "rviz/display_context.h"
00039 #include "rviz/frame_manager.h"
00040 #include "rviz/load_resource.h"
00041 #include "rviz/properties/enum_property.h"
00042 #include "rviz/properties/float_property.h"
00043 #include "rviz/render_panel.h"
00044 #include "rviz/selection/selection_manager.h"
00045 #include "rviz/view_manager.h"
00046 #include "rviz/viewport_mouse_event.h"
00047 #include "rviz/window_manager_interface.h"
00048 
00049 #include "rviz/view_controller.h"
00050 
00051 namespace rviz
00052 {
00053 
00054 ViewController::ViewController()
00055   : context_( NULL )
00056   , camera_( NULL )
00057   , is_active_( false )
00058   , type_property_( NULL )
00059 {
00060   near_clip_property_ = new FloatProperty( "Near Clip Distance", 0.01f,
00061                                       "Anything closer to the camera than this threshold will not get rendered.",
00062                                       this, SLOT( updateNearClipDistance() ) );
00063   near_clip_property_->setMin( 0.001 );
00064   near_clip_property_->setMax( 10000 );
00065 }
00066 
00067 void ViewController::initialize( DisplayContext* context )
00068 {
00069   context_ = context;
00070 
00071   std::stringstream ss;
00072   static int count = 0;
00073   ss << "ViewControllerCamera" << count++;
00074   camera_ = context_->getSceneManager()->createCamera( ss.str() );
00075   context_->getSceneManager()->getRootSceneNode()->attachObject( camera_ );
00076 
00077   setValue( formatClassId( getClassId() ));
00078   setReadOnly( true );
00079 
00080   // Do subclass initialization.
00081   onInitialize();
00082 
00083   cursor_ = getDefaultCursor();
00084 
00085   standard_cursors_[Default] = getDefaultCursor();
00086   standard_cursors_[Rotate2D] = makeIconCursor( "package://rviz/icons/rotate.svg" );
00087   standard_cursors_[Rotate3D] = makeIconCursor( "package://rviz/icons/rotate_cam.svg" );
00088   standard_cursors_[MoveXY] = makeIconCursor( "package://rviz/icons/move2d.svg" );
00089   standard_cursors_[MoveZ] = makeIconCursor( "package://rviz/icons/move_z.svg" );
00090   standard_cursors_[Zoom] = makeIconCursor( "package://rviz/icons/zoom.svg" );
00091   standard_cursors_[Crosshair] = makeIconCursor( "package://rviz/icons/crosshair.svg" );
00092 
00093   updateNearClipDistance();
00094 }
00095 
00096 ViewController::~ViewController()
00097 {
00098   context_->getSceneManager()->destroyCamera( camera_ );
00099 }
00100 
00101 QString ViewController::formatClassId( const QString& class_id )
00102 {
00103   QStringList id_parts = class_id.split( "/" );
00104   if( id_parts.size() != 2 )
00105   {
00106     // Should never happen with pluginlib class ids, which are
00107     // formatted like "package_name/class_name".  Not worth crashing
00108     // over though.
00109     return class_id;
00110   }
00111   else
00112   {
00113     return id_parts[ 1 ] + " (" + id_parts[ 0 ] + ")";
00114   }
00115 }
00116 
00117 QVariant ViewController::getViewData( int column, int role ) const
00118 {
00119   if( is_active_ )
00120   {
00121     switch( role )
00122     {
00123     case Qt::BackgroundRole:
00124     {
00125       return QColor( 0xba, 0xad, 0xa4 );
00126     }
00127     case Qt::FontRole:
00128     {
00129       QFont font;
00130       font.setBold( true );
00131       return font;
00132     }
00133     }
00134   }
00135   return Property::getViewData( column, role );
00136 }
00137 
00138 Qt::ItemFlags ViewController::getViewFlags( int column ) const
00139 {
00140   if( is_active_ )
00141   {
00142     return Property::getViewFlags( column );
00143   }
00144   else
00145   {
00146     return Property::getViewFlags( column ) | Qt::ItemIsDragEnabled;
00147   }
00148 }
00149 
00150 void ViewController::activate()
00151 {
00152   is_active_ = true;
00153   onActivate();
00154 }
00155 
00156 void ViewController::emitConfigChanged()
00157 {
00158   Q_EMIT configChanged();
00159 }
00160 
00161 void ViewController::load( const Config& config )
00162 {
00163   // Load the name by hand.
00164   QString name;
00165   if( config.mapGetString( "Name", &name ))
00166   {
00167     setName( name );
00168   }
00169   // Load all sub-properties the same way the base class does.
00170   Property::load( config );
00171 }
00172 
00173 void ViewController::save( Config config ) const
00174 {
00175   config.mapSetValue( "Class", getClassId() );
00176   config.mapSetValue( "Name", getName() );
00177 
00178   Property::save( config );
00179 }
00180 
00181 void ViewController::handleKeyEvent( QKeyEvent* event, RenderPanel* panel )
00182 {
00183   if( event->key() == Qt::Key_F &&
00184       panel->getViewport() &&
00185       context_->getSelectionManager() )
00186   {
00187     QPoint mouse_rel_panel = panel->mapFromGlobal( QCursor::pos() );
00188     Ogre::Vector3 point_rel_world; // output of get3DPoint().
00189     if( context_->getSelectionManager()->get3DPoint( panel->getViewport(),
00190                                                      mouse_rel_panel.x(), mouse_rel_panel.y(),
00191                                                      point_rel_world ))
00192     {
00193       lookAt( point_rel_world );
00194     }
00195   }
00196 
00197   if( event->key() == Qt::Key_Z )
00198   {
00199     reset();
00200   }
00201 }
00202 
00203 void ViewController::setCursor( CursorType cursor_type )
00204 {
00205   cursor_=standard_cursors_[cursor_type];
00206 }
00207 
00208 void ViewController::lookAt( float x, float y, float z )
00209 {
00210   Ogre::Vector3 point( x, y, z );
00211   lookAt( point );
00212 }
00213 
00214 void ViewController::setStatus( const QString & message )
00215 {
00216   if ( context_ )
00217   {
00218     context_->setStatus( message );
00219   }
00220 }
00221 
00222 void ViewController::updateNearClipDistance()
00223 {
00224   float n = near_clip_property_->getFloat();
00225   camera_->setNearClipDistance( n );
00226 }
00227 
00228 
00229 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Oct 6 2014 07:26:36