root/OpenSceneGraph/trunk/examples/osgQtBrowser/osgQtBrowser.cpp @ 13374

Revision 12292, 4.5 kB (checked in by robert, 4 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
Line 
1/* OpenSceneGraph example, osgcompositeviewer.
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 <iostream>
20
21#include <osg/Notify>
22#include <osg/io_utils>
23
24#include <osg/ArgumentParser>
25#include <osgDB/WriteFile>
26#include <osgGA/TrackballManipulator>
27#include <osgGA/StateSetManipulator>
28#include <osgViewer/Viewer>
29#include <osgViewer/ViewerEventHandlers>
30#include <osgWidget/Browser>
31
32
33#include <QtWebKit/QWebSettings>
34#include <QtWebKit/QtWebKit>
35#include <QtGui/QGraphicsScene>
36#include <QtGui/QGraphicsView>
37#include <QtGui/QApplication>
38#include <QtGui/QPainter>
39#include <QtGui/QtEvents>
40
41#include <osgQt/QGraphicsViewAdapter>
42#include <osgQt/QWebViewImage>
43
44
45// Thread that runs the viewer's frame loop as we can't run Qt in the background...
46class ViewerFrameThread : public OpenThreads::Thread
47{
48    public:
49
50        ViewerFrameThread(osgViewer::ViewerBase* viewerBase, bool doQApplicationExit):
51            _viewerBase(viewerBase),
52            _doQApplicationExit(doQApplicationExit) {}
53
54        ~ViewerFrameThread()
55        {
56            cancel();
57            while(isRunning())
58            {
59                OpenThreads::Thread::YieldCurrentThread();
60            }
61        }
62
63        int cancel()
64        {
65            _viewerBase->setDone(true);
66            return 0;
67        }
68
69        void run()
70        {
71            int result = _viewerBase->run();
72
73            if (_doQApplicationExit) QApplication::exit(result);
74        }
75
76        osg::ref_ptr<osgViewer::ViewerBase> _viewerBase;
77        bool _doQApplicationExit;
78};
79
80
81int main(int argc, char **argv)
82{
83    // Qt requires that we construct the global QApplication before creating any widgets.
84    QApplication app(argc, argv);
85
86    // use an ArgumentParser object to manage the program arguments.
87    osg::ArgumentParser arguments(&argc,argv);
88
89    bool useFrameLoopThread = false;
90    if (arguments.read("--no-frame-thread")) useFrameLoopThread = false;
91    if (arguments.read("--frame-thread")) useFrameLoopThread = true;
92
93    osg::ref_ptr<osgQt::QWebViewImage> image = new osgQt::QWebViewImage;
94
95    if (arguments.argc()>1) image->navigateTo((arguments[1]));
96    else image->navigateTo("http://www.youtube.com/");
97
98    osgWidget::GeometryHints hints(osg::Vec3(0.0f,0.0f,0.0f),
99                                   osg::Vec3(1.0f,0.0f,0.0f),
100                                   osg::Vec3(0.0f,0.0f,1.0f),
101                                   osg::Vec4(1.0f,1.0f,1.0f,1.0f),
102                                   osgWidget::GeometryHints::RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO);
103
104
105    osg::ref_ptr<osgWidget::Browser> browser = new osgWidget::Browser;
106    browser->assign(image.get(), hints);
107
108    // image->focusBrowser(true);
109
110    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(arguments);
111    viewer->setSceneData(browser.get());
112    viewer->setCameraManipulator(new osgGA::TrackballManipulator());
113    viewer->addEventHandler(new osgViewer::StatsHandler);
114    viewer->addEventHandler(new osgViewer::WindowSizeHandler);
115
116    if (useFrameLoopThread)
117    {
118        // create a thread to run the viewer's frame loop
119        ViewerFrameThread viewerThread(viewer.get(), true);
120        viewerThread.startThread();
121
122        // now start the standard Qt event loop, then exists when the viewerThead sends the QApplication::exit() signal.
123        return QApplication::exec();
124
125    }
126    else
127    {
128        // run the frame loop, interleaving Qt and the main OSG frame loop
129        while(!viewer->done())
130        {
131            // process Qt events - this handles both events and paints the browser image
132            QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
133
134            viewer->frame();
135        }
136
137        return 0;
138    }
139}
Note: See TracBrowser for help on using the browser.