File: Widgets.py

package info (click to toggle)
pyjamas 0.7~%2Bpre2-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,656 kB
  • ctags: 12,331
  • sloc: python: 74,493; php: 805; sh: 291; makefile: 59; xml: 9
file content (134 lines) | stat: -rw-r--r-- 3,679 bytes parent folder | download
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
import pyjd # dummy in pyjs
from pyjamas.ui.Button import Button
from pyjamas.ui.RootPanel import RootPanel
from pyjamas import Window
from pyjamas.ui.Image import Image
from pyjamas.ui.HTML import HTML
from pyjamas.Canvas2D import Canvas, CanvasImage, ImageLoadListener
from pyjamas.Timer import Timer
from pyjamas import DOM
import time

class Widgets:
    def onModuleLoad(self):

        img_url = Window.getLocation().getSearchVar("img")
        if not img_url:
            img_url = 'images/chrome_clock.png'
        self.solar = SolarCanvas(img_url)
        
        RootPanel().add(self.solar)
        self.onShow()

    def onShow(self):
        self.solar.isActive = True
        self.solar.onTimer()
    
    def onHide(self):
        self.solar.isActive = False


class SolarCanvas(Canvas):

    def __init__(self, img_url):
        Canvas.__init__(self, 300, 300)
        self.clock = CanvasImage(img_url)
        self.width = 150
        self.height = 150
        
        self.loader = ImageLoadListener(self)
        self.loader.add(self.clock)
        
        self.isActive = True
        self.onTimer()

    def onLoad(self, sender=None):
        el = self.clock.getElement()
        self.width = DOM.getIntAttribute(el, "width")
        self.height = DOM.getIntAttribute(el, "height")
        self.setWidth("%dpx" % self.width)
        self.setHeight("%dpx" % self.height)
 
    def onError(self, sender):
        Window.alert("error of some kind (probably missing image at url)")

    def onTimer(self, sender=None):
        if not self.isActive:
            return
        
        Timer(1000, self)
        self.draw()

    def getTimeSeconds(self):
        return time.time() % 60.0

    def getTimeMilliseconds(self):
        return (time.time() * 1000.0) % 1.0

    def getTimeMinutes(self):
        return (time.time() / 60) % 60.0

    def getTimeHours(self):
        return (time.time() / 3600) % 12.0

    def draw(self):
        pi = 3.14159265358979323
        if not self.loader.isLoaded():
            return
        
        self.context.globalCompositeOperation = 'destination-over'

        # clear canvas
        self.context.clearRect(0,0,self.width,self.height) 
        
        self.context.save()
        self.context.fillStyle = 'rgba(0,0,0,0.4)'
        self.context.strokeStyle = 'rgba(0,153,255,0.4)'
        self.context.translate(self.width/2,self.height/2)
        
        secs = self.getTimeSeconds()
        mins = self.getTimeMinutes() + secs / 60.0
        hours = self.getTimeHours() + mins / 60.0

        # Seconds
        self.context.save()
        self.context.fillStyle = 'rgba(255,0,0,0.4)'
        self.context.rotate( ((2*pi)/60)*secs + pi)
        self.context.fillRect(-1,-(self.width * 0.04),2, self.width * 0.38) 
        self.context.restore()
        
        # Minutes
        self.context.save()
        self.context.rotate( ((2*pi)/60)*mins + pi)
        self.context.fillRect(-1,-1,3,self.width * 0.35) 
        self.context.restore()
        
        # Hours
        self.context.save()
        self.context.rotate( ((2*pi)/12)*hours + pi)
        self.context.fillRect(-2,-2,4,self.width * 0.2) 
        self.context.restore()
        
        self.context.restore()
        
        self.context.drawImage(self.clock.getElement(),0,0)


def AppInit():

    img_url = Window.getLocation().getSearchVar("img")
    if not img_url:
        img_url = 'images/chrome_clock.png'
    solar = SolarCanvas(img_url)
    
    solar.isActive = True
    solar.onTimer()

    return solar


if __name__ == '__main__':
    pyjd.setup("./public/Widgets.html")
    app = Widgets()
    app.onModuleLoad()
    pyjd.run()