CEDRIC  Revision_backup-2009-02
OsgWidget.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Copyright (C) 2008 Boris Iven, Max Hermann
4  *
5  * This file is part of the "CEDRIC Event Display" application.
6  *
7  *****************************************************************************/
8 
9 #ifndef OSGWIDGET_H
10 #define OSGWIDGET_H
11 
12 #include <osgViewer/Viewer>
13 #include <osgViewer/ViewerEventHandlers>
14 
15 #include <QtOpenGL/QGLWidget>
16 class QTimer;
17 
18 #include <iostream>
19 using std::cout;
20 using std::cerr;
21 using std::endl;
22 
23 
24 //#define OSGADAPTER_GW_DERIVED // <-- derive OsgAdapterWidget from osgViewer::GraphicsWindowEmbedded (not needed)
25 //#define OSGVIEWER_OVERPAINT // <-- provide overpaintGL() function for overpaint Gl scene with QPainter stuff
26  // Does not to work properly with Qt4.3 and osg2.0, gives lots of the following
27  // Warning: detected OpenGL error 'invalid enumerant' after RenderBin::draw(,)
28 
29 //#define OSGWIDGET_MULTITHREADED // <-- multithreading support UNDER DEVELOPMENT (DO NOT USE)
30 //#define OSGWIDGET_IDLE_TIMER // <-- use a 120hz timer for calling idle()
31 #define OSGWIDGET_IDLE_COUNT // <-- count how often idle is called (used to analyze performance issues and wether to use OSGWIDGET_IDLE_TIMER)
32 #define OSGWIDGET_SUPPORT_GL2PS
33 
45 #ifdef OSGADAPTER_GW_DERIVED
46 class OsgAdapterWidget : public QGLWidget, public osgViewer::GraphicsWindowEmbedded
47 #else
48 class OsgAdapterWidget : public QGLWidget
49 #endif
50 {
51  Q_OBJECT
52 
53 public:
54  OsgAdapterWidget( QWidget* parent = 0 );
55  virtual ~OsgAdapterWidget() {}
56 
57  osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
58  const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
59 
60 signals:
61  void processEvents();
62 
63 protected:
64  void init();
65 
66  virtual void resizeGL( int width, int height );
67  virtual void keyPressEvent ( QKeyEvent* event );
68  virtual void keyReleaseEvent ( QKeyEvent* event );
69  virtual void mousePressEvent ( QMouseEvent* event );
70  virtual void mouseReleaseEvent( QMouseEvent* event );
71  virtual void mouseMoveEvent ( QMouseEvent* event );
72  virtual void wheelEvent ( QWheelEvent* event );
73 
74  #ifdef OSGADAPTER_GW_DERIVED
75  const osgViewer::GraphicsWindowEmbedded* _gw; // this pointer
76  #else
77  osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
78  #endif
79 
80  osgGA::GUIEventAdapter* makeEventAdapter( QMouseEvent* event, osgGA::GUIEventAdapter::EventType type );
81 };
82 
83 
109 class OsgViewerWidget : public OsgAdapterWidget, public osgViewer::Viewer
110 {
111  Q_OBJECT
112 
113 public:
114  OsgViewerWidget( QWidget* parent=0 );
115  virtual ~OsgViewerWidget();
116 
117  #ifdef OSGWIDGET_IDLE_COUNT
118  unsigned long idleCount;
119  #endif
120 
121 public slots:
123  virtual void idle( double simulationTime=USE_REFERENCE_TIME );
124 
126  void triggerRedraw();
127 
129  void toggleContinuousUpdate( bool activate );
130 
132  void forceContinuousUpdate( bool force )
133  {
134  _continuousUpdateForced = force;
135  toggleContinuousUpdate( force );
136  }
137 
138 public:
140  virtual void paintGL();
141 
142 #ifdef OSGVIEWER_OVERPAINT
143  virtual void overpaintGL( QPainter& /*painter*/ ) {}
145 #endif
146 
147 #ifdef OSGWIDGET_SUPPORT_GL2PS
148  virtual bool paintGL2PS( const char* filename, int format );
150 #endif
151 
155  virtual void frame( double simulationTime=USE_REFERENCE_TIME );
157 
160  virtual void requestRedraw()
161  {
162  //cout << "OsgViewerWidget::requestRedraw() called" << endl;
163  triggerRedraw();
164  }
165 
166  virtual void requestContinuousUpdate( bool needed = true )
167  {
168  //cout << "OsgViewerWidget::requestContinuousUpdate(" << (needed?"true":"false") << ") called" << endl;
169  //if( needed ) cout << "OsgViewerWidget::requestContinuousUpdate( true ) called!" << endl;
170  toggleContinuousUpdate( needed );
171  triggerRedraw();
172  }
173 
175  virtual void requestWarpPointer( float x, float y )
176  {
177  cout << "OsgViewerWidget::requestWarpPointer(" << x << "," << y << ") called" << endl;
178  }
180 
183  {
184  return _continuousUpdateForced;
185  }
186 
187 protected:
188  /* ---> UNUSED
190  virtual void prePaintGL();
192  virtual void postPaintGL();
193  */
194 
195 private:
196  bool _idleProcessed;
197  bool _continuousUpdate;
198  bool _continuousUpdateForced;
199 
200  #ifdef OSGWIDGET_IDLE_TIMER
201  QTimer *idleTimer;
202  #endif
203  QTimer *redrawTimer;
204 };
205 
206 #endif
virtual void paintGL()
Render the scenegraph by calling frame() (QGLWidget override)
Definition: OsgWidget.cpp:121
virtual ~OsgAdapterWidget()
Definition: OsgWidget.h:55
const osgViewer::GraphicsWindow * getGraphicsWindow() const
Definition: OsgWidget.h:58
Dispatch Qt Events to osgViewer::GraphicsWindowEmbedded.
Definition: OsgWidget.h:48
virtual void frame(double simulationTime=USE_REFERENCE_TIME)
Definition: OsgWidget.cpp:262
osgViewer::GraphicsWindow * getGraphicsWindow()
Definition: OsgWidget.h:57
virtual void keyPressEvent(QKeyEvent *event)
Definition: OsgWidget.cpp:305
virtual void requestRedraw()
Definition: OsgWidget.h:160
virtual void keyReleaseEvent(QKeyEvent *event)
Definition: OsgWidget.cpp:312
virtual bool paintGL2PS(const char *filename, int format)
Render scene to postscript using GL2PS library.
Definition: OsgWidget.cpp:163
virtual void idle(double simulationTime=USE_REFERENCE_TIME)
Do event and update traversal.
Definition: OsgWidget.cpp:231
Qt Widget for osgViewer::Viewer.
Definition: OsgWidget.h:109
virtual void mouseMoveEvent(QMouseEvent *event)
Definition: OsgWidget.cpp:389
virtual ~OsgViewerWidget()
Definition: OsgWidget.cpp:71
virtual void wheelEvent(QWheelEvent *event)
Definition: OsgWidget.cpp:398
void triggerRedraw()
Trigger a redraw in 1/60 sec.
Definition: OsgWidget.cpp:203
virtual void requestContinuousUpdate(bool needed=true)
Definition: OsgWidget.h:166
void toggleContinuousUpdate(bool activate)
Toggle continous redraw (usefull for animation)
Definition: OsgWidget.cpp:211
osgGA::GUIEventAdapter * makeEventAdapter(QMouseEvent *event, osgGA::GUIEventAdapter::EventType type)
Definition: OsgWidget.cpp:319
OsgViewerWidget(QWidget *parent=0)
Definition: OsgWidget.cpp:23
virtual void resizeGL(int width, int height)
Definition: OsgWidget.cpp:296
void forceContinuousUpdate(bool force)
Forces continous redraw (i.e. continuous redraw cannot be disabled via toggleContinousUpdate(false)) ...
Definition: OsgWidget.h:132
osg::ref_ptr< osgViewer::GraphicsWindowEmbedded > _gw
Definition: OsgWidget.h:77
virtual void mousePressEvent(QMouseEvent *event)
Definition: OsgWidget.cpp:371
virtual void requestWarpPointer(float x, float y)
Definition: OsgWidget.h:175
unsigned long idleCount
Definition: OsgWidget.h:118
bool isContinuousUpdateForced() const
Returns true if continous update was forced (i.e. continuous redraw cannot be disable via toggleConti...
Definition: OsgWidget.h:182
OsgAdapterWidget(QWidget *parent=0)
Definition: OsgWidget.cpp:278
virtual void mouseReleaseEvent(QMouseEvent *event)
Definition: OsgWidget.cpp:380