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 #include <utility>
34 
35 
36 #include <OgreSceneManager.h>
37 #include <OgreCamera.h>
38 
39 #include <rviz/display.h>
40 #include <rviz/view_controller.h>
44 
45 #include <rviz/render_panel.h>
46 
47 namespace rviz
48 {
49 RenderPanel::RenderPanel(QWidget* parent)
50  : QtOgreRenderWindow(parent)
51  , mouse_x_(0)
52  , mouse_y_(0)
53  , focus_on_mouse_move_(true)
54  , context_(nullptr)
55  , scene_manager_(nullptr)
56  , view_controller_(nullptr)
57  , context_menu_visible_(false)
58  , default_camera_(nullptr)
59 {
60  setFocusPolicy(Qt::WheelFocus);
61  setFocus(Qt::OtherFocusReason);
62  setMouseTracking(true);
63 }
64 
66 {
68  {
69  scene_manager_->destroyCamera(default_camera_);
70  }
71  if (scene_manager_)
72  {
73  scene_manager_->removeListener(this);
74  }
75 }
76 
77 void RenderPanel::initialize(Ogre::SceneManager* scene_manager, DisplayContext* context)
78 {
79  context_ = context;
80  scene_manager_ = scene_manager;
81  scene_manager_->addListener(this);
82 
83  std::stringstream ss;
84  static int count = 0;
85  ss << "RenderPanelCamera" << count++;
86  default_camera_ = scene_manager_->createCamera(ss.str());
87  default_camera_->setNearClipDistance(0.01f);
88  default_camera_->setPosition(0, 10, 15);
89  default_camera_->lookAt(0, 0, 0);
90 
92 }
93 
94 void RenderPanel::leaveEvent(QEvent* /*event*/)
95 {
96  setCursor(Qt::ArrowCursor);
97  if (context_)
98  {
99  context_->setStatus("");
100  }
101 }
102 
104 {
105  int last_x = mouse_x_;
106  int last_y = mouse_y_;
107 
108  mouse_x_ = event->x();
109  mouse_y_ = event->y();
110 
111  if (context_)
112  {
114  {
115  setFocus(Qt::MouseFocusReason);
116  }
117 
118  ViewportMouseEvent vme(this, getViewport(), event, last_x, last_y);
120  event->accept();
121  }
122 }
123 
124 void RenderPanel::wheelEvent(QWheelEvent* event)
125 {
126  int last_x = mouse_x_;
127  int last_y = mouse_y_;
128 
129 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
130  mouse_x_ = event->position().x();
131  mouse_y_ = event->position().y();
132 #else
133  mouse_x_ = event->x();
134  mouse_y_ = event->y();
135 #endif
136 
137  if (context_)
138  {
139  ViewportMouseEvent vme(this, getViewport(), event, last_x, last_y);
141  event->accept();
142  }
143 }
144 
145 void RenderPanel::keyPressEvent(QKeyEvent* event)
146 {
147  if (context_)
148  {
149  context_->handleChar(event, this);
150  }
151 }
152 
154 {
155  view_controller_ = controller;
156 
157  if (view_controller_)
158  {
161  }
162  else
163  {
164  setCamera(nullptr);
165  }
166 }
167 
169 {
170  boost::mutex::scoped_lock lock(context_menu_mutex_);
171  context_menu_ = std::move(menu);
172  context_menu_visible_ = true;
173 
174  QApplication::postEvent(this, new QContextMenuEvent(QContextMenuEvent::Mouse, QPoint()));
175 }
176 
178 {
179  context_menu_visible_ = false;
180 }
181 
183 {
184  return context_menu_visible_;
185 }
186 
187 void RenderPanel::contextMenuEvent(QContextMenuEvent* /*event*/)
188 {
189  boost::shared_ptr<QMenu> context_menu;
190  {
191  boost::mutex::scoped_lock lock(context_menu_mutex_);
192  context_menu.swap(context_menu_);
193  }
194 
195  if (context_menu)
196  {
197  connect(context_menu.get(), &QMenu::aboutToHide, this, &RenderPanel::onContextMenuHide);
198  context_menu->exec(QCursor::pos());
199  }
200 }
201 
202 void RenderPanel::sceneManagerDestroyed(Ogre::SceneManager* destroyed_scene_manager)
203 {
204  if (destroyed_scene_manager == scene_manager_)
205  {
206  scene_manager_ = nullptr;
207  default_camera_ = nullptr;
208  setCamera(nullptr);
209  }
210 }
211 
213 {
214  return focus_on_mouse_move_;
215 }
216 
218 {
219  focus_on_mouse_move_ = enabled;
220 }
221 
222 } // namespace rviz
rviz::RenderPanel::context_menu_mutex_
boost::mutex context_menu_mutex_
Definition: render_panel.h:158
window_manager_interface.h
rviz::RenderPanel::onContextMenuHide
void onContextMenuHide()
Definition: render_panel.cpp:177
boost::shared_ptr< QMenu >
rviz::RenderPanel::focus_on_mouse_move_
bool focus_on_mouse_move_
a moving the mouse catches keyboard focus
Definition: render_panel.h:150
rviz::ViewportMouseEvent
Definition: viewport_mouse_event.h:45
viewport_mouse_event.h
rviz::DisplayContext::handleChar
virtual void handleChar(QKeyEvent *event, RenderPanel *panel)=0
Handle a single key event for a given RenderPanel.
rviz::QtOgreRenderWindow
Definition: qt_ogre_render_window.h:54
rviz::RenderPanel::contextMenuEvent
void contextMenuEvent(QContextMenuEvent *event) override
Definition: render_panel.cpp:187
rviz::RenderPanel::context_menu_
boost::shared_ptr< QMenu > context_menu_
Definition: render_panel.h:157
rviz::RenderPanel::leaveEvent
void leaveEvent(QEvent *event) override
Definition: render_panel.cpp:94
rviz::RenderPanel::~RenderPanel
~RenderPanel() override
Definition: render_panel.cpp:65
rviz::RenderPanel::setViewController
void setViewController(ViewController *controller)
Set the ViewController which should control the camera position for this view.
Definition: render_panel.cpp:153
f
f
rviz::RenderPanel::context_menu_visible_
bool context_menu_visible_
Definition: render_panel.h:160
rviz::RenderPanel::contextMenuVisible
bool contextMenuVisible()
Definition: render_panel.cpp:182
rviz::RenderPanel::scene_manager_
Ogre::SceneManager * scene_manager_
Definition: render_panel.h:153
rviz::RenderPanel::initialize
void initialize(Ogre::SceneManager *scene_manager, DisplayContext *manager)
Definition: render_panel.cpp:77
rviz::ViewController::activate
void activate()
Called by RenderPanel when this view controller is about to be used.
Definition: view_controller.cpp:174
rviz::RenderPanel::RenderPanel
RenderPanel(QWidget *parent=nullptr)
Definition: render_panel.cpp:49
rviz::DisplayContext::setStatus
virtual void setStatus(const QString &message)=0
rviz
Definition: add_display_dialog.cpp:54
rviz::RenderPanel::view_controller_
ViewController * view_controller_
Definition: render_panel.h:155
rviz::RenderPanel::mouse_y_
int mouse_y_
Y position of the last mouse event.
Definition: render_panel.h:149
rviz::RenderPanel::wheelEvent
void wheelEvent(QWheelEvent *event) override
Called when there is a mouse-wheel event.
Definition: render_panel.cpp:124
rviz::RenderPanel::showContextMenu
void showContextMenu(boost::shared_ptr< QMenu > menu)
Definition: render_panel.cpp:168
rviz::RenderPanel::keyPressEvent
void keyPressEvent(QKeyEvent *event) override
Definition: render_panel.cpp:145
rviz::RenderPanel::setFocusOnMouseMove
void setFocusOnMouseMove(bool enabled)
Definition: render_panel.cpp:217
rviz::DisplayContext
Pure-virtual base class for objects which give Display subclasses context in which to work.
Definition: display_context.h:81
rviz::RenderPanel::mouse_x_
int mouse_x_
X position of the last mouse event.
Definition: render_panel.h:148
visualization_manager.h
render_panel.h
rviz::ViewController::getCamera
Ogre::Camera * getCamera() const
Definition: view_controller.h:147
display.h
rviz::RenderPanel::context_
DisplayContext * context_
Definition: render_panel.h:152
rviz::RenderPanel::onRenderWindowMouseEvents
void onRenderWindowMouseEvents(QMouseEvent *event)
Called when any mouse event happens inside the render window.
Definition: render_panel.cpp:103
rviz::ViewController
Definition: view_controller.h:55
rviz::RenderPanel::getFocusOnMouseMove
bool getFocusOnMouseMove() const
Definition: render_panel.cpp:212
rviz::QtOgreRenderWindow::setCamera
void setCamera(Ogre::Camera *camera)
Definition: qt_ogre_render_window.cpp:219
view_controller.h
rviz::RenderPanel::default_camera_
Ogre::Camera * default_camera_
A default camera created in initialize().
Definition: render_panel.h:170
rviz::RenderPanel::sceneManagerDestroyed
void sceneManagerDestroyed(Ogre::SceneManager *source) override
Definition: render_panel.cpp:202
rviz::QtOgreRenderWindow::getViewport
Ogre::Viewport * getViewport() const
Definition: qt_ogre_render_window.cpp:214
rviz::DisplayContext::handleMouseEvent
virtual void handleMouseEvent(const ViewportMouseEvent &event)=0
Handle a mouse event.


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Dec 13 2024 03:31:02