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


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