| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 2009 Wang Rui |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGVIEWER_GRAPHICSWINDOWQT |
|---|
| 15 | #define OSGVIEWER_GRAPHICSWINDOWQT |
|---|
| 16 | |
|---|
| 17 | #include <QtCore/QMutex> |
|---|
| 18 | #include <QtCore/QEvent> |
|---|
| 19 | #include <QtCore/QQueue> |
|---|
| 20 | #include <QtCore/QSet> |
|---|
| 21 | #include <QtOpenGL/QGLWidget> |
|---|
| 22 | |
|---|
| 23 | #include <osgViewer/GraphicsWindow> |
|---|
| 24 | #include <osgQt/Export> |
|---|
| 25 | |
|---|
| 26 | class QInputEvent; |
|---|
| 27 | |
|---|
| 28 | namespace osgViewer { |
|---|
| 29 | class ViewerBase; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | namespace osgQt |
|---|
| 33 | { |
|---|
| 34 | |
|---|
| 35 | // forward declarations |
|---|
| 36 | class GraphicsWindowQt; |
|---|
| 37 | |
|---|
| 38 | /// The function sets the WindowingSystem to Qt. |
|---|
| 39 | void OSGQT_EXPORT initQtWindowingSystem(); |
|---|
| 40 | |
|---|
| 41 | /** The function sets the viewer that will be used after entering |
|---|
| 42 | * the Qt main loop (QCoreApplication::exec()). |
|---|
| 43 | * |
|---|
| 44 | * The function also initializes internal structures required for proper |
|---|
| 45 | * scene rendering. |
|---|
| 46 | * |
|---|
| 47 | * The method must be called from main thread. */ |
|---|
| 48 | void OSGQT_EXPORT setViewer( osgViewer::ViewerBase *viewer ); |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | class OSGQT_EXPORT GLWidget : public QGLWidget |
|---|
| 52 | { |
|---|
| 53 | typedef QGLWidget inherited; |
|---|
| 54 | |
|---|
| 55 | public: |
|---|
| 56 | |
|---|
| 57 | GLWidget( QWidget* parent = NULL, const QGLWidget* shareWidget = NULL, Qt::WindowFlags f = 0, bool forwardKeyEvents = false ); |
|---|
| 58 | GLWidget( QGLContext* context, QWidget* parent = NULL, const QGLWidget* shareWidget = NULL, Qt::WindowFlags f = 0, bool forwardKeyEvents = false ); |
|---|
| 59 | GLWidget( const QGLFormat& format, QWidget* parent = NULL, const QGLWidget* shareWidget = NULL, Qt::WindowFlags f = 0, bool forwardKeyEvents = false ); |
|---|
| 60 | virtual ~GLWidget(); |
|---|
| 61 | |
|---|
| 62 | inline void setGraphicsWindow( GraphicsWindowQt* gw ) { _gw = gw; } |
|---|
| 63 | inline GraphicsWindowQt* getGraphicsWindow() { return _gw; } |
|---|
| 64 | inline const GraphicsWindowQt* getGraphicsWindow() const { return _gw; } |
|---|
| 65 | |
|---|
| 66 | inline bool getForwardKeyEvents() const { return _forwardKeyEvents; } |
|---|
| 67 | virtual void setForwardKeyEvents( bool f ) { _forwardKeyEvents = f; } |
|---|
| 68 | |
|---|
| 69 | void setKeyboardModifiers( QInputEvent* event ); |
|---|
| 70 | |
|---|
| 71 | virtual void keyPressEvent( QKeyEvent* event ); |
|---|
| 72 | virtual void keyReleaseEvent( QKeyEvent* event ); |
|---|
| 73 | virtual void mousePressEvent( QMouseEvent* event ); |
|---|
| 74 | virtual void mouseReleaseEvent( QMouseEvent* event ); |
|---|
| 75 | virtual void mouseDoubleClickEvent( QMouseEvent* event ); |
|---|
| 76 | virtual void mouseMoveEvent( QMouseEvent* event ); |
|---|
| 77 | virtual void wheelEvent( QWheelEvent* event ); |
|---|
| 78 | |
|---|
| 79 | protected: |
|---|
| 80 | |
|---|
| 81 | int getNumDeferredEvents() |
|---|
| 82 | { |
|---|
| 83 | QMutexLocker lock(&_deferredEventQueueMutex); |
|---|
| 84 | return _deferredEventQueue.count(); |
|---|
| 85 | } |
|---|
| 86 | void enqueueDeferredEvent(QEvent::Type eventType, QEvent::Type removeEventType = QEvent::None) |
|---|
| 87 | { |
|---|
| 88 | QMutexLocker lock(&_deferredEventQueueMutex); |
|---|
| 89 | |
|---|
| 90 | if (removeEventType != QEvent::None) |
|---|
| 91 | { |
|---|
| 92 | if (_deferredEventQueue.removeOne(removeEventType)) |
|---|
| 93 | _eventCompressor.remove(eventType); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | if (_eventCompressor.find(eventType) == _eventCompressor.end()) |
|---|
| 97 | { |
|---|
| 98 | _deferredEventQueue.enqueue(eventType); |
|---|
| 99 | _eventCompressor.insert(eventType); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | void processDeferredEvents(); |
|---|
| 103 | |
|---|
| 104 | friend class GraphicsWindowQt; |
|---|
| 105 | GraphicsWindowQt* _gw; |
|---|
| 106 | |
|---|
| 107 | QMutex _deferredEventQueueMutex; |
|---|
| 108 | QQueue<QEvent::Type> _deferredEventQueue; |
|---|
| 109 | QSet<QEvent::Type> _eventCompressor; |
|---|
| 110 | |
|---|
| 111 | bool _forwardKeyEvents; |
|---|
| 112 | |
|---|
| 113 | virtual void resizeEvent( QResizeEvent* event ); |
|---|
| 114 | virtual void moveEvent( QMoveEvent* event ); |
|---|
| 115 | virtual void glDraw(); |
|---|
| 116 | virtual bool event( QEvent* event ); |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | class OSGQT_EXPORT GraphicsWindowQt : public osgViewer::GraphicsWindow |
|---|
| 120 | { |
|---|
| 121 | public: |
|---|
| 122 | GraphicsWindowQt( osg::GraphicsContext::Traits* traits, QWidget* parent = NULL, const QGLWidget* shareWidget = NULL, Qt::WindowFlags f = 0 ); |
|---|
| 123 | GraphicsWindowQt( GLWidget* widget ); |
|---|
| 124 | virtual ~GraphicsWindowQt(); |
|---|
| 125 | |
|---|
| 126 | inline GLWidget* getGLWidget() { return _widget; } |
|---|
| 127 | inline const GLWidget* getGLWidget() const { return _widget; } |
|---|
| 128 | |
|---|
| 129 | /// deprecated |
|---|
| 130 | inline GLWidget* getGraphWidget() { return _widget; } |
|---|
| 131 | /// deprecated |
|---|
| 132 | inline const GLWidget* getGraphWidget() const { return _widget; } |
|---|
| 133 | |
|---|
| 134 | struct WindowData : public osg::Referenced |
|---|
| 135 | { |
|---|
| 136 | WindowData( GLWidget* widget = NULL, QWidget* parent = NULL ): _widget(widget), _parent(parent) {} |
|---|
| 137 | GLWidget* _widget; |
|---|
| 138 | QWidget* _parent; |
|---|
| 139 | }; |
|---|
| 140 | |
|---|
| 141 | bool init( QWidget* parent, const QGLWidget* shareWidget, Qt::WindowFlags f ); |
|---|
| 142 | |
|---|
| 143 | static QGLFormat traits2qglFormat( const osg::GraphicsContext::Traits* traits ); |
|---|
| 144 | static void qglFormat2traits( const QGLFormat& format, osg::GraphicsContext::Traits* traits ); |
|---|
| 145 | static osg::GraphicsContext::Traits* createTraits( const QGLWidget* widget ); |
|---|
| 146 | |
|---|
| 147 | virtual bool setWindowRectangleImplementation( int x, int y, int width, int height ); |
|---|
| 148 | virtual void getWindowRectangle( int& x, int& y, int& width, int& height ); |
|---|
| 149 | virtual bool setWindowDecorationImplementation( bool windowDecoration ); |
|---|
| 150 | virtual bool getWindowDecoration() const; |
|---|
| 151 | virtual void grabFocus(); |
|---|
| 152 | virtual void grabFocusIfPointerInWindow(); |
|---|
| 153 | virtual void raiseWindow(); |
|---|
| 154 | virtual void setWindowName( const std::string& name ); |
|---|
| 155 | virtual std::string getWindowName(); |
|---|
| 156 | virtual void useCursor( bool cursorOn ); |
|---|
| 157 | virtual void setCursor( MouseCursor cursor ); |
|---|
| 158 | |
|---|
| 159 | virtual bool valid() const; |
|---|
| 160 | virtual bool realizeImplementation(); |
|---|
| 161 | virtual bool isRealizedImplementation() const; |
|---|
| 162 | virtual void closeImplementation(); |
|---|
| 163 | virtual bool makeCurrentImplementation(); |
|---|
| 164 | virtual bool releaseContextImplementation(); |
|---|
| 165 | virtual void swapBuffersImplementation(); |
|---|
| 166 | virtual void runOperations(); |
|---|
| 167 | |
|---|
| 168 | virtual void requestWarpPointer( float x, float y ); |
|---|
| 169 | |
|---|
| 170 | protected: |
|---|
| 171 | |
|---|
| 172 | friend class GLWidget; |
|---|
| 173 | GLWidget* _widget; |
|---|
| 174 | bool _ownsWidget; |
|---|
| 175 | QCursor _currentCursor; |
|---|
| 176 | bool _realized; |
|---|
| 177 | }; |
|---|
| 178 | |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | #endif |
|---|