gfbrowser.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <QApplication>
  2. #include <QGraphicsView>
  3. #include <QGraphicsWebView>
  4. #include <QDesktopWidget>
  5. #include <QString>
  6. #include <QWebFrame>
  7. #ifdef QT_OPEN_GLWIDGETS
  8. #include <QOpenGLWidget>
  9. #endif
  10. #ifdef QT_BLOG_RECOMMENDS
  11. #include <QGLWidget>
  12. #endif
  13. #include <QWebSecurityOrigin>
  14. /* Implementation that uses QGraphicsWebView to support hardware acceleration
  15. * on mobile devices.
  16. */
  17. int main(int argc, char *argv[])
  18. {
  19. QApplication app(argc, argv);
  20. /* Specifies whether images are automatically loaded in web pages. This is
  21. * enabled by default.
  22. */
  23. QWebSettings::globalSettings()->setAttribute(QWebSettings::AutoLoadImages, true);
  24. /* Enables or disables the running of JavaScript programs. This is enabled by
  25. * default.
  26. */
  27. QWebSettings::globalSettings()->setAttribute(QWebSettings::JavascriptEnabled, true);
  28. /* Private browsing prevents WebKit from recording visited pages in the
  29. * history and storing web page icons. This is disabled by default.
  30. */
  31. QWebSettings::globalSettings()->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
  32. /* This feature, when used in conjunction with QGraphicsWebView, accelerates
  33. * animations of web content. CSS animations of the transform and opacity
  34. * properties will be rendered by composing the cached content of the animated
  35. * elements. This is enabled by default.
  36. */
  37. QWebSettings::globalSettings()->setAttribute(QWebSettings::AcceleratedCompositingEnabled, true);
  38. /* This setting enables the tiled backing store feature for a
  39. * QGraphicsWebView. With the tiled backing store enabled, the web page
  40. * contents in and around the current visible area is speculatively cached
  41. * to bitmap tiles. The tiles are automatically kept in sync with the web page
  42. * as it changes. Enabling tiling can significantly speed up painting heavy
  43. * operations like scrolling. Enabling the feature increases memory
  44. * consumption. It does not work well with contents using CSS fixed
  45. * positioning (see also resizesToContents property).
  46. * tiledBackingStoreFrozen property allows application to temporarily freeze
  47. * the contents of the backing store. This is disabled by default.
  48. */
  49. QWebSettings::globalSettings()->setAttribute(QWebSettings::TiledBackingStoreEnabled, true);
  50. // Hide the cursor (it still exists).
  51. //QApplication::setOverrideCursor(Qt::BlankCursor);
  52. // Read first application argument as url.
  53. QString url;
  54. if (1 < app.arguments().count()) {url = app.arguments().at(1);}
  55. else {url = "http://localhost";}
  56. // Create the web content object.
  57. QGraphicsScene scene;
  58. QGraphicsView view(&scene);
  59. QGraphicsWebView webView;
  60. // Set accelerated composting for gpu acceleration. (QtWebKitWebGL - default true)
  61. webView.page()->settings()->setAttribute(QWebSettings::AcceleratedCompositingEnabled, true);
  62. #ifndef ENABLE_CORS
  63. webView.settings()->setAttribute(QWebSettings::WebSecurityEnabled, false);
  64. #endif
  65. #ifdef DEV_REMOTE_INSPECTOR
  66. // Show the developer interface.
  67. webView.page()->setProperty("_q_webInspectorServerPort",9999);
  68. #endif
  69. // Alternative for remote inspector is to pass a second parameter. Anything is ok.
  70. if (2 < app.arguments().count()) {
  71. webView.page()->setProperty("_q_webInspectorServerPort",9999);
  72. }
  73. // Add the webView to scene.
  74. scene.addItem(&webView);
  75. // Configure view properties.
  76. #ifdef QT_OPEN_GLWIDGETS
  77. view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  78. view.setViewport(new QOpenGLWidget());
  79. #endif
  80. view.setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
  81. view.setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
  82. view.setWindowFlags(Qt::FramelessWindowHint);
  83. view.showFullScreen();
  84. #ifdef QT_BLOG_RECOMMENDS
  85. // Reccommended - http://blog.qt.io/blog/2010/05/17/qtwebkit-now-accelerates-css-animations-3d-transforms
  86. view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
  87. view.setViewport(new QGLWidget);
  88. #endif
  89. // Load url.
  90. webView.load(QUrl(url));
  91. view.setStyleSheet( "QGraphicsView { border-style: none; }" );
  92. view.showFullScreen();
  93. // Disable scrollbars of view.
  94. view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  95. view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  96. // Bugfix for QGraphicsWebView not same as QWebView.
  97. webView.resize(app.desktop()->screenGeometry().size());
  98. // Disable scrollbars of webView.
  99. QWebFrame* frame = webView.page()->mainFrame();
  100. frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
  101. frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
  102. return app.exec();
  103. }