Merge branch 'master' into master_internal
[enigma2-plugins.git] / audiosync / src / MovableScreen.py
1 from HelpableNumberActionMap import HelpableNumberActionMap
2 from Components.config import config
3 from __init__ import _
4 from enigma import ePoint, eTimer, getDesktop
5
6 class MovableScreen():
7     def __init__(self, configRoot, disableKeymaps, screenSize_x, screenSize_y, moveMinMargin=30, moveStepSize=10):
8         self.configRoot = configRoot
9         self.disableKeymaps = disableKeymaps
10         self.screenSize_x = screenSize_x
11         self.screenSize_y = screenSize_y
12         self.moveMinMargin = moveMinMargin
13         self.moveStepSize = moveStepSize
14
15         self["MovableScreenActions"] = HelpableNumberActionMap(self, "MovableScreenActions",
16         {
17             "ok":       (self.moveKeyOk,                   _("Save values and close screen")),
18             "cancel":   (self.moveKeyCancel,            _("Discard changes and close screen")),
19             "left":     (self.moveKeyLeft,              _("Move screen to the left")),
20             "right":    (self.moveKeyRight,             _("Move screen to the right")),
21             "up":       (self.moveKeyUp,                _("Move screen up")),
22             "down":     (self.moveKeyDown,              _("Move screen down")),
23             "red":      (self.moveKeyCancel,            _("Discard changes and close screen")),
24             "green":    (self.moveKeyOk,                _("Save values and close screen")),
25             "1":        (self.moveKeyNumber,            _("Move screen to the upper left corner")),
26             "2":        (self.moveKeyNumber,            _("Center screen at the upper border")),
27             "3":        (self.moveKeyNumber,            _("Move screen to the upper right corner")),
28             "4":        (self.moveKeyNumber,            _("Move screen to the middle of the left border")),
29             "5":        (self.moveKeyNumber,            _("Move screen to the center of your TV")),
30             "6":        (self.moveKeyNumber,            _("Move screen to the middle of the right border")),
31             "7":        (self.moveKeyNumber,            _("Move screen to the lower left corner")),
32             "8":        (self.moveKeyNumber,            _("Center screen at the lower border")),
33             "9":        (self.moveKeyNumber,            _("Move screen to the lower right corner")),
34             "0":        (self.moveKeyNumber,            _("Reset saved position"))
35         }, -1)
36              
37         self["MovableScreenActions"].setEnabled(False)
38       
39         desktop = getDesktop(0)
40         self.desktopWidth = desktop.size().width()
41         self.desktopHeight = desktop.size().height()
42         
43
44     def startMoving(self):
45         self.setEnableMoveKeymap(True)
46
47         self.moveTimer = eTimer()
48         self.moveTimer_conn = self.moveTimer.timeout.connect(self.movePositionTimer)
49         self.moveTimer.start(50, 1)
50
51     def movePositionTimer(self):
52         if self.configRoot.position_x.value == 0 and self.configRoot.position_y.value == 0:
53             self.configRoot.position_x.value = (self.desktopWidth - self.screenSize_x)/2
54             self.configRoot.position_y.value = self.moveMinMargin
55         self.instance.move(ePoint(self.configRoot.position_x.value, self.configRoot.position_y.value))
56             
57         self.moveTimer.start(50, 1)
58
59     def movePosition(self):
60         if self.configRoot.position_x.value != 0 or self.configRoot.position_y.value != 0:
61             self.instance.move(ePoint(self.configRoot.position_x.value, self.configRoot.position_y.value))
62             
63     def moveKeyLeft(self):
64         value = self.configRoot.position_x.value
65         value -= self.moveStepSize
66         if value < 0:
67             value = 0
68         self.configRoot.position_x.value = value
69
70     def moveKeyUp(self):
71         value = self.configRoot.position_y.value
72         value -= self.moveStepSize
73         if value < 0:
74             value = 0
75         self.configRoot.position_y.value = value
76
77     def moveKeyRight(self):
78         value = self.configRoot.position_x.value
79         value += self.moveStepSize
80         if value > self.desktopWidth:
81             value = self.desktopWidth
82         self.configRoot.position_x.value = value
83
84     def moveKeyDown(self):
85         value = self.configRoot.position_y.value
86         value += self.moveStepSize
87         if value > self.desktopHeight:
88             value = self.desktopHeight
89         self.configRoot.position_y.value = value
90
91     def moveKeyNumber(self, number):
92         #x- Positioning
93         if number in (1,4,7):
94             iPosX = self.moveMinMargin
95         elif number in (2,5,8,0):
96             iPosX = (self.desktopWidth - self.screenSize_x)/2
97         else:
98             iPosX = self.desktopWidth - self.moveMinMargin - self.screenSize_x
99         
100         self.configRoot.position_x.value = iPosX
101
102         #y- positioning
103         if number in (0,1,2,3):
104             iPosY = self.moveMinMargin
105         elif number in (4,5,6):
106             iPosY = (self.desktopHeight - self.screenSize_y)/2
107         elif number in (7,8,9):    
108             iPosY = self.desktopHeight - self.moveMinMargin - self.screenSize_y
109             
110         self.configRoot.position_y.value = iPosY
111             
112     def moveKeyOk(self):
113         self.configRoot.position_x.save()
114         self.configRoot.position_y.save()
115         self.setEnableMoveKeymap(False)
116
117     def moveKeyCancel(self):
118         self.configRoot.position_x.cancel()
119         self.configRoot.position_y.cancel()
120         self.setEnableMoveKeymap(False)
121
122     def setEnableMoveKeymap(self,enabled):
123         self["MovableScreenActions"].setEnabled(enabled)
124         for keymap in self.disableKeymaps:
125             keymap.setEnabled(not(enabled))