1 | /* compile with
|
---|
2 | moc webkit-image.cpp >webkit-image.h
|
---|
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
|
---|
8 | */
|
---|
9 | #include <QtGui/QApplication>
|
---|
10 | #include <QtCore/QFile>
|
---|
11 | #include <QtCore/QString>
|
---|
12 | #include <QtWebKit/QWebView>
|
---|
13 |
|
---|
14 | /* using mingw to set binary mode */
|
---|
15 | #ifdef WIN32
|
---|
16 | #include <io.h>
|
---|
17 | #include <fcntl.h>
|
---|
18 | #define BINARYSTDOUT setmode(fileno(stdout), O_BINARY);
|
---|
19 | #else
|
---|
20 | #define BINARYSTDOUT
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | #define WIDTH 2000
|
---|
24 |
|
---|
25 | class Save : public QObject
|
---|
26 | {
|
---|
27 | Q_OBJECT
|
---|
28 | public:
|
---|
29 | Save(QWebView *v) : view(v) {};
|
---|
30 |
|
---|
31 | public slots:
|
---|
32 | void setGeometry(const QRect &r)
|
---|
33 | {
|
---|
34 | view->setGeometry(r);
|
---|
35 | }
|
---|
36 | void loaded(bool ok)
|
---|
37 | {
|
---|
38 | if(ok)
|
---|
39 | {
|
---|
40 | QImage im = QPixmap::grabWidget(view).toImage();
|
---|
41 |
|
---|
42 | QFile f;
|
---|
43 | BINARYSTDOUT
|
---|
44 | if(f.open(stdout, QIODevice::WriteOnly|QIODevice::Unbuffered))
|
---|
45 | {
|
---|
46 | if(!im.save(&f, "JPEG"))
|
---|
47 | {
|
---|
48 | im.save(&f, "PNG");
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 | emit finish();
|
---|
53 | }
|
---|
54 | signals:
|
---|
55 | void finish(void);
|
---|
56 |
|
---|
57 | private:
|
---|
58 | QWebView *view;
|
---|
59 | };
|
---|
60 |
|
---|
61 | #include "webkit-image.h"
|
---|
62 |
|
---|
63 | int main(int argc, char **argv)
|
---|
64 | {
|
---|
65 | if(argc != 2)
|
---|
66 | return 20;
|
---|
67 | QString url = QString(argv[1]);
|
---|
68 |
|
---|
69 | QApplication a( argc, argv );
|
---|
70 | QWebView *view = new QWebView();
|
---|
71 | Save *s = new Save(view);
|
---|
72 | view->resize(WIDTH,WIDTH);
|
---|
73 |
|
---|
74 | QObject::connect(view, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
|
---|
75 | QObject::connect(s, SIGNAL(finish(void)), &a, SLOT(quit()));
|
---|
76 | QObject::connect(view->page(), SIGNAL(geometryChangeRequested(const QRect &)), s, SLOT(setGeometry(const QRect &)));
|
---|
77 | view->load(QUrl(url));
|
---|
78 | return a.exec();
|
---|
79 | }
|
---|