OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GraphicsWindowQt.h
Go to the documentation of this file.
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 <QGLWidget>
18 
19 #include <osgViewer/GraphicsWindow>
20 #include <osgQt/Export>
21 #include <osgQt/Version>
22 
23 #include <QMutex>
24 #include <QEvent>
25 #include <QQueue>
26 #include <QSet>
27 #include <QGLWidget>
28 
29 class QInputEvent;
30 class QGestureEvent;
31 
32 namespace osgViewer {
33  class ViewerBase;
34 }
35 
36 namespace osgQt
37 {
38 
39 // forward declarations
40 class GraphicsWindowQt;
41 
44 
53 
54 
55 class OSGQT_EXPORT GLWidget : public QGLWidget
56 {
57  typedef QGLWidget inherited;
58 
59 public:
60 
61  GLWidget( QWidget* parent = NULL, const QGLWidget* shareWidget = NULL, Qt::WindowFlags f = 0, bool forwardKeyEvents = false );
62  GLWidget( QGLContext* context, QWidget* parent = NULL, const QGLWidget* shareWidget = NULL, Qt::WindowFlags f = 0, bool forwardKeyEvents = false );
63  GLWidget( const QGLFormat& format, QWidget* parent = NULL, const QGLWidget* shareWidget = NULL, Qt::WindowFlags f = 0, bool forwardKeyEvents = false );
64  virtual ~GLWidget();
65 
66  inline void setGraphicsWindow( GraphicsWindowQt* gw ) { _gw = gw; }
67  inline GraphicsWindowQt* getGraphicsWindow() { return _gw; }
68  inline const GraphicsWindowQt* getGraphicsWindow() const { return _gw; }
69 
70  inline bool getForwardKeyEvents() const { return _forwardKeyEvents; }
71  virtual void setForwardKeyEvents( bool f ) { _forwardKeyEvents = f; }
72 
73  inline bool getTouchEventsEnabled() const { return _touchEventsEnabled; }
74  void setTouchEventsEnabled( bool e );
75 
76  void setKeyboardModifiers( QInputEvent* event );
77 
78  virtual void keyPressEvent( QKeyEvent* event );
79  virtual void keyReleaseEvent( QKeyEvent* event );
80  virtual void mousePressEvent( QMouseEvent* event );
81  virtual void mouseReleaseEvent( QMouseEvent* event );
82  virtual void mouseDoubleClickEvent( QMouseEvent* event );
83  virtual void mouseMoveEvent( QMouseEvent* event );
84  virtual void wheelEvent( QWheelEvent* event );
85  virtual bool gestureEvent( QGestureEvent* event );
86 
87 protected:
88 
90  {
91  QMutexLocker lock(&_deferredEventQueueMutex);
92  return _deferredEventQueue.count();
93  }
94  void enqueueDeferredEvent(QEvent::Type eventType, QEvent::Type removeEventType = QEvent::None)
95  {
96  QMutexLocker lock(&_deferredEventQueueMutex);
97 
98  if (removeEventType != QEvent::None)
99  {
100  if (_deferredEventQueue.removeOne(removeEventType))
101  _eventCompressor.remove(eventType);
102  }
103 
104  if (_eventCompressor.find(eventType) == _eventCompressor.end())
105  {
106  _deferredEventQueue.enqueue(eventType);
107  _eventCompressor.insert(eventType);
108  }
109  }
110  void processDeferredEvents();
111 
112  friend class GraphicsWindowQt;
114 
116  QQueue<QEvent::Type> _deferredEventQueue;
117  QSet<QEvent::Type> _eventCompressor;
118 
120 
123 
124  virtual void resizeEvent( QResizeEvent* event );
125  virtual void moveEvent( QMoveEvent* event );
126  virtual void glDraw();
127  virtual bool event( QEvent* event );
128 };
129 
131 {
132 public:
133  GraphicsWindowQt( osg::GraphicsContext::Traits* traits, QWidget* parent = NULL, const QGLWidget* shareWidget = NULL, Qt::WindowFlags f = 0 );
134  GraphicsWindowQt( GLWidget* widget );
135  virtual ~GraphicsWindowQt();
136 
137  inline GLWidget* getGLWidget() { return _widget; }
138  inline const GLWidget* getGLWidget() const { return _widget; }
139 
141  inline GLWidget* getGraphWidget() { return _widget; }
143  inline const GLWidget* getGraphWidget() const { return _widget; }
144 
145  struct WindowData : public osg::Referenced
146  {
147  WindowData( GLWidget* widget = NULL, QWidget* parent = NULL ): _widget(widget), _parent(parent) {}
149  QWidget* _parent;
150  };
151 
152  bool init( QWidget* parent, const QGLWidget* shareWidget, Qt::WindowFlags f );
153 
154  static QGLFormat traits2qglFormat( const osg::GraphicsContext::Traits* traits );
155  static void qglFormat2traits( const QGLFormat& format, osg::GraphicsContext::Traits* traits );
156  static osg::GraphicsContext::Traits* createTraits( const QGLWidget* widget );
157 
158  virtual bool setWindowRectangleImplementation( int x, int y, int width, int height );
159  virtual void getWindowRectangle( int& x, int& y, int& width, int& height );
160  virtual bool setWindowDecorationImplementation( bool windowDecoration );
161  virtual bool getWindowDecoration() const;
162  virtual void grabFocus();
163  virtual void grabFocusIfPointerInWindow();
164  virtual void raiseWindow();
165  virtual void setWindowName( const std::string& name );
166  virtual std::string getWindowName();
167  virtual void useCursor( bool cursorOn );
168  virtual void setCursor( MouseCursor cursor );
169  inline bool getTouchEventsEnabled() const { return _widget->getTouchEventsEnabled(); }
170  virtual void setTouchEventsEnabled( bool e ) { _widget->setTouchEventsEnabled(e); }
171 
172 
173  virtual bool valid() const;
174  virtual bool realizeImplementation();
175  virtual bool isRealizedImplementation() const;
176  virtual void closeImplementation();
177  virtual bool makeCurrentImplementation();
178  virtual bool releaseContextImplementation();
179  virtual void swapBuffersImplementation();
180  virtual void runOperations();
181 
182  virtual void requestWarpPointer( float x, float y );
183 
184 protected:
185 
186  friend class GLWidget;
189  QCursor _currentCursor;
190  bool _realized;
191 };
192 
193 }
194 
195 #endif
void enqueueDeferredEvent(QEvent::Type eventType, QEvent::Type removeEventType=QEvent::None)
#define NULL
Definition: Export.h:59
#define OSGQT_EXPORT
Definition: Export.h:40
void OSGQT_EXPORT initQtWindowingSystem()
The function sets the WindowingSystem to Qt.
GraphicsWindowQt * _gw
void OSGQT_EXPORT setViewer(osgViewer::ViewerBase *viewer)
const GLWidget * getGraphWidget() const
deprecated
bool getForwardKeyEvents() const
GLint GLenum GLsizei width
Definition: GLU.h:71
GLWidget * getGraphWidget()
deprecated
void setGraphicsWindow(GraphicsWindowQt *gw)
QSet< QEvent::Type > _eventCompressor
QQueue< QEvent::Type > _deferredEventQueue
WindowData(GLWidget *widget=NULL, QWidget *parent=NULL)
const GLWidget * getGLWidget() const
GLint GLenum GLsizei GLsizei height
Definition: GLU.h:71
virtual void setTouchEventsEnabled(bool e)
const GraphicsWindowQt * getGraphicsWindow() const
bool getTouchEventsEnabled() const
QMutex _deferredEventQueueMutex
virtual void setForwardKeyEvents(bool f)
GraphicsWindowQt * getGraphicsWindow()
bool getTouchEventsEnabled() const
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum format
Definition: GLU.h:71