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 <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 );
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 }