Browser.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // To build the source, set your drive letter Q: to the Qt SDK root,
  2. // e.g., use "subst q: C:\QtSDK\Desktop\Qt\4.8.0\msvc2010" on the cmd line.
  3. // This minimal source is derived from http://trac.webkit.org/wiki/QtWebKitTiling,
  4. // for an example with more features see http://doc.qt.nokia.com/qt-maemo/maemo5-graphicswebview.html
  5. #include <QApplication.h>
  6. #include <QGraphicsScene>
  7. #include <QGraphicsView>
  8. #include <QGraphicsWebView>
  9. #include <QWebSettings>
  10. #include <QNetworkProxy>
  11. #include <windows.h>
  12. #include <string>
  13. const char * DEFAULT_URL = "http://localhost/";
  14. const char * DEFAULT_SIZE = "550x720";
  15. const char * DEFAULT_PROXY = 0;
  16. static void ShowUsage(const char * exeName) {
  17. const char * name = "";
  18. if (exeName) {
  19. name = strrchr(exeName, '\\');
  20. if (name) {
  21. name++;
  22. } else {
  23. name = exeName;
  24. }
  25. }
  26. std::string msg = "Usage:\n";
  27. msg += " " + std::string(name) + " [-w###x###] [url]\n";
  28. msg += " " + std::string(name) + " -h\n\n";
  29. msg += "Use -w###x### to specify the window size\n";
  30. MessageBoxA(NULL, msg.c_str(), name, MB_ICONINFORMATION);
  31. }
  32. class BGraphicsWebView : public QGraphicsWebView {
  33. public:
  34. bool loadFinished(bool);
  35. };
  36. bool BGraphicsWebView::loadFinished(bool) {
  37. return 0;
  38. }
  39. int main(int argc, char *argv[]) {
  40. const char * url = DEFAULT_URL;
  41. const char * size = DEFAULT_SIZE;
  42. const char * proxy = DEFAULT_PROXY;
  43. for (int i = 1; i<argc; i++) {
  44. if (argv[i][0]=='/' || argv[i][0]=='-') {
  45. if (argv[i][1]=='h' || argv[i][1]=='H' || argv[i][1]=='?') {
  46. ShowUsage(argv[0]);
  47. return 0;
  48. }
  49. else if ((argv[i][1]=='w') || (argv[i][1]=='W')) {
  50. if ((argv[i][2]!=0) && (strchr(argv[1],'x')!=0) && (size==DEFAULT_SIZE)) {
  51. size = argv[i]+2;
  52. }
  53. }
  54. else if ((argv[i][1]=='p') || (argv[i][1]=='P')) {
  55. proxy = argv[i]+2;
  56. }
  57. else {
  58. ShowUsage(argv[0]);
  59. return 1;
  60. }
  61. } else {
  62. if (url==DEFAULT_URL) {
  63. url = argv[i];
  64. } else {
  65. ShowUsage(argv[0]);
  66. return 1;
  67. }
  68. }
  69. }
  70. QApplication app(argc, argv);
  71. if (proxy) {
  72. QUrl proxyUrl = proxy;
  73. QNetworkProxy netProxy(QNetworkProxy::HttpProxy, proxyUrl.host(), proxyUrl.port());
  74. QNetworkProxy::setApplicationProxy(netProxy);
  75. }
  76. QGraphicsScene scene;
  77. QGraphicsView view(&scene);
  78. view.setFrameShape(QFrame::NoFrame);
  79. view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  80. view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  81. BGraphicsWebView webview;
  82. int width=0, height=0;
  83. if (2!=sscanf(size,"%ux%u", &width, &height)) {
  84. ShowUsage(argv[0]);
  85. return 1;
  86. }
  87. webview.resize(width, height);
  88. webview.load(QUrl(url));
  89. scene.addItem(&webview);
  90. view.setFixedSize(width, height);
  91. view.show();
  92. int ret = app.exec();
  93. return ret;
  94. }