Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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 );
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 }