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 RenderPanel::RenderPanel(QWidget* parent)
48  : QtOgreRenderWindow(parent)
49  , mouse_x_(0)
50  , mouse_y_(0)
51  , focus_on_mouse_move_(true)
52  , context_(nullptr)
53  , scene_manager_(nullptr)
54  , view_controller_(nullptr)
55  , context_menu_visible_(false)
56  // TODO(simonschmeisser) remove this in noetic
57  , fake_mouse_move_event_timer_(new QTimer())
58  , default_camera_(nullptr)
59 {
60  setFocusPolicy(Qt::WheelFocus);
61  setFocus(Qt::OtherFocusReason);
62  setMouseTracking(true);
63 }
64 
66 {
67  // TODO(simonschmeisser) remove this in noetic
70  {
71  scene_manager_->destroyCamera(default_camera_);
72  }
73  if (scene_manager_)
74  {
75  scene_manager_->removeListener(this);
76  }
77 }
78 
79 void RenderPanel::initialize(Ogre::SceneManager* scene_manager, DisplayContext* context)
80 {
81  context_ = context;
82  scene_manager_ = scene_manager;
83  scene_manager_->addListener(this);
84 
85  std::stringstream ss;
86  static int count = 0;
87  ss << "RenderPanelCamera" << count++;
88  default_camera_ = scene_manager_->createCamera(ss.str());
89  default_camera_->setNearClipDistance(0.01f);
90  default_camera_->setPosition(0, 10, 15);
91  default_camera_->lookAt(0, 0, 0);
92 
94 }
95 
96 // TODO(simonschmeisser) remove this in noetic
98 {
99  QPoint cursor_pos = QCursor::pos();
100  QPoint mouse_rel_widget = mapFromGlobal(cursor_pos);
101  if (rect().contains(mouse_rel_widget))
102  {
103  bool mouse_over_this = false;
104  QWidget* w = QApplication::widgetAt(cursor_pos);
105  while (w)
106  {
107  if (w == this)
108  {
109  mouse_over_this = true;
110  break;
111  }
112  w = w->parentWidget();
113  }
114  if (!mouse_over_this)
115  {
116  return;
117  }
118 
119  QMouseEvent fake_event(QEvent::MouseMove, mouse_rel_widget, Qt::NoButton,
120  QApplication::mouseButtons(), QApplication::queryKeyboardModifiers());
121  onRenderWindowMouseEvents(&fake_event);
122  }
123 }
124 
125 void RenderPanel::leaveEvent(QEvent* /*event*/)
126 {
127  setCursor(Qt::ArrowCursor);
128  if (context_)
129  {
130  context_->setStatus("");
131  }
132 }
133 
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  {
145  {
146  setFocus(Qt::MouseFocusReason);
147  }
148 
149  ViewportMouseEvent vme(this, getViewport(), event, last_x, last_y);
151  event->accept();
152  }
153 }
154 
155 void RenderPanel::wheelEvent(QWheelEvent* event)
156 {
157  int last_x = mouse_x_;
158  int last_y = mouse_y_;
159 
160  mouse_x_ = event->x();
161  mouse_y_ = event->y();
162 
163  if (context_)
164  {
165  ViewportMouseEvent vme(this, getViewport(), event, last_x, last_y);
167  event->accept();
168  }
169 }
170 
171 void RenderPanel::keyPressEvent(QKeyEvent* event)
172 {
173  if (context_)
174  {
175  context_->handleChar(event, this);
176  }
177 }
178 
180 {
181  view_controller_ = controller;
182 
183  if (view_controller_)
184  {
187  }
188  else
189  {
190  setCamera(nullptr);
191  }
192 }
193 
195 {
196  boost::mutex::scoped_lock lock(context_menu_mutex_);
197  context_menu_ = menu;
198  context_menu_visible_ = true;
199 
200  QApplication::postEvent(this, new QContextMenuEvent(QContextMenuEvent::Mouse, QPoint()));
201 }
202 
204 {
205  context_menu_visible_ = false;
206 }
207 
209 {
210  return context_menu_visible_;
211 }
212 
213 void RenderPanel::contextMenuEvent(QContextMenuEvent* /*event*/)
214 {
215  boost::shared_ptr<QMenu> context_menu;
216  {
217  boost::mutex::scoped_lock lock(context_menu_mutex_);
218  context_menu.swap(context_menu_);
219  }
220 
221  if (context_menu)
222  {
223  connect(context_menu.get(), SIGNAL(aboutToHide()), this, SLOT(onContextMenuHide()));
224  context_menu->exec(QCursor::pos());
225  }
226 }
227 
228 void RenderPanel::sceneManagerDestroyed(Ogre::SceneManager* destroyed_scene_manager)
229 {
230  if (destroyed_scene_manager == scene_manager_)
231  {
232  scene_manager_ = nullptr;
233  default_camera_ = nullptr;
234  setCamera(nullptr);
235  }
236 }
237 
239 {
240  return focus_on_mouse_move_;
241 }
242 
244 {
245  focus_on_mouse_move_ = enabled;
246 }
247 
248 } // namespace rviz
virtual void setStatus(const QString &message)=0
ViewController * view_controller_
Definition: render_panel.h:155
virtual void handleMouseEvent(const ViewportMouseEvent &event)=0
Handle a mouse event.
void initialize(Ogre::SceneManager *scene_manager, DisplayContext *manager)
f
void sceneManagerDestroyed(Ogre::SceneManager *source) override
Ogre::Camera * getCamera() const
void setFocusOnMouseMove(bool enabled)
void setViewController(ViewController *controller)
Set the ViewController which should control the camera position for this view.
void showContextMenu(boost::shared_ptr< QMenu > menu)
bool getFocusOnMouseMove() const
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:153
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:149
Ogre::Viewport * getViewport() const
void leaveEvent(QEvent *event) override
DisplayContext * context_
Definition: render_panel.h:152
QTimer * fake_mouse_move_event_timer_
Definition: render_panel.h:173
bool focus_on_mouse_move_
a moving the mouse catches keyboard focus
Definition: render_panel.h:150
int mouse_x_
X position of the last mouse event.
Definition: render_panel.h:148
boost::shared_ptr< QMenu > context_menu_
Definition: render_panel.h:157
~RenderPanel() override
void wheelEvent(QWheelEvent *event) override
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)
void keyPressEvent(QKeyEvent *event) override
boost::mutex context_menu_mutex_
Definition: render_panel.h:158
RenderPanel(QWidget *parent=nullptr)
Ogre::Camera * default_camera_
A default camera created in initialize().
Definition: render_panel.h:174
void contextMenuEvent(QContextMenuEvent *event) override


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:25