timer fixes and improvements by adenin
[enigma2.git] / lib / python / Screens / PiPSetup.py
1 from Screens.Screen import Screen
2 from Components.ActionMap import NumberActionMap
3 from Components.Label import Label
4
5 # this is not so great.
6 MAX_X = 720
7 MAX_Y = 576
8 MAX_W = MAX_X * 3 / 4
9 MAX_H = MAX_Y * 3 / 4
10 MIN_W = MAX_X / 8
11 MIN_H = MAX_Y / 8
12
13 def clip(val, min, max):
14         if min <= val <= max:
15                 return val
16         if min <= val:
17                 return max
18         return min
19
20 class PiPSetup(Screen):
21         def __init__(self, session, pip):
22                 Screen.__init__(self, session)
23                 
24                 self.pip = pip
25                 
26                 self.pos = (self.pip.instance.position().x(), self.pip.instance.position().y())
27                 self.size = self.pip.getSize()
28
29                 self.orgpos = self.pos
30                 self.orgsize = self.size
31                 
32                 self.resize = 100
33
34                 self["text"] = Label(_("Please use direction keys to move the PiP window.\nPress Bouquet +/- to resize the window.\nPress OK to go back to the TV mode or EXIT to cancel the moving."))
35
36                 self["actions"] = NumberActionMap(["PiPSetupActions", "NumberActions"], 
37                 {
38                         "ok": self.go,
39                         "cancel": self.cancel,
40                         "up": self.up,
41                         "down": self.down,
42                         "left": self.left,
43                         "right": self.right,
44                         "size+": self.bigger,
45                         "size-": self.smaller,
46                         "1": self.keyNumberGlobal,
47                         "2": self.keyNumberGlobal,
48                         "3": self.keyNumberGlobal,
49                         "4": self.keyNumberGlobal,
50                         "5": self.keyNumberGlobal,
51                         "6": self.keyNumberGlobal,
52                         "7": self.keyNumberGlobal,
53                         "8": self.keyNumberGlobal,
54                         "9": self.keyNumberGlobal,
55                         "0": self.keyNumberGlobal
56                 }, -1)
57                 
58         def go(self):
59                 self.close()
60         
61         def cancel(self):
62                 self.pos = self.orgpos
63                 self.size = self.orgsize
64                 self.setPiPPosition()
65                 self.close()
66         
67         def setPiPPosition(self):
68                 self.pip.move(self.pos[0], self.pos[1])
69                 self.pip.resize(self.size[0], self.size[1])
70
71         def resizePiP(self, resize):
72                 resize += 100 # resize is in percent, so resize=+20 means: 120%
73                 
74                 oldsize = self.size
75                 w = clip(self.size[0] * resize / 100, MIN_W, MAX_W)
76                 h = clip(self.size[1] * resize / 100, MIN_H, MAX_H)
77                 
78                 # calculate offset from center
79                 mx = (oldsize[0] - w) / 2
80                 my = (oldsize[1] - h) / 2
81                 
82                 self.size = (w, h)
83                 # reclip, account for new center
84                 self.moveRelative(x=mx, y=my)
85         
86         def moveRelative(self, x=0, y=0):
87                 self.pos = (clip(self.pos[0] + x, 0, MAX_X - self.size[0]), clip(self.pos[1] + y, 0, MAX_Y - self.size[1]))
88                 self.setPiPPosition()
89         
90         def up(self):
91                 self.moveRelative(y=-32)
92
93         def down(self):
94                 self.moveRelative(y=+32)
95         
96         def left(self):
97                 self.moveRelative(x=-24)
98         
99         def right(self):
100                 self.moveRelative(x=+24)
101         
102         def bigger(self):
103                 self.resizePiP(+10)
104         
105         def smaller(self):
106                 self.resizePiP(-10)
107         
108         def keyNumberGlobal(self, number):
109                 
110                 if number > 0:
111                         colsize = MAX_X / 3
112                         rowsize = MAX_Y / 3
113                         col = (number-1) % 3
114                         row = (number-1) / 3
115
116                         self.size = (180, 135)
117
118                         # offset to keep center
119                         ox = (colsize - self.size[0]) / 2
120                         oy = (rowsize - self.size[1]) / 2
121
122                         self.pos = (col * colsize + ox, row * rowsize + oy)
123                 else:
124                         # restore old position
125                         self.pos = self.orgpos
126                         self.size = self.orgsize
127                 # reclip
128                 self.moveRelative()