[11173] | 1 | /* compile with
|
---|
| 2 | moc webkit-image.cpp >webkit-image.h
|
---|
[12009] | 3 | g++ webkit-image.cpp -o webkit-image -lQtCore -lQtWebKit -lQtGui -s -O2
|
---|
| 4 | or under Windows:
|
---|
| 5 | g++ webkit-image.cpp -o webkit-image -lQtCore4 -lQtWebKit4 -lQtGui4 -s O2
|
---|
| 6 | adding the correct directories with -L or -I:
|
---|
| 7 | -I C:\Progra~1\Qt\include -L C:\Progra~1\Qt\lib
|
---|
[11173] | 8 | */
|
---|
| 9 | #include <QtGui/QApplication>
|
---|
[12421] | 10 | #include <QtGui/QPainter>
|
---|
[11173] | 11 | #include <QtCore/QFile>
|
---|
| 12 | #include <QtCore/QString>
|
---|
[12421] | 13 | #include <QtWebKit/QWebPage>
|
---|
| 14 | #include <QtWebKit/QWebFrame>
|
---|
[13262] | 15 | #include <QtNetwork/QNetworkProxy>
|
---|
| 16 | #include <QtCore/QProcess>
|
---|
[11173] | 17 |
|
---|
[11394] | 18 | /* using mingw to set binary mode */
|
---|
| 19 | #ifdef WIN32
|
---|
| 20 | #include <io.h>
|
---|
| 21 | #include <fcntl.h>
|
---|
| 22 | #define BINARYSTDOUT setmode(fileno(stdout), O_BINARY);
|
---|
| 23 | #else
|
---|
| 24 | #define BINARYSTDOUT
|
---|
| 25 | #endif
|
---|
| 26 |
|
---|
[11173] | 27 | class Save : public QObject
|
---|
| 28 | {
|
---|
| 29 | Q_OBJECT
|
---|
| 30 | public:
|
---|
[12421] | 31 | Save(QWebPage *p) : page(p) {};
|
---|
[11173] | 32 |
|
---|
| 33 | public slots:
|
---|
| 34 | void loaded(bool ok)
|
---|
| 35 | {
|
---|
| 36 | if(ok)
|
---|
| 37 | {
|
---|
[12421] | 38 | page->setViewportSize(page->mainFrame()->contentsSize());
|
---|
| 39 | QImage im(page->viewportSize(), QImage::Format_ARGB32);
|
---|
| 40 | QPainter painter(&im);
|
---|
| 41 | page->mainFrame()->render(&painter);
|
---|
[11173] | 42 |
|
---|
| 43 | QFile f;
|
---|
[11394] | 44 | BINARYSTDOUT
|
---|
| 45 | if(f.open(stdout, QIODevice::WriteOnly|QIODevice::Unbuffered))
|
---|
[12009] | 46 | {
|
---|
[12093] | 47 | if(!im.save(&f, "JPEG"))
|
---|
[12009] | 48 | {
|
---|
[12093] | 49 | im.save(&f, "PNG");
|
---|
[12009] | 50 | }
|
---|
| 51 | }
|
---|
[11173] | 52 | }
|
---|
| 53 | emit finish();
|
---|
| 54 | }
|
---|
| 55 | signals:
|
---|
| 56 | void finish(void);
|
---|
| 57 |
|
---|
| 58 | private:
|
---|
[12421] | 59 | QWebPage * page;
|
---|
[11173] | 60 | };
|
---|
| 61 |
|
---|
| 62 | #include "webkit-image.h"
|
---|
| 63 |
|
---|
| 64 | int main(int argc, char **argv)
|
---|
| 65 | {
|
---|
| 66 | if(argc != 2)
|
---|
| 67 | return 20;
|
---|
| 68 | QString url = QString(argv[1]);
|
---|
| 69 |
|
---|
[12421] | 70 | QApplication a(argc, argv);
|
---|
| 71 | QWebPage * page = new QWebPage();
|
---|
| 72 | Save * s = new Save(page);
|
---|
[11173] | 73 |
|
---|
[13262] | 74 | QStringList environment = QProcess::systemEnvironment();
|
---|
| 75 | int idx = environment.indexOf(QRegExp("http_proxy=.*"));
|
---|
| 76 | if(idx != -1)
|
---|
| 77 | {
|
---|
| 78 | QString proxyHost;
|
---|
| 79 | int proxyPort = 8080;
|
---|
| 80 | QStringList tmpList = environment.at(idx).split("=");
|
---|
| 81 | QStringList host_port = tmpList.at(1).split(":");
|
---|
| 82 | proxyHost = host_port.at(0);
|
---|
| 83 | if(host_port.size() == 2)
|
---|
| 84 | {
|
---|
| 85 | bool ok;
|
---|
| 86 | int port = host_port.at(1).toInt(&ok);
|
---|
| 87 | if(ok)
|
---|
| 88 | proxyPort = port;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | QNetworkProxy proxy;
|
---|
| 92 | proxy.setType(QNetworkProxy::HttpCachingProxy);
|
---|
| 93 | proxy.setHostName(proxyHost);
|
---|
| 94 | proxy.setPort(proxyPort);
|
---|
| 95 | page->networkAccessManager()->setProxy(proxy);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[12421] | 98 | QObject::connect(page, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
|
---|
[11173] | 99 | QObject::connect(s, SIGNAL(finish(void)), &a, SLOT(quit()));
|
---|
[12588] | 100 | /* set some useful defaults for a webpage */
|
---|
| 101 | // page->setViewportSize(QSize(1280,1024));
|
---|
| 102 | // page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
|
---|
| 103 | // page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
|
---|
[12421] | 104 | page->mainFrame()->load (QUrl(url));
|
---|
[11173] | 105 | return a.exec();
|
---|
[11394] | 106 | }
|
---|