render_widget.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 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 
33 
34 #include <OgreRenderWindow.h>
35 
36 #include <QtGlobal>
37 #include <QApplication>
38 #include <QMoveEvent>
39 #include <QPaintEvent>
40 #include <QShowEvent>
41 #include <QVBoxLayout>
42 #include <QWindow>
43 
44 namespace rviz
45 {
46 RenderWidget::RenderWidget(RenderSystem* render_system, QWidget* parent)
47  : QWidget(parent), render_system_(render_system), render_window_(nullptr)
48 {
49  setAttribute(Qt::WA_OpaquePaintEvent, true);
50  setAttribute(Qt::WA_PaintOnScreen, true);
51 
52  rviz::RenderSystem::WindowIDType win_id = this->winId();
53  QWindow* window = windowHandle();
54  pixel_ratio_ = window ? window->devicePixelRatio() : 1.0;
55 
56  render_window_ = render_system_->makeRenderWindow(win_id, width(), height(), pixel_ratio_);
57 }
58 
60 {
61  if (render_window_)
62  {
63  render_window_->removeViewport(0);
64  render_window_->destroy();
65  }
66 
67  render_window_ = nullptr;
68 }
69 
70 void RenderWidget::moveEvent(QMoveEvent* e)
71 {
72  QWidget::moveEvent(e);
73 
74  if (e->isAccepted() && render_window_)
75  {
76  render_window_->windowMovedOrResized();
77  }
78 }
79 
80 void RenderWidget::paintEvent(QPaintEvent* e)
81 {
82  if (render_window_)
83  {
84  render_window_->update();
85  }
86  e->accept();
87 }
88 
89 void RenderWidget::resizeEvent(QResizeEvent* e)
90 {
91  QWidget::resizeEvent(e);
92  if (e->isAccepted() && render_window_)
93  {
94  /* render_window_->writeContentsToFile() (used in VisualizationFrame::onSaveImage())
95  * does not work right for window with an odd width.
96  * So here we just always force it to be even. */
97  const int w = width() * pixel_ratio_;
98  render_window_->resize(w + (w % 2), height() * pixel_ratio_);
99 #if OGRE_VERSION < OGRE_VERSION_CHECK(1, 12, 0)
100  render_window_->windowMovedOrResized();
101 #endif
102  }
103 }
104 
105 } // end namespace rviz
render_system.h
version_check.h
rviz::RenderSystem::WindowIDType
unsigned long WindowIDType
Definition: render_system.h:51
rviz::RenderSystem::makeRenderWindow
Ogre::RenderWindow * makeRenderWindow(WindowIDType window_id, unsigned int width, unsigned int height, double pixel_ratio=1.0)
Definition: render_system.cpp:392
rviz
Definition: add_display_dialog.cpp:54
rviz::RenderSystem
Definition: render_system.h:45
rviz::RenderWidget::RenderWidget
RenderWidget(RenderSystem *render_system, QWidget *parent=nullptr)
Definition: render_widget.cpp:46
rviz::RenderWidget::paintEvent
void paintEvent(QPaintEvent *e) override
Definition: render_widget.cpp:80
render_widget.h
rviz::RenderWidget::render_system_
RenderSystem * render_system_
Definition: render_widget.h:64
rviz::RenderWidget::~RenderWidget
~RenderWidget() override
Definition: render_widget.cpp:59
rviz::RenderWidget::resizeEvent
void resizeEvent(QResizeEvent *e) override
Definition: render_widget.cpp:89
rviz::RenderWidget::pixel_ratio_
qreal pixel_ratio_
Definition: render_widget.h:66
rviz::RenderWidget::moveEvent
void moveEvent(QMoveEvent *e) override
Definition: render_widget.cpp:70
rviz::RenderWidget::render_window_
Ogre::RenderWindow * render_window_
Definition: render_widget.h:65


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:10