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 
32 
33 #include <OgreRenderWindow.h>
34 
35 #include <QtGlobal>
36 #include <QApplication>
37 #include <QMoveEvent>
38 #include <QPaintEvent>
39 #include <QShowEvent>
40 #include <QVBoxLayout>
41 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
42 #include <QWindow>
43 #endif
44 
45 namespace rviz
46 {
47 RenderWidget::RenderWidget(RenderSystem* render_system, QWidget* parent)
48  : QWidget(parent), render_system_(render_system), render_window_(nullptr)
49 {
50  setAttribute(Qt::WA_OpaquePaintEvent, true);
51  setAttribute(Qt::WA_PaintOnScreen, true);
52 
53 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
54  // It is not clear to me why, but having this frame sub-widget
55  // inside the main widget makes an important difference (under X at
56  // least). Without the frame and using this widget's winId()
57  // below causes trouble when using RenderWidget as a child
58  // widget. The frame graphics are completely covered up by the 3D
59  // render, so using it does not affect the appearance at all.
60  this->renderFrame = new QFrame;
61  this->renderFrame->setLineWidth(1);
62  this->renderFrame->setFrameShadow(QFrame::Sunken);
63  this->renderFrame->setFrameShape(QFrame::Box);
64  this->renderFrame->show();
65 
66  QVBoxLayout* mainLayout = new QVBoxLayout;
67  mainLayout->setContentsMargins(0, 0, 0, 0);
68  mainLayout->addWidget(this->renderFrame);
69  this->setLayout(mainLayout);
70 #endif
71 
72 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
73  rviz::RenderSystem::WindowIDType win_id = this->renderFrame->winId();
74 #else
75  rviz::RenderSystem::WindowIDType win_id = this->winId();
76 #endif
77 
78  render_window_ = render_system_->makeRenderWindow(win_id, width(), height(), pixelRatio());
79 }
80 
82 {
83  if (render_window_)
84  {
85  render_window_->removeViewport(0);
86  render_window_->destroy();
87  }
88 
89  render_window_ = nullptr;
90 }
91 
92 void RenderWidget::moveEvent(QMoveEvent* e)
93 {
94  QWidget::moveEvent(e);
95 
96  if (e->isAccepted() && render_window_)
97  {
98  render_window_->windowMovedOrResized();
99  }
100 }
101 
102 void RenderWidget::paintEvent(QPaintEvent* e)
103 {
104  if (render_window_)
105  {
106  render_window_->update();
107  }
108  e->accept();
109 }
110 
111 void RenderWidget::resizeEvent(QResizeEvent* /*event*/)
112 {
113  if (render_window_)
114  {
115  // render_window_->writeContentsToFile() (used in
116  // VisualizationFrame::onSaveImage()) does not work right for
117  // window with an odd width, so here I just always force it to be
118  // even.
119  const qreal ratio = pixelRatio();
120  const int w = width() * ratio;
121  render_window_->resize(w + (w % 2), height() * ratio);
122  render_window_->windowMovedOrResized();
123  }
124 }
125 
127 {
128 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
129  return 1.0;
130 #else
131  QWindow* window = windowHandle();
132  return window ? window->devicePixelRatio() : 1.0;
133 #endif
134 }
135 
136 } // end namespace rviz
RenderWidget(RenderSystem *render_system, QWidget *parent=nullptr)
Ogre::RenderWindow * makeRenderWindow(WindowIDType window_id, unsigned int width, unsigned int height, double pixel_ratio=1.0)
unsigned long WindowIDType
Definition: render_system.h:51
RenderSystem * render_system_
Definition: render_widget.h:67
Ogre::RenderWindow * render_window_
Definition: render_widget.h:68
void paintEvent(QPaintEvent *e) override
void resizeEvent(QResizeEvent *e) override
void moveEvent(QMoveEvent *e) override
~RenderWidget() override
qreal pixelRatio() const


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