render_panel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <QApplication>
31 #include <QMenu>
32 #include <QTimer>
33 
34 #include <OgreSceneManager.h>
35 #include <OgreCamera.h>
36 
37 #include "rviz/display.h"
38 #include "rviz/view_controller.h"
42 
43 #include "rviz/render_panel.h"
44 
45 namespace rviz
46 {
47 
48 RenderPanel::RenderPanel( QWidget* parent )
49  : QtOgreRenderWindow( parent )
50  , mouse_x_( 0 )
51  , mouse_y_( 0 )
52  , context_( 0 )
53  , scene_manager_( 0 )
54  , view_controller_( 0 )
55  , context_menu_visible_(false)
56  , fake_mouse_move_event_timer_( new QTimer() )
57  , default_camera_(0)
58 {
59  setFocus( Qt::OtherFocusReason );
60 }
61 
63 {
66  {
67  scene_manager_->destroyCamera( default_camera_ );
68  }
69  if( scene_manager_ )
70  {
71  scene_manager_->removeListener( this );
72  }
73 }
74 
75 void RenderPanel::initialize(Ogre::SceneManager* scene_manager, DisplayContext* context)
76 {
77  context_ = context;
78  scene_manager_ = scene_manager;
79  scene_manager_->addListener( this );
80 
81  std::stringstream ss;
82  static int count = 0;
83  ss << "RenderPanelCamera" << count++;
84  default_camera_ = scene_manager_->createCamera(ss.str());
85  default_camera_->setNearClipDistance(0.01f);
86  default_camera_->setPosition(0, 10, 15);
87  default_camera_->lookAt(0, 0, 0);
88 
90 
91  connect( fake_mouse_move_event_timer_, SIGNAL( timeout() ), this, SLOT( sendMouseMoveEvent() ));
92  fake_mouse_move_event_timer_->start( 33 /*milliseconds*/ );
93 }
94 
96 {
97  QPoint cursor_pos = QCursor::pos();
98  QPoint mouse_rel_widget = mapFromGlobal( cursor_pos );
99  if( rect().contains( mouse_rel_widget ))
100  {
101  bool mouse_over_this = false;
102  QWidget *w = QApplication::widgetAt( cursor_pos );
103  while( w )
104  {
105  if( w == this )
106  {
107  mouse_over_this = true;
108  break;
109  }
110  w = w->parentWidget();
111  }
112  if( !mouse_over_this )
113  {
114  return;
115  }
116 
117  QMouseEvent fake_event( QEvent::MouseMove,
118  mouse_rel_widget,
119  Qt::NoButton,
120  QApplication::mouseButtons(),
121  QApplication::keyboardModifiers() );
122  onRenderWindowMouseEvents( &fake_event );
123  }
124 }
125 void RenderPanel::leaveEvent ( QEvent * event )
126 {
127  setCursor( Qt::ArrowCursor );
128  if ( context_ )
129  {
130  context_->setStatus("");
131  }
132 }
133 
134 void RenderPanel::onRenderWindowMouseEvents( QMouseEvent* event )
135 {
136  int last_x = mouse_x_;
137  int last_y = mouse_y_;
138 
139  mouse_x_ = event->x();
140  mouse_y_ = event->y();
141 
142  if (context_)
143  {
144  setFocus( Qt::MouseFocusReason );
145 
146  ViewportMouseEvent vme(this, getViewport(), event, last_x, last_y);
148  event->accept();
149  }
150 }
151 
152 void RenderPanel::wheelEvent( QWheelEvent* event )
153 {
154  int last_x = mouse_x_;
155  int last_y = mouse_y_;
156 
157  mouse_x_ = event->x();
158  mouse_y_ = event->y();
159 
160  if (context_)
161  {
162  setFocus( Qt::MouseFocusReason );
163 
164  ViewportMouseEvent vme(this, getViewport(), event, last_x, last_y);
166  event->accept();
167  }
168 }
169 
170 void RenderPanel::keyPressEvent( QKeyEvent* event )
171 {
172  if( context_ )
173  {
174  context_->handleChar( event, this );
175  }
176 }
177 
179 {
180  view_controller_ = controller;
181 
182  if( view_controller_ )
183  {
186  }
187  else
188  {
189  setCamera( NULL );
190  }
191 }
192 
194 {
195  boost::mutex::scoped_lock lock(context_menu_mutex_);
196  context_menu_ = menu;
197  context_menu_visible_ = true;
198 
199  QApplication::postEvent( this, new QContextMenuEvent( QContextMenuEvent::Mouse, QPoint() ));
200 }
201 
203 {
204  context_menu_visible_ = false;
205 }
206 
208 {
209  return context_menu_visible_;
210 }
211 
212 void RenderPanel::contextMenuEvent( QContextMenuEvent* event )
213 {
214  boost::shared_ptr<QMenu> context_menu;
215  {
216  boost::mutex::scoped_lock lock(context_menu_mutex_);
217  context_menu.swap(context_menu_);
218  }
219 
220  if ( context_menu )
221  {
222  connect( context_menu.get(), SIGNAL( aboutToHide() ), this, SLOT( onContextMenuHide() ) );
223  context_menu->exec( QCursor::pos() );
224  }
225 }
226 
227 void RenderPanel::sceneManagerDestroyed( Ogre::SceneManager* destroyed_scene_manager )
228 {
229  if( destroyed_scene_manager == scene_manager_ )
230  {
233  setCamera( NULL );
234  }
235 }
236 
237 } // namespace rviz
virtual void keyPressEvent(QKeyEvent *event)
virtual void setStatus(const QString &message)=0
#define NULL
Definition: global.h:37
ViewController * view_controller_
Definition: render_panel.h:131
virtual void handleMouseEvent(const ViewportMouseEvent &event)=0
Handle a mouse event.
void initialize(Ogre::SceneManager *scene_manager, DisplayContext *manager)
f
void setViewController(ViewController *controller)
Set the ViewController which should control the camera position for this view.
void showContextMenu(boost::shared_ptr< QMenu > menu)
virtual ~RenderPanel()
virtual void handleChar(QKeyEvent *event, RenderPanel *panel)=0
Handle a single key event for a given RenderPanel.
Ogre::SceneManager * scene_manager_
Definition: render_panel.h:129
void onRenderWindowMouseEvents(QMouseEvent *event)
Called when any mouse event happens inside the render window.
Pure-virtual base class for objects which give Display subclasses context in which to work...
int mouse_y_
Y position of the last mouse event.
Definition: render_panel.h:126
Ogre::Camera * getCamera() const
DisplayContext * context_
Definition: render_panel.h:128
QTimer * fake_mouse_move_event_timer_
Definition: render_panel.h:147
int mouse_x_
X position of the last mouse event.
Definition: render_panel.h:125
TFSIMD_FORCE_INLINE const tfScalar & w() const
virtual void sceneManagerDestroyed(Ogre::SceneManager *source)
Ogre::Viewport * getViewport() const
boost::shared_ptr< QMenu > context_menu_
Definition: render_panel.h:133
virtual void wheelEvent(QWheelEvent *event)
Called when there is a mouse-wheel event.
void activate()
Called by RenderPanel when this view controller is about to be used.
void setCamera(Ogre::Camera *camera)
virtual void leaveEvent(QEvent *event)
boost::mutex context_menu_mutex_
Definition: render_panel.h:134
Ogre::Camera * default_camera_
A default camera created in initialize().
Definition: render_panel.h:148
void contextMenuEvent(QContextMenuEvent *event)
RenderPanel(QWidget *parent=0)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51