QWebViewImage 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef QWEBVIEWIMAGE
  14. #define QWEBVIEWIMAGE
  15. // make sure this header isn't built as par of osgQt, leaving it to applications to build
  16. #if !defined(OSGQT_LIBRARY) && !defined(OSG_LIBRARY_STATIC)
  17. #if QT_VERSION >= 0x050000
  18. # include <QtWebKitWidgets>
  19. #else
  20. # include <QtWebKit>
  21. #endif
  22. #include <osgWidget/Browser>
  23. #include <osgQt/QGraphicsViewAdapter>
  24. namespace osgQt
  25. {
  26. class QWebViewImage : public osgWidget::BrowserImage
  27. {
  28. public:
  29. QWebViewImage()
  30. {
  31. // make sure we have a valid QApplication before we start creating widgets.
  32. getOrCreateQApplication();
  33. _webView = new QWebView;
  34. _webPage = new QWebPage;
  35. _webPage->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
  36. _webPage->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
  37. _webView->setPage(_webPage);
  38. _adapter = new QGraphicsViewAdapter(this, _webView.data());
  39. }
  40. virtual void navigateTo(const std::string& url)
  41. {
  42. _webView->load(QUrl(url.c_str()));
  43. }
  44. QWebView* getQWebView() { return _webView; }
  45. QWebPage* getQWebPage() { return _webPage; }
  46. QGraphicsViewAdapter* getQGraphicsViewAdapter() { return _adapter; }
  47. void clearWriteBuffer()
  48. {
  49. _adapter->clearWriteBuffer();
  50. }
  51. void render()
  52. {
  53. if (_adapter->requiresRendering()) _adapter->render();
  54. }
  55. virtual bool requiresUpdateCall() const { return true; }
  56. virtual void update( osg::NodeVisitor* nv ) { render(); }
  57. virtual bool sendFocusHint(bool focus)
  58. {
  59. QFocusEvent event(focus ? QEvent::FocusIn : QEvent::FocusOut, Qt::OtherFocusReason);
  60. QCoreApplication::sendEvent(_webPage, &event);
  61. return true;
  62. }
  63. virtual bool sendPointerEvent(int x, int y, int buttonMask)
  64. {
  65. return _adapter->sendPointerEvent(x,y,buttonMask);
  66. }
  67. virtual bool sendKeyEvent(int key, bool keyDown)
  68. {
  69. return QWebViewImage::_adapter->sendKeyEvent(key, keyDown);
  70. }
  71. virtual void setFrameLastRendered(const osg::FrameStamp* frameStamp)
  72. {
  73. _adapter->setFrameLastRendered(frameStamp);
  74. }
  75. protected:
  76. QPointer<QGraphicsViewAdapter> _adapter;
  77. QPointer<QWebView> _webView;
  78. QPointer<QWebPage> _webPage;
  79. };
  80. }
  81. #endif
  82. #endif