GraphicsWindowQt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #ifndef OSGVIEWER_GRAPHICSWINDOWQT
  14. #define OSGVIEWER_GRAPHICSWINDOWQT
  15. #include <osgViewer/GraphicsWindow>
  16. #include <osgQt/Export>
  17. #include <QMutex>
  18. #include <QEvent>
  19. #include <QQueue>
  20. #include <QSet>
  21. #include <QGLWidget>
  22. #include <osg/Version>
  23. class QInputEvent;
  24. class QGestureEvent;
  25. namespace osgViewer {
  26. class ViewerBase;
  27. }
  28. namespace osgQt
  29. {
  30. // forward declarations
  31. class GraphicsWindowQt;
  32. #if OSG_VERSION_LESS_THAN(3, 5, 6)
  33. /// The function sets the WindowingSystem to Qt.
  34. void OSGQT_EXPORT initQtWindowingSystem();
  35. #endif
  36. /** The function sets the viewer that will be used after entering
  37. * the Qt main loop (QCoreApplication::exec()).
  38. *
  39. * The function also initializes internal structures required for proper
  40. * scene rendering.
  41. *
  42. * The method must be called from main thread. */
  43. void OSGQT_EXPORT setViewer( osgViewer::ViewerBase *viewer );
  44. class OSGQT_EXPORT GLWidget : public QGLWidget
  45. {
  46. typedef QGLWidget inherited;
  47. public:
  48. GLWidget( QWidget* parent = NULL, const QGLWidget* shareWidget = NULL, Qt::WindowFlags f = 0, bool forwardKeyEvents = false );
  49. GLWidget( QGLContext* context, QWidget* parent = NULL, const QGLWidget* shareWidget = NULL, Qt::WindowFlags f = 0, bool forwardKeyEvents = false );
  50. GLWidget( const QGLFormat& format, QWidget* parent = NULL, const QGLWidget* shareWidget = NULL, Qt::WindowFlags f = 0, bool forwardKeyEvents = false );
  51. virtual ~GLWidget();
  52. inline void setGraphicsWindow( GraphicsWindowQt* gw ) { _gw = gw; }
  53. inline GraphicsWindowQt* getGraphicsWindow() { return _gw; }
  54. inline const GraphicsWindowQt* getGraphicsWindow() const { return _gw; }
  55. inline bool getForwardKeyEvents() const { return _forwardKeyEvents; }
  56. virtual void setForwardKeyEvents( bool f ) { _forwardKeyEvents = f; }
  57. inline bool getTouchEventsEnabled() const { return _touchEventsEnabled; }
  58. void setTouchEventsEnabled( bool e );
  59. void setKeyboardModifiers( QInputEvent* event );
  60. virtual void keyPressEvent( QKeyEvent* event );
  61. virtual void keyReleaseEvent( QKeyEvent* event );
  62. virtual void mousePressEvent( QMouseEvent* event );
  63. virtual void mouseReleaseEvent( QMouseEvent* event );
  64. virtual void mouseDoubleClickEvent( QMouseEvent* event );
  65. virtual void mouseMoveEvent( QMouseEvent* event );
  66. virtual void wheelEvent( QWheelEvent* event );
  67. virtual bool gestureEvent( QGestureEvent* event );
  68. protected:
  69. int getNumDeferredEvents()
  70. {
  71. QMutexLocker lock(&_deferredEventQueueMutex);
  72. return _deferredEventQueue.count();
  73. }
  74. void enqueueDeferredEvent(QEvent::Type eventType, QEvent::Type removeEventType = QEvent::None)
  75. {
  76. QMutexLocker lock(&_deferredEventQueueMutex);
  77. if (removeEventType != QEvent::None)
  78. {
  79. if (_deferredEventQueue.removeOne(removeEventType))
  80. _eventCompressor.remove(eventType);
  81. }
  82. if (_eventCompressor.find(eventType) == _eventCompressor.end())
  83. {
  84. _deferredEventQueue.enqueue(eventType);
  85. _eventCompressor.insert(eventType);
  86. }
  87. }
  88. void processDeferredEvents();
  89. friend class GraphicsWindowQt;
  90. GraphicsWindowQt* _gw;
  91. QMutex _deferredEventQueueMutex;
  92. QQueue<QEvent::Type> _deferredEventQueue;
  93. QSet<QEvent::Type> _eventCompressor;
  94. bool _touchEventsEnabled;
  95. bool _forwardKeyEvents;
  96. qreal _devicePixelRatio;
  97. virtual void resizeEvent( QResizeEvent* event );
  98. virtual void moveEvent( QMoveEvent* event );
  99. virtual void glDraw();
  100. virtual bool event( QEvent* event );
  101. };
  102. class OSGQT_EXPORT GraphicsWindowQt : public osgViewer::GraphicsWindow
  103. {
  104. public:
  105. GraphicsWindowQt( osg::GraphicsContext::Traits* traits, QWidget* parent = NULL, const QGLWidget* shareWidget = NULL, Qt::WindowFlags f = 0 );
  106. GraphicsWindowQt( GLWidget* widget );
  107. virtual ~GraphicsWindowQt();
  108. inline GLWidget* getGLWidget() { return _widget; }
  109. inline const GLWidget* getGLWidget() const { return _widget; }
  110. /// deprecated
  111. inline GLWidget* getGraphWidget() { return _widget; }
  112. /// deprecated
  113. inline const GLWidget* getGraphWidget() const { return _widget; }
  114. struct WindowData : public osg::Referenced
  115. {
  116. WindowData( GLWidget* widget = NULL, QWidget* parent = NULL ): _widget(widget), _parent(parent) {}
  117. GLWidget* _widget;
  118. QWidget* _parent;
  119. };
  120. bool init( QWidget* parent, const QGLWidget* shareWidget, Qt::WindowFlags f );
  121. static QGLFormat traits2qglFormat( const osg::GraphicsContext::Traits* traits );
  122. static void qglFormat2traits( const QGLFormat& format, osg::GraphicsContext::Traits* traits );
  123. static osg::GraphicsContext::Traits* createTraits( const QGLWidget* widget );
  124. virtual bool setWindowRectangleImplementation( int x, int y, int width, int height );
  125. virtual void getWindowRectangle( int& x, int& y, int& width, int& height );
  126. virtual bool setWindowDecorationImplementation( bool windowDecoration );
  127. virtual bool getWindowDecoration() const;
  128. virtual void grabFocus();
  129. virtual void grabFocusIfPointerInWindow();
  130. virtual void raiseWindow();
  131. virtual void setWindowName( const std::string& name );
  132. virtual std::string getWindowName();
  133. virtual void useCursor( bool cursorOn );
  134. virtual void setCursor( MouseCursor cursor );
  135. inline bool getTouchEventsEnabled() const { return _widget->getTouchEventsEnabled(); }
  136. virtual void setTouchEventsEnabled( bool e ) { _widget->setTouchEventsEnabled(e); }
  137. virtual bool valid() const;
  138. virtual bool realizeImplementation();
  139. virtual bool isRealizedImplementation() const;
  140. virtual void closeImplementation();
  141. virtual bool makeCurrentImplementation();
  142. virtual bool releaseContextImplementation();
  143. virtual void swapBuffersImplementation();
  144. virtual void runOperations();
  145. virtual void requestWarpPointer( float x, float y );
  146. protected:
  147. friend class GLWidget;
  148. GLWidget* _widget;
  149. bool _ownsWidget;
  150. QCursor _currentCursor;
  151. bool _realized;
  152. };
  153. }
  154. #endif