source: osm/applications/editors/josm/webgrabber/webkit-image.cpp@ 30837

Last change on this file since 30837 was 25399, checked in by stoecker, 14 years ago

fix size issue

File size: 4.2 KB
RevLine 
[11173]1#include <QtGui/QApplication>
[12421]2#include <QtGui/QPainter>
[11173]3#include <QtCore/QFile>
4#include <QtCore/QString>
[19704]5#include <QtCore/QUrl>
[12421]6#include <QtWebKit/QWebPage>
7#include <QtWebKit/QWebFrame>
[13262]8#include <QtNetwork/QNetworkProxy>
9#include <QtCore/QProcess>
[19704]10#if QT_VERSION >= 0x040500
[20045]11#include <QtGui/QDesktopServices>
12#include <QtNetwork/QNetworkDiskCache>
[19704]13#endif
[11173]14
[11394]15/* using mingw to set binary mode */
16#ifdef WIN32
17#include <io.h>
18#include <fcntl.h>
19#define BINARYSTDOUT setmode(fileno(stdout), O_BINARY);
20#else
21#define BINARYSTDOUT
22#endif
23
[11173]24class Save : public QObject
25{
26Q_OBJECT
27public:
[12421]28 Save(QWebPage *p) : page(p) {};
[11173]29
30public slots:
31 void loaded(bool ok)
32 {
33 if(ok)
34 {
[12421]35 page->setViewportSize(page->mainFrame()->contentsSize());
36 QImage im(page->viewportSize(), QImage::Format_ARGB32);
37 QPainter painter(&im);
38 page->mainFrame()->render(&painter);
[11173]39
40 QFile f;
[11394]41 BINARYSTDOUT
42 if(f.open(stdout, QIODevice::WriteOnly|QIODevice::Unbuffered))
[12009]43 {
[12093]44 if(!im.save(&f, "JPEG"))
[12009]45 {
[12093]46 im.save(&f, "PNG");
[12009]47 }
48 }
[11173]49 }
50 emit finish();
51 }
52signals:
53 void finish(void);
54
55private:
[12421]56 QWebPage * page;
[11173]57};
58
59#include "webkit-image.h"
60
61int main(int argc, char **argv)
62{
63 if(argc != 2)
64 return 20;
65 QString url = QString(argv[1]);
66
[12421]67 QApplication a(argc, argv);
68 QWebPage * page = new QWebPage();
69 Save * s = new Save(page);
[11173]70
[13262]71 QStringList environment = QProcess::systemEnvironment();
72 int idx = environment.indexOf(QRegExp("http_proxy=.*"));
73 if(idx != -1)
74 {
[19704]75 QString scheme = "http"; // default
76 QString proxyHost;
[13262]77
[19704]78 int proxyPort = 8080;
79 QStringList tmpList = environment.at(idx).split("=");
80
81#if QT_VERSION >= 0x040600 // Qt4.6: Use QUrl::fromUserInput
82 // set URL (and guess if proto scheme missing)
83 QUrl url (QUrl::fromUserInput(tmpList.at(1)));
84#else
85 // set URL
86 QUrl url (tmpList.at(1));
87#endif
88 if (url.isValid() && !url.host().isEmpty())
89 {
90 proxyHost = url.host();
91
92 if (url.port() != -1)
93 proxyPort = url.port();
94
95 if (!url.scheme().isEmpty())
96 scheme = url.scheme();
97
98 if (scheme == "http") // we support only http
99 {
100 QNetworkProxy proxy;
101 proxy.setType(QNetworkProxy::HttpCachingProxy);
102 proxy.setHostName(proxyHost);
103 proxy.setPort(proxyPort);
104 if (!url.userName().isEmpty())
105 proxy.setUser(url.userName());
106 if (!url.password().isEmpty())
107 proxy.setPassword(url.password());
108 page->networkAccessManager()->setProxy(proxy);
109 }
110 }
111 else /* manual mode */
112 {
113 QStringList proto_host_port = tmpList.at(1).split("://");
114 QStringList host_port;
115 if (proto_host_port.size() == 2) // string has proto
116 {
117 scheme = proto_host_port.at(0);
118 host_port = proto_host_port.at(1).split(":");
119 }
120 else // no proto (or invalid format with several delimiters)
121 {
122 host_port = tmpList.at(1).split(":");
123 }
124 if (scheme == "http") // we support only http
125 {
126 proxyHost = host_port.at(0);
127 if(host_port.size() == 2)
128 {
129 bool ok;
130 int port = host_port.at(1).toInt(&ok);
131 if(ok)
132 proxyPort = port;
133 }
134
135 QNetworkProxy proxy;
136 proxy.setType(QNetworkProxy::HttpCachingProxy);
137 proxy.setHostName(proxyHost);
138 proxy.setPort(proxyPort);
139 page->networkAccessManager()->setProxy(proxy);
140 }
141 }
[13262]142 }
143
[19704]144#if QT_VERSION >= 0x040500
145 QNetworkDiskCache *diskCache = new QNetworkDiskCache(page);
146 QString location = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
147 diskCache->setCacheDirectory(location);
148 page->networkAccessManager()->setCache(diskCache);
149#endif
150
[12421]151 QObject::connect(page, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
[11173]152 QObject::connect(s, SIGNAL(finish(void)), &a, SLOT(quit()));
[12588]153 /* set some useful defaults for a webpage */
[25399]154 // page->setViewportSize(QSize(1280,1024));
155 // don't set size, or it breaks josm */
[24625]156 page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
157 page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
[12421]158 page->mainFrame()->load (QUrl(url));
[11173]159 return a.exec();
[11394]160}
Note: See TracBrowser for help on using the repository browser.