From ea990dddfa7ee9b0af834864a09928a186bd5adb Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sat, 2 Jun 2007 16:01:56 +0000 Subject: [PATCH] Ported QT3 example across to use osgViewer::Viewer, and made it possible to use the same source for both QT3 and QT4 --- examples/osgviewerQT3/CMakeLists.txt | 2 + examples/osgviewerQT3/osgviewerQT3.cpp | 86 +++++++++++++++++--------- 2 files changed, 58 insertions(+), 30 deletions(-) diff --git a/examples/osgviewerQT3/CMakeLists.txt b/examples/osgviewerQT3/CMakeLists.txt index 9992e418a..3c237703a 100644 --- a/examples/osgviewerQT3/CMakeLists.txt +++ b/examples/osgviewerQT3/CMakeLists.txt @@ -4,5 +4,7 @@ SET(TARGET_EXTERNAL_LIBRARIES ${QT_LIBRARIES} ) INCLUDE_DIRECTORIES(${QT_INCLUDE_DIR} ) +ADD_DEFINITIONS(-DUSE_QT3) + #### end var setup ### SETUP_EXAMPLE(osgviewerQT3) diff --git a/examples/osgviewerQT3/osgviewerQT3.cpp b/examples/osgviewerQT3/osgviewerQT3.cpp index fea4ccf6f..d0c91e676 100644 --- a/examples/osgviewerQT3/osgviewerQT3.cpp +++ b/examples/osgviewerQT3/osgviewerQT3.cpp @@ -1,28 +1,44 @@ // C++ source file - (C) 2003 Robert Osfield, released under the OSGPL. -// (C) 2005 Mike Weiblen http://mew.cx/ released under the OSGPL. -// Simple example using GLUT to create an OpenGL window and OSG for rendering. -// Derived from osgGLUTsimple.cpp and osgkeyboardmouse.cpp -#include +#include +#include #include #include -class QWidget; -#include -#include -#include +#if USE_QT3 + + class QWidget; + #include + #include + #include + +#else + + #include + #include + #include + #include + + using Qt::WFlags; +#endif #include -class GraphicsWindowQT : public QGLWidget, virtual public osgViewer::GraphicsWindow +class AdapterWidget : public QGLWidget { public: - GraphicsWindowQT( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0 ); - virtual ~GraphicsWindowQT() {} + AdapterWidget( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0 ); + + virtual ~AdapterWidget() {} + + osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); } + const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); } protected: + void init(); + virtual void resizeGL( int width, int height ); virtual void keyPressEvent( QKeyEvent* event ); virtual void keyReleaseEvent( QKeyEvent* event ); @@ -31,31 +47,36 @@ protected: virtual void mouseMoveEvent( QMouseEvent* event ); QTimer _timer; + + osg::ref_ptr _gw; }; -GraphicsWindowQT::GraphicsWindowQT( QWidget * parent, const char * name, const QGLWidget * shareWidget, WFlags f): +AdapterWidget::AdapterWidget( QWidget * parent, const char * name, const QGLWidget * shareWidget, WFlags f): QGLWidget(parent, name, shareWidget, f) { connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL())); _timer.start(10); + + _gw = new osgViewer::GraphicsWindowEmbedded(0,0,width(),height()); } -void GraphicsWindowQT::resizeGL( int width, int height ) +void AdapterWidget::resizeGL( int width, int height ) { - getEventQueue()->windowResize(0, 0, width, height ); + _gw->getEventQueue()->windowResize(0, 0, width, height ); + _gw->resized(0,0,width,height); } -void GraphicsWindowQT::keyPressEvent( QKeyEvent* event ) +void AdapterWidget::keyPressEvent( QKeyEvent* event ) { - getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() ); + _gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() ); } -void GraphicsWindowQT::keyReleaseEvent( QKeyEvent* event ) +void AdapterWidget::keyReleaseEvent( QKeyEvent* event ) { - getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() ); + _gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() ); } -void GraphicsWindowQT::mousePressEvent( QMouseEvent* event ) +void AdapterWidget::mousePressEvent( QMouseEvent* event ) { int button = 0; switch(event->button()) @@ -66,10 +87,10 @@ void GraphicsWindowQT::mousePressEvent( QMouseEvent* event ) case(Qt::NoButton): button = 0; break; default: button = 0; break; } - getEventQueue()->mouseButtonPress(event->x(), event->y(), button); + _gw->getEventQueue()->mouseButtonPress(event->x(), event->y(), button); } -void GraphicsWindowQT::mouseReleaseEvent( QMouseEvent* event ) +void AdapterWidget::mouseReleaseEvent( QMouseEvent* event ) { int button = 0; switch(event->button()) @@ -80,26 +101,30 @@ void GraphicsWindowQT::mouseReleaseEvent( QMouseEvent* event ) case(Qt::NoButton): button = 0; break; default: button = 0; break; } - getEventQueue()->mouseButtonRelease(event->x(), event->y(), button); + _gw->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button); } -void GraphicsWindowQT::mouseMoveEvent( QMouseEvent* event ) +void AdapterWidget::mouseMoveEvent( QMouseEvent* event ) { - getEventQueue()->mouseMotion(event->x(), event->y()); + _gw->getEventQueue()->mouseMotion(event->x(), event->y()); } -class SimpleViewerQT : public osgViewer::SimpleViewer, public GraphicsWindowQT +class ViewerQT : public AdapterWidget, public osgViewer::Viewer { public: - SimpleViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0): - GraphicsWindowQT( parent, name, shareWidget, f ) - {} + ViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0): + AdapterWidget( parent, name, shareWidget, f ) + { + getCamera()->setViewport(new osg::Viewport(0,0,width(),height())); + getCamera()->setGraphicsContext(getGraphicsWindow()); + setThreadingModel(osgViewer::Viewer::SingleThreaded); + } virtual void initializeGL() { - QGLWidget::initializeGL(); + QGLWidget::initializeGL(); } virtual void paintGL() @@ -128,10 +153,11 @@ int main( int argc, char **argv ) } - SimpleViewerQT* viewerWindow = new SimpleViewerQT; + ViewerQT* viewerWindow = new ViewerQT; viewerWindow->setSceneData(loadedModel.get()); viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator); + viewerWindow->addEventHandler(new osgViewer::StatsHandler); viewerWindow->show(); a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );