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

Revision 13482, 4.6 kB (checked in by robert, 6 hours ago)

Changed the osgUI behaviour so that events are set to be handled by Widgets that have focus even if they don't directly use them.

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