summaryrefslogtreecommitdiffstats
path: root/gweather/gweather.cpp
blob: dbb6accd9bfb0caa6bd90007496a33dbb7e84937 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Graphics Dojo project on Qt Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include <QtCore>
#include <QtGui>
#include <QtWebKit>

#include "ui_form.h"

#if QT_VERSION < 0x0040500
#error You need Qt 4.5 or newer
#endif


#define EVAL(x) webPage->mainFrame()->evaluateJavaScript((x)).toInt()

class GWeatherWidget: public QWidget
{
    Q_OBJECT

private:
    Ui::Form form;
    QWebPage *webPage;

public:
    GWeatherWidget(): QWidget() {
        form.setupUi(this);
        form.progressBar->hide();
        form.weatherLabel->hide();

        form.locationEdit->setFocus();
        connect(form.locationEdit, SIGNAL(returnPressed()), SLOT(start()));

        webPage = new QWebPage(this);
        webPage->setViewportSize(QSize(1024, 768));
        connect(webPage, SIGNAL(loadStarted()), form.progressBar, SLOT(show()));
        connect(webPage, SIGNAL(loadProgress(int)), form.progressBar, SLOT(setValue(int)));
        connect(webPage, SIGNAL(loadFinished(bool)), SLOT(finish(bool)));

        setWindowTitle("Weather Information - provided by Google");
    }

private slots:

    void start() {
        form.locationLabel->hide();
        form.locationEdit->hide();

        QUrl url("http://www.google.com/search");
        QString query = "Weather in " + form.locationEdit->text();
        url.addEncodedQueryItem("q", QUrl::toPercentEncoding(query));
        webPage->mainFrame()->setUrl(url);
    }


    void finish(bool ok) {
        form.progressBar->hide();
        if (!ok) {
            form.weatherLabel->setText("Can't connect to Google");
            form.weatherLabel->show();
        } else {
            QWebFrame *frame = webPage->mainFrame();

            // insert jQuery framework
            QFile file(":/jquery.min.js");
            file.open(QIODevice::ReadOnly);
            EVAL(file.readAll());
            file.close();

            // this hides the "Add to iGoogle" link
            EVAL("$('div#res.med > div.e > table.ts.std > tbody > tr > td > "
                 "div > a').css('visibility','hidden');");

            // this grabs the element for the weather info
            EVAL("var e = $('div#res.med > div.e > table.ts.std');");

            // locate the element
            int x = EVAL("e.position().left;");
            int y = EVAL("e.position().top;");
            int w = EVAL("e.outerWidth(true);");
            int h = EVAL("e.outerHeight(true);");

            if (!w || !h || y <= 0 || y > 250) {
                QString str = QString("No weather information found for %1").arg(form.locationEdit->text());
                form.weatherLabel->setText(str);
                form.weatherLabel->show();
            } else {
                // render to a pixmap and then show it
                QPixmap pixmap(w, h);
                pixmap.fill(Qt::transparent);
                QPainter p;
                p.begin(&pixmap);
                p.translate(-x, -y);
                frame->render(&p);
                p.end();

                setStyleSheet("background: white;");
                form.weatherLabel->setPixmap(pixmap);
                form.weatherLabel->show();
                setWindowTitle(frame->title());
            }
        }
    }

};

#include "gweather.moc"

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    GWeatherWidget weather;
    weather.show();
    return app.exec();
}