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


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:28