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_tools/render_widget.h"
00031 #include "ogre_tools/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 ogre_tools
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() in
00056   // showEvent() 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 
00071 RenderWidget::~RenderWidget()
00072 {
00073   if( render_window_ )
00074   {
00075     render_window_->removeViewport( 0 );
00076     render_window_->destroy();
00077   }
00078 
00079   render_window_ = 0;
00080 }
00081 
00082 void RenderWidget::showEvent(QShowEvent *event)
00083 {
00084 #ifdef Q_OS_MAC
00085   uintptr_t win_id = winId();
00086 #else
00087   unsigned int win_id = renderFrame->winId();
00088 #endif  
00089   QApplication::flush();
00090   render_window_ = render_system_->makeRenderWindow( win_id, width(), height() );
00091 
00092   QWidget::showEvent(event);
00093 }
00094 
00095 void RenderWidget::moveEvent(QMoveEvent *e)
00096 {
00097   QWidget::moveEvent(e);
00098 
00099   if(e->isAccepted() && render_window_)
00100   {
00101     render_window_->windowMovedOrResized();
00102   }
00103 }
00104 
00105 void RenderWidget::paintEvent(QPaintEvent *e)
00106 {
00107   if( render_window_ )
00108   {
00109     render_window_->update();
00110   }
00111   e->accept();
00112 }
00113 
00114 void RenderWidget::resizeEvent(QResizeEvent *e)
00115 {
00116   if( render_window_ )
00117   {
00118     render_window_->resize( width(), height() );
00119     render_window_->windowMovedOrResized();
00120   }
00121 }
00122 
00123 } // end namespace ogre_tools


ogre_tools_qt
Author(s): Josh Faust
autogenerated on Fri Dec 6 2013 20:56:42