#ifndef VIEW_H #define VIEW_H using Qt::WindowFlags; class AdapterWidget : public QGLWidget { Q_OBJECT public: AdapterWidget(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0): QGLWidget(parent, shareWidget, f) { _gw = new osgViewer::GraphicsWindowEmbedded(0, 0, width(), height()); setFocusPolicy(Qt::ClickFocus); } virtual ~AdapterWidget() {} osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); } const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); } protected: virtual void resizeGL(int width, int height) { _gw->getEventQueue()->windowResize(0, 0, width, height); _gw->resized(0, 0, width, height); } virtual void keyPressEvent(QKeyEvent* event); virtual void keyReleaseEvent(QKeyEvent* event); virtual void mousePressEvent(QMouseEvent* event); virtual void mouseReleaseEvent(QMouseEvent* event); virtual void mouseMoveEvent(QMouseEvent* event); osg::ref_ptr _gw; }; class ViewerQT : public osgViewer::Viewer, public AdapterWidget { public: ViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0) : AdapterWidget(parent, name, shareWidget, f) { getCamera()->setViewport(new osg::Viewport(0, 0, width(), height())); getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast(width()) / static_cast(height()), 1.0f, 10000.0f); getCamera()->setGraphicsContext(getGraphicsWindow()); setThreadingModel(osgViewer::Viewer::SingleThreaded); connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL())); _timer.start(10); } virtual void paintGL() { frame(); } protected: QTimer _timer; }; class CompositeViewerQT : public osgViewer::CompositeViewer, public AdapterWidget { public: CompositeViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0) : AdapterWidget(parent, name, shareWidget, f) { setThreadingModel(osgViewer::CompositeViewer::SingleThreaded); connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL())); _timer.start(10); } virtual void paintGL() { frame(); } protected: QTimer _timer; }; #endif // VIEW_H