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

Revision 11147, 4.4 kB (checked in by robert, 4 years ago)

Moved QWebViewImage and QGraphicsViewAdapter into new osgQt utility library.

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 = true;
90    if (arguments.read("--no-frame-thread")) useFrameLoopThread = false;
91
92    osg::ref_ptr<osgQt::QWebViewImage> image = new osgQt::QWebViewImage;
93
94    if (arguments.argc()>1) image->navigateTo((arguments[1]));
95    else image->navigateTo("http://www.youtube.com/");
96
97    osgWidget::GeometryHints hints(osg::Vec3(0.0f,0.0f,0.0f),
98                                   osg::Vec3(1.0f,0.0f,0.0f),
99                                   osg::Vec3(0.0f,0.0f,1.0f),
100                                   osg::Vec4(1.0f,1.0f,1.0f,1.0f),
101                                   osgWidget::GeometryHints::RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO);
102
103
104    osg::ref_ptr<osgWidget::Browser> browser = new osgWidget::Browser;
105    browser->assign(image.get(), hints);
106
107    // image->focusBrowser(true);
108
109    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(arguments);
110    viewer->setSceneData(browser.get());
111    viewer->setCameraManipulator(new osgGA::TrackballManipulator());
112    viewer->addEventHandler(new osgViewer::StatsHandler);
113    viewer->addEventHandler(new osgViewer::WindowSizeHandler);
114
115    if (useFrameLoopThread)
116    {
117        // create a thread to run the viewer's frame loop
118        ViewerFrameThread viewerThread(viewer.get(), true);
119        viewerThread.startThread();
120
121        // now start the standard Qt event loop, then exists when the viewerThead sends the QApplication::exit() signal.
122        return QApplication::exec();
123
124    }
125    else
126    {
127        // run the frame loop, interleaving Qt and the main OSG frame loop
128        while(!viewer->done())
129        {
130            // process Qt events - this handles both events and paints the browser image
131            QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
132
133            viewer->frame();
134        }
135
136        return 0;
137    }
138}
Note: See TracBrowser for help on using the browser.