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 
00033 #include "render_panel.h"
00034 #include "visualization_manager.h"
00035 #include "display.h"
00036 #include "display_wrapper.h"
00037 #include "tools/tool.h"
00038 #include "viewport_mouse_event.h"
00039 #include "view_controller.h"
00040 
00041 #include <OGRE/OgreRoot.h>
00042 #include <OGRE/OgreViewport.h>
00043 
00044 namespace rviz
00045 {
00046 
00047 RenderPanel::RenderPanel( QWidget* parent )
00048   : QtOgreRenderWindow( parent )
00049   , mouse_x_( 0 )
00050   , mouse_y_( 0 )
00051   , manager_( 0 )
00052   , scene_manager_( 0 )
00053   , camera_( 0 )
00054   , view_controller_( 0 )
00055   , fake_mouse_move_event_timer_( new QTimer() )
00056 {
00057   setFocus( Qt::OtherFocusReason );
00058 }
00059 
00060 RenderPanel::~RenderPanel()
00061 {
00062   delete fake_mouse_move_event_timer_;
00063   delete view_controller_;
00064   scene_manager_->destroyCamera(camera_);
00065 }
00066 
00067 void RenderPanel::initialize(Ogre::SceneManager* scene_manager, VisualizationManager* manager)
00068 {
00069   manager_ = manager;
00070   scene_manager_ = scene_manager;
00071 
00072   std::stringstream ss;
00073   static int count = 0;
00074   ss << "RenderPanelCamera" << count++;
00075   camera_ = scene_manager_->createCamera(ss.str());
00076 
00077   setCamera( camera_ );
00078 
00079   connect( fake_mouse_move_event_timer_, SIGNAL( timeout() ), this, SLOT( sendMouseMoveEvent() ));
00080   fake_mouse_move_event_timer_->start( 33 /*milliseconds*/ );
00081 }
00082 
00083 void RenderPanel::sendMouseMoveEvent()
00084 {
00085   QPoint cursor_pos = QCursor::pos();
00086   QPoint mouse_rel_widget = mapFromGlobal( cursor_pos );
00087   if( rect().contains( mouse_rel_widget ))
00088   {
00089     bool mouse_over_this = false;
00090     QWidget *w = QApplication::widgetAt( cursor_pos );
00091     while( w )
00092     {
00093       if( w == this )
00094       {
00095         mouse_over_this = true;
00096         break;
00097       }
00098       w = w->parentWidget();
00099     }
00100     if( !mouse_over_this )
00101     {
00102       return;
00103     }
00104 
00105     QMouseEvent fake_event( QEvent::MouseMove,
00106                             mouse_rel_widget,
00107                             Qt::NoButton,
00108                             QApplication::mouseButtons(),
00109                             QApplication::keyboardModifiers() );
00110     onRenderWindowMouseEvents( &fake_event );
00111   }
00112 }
00113 
00114 void RenderPanel::onRenderWindowMouseEvents( QMouseEvent* event )
00115 {
00116   int last_x = mouse_x_;
00117   int last_y = mouse_y_;
00118 
00119   mouse_x_ = event->x();
00120   mouse_y_ = event->y();
00121 
00122   if (manager_)
00123   {
00124     setFocus( Qt::MouseFocusReason );
00125 
00126     ViewportMouseEvent vme(this, getViewport(), event, last_x, last_y);
00127     manager_->handleMouseEvent(vme);
00128     event->accept();
00129   }
00130 }
00131 
00132 void RenderPanel::wheelEvent( QWheelEvent* event )
00133 {
00134   int last_x = mouse_x_;
00135   int last_y = mouse_y_;
00136 
00137   mouse_x_ = event->x();
00138   mouse_y_ = event->y();
00139 
00140   if (manager_)
00141   {
00142     setFocus( Qt::MouseFocusReason );
00143 
00144     ViewportMouseEvent vme(this, getViewport(), event, last_x, last_y);
00145     manager_->handleMouseEvent(vme);
00146     event->accept();
00147   }
00148 }
00149 
00150 void RenderPanel::keyPressEvent( QKeyEvent* event )
00151 {
00152   if( manager_ )
00153   {
00154     manager_->handleChar( event, this );
00155   }
00156 }
00157 
00158 void RenderPanel::setViewController(ViewController* controller)
00159 {
00160   if (view_controller_)
00161   {
00162     view_controller_->deactivate();
00163   }
00164 
00165   delete view_controller_;
00166   view_controller_ = controller;
00167 
00168   view_controller_->activate(camera_, manager_ ? manager_->getTargetFrame() : "");
00169 }
00170 
00171 void RenderPanel::showContextMenu( boost::shared_ptr<QMenu> menu )
00172 {
00173   boost::mutex::scoped_lock lock(context_menu_mutex_);
00174   context_menu_ = menu;
00175 
00176   QApplication::postEvent( this, new QContextMenuEvent( QContextMenuEvent::Mouse, QPoint() ));
00177 }
00178 
00179 void RenderPanel::contextMenuEvent( QContextMenuEvent* event )
00180 {
00181   boost::shared_ptr<QMenu> context_menu;
00182   {
00183     boost::mutex::scoped_lock lock(context_menu_mutex_);
00184     context_menu.swap(context_menu_);
00185   }
00186 
00187   if ( context_menu )
00188   {
00189     context_menu->exec( QCursor::pos() );
00190   }
00191 }
00192 
00193 } // namespace rviz


rviz
Author(s): Dave Hershberger, Josh Faust
autogenerated on Mon Jan 6 2014 11:54:32