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


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