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

Revision 6946, 7.8 kB (checked in by robert, 7 years ago)

Added in QT4 keyboard event adaption

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osganimate.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osgViewer/Viewer>
20#include <osgViewer/CompositeViewer>
21#include <osgViewer/ViewerEventHandlers>
22#include <osgGA/TrackballManipulator>
23#include <osgDB/ReadFile>
24
25#if USE_QT4
26
27    #include <QtCore/QString>
28    #include <QtCore/QTimer>
29    #include <QtGui/QKeyEvent>
30    #include <QtGui/QApplication>
31    #include <QtOpenGL/QGLWidget>
32   
33    using Qt::WindowFlags;
34
35#else
36
37    class QWidget;
38    #include <qtimer.h>
39    #include <qgl.h>
40    #include <qapplication.h>
41
42    #define WindowFlags WFlags
43
44#endif
45
46#include <iostream>
47
48class AdapterWidget : public QGLWidget
49{
50    public:
51
52        AdapterWidget( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0 );
53
54        virtual ~AdapterWidget() {}
55
56        osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
57        const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
58
59    protected:
60
61        void init();
62
63        virtual void resizeGL( int width, int height );
64        virtual void keyPressEvent( QKeyEvent* event );
65        virtual void keyReleaseEvent( QKeyEvent* event );
66        virtual void mousePressEvent( QMouseEvent* event );
67        virtual void mouseReleaseEvent( QMouseEvent* event );
68        virtual void mouseMoveEvent( QMouseEvent* event );
69
70        osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
71};
72
73AdapterWidget::AdapterWidget( QWidget * parent, const char * name, const QGLWidget * shareWidget, WindowFlags f):
74#if USE_QT4
75    QGLWidget(parent, shareWidget, f)
76#else
77    QGLWidget(parent, name, shareWidget, f)
78#endif
79{
80    _gw = new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());
81}
82
83void AdapterWidget::resizeGL( int width, int height )
84{
85    _gw->getEventQueue()->windowResize(0, 0, width, height );
86    _gw->resized(0,0,width,height);
87}
88
89void AdapterWidget::keyPressEvent( QKeyEvent* event )
90{
91#if USE_QT4
92    _gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
93#else
94    _gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
95#endif
96}
97
98void AdapterWidget::keyReleaseEvent( QKeyEvent* event )
99{
100#if USE_QT4
101    _gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
102#else
103    _gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
104#endif
105}
106
107void AdapterWidget::mousePressEvent( QMouseEvent* event )
108{
109    int button = 0;
110    switch(event->button())
111    {
112        case(Qt::LeftButton): button = 1; break;
113        case(Qt::MidButton): button = 2; break;
114        case(Qt::RightButton): button = 3; break;
115        case(Qt::NoButton): button = 0; break;
116        default: button = 0; break;
117    }
118    _gw->getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
119}
120
121void AdapterWidget::mouseReleaseEvent( QMouseEvent* event )
122{
123    int button = 0;
124    switch(event->button())
125    {
126        case(Qt::LeftButton): button = 1; break;
127        case(Qt::MidButton): button = 2; break;
128        case(Qt::RightButton): button = 3; break;
129        case(Qt::NoButton): button = 0; break;
130        default: button = 0; break;
131    }
132    _gw->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
133}
134
135void AdapterWidget::mouseMoveEvent( QMouseEvent* event )
136{
137    _gw->getEventQueue()->mouseMotion(event->x(), event->y());
138}
139
140
141class ViewerQT : public osgViewer::Viewer, public AdapterWidget
142{
143    public:
144
145        ViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0):
146            AdapterWidget( parent, name, shareWidget, f )
147        {
148            getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
149            getCamera()->setGraphicsContext(getGraphicsWindow());
150            setThreadingModel(osgViewer::Viewer::SingleThreaded);
151
152            connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
153            _timer.start(10);
154        }
155
156        virtual void paintGL()
157        {
158            frame();
159        }
160   
161    protected:
162
163        QTimer _timer;
164};
165
166class CompositeViewerQT : public osgViewer::CompositeViewer, public AdapterWidget
167{
168    public:
169
170        CompositeViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0):
171            AdapterWidget( parent, name, shareWidget, f )
172        {
173            setThreadingModel(osgViewer::CompositeViewer::SingleThreaded);
174
175            connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
176            _timer.start(10);
177        }
178
179        virtual void paintGL()
180        {
181            frame();
182        }
183   
184    protected:
185
186        QTimer _timer;
187};
188
189int main( int argc, char **argv )
190{
191    QApplication a( argc, argv );
192   
193    if (argc<2)
194    {
195        std::cout << argv[0] <<": requires filename argument." << std::endl;
196        return 1;
197    }
198
199    osg::ArgumentParser arguments(&argc, argv);
200
201    // load the scene.
202    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
203    if (!loadedModel)
204    {
205        std::cout << argv[0] <<": No data loaded." << std::endl;
206        return 1;
207    }
208   
209   
210    if (arguments.read("--CompositeViewer"))
211    {
212        CompositeViewerQT* viewerWindow = new CompositeViewerQT;
213
214        unsigned int width = viewerWindow->width();
215        unsigned int height = viewerWindow->height();
216       
217        {
218            osgViewer::View* view1 = new osgViewer::View;
219            view1->getCamera()->setGraphicsContext(viewerWindow->getGraphicsWindow());
220            view1->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height/2), 1.0, 1000.0);
221            view1->getCamera()->setViewport(new osg::Viewport(0,0,width,height/2));
222            view1->setCameraManipulator(new osgGA::TrackballManipulator);
223            view1->setSceneData(loadedModel.get());
224           
225            viewerWindow->addView(view1);
226        }
227       
228        {
229            osgViewer::View* view2 = new osgViewer::View;
230            view2->getCamera()->setGraphicsContext(viewerWindow->getGraphicsWindow());
231            view2->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height/2), 1.0, 1000.0);
232            view2->getCamera()->setViewport(new osg::Viewport(0,height/2,width,height/2));
233            view2->setCameraManipulator(new osgGA::TrackballManipulator);
234            view2->setSceneData(loadedModel.get());
235           
236            viewerWindow->addView(view2);
237        }
238
239        viewerWindow->show();
240    }
241    else
242    {
243        ViewerQT* viewerWindow = new ViewerQT;
244
245        viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
246        viewerWindow->setSceneData(loadedModel.get());
247
248        viewerWindow->show();
249    }   
250   
251   
252    a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
253   
254    return a.exec();
255}
256
257/*EOF*/
Note: See TracBrowser for help on using the browser.