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_( nullptr )
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 
82 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
83  QApplication::syncX();
84  double pixel_ratio = 1.0;
85 #else
86  QWindow* window = windowHandle();
87  double pixel_ratio = window ? window->devicePixelRatio() : 1.0;
88 #endif
89  render_window_ = render_system_->makeRenderWindow(win_id, width(), height(), pixel_ratio);
90 }
91 
93 {
94  if( render_window_ )
95  {
96  render_window_->removeViewport( 0 );
97  render_window_->destroy();
98  }
99 
100  render_window_ = 0;
101 }
102 
103 void RenderWidget::moveEvent(QMoveEvent *e)
104 {
105  QWidget::moveEvent(e);
106 
107  if(e->isAccepted() && render_window_)
108  {
109  render_window_->windowMovedOrResized();
110  }
111 }
112 
113 void RenderWidget::paintEvent(QPaintEvent *e)
114 {
115  if( render_window_ )
116  {
117  render_window_->update();
118  }
119  e->accept();
120 }
121 
122 void RenderWidget::resizeEvent(QResizeEvent *)
123 {
124  if( render_window_ )
125  {
126  // render_window_->writeContentsToFile() (used in
127  // VisualizationFrame::onSaveImage()) does not work right for
128  // window with an odd width, so here I just always force it to be
129  // even.
130  render_window_->resize( width() + (width() % 2), height() );
131  render_window_->windowMovedOrResized();
132  }
133 }
134 
135 } // 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:61
RenderWidget(RenderSystem *render_system, QWidget *parent=0)
Ogre::RenderWindow * render_window_
Definition: render_widget.h:62
virtual void paintEvent(QPaintEvent *e)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat Apr 27 2019 02:33:42