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 <OGRE/OgreRenderWindow.h>
00034 
00035 #include <QtGlobal>
00036 #include <QApplication>
00037 #include <QMoveEvent>
00038 #include <QPaintEvent>
00039 #include <QShowEvent>
00040 #include <QVBoxLayout>
00041 
00042 namespace rviz
00043 {
00044 
00045 RenderWidget::RenderWidget( RenderSystem* render_system, QWidget *parent )
00046   : QWidget( parent )
00047   , render_system_( render_system )
00048   , render_window_( 0 )
00049 {
00050   setAttribute(Qt::WA_OpaquePaintEvent,true);
00051   setAttribute(Qt::WA_PaintOnScreen,true);
00052 
00053   // It is not clear to me why, but having this frame sub-widget
00054   // inside the main widget makes an important difference (under X at
00055   // least).  Without the frame and using this widget's winId() 
00056   // below causes trouble when using RenderWidget as a child
00057   // widget.  The frame graphics are completely covered up by the 3D
00058   // render, so using it does not affect the appearance at all.
00059   this->renderFrame = new QFrame;
00060   this->renderFrame->setLineWidth(1);
00061   this->renderFrame->setFrameShadow(QFrame::Sunken);
00062   this->renderFrame->setFrameShape(QFrame::Box);
00063   this->renderFrame->show();
00064 
00065   QVBoxLayout *mainLayout = new QVBoxLayout;
00066   mainLayout->setContentsMargins( 0, 0, 0, 0 );
00067   mainLayout->addWidget(this->renderFrame);
00068   this->setLayout(mainLayout);
00069 
00070 #ifdef Q_OS_MAC
00071   uintptr_t win_id = winId();
00072 #else
00073   unsigned int win_id = renderFrame->winId();
00074 #endif  
00075   QApplication::flush();
00076   QApplication::syncX();
00077   render_window_ = render_system_->makeRenderWindow( win_id, width(), height() );
00078 }
00079 
00080 RenderWidget::~RenderWidget()
00081 {
00082   if( render_window_ )
00083   {
00084     render_window_->removeViewport( 0 );
00085     render_window_->destroy();
00086   }
00087 
00088   render_window_ = 0;
00089 }
00090 
00091 void RenderWidget::moveEvent(QMoveEvent *e)
00092 {
00093   QWidget::moveEvent(e);
00094 
00095   if(e->isAccepted() && render_window_)
00096   {
00097     render_window_->windowMovedOrResized();
00098   }
00099 }
00100 
00101 void RenderWidget::paintEvent(QPaintEvent *e)
00102 {
00103   if( render_window_ )
00104   {
00105     render_window_->update();
00106   }
00107   e->accept();
00108 }
00109 
00110 void RenderWidget::resizeEvent(QResizeEvent *e)
00111 {
00112   if( render_window_ )
00113   {
00114     // render_window_->writeContentsToFile() (used in
00115     // VisualizationFrame::onSaveImage()) does not work right for
00116     // window with an odd width, so here I just always force it to be
00117     // even.
00118     render_window_->resize( width() + (width() % 2), height() );
00119     render_window_->windowMovedOrResized();
00120   }
00121 }
00122 
00123 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Oct 6 2014 07:26:36