Fixed resizing issue.
[drlaunch] / src / widget.py
1 #!/usr/bin/env python
2 # coding=UTF-8
3
4 # Copyright (C) 2010 Stefanos Harhalakis
5 #
6 # This file is part of wifieye.
7 #
8 # wifieye is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # wifieye is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with wifieye.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 # $Id: 0.py 2265 2010-02-21 19:16:26Z v13 $
22
23 __version__ = "$Id: 0.py 2265 2010-02-21 19:16:26Z v13 $"
24
25 # HACK
26 # Add the current module's directory to sys.path to bypass
27 # problems when running as widget.
28 # Restore the path at the end of the imports
29 import sys
30 import os
31
32 orig_path=sys.path[:]
33 tmp_path=os.path.dirname( os.path.realpath( __file__ ) )
34 sys.path.append(tmp_path)
35
36 # End of hack
37
38 import gtk
39 import gobject
40 import hildon
41 from hildondesktop import *
42 from gtk import gdk
43 from math import pi
44 import cairo
45 import time
46
47 from subprocess import Popen,PIPE
48
49 from portrait import FremantleRotation
50 import launcher
51 from xdg.IconTheme import getIconPath
52 from win_config import WinConfig
53
54 import config
55 import apps
56 from icon import Icon
57 from icongrid import IconGrid
58
59 # Restore path
60 sys.path=orig_path
61
62 # IconGrid must be before HomePluginItem for its connect()
63 # and do_button_*() to override those of HomePluginItem
64 class DrlaunchPlugin(IconGrid, HomePluginItem, FremantleRotation):
65     def __init__(self):
66         IconGrid.__init__(self)
67         HomePluginItem.__init__(self)
68         FremantleRotation.__init__(self, 'DrlaunchPlugin')
69
70         self.winConfig=None
71
72         self.set_settings(True)
73
74         self.id=None
75         self.config=None
76
77     def get_id0(self):
78         """If this is called from the constructor then the program
79         core dumps """
80         aid=self.get_applet_id()
81
82         # Get desktop activity if D.A.M. is present
83         
84         act="/usr/bin/activity"
85
86         if os.path.exists(act):
87             r=Popen([act, "current"], stdout=PIPE).communicate()
88             activity=r[0].strip()
89         else:
90             activity=""
91
92         ret="%s-%s" % (aid, activity)
93
94         return(ret)
95
96     def get_id(self):
97         if self.id==None:
98             self.id=self.get_id0()
99
100         return(self.id)
101
102     def get_config(self):
103         if self.config==None:
104             id=self.get_id()
105             self.config=config.Config(id)
106
107         return(self.config)
108
109     def do_realize(self):
110         launcher.init()
111         config=self.get_config()
112         config.load()
113
114         IconGrid.do_realize(self, config)
115
116         self.setSize(config.getSize())
117         self.reloadIcons()
118
119         screen=self.get_screen()
120         self.set_colormap(screen.get_rgba_colormap())
121         self.set_app_paintable(True)
122
123         self.connect('show-settings', self.slot_show_settings)
124         self.connect('long-press', self.signalLongpress)
125         self.connect('click', self.signalClick)
126         self.connect('notify', self.signalNotify)
127
128         HomePluginItem.do_realize(self)
129
130     def on_orientation_changed(self, orientation):
131         # Get the first character of the string (l/p)
132         o=orientation[0]
133         self.setMode(o)
134 #       self.queue_draw()
135
136     def do_expose_event(self, event):
137         IconGrid.do_expose_event(self, event)
138         HomePluginItem.do_expose_event(self, event)
139
140     def slot_show_settings(self, dt):
141         if self.winConfig!=None:
142             # Doesn't work
143             # self.winConfig.show_all()
144             return
145
146         s=WinConfig(self.get_config())
147         s.show_all()
148         s.connect('destroy', self.slotConfigDestroy)
149         self.winConfig=s
150
151     def slotConfigDestroy(self, sender):
152         self.winConfig=None
153
154         dt=sender.getData()
155
156         config=self.get_config()
157
158         config.setSize(dt['size'])
159         config.setApps(dt['apps'])
160         config.setIndiv(dt['indiv'])
161         config.setLongpress(dt['longpress'])
162         config.save()
163         
164         # Resize widget
165         self.setSize(dt['size'])
166         self.reloadIcons()
167
168         self.queue_draw()
169
170     def handle_click(self, sender, icon):
171         """ common handler for longpress and click """
172         if icon.name!=None and icon.name!='':
173             launcher.launch(icon.name)
174
175     def signalLongpress(self, sender, icon):
176         self.handle_click(sender, icon)
177
178     def signalClick(self, sender, icon):
179         config=self.get_config()
180
181         if not config.getLongpress():
182             self.handle_click(sender, icon)
183
184     def signalNotify(self, sender, property):
185         if property.name=='is-on-current-desktop':
186             v=self.get_property(property.name)
187             if v and self.draw_pending:
188                 self.queue_draw()
189
190     def resize2(self):
191         config=self.get_config()
192
193         w=(self.size[0] * config.iconsize) + \
194             (self.size[0] * config.iconspace)
195         h=(self.size[1] * config.iconsize) + \
196             (self.size[1] * config.iconspace)
197         self.set_size_request(w, h)
198         self.resize(w, h)
199
200     def setSize(self, size):
201         IconGrid.setSize(self, size)
202         self.resize2()
203
204 hd_plugin_type = DrlaunchPlugin
205
206 if __name__=="__main__":
207     gobject.type_register(hd_plugin_type)
208     obj=gobject.new(hd_plugin_type, plugin_id="plugin_id")
209     obj.show_all()
210     gtk.main()
211
212
213
214 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
215