render_panel.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, 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 <QApplication>
00031 #include <QMenu>
00032 #include <QTimer>
00033 
00034 #include <OgreSceneManager.h>
00035 #include <OgreCamera.h>
00036 
00037 #include "rviz/display.h"
00038 #include "rviz/view_controller.h"
00039 #include "rviz/viewport_mouse_event.h"
00040 #include "rviz/visualization_manager.h"
00041 #include "rviz/window_manager_interface.h"
00042 
00043 #include "rviz/render_panel.h"
00044 
00045 namespace rviz
00046 {
00047 
00048 RenderPanel::RenderPanel( QWidget* parent )
00049   : QtOgreRenderWindow( parent )
00050   , mouse_x_( 0 )
00051   , mouse_y_( 0 )
00052   , context_( 0 )
00053   , scene_manager_( 0 )
00054   , view_controller_( 0 )
00055   , fake_mouse_move_event_timer_( new QTimer() )
00056   , default_camera_(0)
00057   , context_menu_visible_(false)
00058 {
00059   setFocus( Qt::OtherFocusReason );
00060 }
00061 
00062 RenderPanel::~RenderPanel()
00063 {
00064   delete fake_mouse_move_event_timer_;
00065   if( scene_manager_ && default_camera_ )
00066   {
00067     scene_manager_->destroyCamera( default_camera_ );
00068   }
00069   if( scene_manager_ )
00070   {
00071     scene_manager_->removeListener( this );
00072   }
00073 }
00074 
00075 void RenderPanel::initialize(Ogre::SceneManager* scene_manager, DisplayContext* context)
00076 {
00077   context_ = context;
00078   scene_manager_ = scene_manager;
00079   scene_manager_->addListener( this );
00080 
00081   std::stringstream ss;
00082   static int count = 0;
00083   ss << "RenderPanelCamera" << count++;
00084   default_camera_ = scene_manager_->createCamera(ss.str());
00085   default_camera_->setNearClipDistance(0.01f);
00086   default_camera_->setPosition(0, 10, 15);
00087   default_camera_->lookAt(0, 0, 0);
00088 
00089   setCamera( default_camera_ );
00090 
00091   connect( fake_mouse_move_event_timer_, SIGNAL( timeout() ), this, SLOT( sendMouseMoveEvent() ));
00092   fake_mouse_move_event_timer_->start( 33 /*milliseconds*/ );
00093 }
00094 
00095 void RenderPanel::sendMouseMoveEvent()
00096 {
00097   QPoint cursor_pos = QCursor::pos();
00098   QPoint mouse_rel_widget = mapFromGlobal( cursor_pos );
00099   if( rect().contains( mouse_rel_widget ))
00100   {
00101     bool mouse_over_this = false;
00102     QWidget *w = QApplication::widgetAt( cursor_pos );
00103     while( w )
00104     {
00105       if( w == this )
00106       {
00107         mouse_over_this = true;
00108         break;
00109       }
00110       w = w->parentWidget();
00111     }
00112     if( !mouse_over_this )
00113     {
00114       return;
00115     }
00116 
00117     QMouseEvent fake_event( QEvent::MouseMove,
00118                             mouse_rel_widget,
00119                             Qt::NoButton,
00120                             QApplication::mouseButtons(),
00121                             QApplication::keyboardModifiers() );
00122     onRenderWindowMouseEvents( &fake_event );
00123   }
00124 }
00125 void RenderPanel::leaveEvent ( QEvent * event )
00126 {
00127   setCursor( Qt::ArrowCursor );
00128   if ( context_ )
00129   {
00130     context_->setStatus("");
00131   }
00132 }
00133 
00134 void RenderPanel::onRenderWindowMouseEvents( QMouseEvent* event )
00135 {
00136   int last_x = mouse_x_;
00137   int last_y = mouse_y_;
00138 
00139   mouse_x_ = event->x();
00140   mouse_y_ = event->y();
00141 
00142   if (context_)
00143   {
00144     setFocus( Qt::MouseFocusReason );
00145 
00146     ViewportMouseEvent vme(this, getViewport(), event, last_x, last_y);
00147     context_->handleMouseEvent(vme);
00148     event->accept();
00149   }
00150 }
00151 
00152 void RenderPanel::wheelEvent( QWheelEvent* event )
00153 {
00154   int last_x = mouse_x_;
00155   int last_y = mouse_y_;
00156 
00157   mouse_x_ = event->x();
00158   mouse_y_ = event->y();
00159 
00160   if (context_)
00161   {
00162     setFocus( Qt::MouseFocusReason );
00163 
00164     ViewportMouseEvent vme(this, getViewport(), event, last_x, last_y);
00165     context_->handleMouseEvent(vme);
00166     event->accept();
00167   }
00168 }
00169 
00170 void RenderPanel::keyPressEvent( QKeyEvent* event )
00171 {
00172   if( context_ )
00173   {
00174     context_->handleChar( event, this );
00175   }
00176 }
00177 
00178 void RenderPanel::setViewController( ViewController* controller )
00179 {
00180   view_controller_ = controller;
00181 
00182   if( view_controller_ )
00183   {
00184     setCamera( view_controller_->getCamera() );
00185     view_controller_->activate();
00186   }
00187   else
00188   {
00189     setCamera( NULL );
00190   }
00191 }
00192 
00193 void RenderPanel::showContextMenu( boost::shared_ptr<QMenu> menu )
00194 {
00195   boost::mutex::scoped_lock lock(context_menu_mutex_);
00196   context_menu_ = menu;
00197   context_menu_visible_ = true;
00198 
00199   QApplication::postEvent( this, new QContextMenuEvent( QContextMenuEvent::Mouse, QPoint() ));
00200 }
00201 
00202 void RenderPanel::onContextMenuHide()
00203 {
00204   context_menu_visible_ = false;
00205 }
00206 
00207 bool RenderPanel::contextMenuVisible()
00208 {
00209   return context_menu_visible_;
00210 }
00211 
00212 void RenderPanel::contextMenuEvent( QContextMenuEvent* event )
00213 {
00214   boost::shared_ptr<QMenu> context_menu;
00215   {
00216     boost::mutex::scoped_lock lock(context_menu_mutex_);
00217     context_menu.swap(context_menu_);
00218   }
00219 
00220   if ( context_menu )
00221   {
00222     connect( context_menu.get(), SIGNAL( aboutToHide() ), this, SLOT( onContextMenuHide() ) );
00223     context_menu->exec( QCursor::pos() );
00224   }
00225 }
00226 
00227 void RenderPanel::sceneManagerDestroyed( Ogre::SceneManager* destroyed_scene_manager )
00228 {
00229   if( destroyed_scene_manager == scene_manager_ )
00230   {
00231     scene_manager_ = NULL;
00232     default_camera_ = NULL;
00233     setCamera( NULL );
00234   }
00235 }
00236 
00237 } // namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Tue Oct 3 2017 03:19:31