Few UI improvements in download-dialog
[mussorgsky] / src / aa_selection_dialog.py
1 import hildon
2 import gtk
3 import gobject
4 from album_art_thread import MussorgskyAlbumArt
5
6 RESPONSE_CLICK = 1
7
8 class ClickableImage (gtk.EventBox):
9
10     def __init__ (self, isRemoveOption=False):
11         gtk.EventBox.__init__ (self)
12
13         self.isRemoveOption = isRemoveOption
14
15         self.img = gtk.Image ()
16         self.img.set_size_request (124, 124)
17         self.add (self.img)
18         self.set_sensitive (False)
19
20         self.img_path = None
21         self.thumb_path = None
22
23         if (self.isRemoveOption):
24             self.img.set_from_icon_name ("mediaplayer_default_album",
25                                          gtk.ICON_SIZE_MENU)
26             self.img.set_pixel_size (124)
27             self.set_sensitive (True)
28             
29     def set_image (self, tmp_img, tmp_thumb):
30         assert not self.isRemoveOption
31         self.img_path = tmp_img
32         self.thumb_path = tmp_thumb
33         self.img.set_from_file (self.thumb_path)
34         self.set_sensitive (True)
35
36     def set_default_image (self):
37         self.img.set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
38
39     def get_paths (self):
40         return self.img_path, self.thumb_path
41
42     def is_remove_option (self):
43         return self.isRemoveOption
44         
45
46 class AlbumArtSelectionDialog (gtk.Dialog):
47
48     def __init__ (self, parent, artist, album, size, downloader=None):
49         """
50         parent window, amount of images to offer
51         Optionally downloader (for testing porpouses)
52         """
53         gtk.Dialog.__init__ (self,
54                              "Select album art", parent,
55                              gtk.DIALOG_DESTROY_WITH_PARENT,
56                              (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT))
57         self.artist = artist
58         self.album = album
59         self.size = size
60         self.__create_view (size)
61         self.cancel = False
62         self.connect ("response", self.handle_response)
63
64         if (downloader):
65             self.downloader = downloader
66         else:
67             self.downloader = MussorgskyAlbumArt ()
68
69         gobject.idle_add (self.__get_alternatives_async)
70         self.selection_img = None
71         self.selection_thumb = None
72         hildon.hildon_gtk_window_set_progress_indicator (self, 1)
73
74
75     def __create_view (self, size):
76         hbox = gtk.HBox (homogeneous=True)
77
78         self.images = []
79         for i in range (0, size):
80             image = ClickableImage ()
81             image.connect ("button-release-event", self.click_on_img)
82             self.images.append (image)
83             hbox.pack_start (image, expand=False, fill=True)
84             
85         # default empty option
86         image = ClickableImage (isRemoveOption=True)
87         image.connect ("button-release-event", self.click_on_img)
88         self.images.append (image)
89         hbox.pack_start (image, expand=False, fill=True)
90         self.vbox.pack_start (hbox, padding=6)
91         
92         label = gtk.Label ("New search:")
93         self.entry = hildon.Entry (gtk.HILDON_SIZE_FINGER_HEIGHT)
94         self.entry.set_text (self.artist + " " +  self.album)
95
96         img = gtk.Image ()
97         img.set_from_icon_name ("general_search", gtk.ICON_SIZE_LARGE_TOOLBAR)
98         button = hildon.Button (gtk.HILDON_SIZE_FINGER_HEIGHT,
99                                 hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
100         button.set_image (img)
101         button.connect ("clicked", self.user_text_search_cb, self.entry)
102         self.hbox_research = gtk.HBox (homogeneous=False, spacing=6)
103         self.hbox_research.pack_start (label, expand=False)
104         self.hbox_research.pack_start (self.entry)
105         self.hbox_research.pack_start (button, expand=False)
106         self.hbox_research.set_sensitive (False)
107         self.vbox.pack_start (self.hbox_research, padding=6)
108
109     def __get_alternatives_async (self):
110         results = self.downloader.get_alternatives (self.album,
111                                                     self.artist,
112                                                     self.size)
113         self.__show_results (results)
114         
115     def __show_results (self, generator):
116         counter = 0
117         for (path, thumb) in generator:
118             print path, thumb
119             if (self.cancel):
120                 return False
121             if (thumb):
122                 print "Setting", thumb, "as image"
123                 self.images[counter].set_image (path, thumb)
124             else:
125                 continue
126             counter += 1
127             while (gtk.events_pending()):
128                 gtk.main_iteration()
129
130         while (counter < self.size):
131                 self.images[counter].set_default_image ()
132                 counter += 1
133                 
134         hildon.hildon_gtk_window_set_progress_indicator (self, 0)
135         self.hbox_research.set_sensitive (True)
136         self.entry.grab_focus ()
137         
138     def user_text_search_cb (self, w, entry):
139         user_text = entry.get_text ()
140         if user_text and len (user_text) > 0:
141             hildon.hildon_gtk_window_set_progress_indicator (self, 1)
142             for ev in self.images[:-1]:
143                 ev.set_sensitive (False)
144             self.hbox_research.set_sensitive (False)
145             while (gtk.events_pending()):
146                 gtk.main_iteration()
147
148             results = self.downloader.get_alternatives_free_text (user_text,
149                                                                   self.size)
150             self.__show_results (results)
151             
152
153     def click_on_img (self, image, event):
154         if (image.is_remove_option ()):
155             self.selection_img = None
156             self.selection_thumb = None
157             self.downloader.reset_alternative (self.artist, self.album)
158         else:
159             tmp_img, tmp_thumb = image.get_paths ()
160             img, thumb = self.downloader.save_alternative (self.artist,
161                                                            self.album,
162                                                            tmp_img,
163                                                            tmp_thumb)
164             self.selection_img, self.selection_thumb = img, thumb
165         self.response (RESPONSE_CLICK)
166
167     def get_selection (self):
168         return (self.selection_img, self.selection_thumb)
169     
170     def handle_response (self, widget, response_id):
171         self.cancel = True
172         # Return False to continue propagating the signal
173         return False
174
175 if __name__ == "__main__":
176
177     import time
178     class MockDownloader:
179         def __init__ (self):
180             self.alt = [("../hendrix.jpeg", "../hendrix-thumb.jpeg"),
181                         ("../hoover.jpeg", "../hoover-thumb.jpeg"),
182                         ("../backbeat.jpeg", "../backbeat-thumb.jpeg"),
183                         ("../dylan.jpeg", "../dylan-thumb.jpeg")]
184         def get_alternatives (self, album, artist, amount):
185             for a in self.alt:
186                 time.sleep (1)
187                 yield a
188         def get_alternatives_free_text (self, user_text, amount=4):
189             for a in [("free%d" % i, "thumb%d" %i) for i in range (0, amount)]:
190                 time.sleep (1)
191                 yield a
192         def save_alternative (self, artist, album, img, thumb):
193             return ("/home/user/.cache/media-art/" + img, "/home/user/.thumbnails/normal/" + thumb)
194         def reset_alternative (self, artist, album):
195             print "Removing the album-art and the thumbnail"
196                               
197
198     def clicked_button (self):
199         aadd = AlbumArtSelectionDialog (w, "rory gallagher", "irish tour", 4, MockDownloader ())
200         aadd.show_all ()
201         response = aadd.run ()
202         if response == gtk.RESPONSE_CLOSE or response == gtk.RESPONSE_DELETE_EVENT or response == gtk.RESPONSE_REJECT:
203             print "Noooo"
204         else:
205             print "RESPONSE_CLICK", response == RESPONSE_CLICK
206             print "Selected", aadd.get_selection ()
207         aadd.hide ()
208         
209     w = gtk.Window ()
210     w.connect ("destroy", gtk.main_quit)
211     box = gtk.VBox ()
212
213     button = gtk.Button ("click")
214     button.connect ("clicked", clicked_button)
215     box.add (button)
216
217     w.add (box)
218     w.show_all ()
219
220
221     gtk.main ()
222