summaryrefslogtreecommitdiffstats
path: root/gweather/gweather.py
blob: fc0253fdf43970f00f9500fe5b986fc9ead79b04 (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
#############################################################################
##
## 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.
##
#############################################################################

import os, sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.uic import loadUi

if QT_VERSION < 0x0040500:
    sys.stderr.write("You need Qt 4.5 or newer to run this example.\n")
    sys.exit(1)

__this_dir__ = os.path.split(__file__)[0]

class GWeatherWidget(QWidget):

    def __init__(self):
    
        QWidget.__init__(self)
        
        self.form = loadUi(os.path.join(__this_dir__, "form.ui"), self)
        self.form.progressBar.hide()
        self.form.weatherLabel.hide()
        
        self.form.locationEdit.setFocus()
        self.form.locationEdit.returnPressed.connect(self.start)
        
        self.webPage = QWebPage()
        self.webPage.setViewportSize(QSize(1024, 768))
        self.webPage.loadStarted.connect(self.form.progressBar.show)
        self.webPage.loadProgress.connect(self.form.progressBar.setValue)
        self.webPage.loadFinished.connect(self.finish)
        
        self.setWindowTitle("Weather Information - provided by Google")
    
    def start(self):
        self.form.locationLabel.hide()
        self.form.locationEdit.hide()

        url = QUrl("http://www.google.com/search")
        query = "Weather in " + self.form.locationEdit.text()
        url.addEncodedQueryItem("q", QUrl.toPercentEncoding(query))
        self.webPage.mainFrame().setUrl(url)
    
    def evalJavaScript(self, x):
        return self.webPage.mainFrame().evaluateJavaScript((x)).toInt()[0]
    
    def finish(self, ok):
        self.form.progressBar.hide()
        if not ok:
            self.form.weatherLabel.setText("Can't connect to Google")
            self.form.weatherLabel.show()
        else:
            frame = self.webPage.mainFrame()
            
            # insert jQuery framework
            file = open(os.path.join(__this_dir__, "jquery.min.js"))
            self.evalJavaScript(file.read())
            file.close()
            
            # this hides the "Add to iGoogle" link
            self.evalJavaScript("$('div#res.med > div.e > table.ts.std > tbody > tr > td > "
                 "div > a').css('visibility','hidden');")
            
            # this grabs the element for the weather info
            self.evalJavaScript("var e = $('div#res.med > div.e > table.ts.std');")
            
            # locate the element
            x = self.evalJavaScript("e.position().left;")
            y = self.evalJavaScript("e.position().top;")
            w = self.evalJavaScript("e.outerWidth(true);")
            h = self.evalJavaScript("e.outerHeight(true);")
            
            if not w or not h or y <= 0 or y > 250:
                str = QString("No weather information found for %1").arg(self.form.locationEdit.text())
                self.form.weatherLabel.setText(str)
                self.form.weatherLabel.show()
            else:
                # render to a pixmap and then show it
                pixmap = QPixmap(w, h)
                pixmap.fill(Qt.transparent)
                p = QPainter()
                p.begin(pixmap)
                p.translate(-x, -y)
                frame.render(p)
                p.end()
                
                self.setStyleSheet("background: white;")
                self.form.weatherLabel.setPixmap(pixmap)
                self.form.weatherLabel.show()
                self.setWindowTitle(frame.title())


if __name__ == "__main__":

    app = QApplication(sys.argv)
    weather = GWeatherWidget()
    weather.show()
    sys.exit(app.exec_())