Commits

Damián Nohales committed a4249e7

Now user can filter search results.

  • Participants
  • Parent commits 42ebad4

Comments (0)

Files changed (3)

data/main_window.ui

                           </packing>
                         </child>
                         <child>
-                          <object class="GtkEntry" id="filter_entry">
+                          <object class="GtkEntry" id="entry_filter">
                             <property name="visible">True</property>
                             <property name="can_focus">True</property>
                             <property name="invisible_char">•</property>
+                            <property name="secondary_icon_stock">gtk-clear</property>
                             <property name="primary_icon_activatable">False</property>
-                            <property name="secondary_icon_activatable">False</property>
+                            <property name="secondary_icon_activatable">True</property>
                             <property name="primary_icon_sensitive">True</property>
                             <property name="secondary_icon_sensitive">True</property>
                             <signal name="changed" handler="on_filter_changed" swapped="no"/>
+                            <signal name="icon-release" handler="on_filter_entry_icon_press" swapped="no"/>
                           </object>
                           <packing>
                             <property name="expand">False</property>
                     <property name="left_padding">8</property>
                     <property name="right_padding">8</property>
                     <child>
-                      <object class="GtkVBox" id="vbox4">
+                      <object class="GtkVBox" id="vbox_results">
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
                         <child>

lib/SearchResultList.py

     """
     
     def __init__(self, view):
+        self.filter_text = ""
+        self.modelfilter = None
+        
         AbstractSongList.__init__(self, view)
     
     def create_model(self):
-        # Model: Song, Title, Artist, Album, ID
-        return gtk.ListStore(object, str, str, str, str)
+        # Model: Song, Title, Artist, Album
+        model = gtk.ListStore(object, str, str, str)
+        self.modelfilter = model.filter_new()
+        self.modelfilter.set_visible_func(self.filter_visible_func)
+        return self.modelfilter
     
     def append_song(self, song):
-        self.get_model().append([
+        self.get_model().get_model().append([
             song,
             song.get_title(),
             song.get_artist(),
-            song.get_album(),
-            song.get_id()
+            song.get_album()
         ])
         
+    def clear(self):
+        self.get_model().get_model().clear()
+        
     def create_view(self):
         self.get_view().get_selection().set_mode(gtk.SELECTION_MULTIPLE)
         
         column.set_resizable(True)
         column.set_max_width(150)
         self.get_view().append_column(column)
-
-        rendererText = gtk.CellRendererText()
-        column = gtk.TreeViewColumn(_("SongID"), rendererText, text = 4)
-        column.set_resizable(True)
-        self.get_view().append_column(column)
+        
+    def set_filter_text(self, text):
+        self.filter_text = " ".join(text.split()).lower()
+        self.modelfilter.refilter()
+    
+    def get_filter_text(self, text):
+        return self.filter_text
+    
+    def filter_visible_func(self, model, iter):
+        if self.filter_text == "":
+            return True
+        else:
+            song = model[iter][0]
+            t = self.filter_text
+            return song.get_title().lower().find(t) >= 0 or song.get_artist().lower().find(t) >= 0 or song.get_album().lower().find(t) >= 0
         self.window.connect("delete-event", self.window.hide_on_delete)
         self.mainmenu = builder.get_object('mainmenu')
         
+        self.vbox_results = builder.get_object('vbox_results')
+        
         self.button_play = builder.get_object('button_play')
         self.widget_image_play = builder.get_object('widget_image_play')
         self.widget_image_stop = builder.get_object('widget_image_stop')
         self.downloads_pane_label = builder.get_object('togglebutton2')
         
         self.entry_search = builder.get_object('toolentry_search')
+        self.entry_filter = builder.get_object('entry_filter')
         
         # GStreamer player initialization
         self.player = gst.element_factory_make("playbin", "player")
         else:
             self.window.unfullscreen()
             menuitem.set_label("gtk-fullscreen")
-
+    
+    def on_filter_entry_icon_press(self, entry, position, event):
+        entry.set_text("")
+    
     def on_filter_changed(self, entry, data = None):
-        print entry.get_text()
+        self.result.set_filter_text(entry.get_text())
 
     def get_iter_last(self, model):
         """
         """
         Adds selected item from the results to the playlist
         """
-        select = self.result.get_view().get_selection().get_selected_rows()
-        for item in select[1]:
+        select = self.result.get_selected_rows()
+        for item in select:
             song = self.result.get_song(item[0])
             self.playlist.append_song(song)
 
         """
         Starts the search thread
         """
-        if widget.get_text() != "":
-            if widget.get_text() in config()['completition'].split("|"):
+        text = " ".join(widget.get_text().split())
+        if text != "":
+            if text in config()['completition'].split("|"):
                 pass
             else:
-                compl = config()['completition'] + widget.get_text() + "|"
+                compl = config()['completition'] + text + "|"
                 config()['completition'] = compl
                 config().write()
+            self.entry_filter.set_text("")
             search_thread = SearchThread(self, widget.get_text(), "Songs")
             search_thread.start()