Added single-click support.
[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 portrait import FremantleRotation
48 import launcher
49 from xdg.IconTheme import getIconPath
50 from win_config import WinConfig
51
52 import config
53 import apps
54 from icon import Icon
55 from icongrid import IconGrid
56
57 # Restore path
58 sys.path=orig_path
59
60 # IconGrid must be before HomePluginItem for its connect()
61 # and do_button_*() to override those of HomePluginItem
62 class DrlaunchPlugin(IconGrid, HomePluginItem, FremantleRotation):
63     def __init__(self):
64         IconGrid.__init__(self)
65         HomePluginItem.__init__(self)
66         FremantleRotation.__init__(self, 'DrlaunchPlugin')
67
68         self.winConfig=None
69
70         self.set_settings(True)
71         self.connect('show-settings', self.slot_show_settings)
72         self.connect('long-press', self.signalLongpress)
73         self.connect('click', self.signalClick)
74
75     def get_id(self):
76         """If this is called from the constructor then the program
77         core dumps """
78         aid=self.get_applet_id()
79
80         ret="%s" % aid
81
82         return(ret)
83
84     def do_realize(self):
85         #print "realize"
86
87         launcher.init()
88         config.init(self.get_id())
89         config.load()
90         self.setSize(config.getSize())
91         self.reloadIcons()
92
93         screen=self.get_screen()
94         self.set_colormap(screen.get_rgba_colormap())
95         self.set_app_paintable(True)
96
97         HomePluginItem.do_realize(self)
98
99     def on_orientation_changed(self, orientation):
100         o=orientation[0]
101         self.setMode(o)
102 #       self.queue_draw()
103
104     def do_expose_event(self, event):
105         IconGrid.do_expose_event(self, event)
106         HomePluginItem.do_expose_event(self, event)
107
108     def slot_show_settings(self, dt):
109         if self.winConfig!=None:
110             # Doesn't work
111             # self.winConfig.show_all()
112             return
113
114         s=WinConfig()
115         s.show_all()
116         s.connect('destroy', self.slotConfigDestroy)
117         self.winConfig=s
118
119     def slotConfigDestroy(self, sender):
120         self.winConfig=None
121
122         dt=sender.getData()
123         config.setSize(dt['size'])
124         config.setApps(dt['apps'])
125         config.setIndiv(dt['indiv'])
126         config.setLongpress(dt['longpress'])
127         config.save()
128         
129         # Resize widget
130         self.setSize(dt['size'])
131         self.reloadIcons()
132
133         self.queue_draw()
134
135     def handle_click(self, sender, icon):
136         """ common handler for longpress and click """
137         if icon.name!=None and icon.name!='':
138             launcher.launch(icon.name)
139
140     def signalLongpress(self, sender, icon):
141         self.handle_click(sender, icon)
142
143     def signalClick(self, sender, icon):
144         if not config.getLongpress():
145             self.handle_click(sender, icon)
146
147     def resize(self):
148         w=(self.size[0] * config.iconsize) + \
149             (self.size[0] * config.iconspace)
150         h=(self.size[1] * config.iconsize) + \
151             (self.size[1] * config.iconspace)
152         self.set_size_request(w, h)
153
154     def setSize(self, size):
155         IconGrid.setSize(self, size)
156         self.resize()
157
158 hd_plugin_type = DrlaunchPlugin
159
160 if __name__=="__main__":
161     gobject.type_register(hd_plugin_type)
162     obj=gobject.new(hd_plugin_type, plugin_id="plugin_id")
163     obj.show_all()
164     gtk.main()
165
166
167
168 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
169