root/OpenSceneGraph/trunk/examples/osgviewerQT/osgviewerQT.cpp @ 6938

Revision 6938, 6.6 kB (checked in by robert, 8 years ago)

Build fixes for QT4

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL.
2
3#include <osgViewer/Viewer>
4#include <osgViewer/CompositeViewer>
5#include <osgViewer/ViewerEventHandlers>
6#include <osgGA/TrackballManipulator>
7#include <osgDB/ReadFile>
8
9#if USE_QT4
10
11    #include <QtCore/QString>
12    #include <QtCore/QTimer>
13    #include <QtGui/QKeyEvent>
14    #include <QtGui/QApplication>
15    #include <QtOpenGL/QGLWidget>
16   
17    using Qt::WindowFlags;
18
19#else
20
21    class QWidget;
22    #include <qtimer.h>
23    #include <qgl.h>
24    #include <qapplication.h>
25
26    #define WindowFlags WFlags
27
28#endif
29
30#include <iostream>
31
32class AdapterWidget : public QGLWidget
33{
34    public:
35
36        AdapterWidget( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0 );
37
38        virtual ~AdapterWidget() {}
39
40        osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
41        const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
42
43    protected:
44
45        void init();
46
47        virtual void resizeGL( int width, int height );
48        virtual void keyPressEvent( QKeyEvent* event );
49        virtual void keyReleaseEvent( QKeyEvent* event );
50        virtual void mousePressEvent( QMouseEvent* event );
51        virtual void mouseReleaseEvent( QMouseEvent* event );
52        virtual void mouseMoveEvent( QMouseEvent* event );
53
54        osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
55};
56
57AdapterWidget::AdapterWidget( QWidget * parent, const char * name, const QGLWidget * shareWidget, WindowFlags f):
58#if USE_QT4
59    QGLWidget(parent, shareWidget, f)
60#else
61    QGLWidget(parent, name, shareWidget, f)
62#endif
63{
64    _gw = new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());
65}
66
67void AdapterWidget::resizeGL( int width, int height )
68{
69    _gw->getEventQueue()->windowResize(0, 0, width, height );
70    _gw->resized(0,0,width,height);
71}
72
73void AdapterWidget::keyPressEvent( QKeyEvent* event )
74{
75    _gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
76}
77
78void AdapterWidget::keyReleaseEvent( QKeyEvent* event )
79{
80    _gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
81}
82
83void AdapterWidget::mousePressEvent( QMouseEvent* event )
84{
85    int button = 0;
86    switch(event->button())
87    {
88        case(Qt::LeftButton): button = 1; break;
89        case(Qt::MidButton): button = 2; break;
90        case(Qt::RightButton): button = 3; break;
91        case(Qt::NoButton): button = 0; break;
92        default: button = 0; break;
93    }
94    _gw->getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
95}
96
97void AdapterWidget::mouseReleaseEvent( QMouseEvent* event )
98{
99    int button = 0;
100    switch(event->button())
101    {
102        case(Qt::LeftButton): button = 1; break;
103        case(Qt::MidButton): button = 2; break;
104        case(Qt::RightButton): button = 3; break;
105        case(Qt::NoButton): button = 0; break;
106        default: button = 0; break;
107    }
108    _gw->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
109}
110
111void AdapterWidget::mouseMoveEvent( QMouseEvent* event )
112{
113    _gw->getEventQueue()->mouseMotion(event->x(), event->y());
114}
115
116
117class ViewerQT : public osgViewer::Viewer, public AdapterWidget
118{
119    public:
120
121        ViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0):
122            AdapterWidget( parent, name, shareWidget, f )
123        {
124            getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
125            getCamera()->setGraphicsContext(getGraphicsWindow());
126            setThreadingModel(osgViewer::Viewer::SingleThreaded);
127
128            connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
129            _timer.start(10);
130        }
131
132        virtual void paintGL()
133        {
134            frame();
135        }
136   
137    protected:
138
139        QTimer _timer;
140};
141
142class CompositeViewerQT : public osgViewer::CompositeViewer, public AdapterWidget
143{
144    public:
145
146        CompositeViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0):
147            AdapterWidget( parent, name, shareWidget, f )
148        {
149            setThreadingModel(osgViewer::CompositeViewer::SingleThreaded);
150
151            connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
152            _timer.start(10);
153        }
154
155        virtual void paintGL()
156        {
157            frame();
158        }
159   
160    protected:
161
162        QTimer _timer;
163};
164
165int main( int argc, char **argv )
166{
167    QApplication a( argc, argv );
168   
169    if (argc<2)
170    {
171        std::cout << argv[0] <<": requires filename argument." << std::endl;
172        return 1;
173    }
174
175    osg::ArgumentParser arguments(&argc, argv);
176
177    // load the scene.
178    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
179    if (!loadedModel)
180    {
181        std::cout << argv[0] <<": No data loaded." << std::endl;
182        return 1;
183    }
184   
185   
186    if (arguments.read("--CompositeViewer"))
187    {
188        CompositeViewerQT* viewerWindow = new CompositeViewerQT;
189
190        unsigned int width = viewerWindow->width();
191        unsigned int height = viewerWindow->height();
192       
193        {
194            osgViewer::View* view1 = new osgViewer::View;
195            view1->getCamera()->setGraphicsContext(viewerWindow->getGraphicsWindow());
196            view1->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height/2), 1.0, 1000.0);
197            view1->getCamera()->setViewport(new osg::Viewport(0,0,width,height/2));
198            view1->setCameraManipulator(new osgGA::TrackballManipulator);
199            view1->setSceneData(loadedModel.get());
200           
201            viewerWindow->addView(view1);
202        }
203       
204        {
205            osgViewer::View* view2 = new osgViewer::View;
206            view2->getCamera()->setGraphicsContext(viewerWindow->getGraphicsWindow());
207            view2->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height/2), 1.0, 1000.0);
208            view2->getCamera()->setViewport(new osg::Viewport(0,height/2,width,height/2));
209            view2->setCameraManipulator(new osgGA::TrackballManipulator);
210            view2->setSceneData(loadedModel.get());
211           
212            viewerWindow->addView(view2);
213        }
214
215        viewerWindow->show();
216    }
217    else
218    {
219        ViewerQT* viewerWindow = new ViewerQT;
220
221        viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
222        viewerWindow->setSceneData(loadedModel.get());
223
224        viewerWindow->show();
225    }   
226   
227   
228    a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
229   
230    return a.exec();
231}
232
233/*EOF*/
Note: See TracBrowser for help on using the browser.