render_widget.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include "ogre_helpers/render_widget.h"
00031 #include "ogre_helpers/render_system.h"
00032 
00033 #include <OgreRenderWindow.h>
00034 
00035 #include <QtGlobal>
00036 #include <QApplication>
00037 #include <QMoveEvent>
00038 #include <QPaintEvent>
00039 #include <QShowEvent>
00040 #include <QVBoxLayout>
00041 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
00042 #include <QWindow>
00043 #endif
00044 
00045 namespace rviz
00046 {
00047 
00048 RenderWidget::RenderWidget( RenderSystem* render_system, QWidget *parent )
00049   : QWidget( parent )
00050   , render_system_( render_system )
00051   , render_window_( 0 )
00052 {
00053   setAttribute(Qt::WA_OpaquePaintEvent,true);
00054   setAttribute(Qt::WA_PaintOnScreen,true);
00055 
00056 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
00057   // It is not clear to me why, but having this frame sub-widget
00058   // inside the main widget makes an important difference (under X at
00059   // least).  Without the frame and using this widget's winId()
00060   // below causes trouble when using RenderWidget as a child
00061   // widget.  The frame graphics are completely covered up by the 3D
00062   // render, so using it does not affect the appearance at all.
00063   this->renderFrame = new QFrame;
00064   this->renderFrame->setLineWidth(1);
00065   this->renderFrame->setFrameShadow(QFrame::Sunken);
00066   this->renderFrame->setFrameShape(QFrame::Box);
00067   this->renderFrame->show();
00068 
00069   QVBoxLayout *mainLayout = new QVBoxLayout;
00070   mainLayout->setContentsMargins( 0, 0, 0, 0 );
00071   mainLayout->addWidget(this->renderFrame);
00072   this->setLayout(mainLayout);
00073 #endif
00074 
00075 #ifdef Q_OS_MAC
00076   uintptr_t win_id = winId();
00077 #else
00078 # if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
00079   unsigned int win_id = renderFrame->winId();
00080 # else
00081   unsigned int win_id = winId();
00082 # endif
00083 #endif
00084 
00085   QApplication::flush();
00086 
00087 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
00088   QApplication::syncX();
00089   double pixel_ratio = 1.0;
00090 #else
00091   QWindow* window = windowHandle();
00092   double pixel_ratio = window ? window->devicePixelRatio() : 1.0;
00093 #endif
00094   render_window_ = render_system_->makeRenderWindow( win_id, width(), height(), pixel_ratio );
00095 }
00096 
00097 RenderWidget::~RenderWidget()
00098 {
00099   if( render_window_ )
00100   {
00101     render_window_->removeViewport( 0 );
00102     render_window_->destroy();
00103   }
00104 
00105   render_window_ = 0;
00106 }
00107 
00108 void RenderWidget::moveEvent(QMoveEvent *e)
00109 {
00110   QWidget::moveEvent(e);
00111 
00112   if(e->isAccepted() && render_window_)
00113   {
00114     render_window_->windowMovedOrResized();
00115   }
00116 }
00117 
00118 void RenderWidget::paintEvent(QPaintEvent *e)
00119 {
00120   if( render_window_ )
00121   {
00122     render_window_->update();
00123   }
00124   e->accept();
00125 }
00126 
00127 void RenderWidget::resizeEvent(QResizeEvent *e)
00128 {
00129   if( render_window_ )
00130   {
00131     // render_window_->writeContentsToFile() (used in
00132     // VisualizationFrame::onSaveImage()) does not work right for
00133     // window with an odd width, so here I just always force it to be
00134     // even.
00135     render_window_->resize( width() + (width() % 2), height() );
00136     render_window_->windowMovedOrResized();
00137   }
00138 }
00139 
00140 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Tue Oct 3 2017 03:19:31