changed: auto add updateData callback to stages so that stagedata can be updated...
[opensg.git] / Source / WindowSystem / QT4 / testPassiveQT4.cpp
blobcbad08c4e1f5e3cadcfe1b4c194fc568e7acb668
1 // little example on how to use OpenSG with a standard QT OpenGL widget
2 // should be independent of QT version, and might also be used as an example
3 // on how ot integrate OpenSG into other windowing toolkits
5 // The only thing lost by doing it like this is the ability to separate the
6 // rendering and interface threads. If you need that you will have to
7 // use the real OpenSG QT widget.
9 // It is ASAP (as simple as possible ;), and thus probably not a good example
10 // for style, but I hope it's good enough to see what's going on.
12 // This code is based on the QT opengl/gear example.
14 #include <math.h>
16 #include <Qt/qapplication.h>
17 #include <QtOpenGL/QGLWidget>
18 #include <QtGui/QMouseEvent>
20 #if defined(Q_CC_MSVC)
21 #pragma warning(disable:4305) // init: truncation from const double to float
22 #endif
24 #include "OSGConfig.h"
25 #include "OSGSceneFileHandler.h"
26 #include "OSGSimpleGeometry.h"
27 #include "OSGPassiveWindow.h"
28 #include "OSGSimpleSceneManager.h"
30 class OpenSGWidget : public QGLWidget
32 public:
33 OpenSGWidget(const QGLFormat &f, QWidget *parent=0);
35 OSG::SimpleSceneManager *getManager(void);
36 protected:
37 void initializeGL();
38 void resizeGL( int, int );
39 void paintGL();
40 void mousePressEvent( QMouseEvent *ev );
41 void mouseMoveEvent( QMouseEvent *ev );
42 void mouseReleaseEvent( QMouseEvent *ev );
43 void wheelEvent( QWheelEvent *ev );
45 OSG::SimpleSceneManagerRefPtr mgr;
46 OSG::PassiveWindowRecPtr pwin;
49 OpenSGWidget::OpenSGWidget(const QGLFormat &f, QWidget *parent)
50 : QGLWidget(f, parent)
52 setAutoBufferSwap(false);
53 mgr = OSG::SimpleSceneManager::create();
54 pwin = OSG::PassiveWindow::create();
55 mgr->setWindow(pwin);
58 OSG::SimpleSceneManager *OpenSGWidget::getManager(void)
60 return mgr;
64 void OpenSGWidget::initializeGL()
66 pwin->init();
69 void OpenSGWidget::resizeGL( int width, int height )
71 mgr->resize(width,height);
74 void OpenSGWidget::paintGL()
76 mgr->redraw();
77 swapBuffers();
80 void OpenSGWidget::mousePressEvent( QMouseEvent *ev )
82 OSG::UInt32 button;
84 switch ( ev->button() )
86 case Qt::LeftButton:
87 button = OSG::SimpleSceneManager::MouseLeft;
88 break;
89 case Qt::MidButton:
90 button = OSG::SimpleSceneManager::MouseMiddle;
91 break;
92 case Qt::RightButton:
93 button = OSG::SimpleSceneManager::MouseRight;
94 break;
95 default: return;
97 mgr->mouseButtonPress(button, ev->x(), ev->y());
98 update();
101 void OpenSGWidget::mouseReleaseEvent( QMouseEvent *ev )
103 OSG::UInt32 button;
105 switch ( ev->button() )
107 case Qt::LeftButton:
108 button = OSG::SimpleSceneManager::MouseLeft;
109 break;
110 case Qt::MidButton:
111 button = OSG::SimpleSceneManager::MouseMiddle;
112 break;
113 case Qt::RightButton:
114 button = OSG::SimpleSceneManager::MouseRight;
115 break;
116 default: return;
118 mgr->mouseButtonRelease(button, ev->x(), ev->y());
119 update();
122 void OpenSGWidget::mouseMoveEvent( QMouseEvent *ev )
124 mgr->mouseMove(ev->x(), ev->y());
125 update();
128 void OpenSGWidget::wheelEvent( QWheelEvent *ev )
130 mgr->mouseButtonPress(ev->delta() > 0 ? OSG::SimpleSceneManager::MouseUp
131 : OSG::SimpleSceneManager::MouseDown,
132 ev->x(), ev->y());
134 ev->accept();
135 update();
139 int main( int argc, char **argv )
141 OSG::osgInit(argc,argv);
143 QApplication::setColorSpec( QApplication::CustomColor );
144 QApplication a( argc, argv );
146 if ( !QGLFormat::hasOpenGL() ) {
147 qWarning( "This system has no OpenGL support. Exiting." );
148 return -1;
151 OpenSGWidget w(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer | QGL::Rgba |
152 QGL::DirectRendering));
154 // create the scene
155 OSG::NodeRecPtr scene;
157 if(argc > 1)
159 scene = OSG::Node::create();
160 OSG::GroupRecPtr g = OSG::Group::create();
162 scene->setCore(g);
164 for(OSG::UInt16 i = 1; i < argc; ++i)
165 scene->addChild(OSG::SceneFileHandler::the()->read(argv[i]));
167 else
169 scene = OSG::makeTorus(.5, 3, 16, 16);
172 w.getManager()->setRoot(scene);
173 w.getManager()->showAll();
175 w.show();
176 return a.exec();