[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>
|
---|
[11173] | 15 |
|
---|
[11394] | 16 | /* using mingw to set binary mode */
|
---|
| 17 | #ifdef WIN32
|
---|
| 18 | #include <io.h>
|
---|
| 19 | #include <fcntl.h>
|
---|
| 20 | #define BINARYSTDOUT setmode(fileno(stdout), O_BINARY);
|
---|
| 21 | #else
|
---|
| 22 | #define BINARYSTDOUT
|
---|
| 23 | #endif
|
---|
| 24 |
|
---|
[11173] | 25 | class Save : public QObject
|
---|
| 26 | {
|
---|
| 27 | Q_OBJECT
|
---|
| 28 | public:
|
---|
[12421] | 29 | Save(QWebPage *p) : page(p) {};
|
---|
[11173] | 30 |
|
---|
| 31 | public slots:
|
---|
| 32 | void loaded(bool ok)
|
---|
| 33 | {
|
---|
| 34 | if(ok)
|
---|
| 35 | {
|
---|
[12421] | 36 | page->setViewportSize(page->mainFrame()->contentsSize());
|
---|
| 37 | QImage im(page->viewportSize(), QImage::Format_ARGB32);
|
---|
| 38 | QPainter painter(&im);
|
---|
| 39 | page->mainFrame()->render(&painter);
|
---|
[11173] | 40 |
|
---|
| 41 | QFile f;
|
---|
[11394] | 42 | BINARYSTDOUT
|
---|
| 43 | if(f.open(stdout, QIODevice::WriteOnly|QIODevice::Unbuffered))
|
---|
[12009] | 44 | {
|
---|
[12093] | 45 | if(!im.save(&f, "JPEG"))
|
---|
[12009] | 46 | {
|
---|
[12093] | 47 | im.save(&f, "PNG");
|
---|
[12009] | 48 | }
|
---|
| 49 | }
|
---|
[11173] | 50 | }
|
---|
| 51 | emit finish();
|
---|
| 52 | }
|
---|
| 53 | signals:
|
---|
| 54 | void finish(void);
|
---|
| 55 |
|
---|
| 56 | private:
|
---|
[12421] | 57 | QWebPage * page;
|
---|
[11173] | 58 | };
|
---|
| 59 |
|
---|
| 60 | #include "webkit-image.h"
|
---|
| 61 |
|
---|
| 62 | int main(int argc, char **argv)
|
---|
| 63 | {
|
---|
| 64 | if(argc != 2)
|
---|
| 65 | return 20;
|
---|
| 66 | QString url = QString(argv[1]);
|
---|
| 67 |
|
---|
[12421] | 68 | QApplication a(argc, argv);
|
---|
| 69 | QWebPage * page = new QWebPage();
|
---|
| 70 | Save * s = new Save(page);
|
---|
[11173] | 71 |
|
---|
[12421] | 72 | QObject::connect(page, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
|
---|
[11173] | 73 | QObject::connect(s, SIGNAL(finish(void)), &a, SLOT(quit()));
|
---|
[12588] | 74 | /* set some useful defaults for a webpage */
|
---|
| 75 | // page->setViewportSize(QSize(1280,1024));
|
---|
| 76 | // page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
|
---|
| 77 | // page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
|
---|
[12421] | 78 | page->mainFrame()->load (QUrl(url));
|
---|
[11173] | 79 | return a.exec();
|
---|
[11394] | 80 | }
|
---|