Source

gSharkDown / lib / PlayList.py

The refactoring branch has multiple heads

from AbstractSongList import AbstractSongList
from enviroment import env
import gtk
import glib
    
class PlayList (AbstractSongList):
    def __init__(self, view):
        AbstractSongList.__init__(self, view)
        self.playing_song = None
    
    def create_model(self):
        # Model: Song, Info, Image, Tooltip, ...
        return gtk.ListStore(object, str, gtk.gdk.Pixbuf, str, str, int)
    
    def get_sliced_string(self, str, max):
        sliced = str[0:max - 3]
        if len(sliced) < len(str):
            return sliced + "..."
        else:
            return str
        
    def create_view(self):
        self.get_view().set_selection_mode(gtk.SELECTION_MULTIPLE)
        self.get_view().set_reorderable(True)
        
        self.get_view().set_markup_column(1)
        self.get_view().set_pixbuf_column(2)
        self.get_view().set_tooltip_column(3)
        self.get_view().set_item_width(120);
        self.get_view().set_columns(999)
        
    def get_selected_rows(self):
        return self.get_view().get_selected_items()
    
    def get_playing_song(self):
        return self.playing_song
    
    def set_playing_song(self, song):
        if song != None:
            path = self.get_song_path(song)
            if song.get_cover_pixbuf() != None:
                self.get_model()[path][2] = self.create_composited_image(song.get_cover_pixbuf())
            self.get_view().scroll_to_path(path, False, 0, 0) 
        
        self.playing_song = song
        
        for i in self.range():
            if self.get_song(i).equals(song) == False:
                self.get_model()[i][2] = self.get_song(i).get_cover_pixbuf()
    
    def append_song(self, song):
        # Change song local ID to fix the search by song
        song = song.clone()
        
        song_string = "<span>%s</span>\n<span fgcolor='#777777'>%s</span>" % (self.get_sliced_string(glib.markup_escape_text(song.get_title()), 25),
                                                                              self.get_sliced_string(glib.markup_escape_text(song.get_artist()), 20))
        #if len(song_string) > 14:
        #    song_string = song_string[:13] + "..."
        tooltip = _("<b>Title:</b> {title}\n").format(title = glib.markup_escape_text(song.get_title()))
        tooltip += _("<b>Artist:</b> {artist}\n").format(artist = glib.markup_escape_text(song.get_artist()))
        tooltip += _("<b>Album:</b> {album}\n").format(album = glib.markup_escape_text(song.get_album()))
        tooltip += _("<b>Year:</b> {year}").format(year = song.get_year())
        
        initial_pixbuf = None
        must_download = None
        if song.get_cover_pixbuf() == None:
            initial_pixbuf = self.create_loading_track_icon()
            must_download = True
        else:
            initial_pixbuf = self.create_cornered_image(song.get_cover_pixbuf())
            must_download = False
        
        self.get_model().append([
            song,
            song_string,
            initial_pixbuf,
            tooltip,
            song.get_id(),
            400
        ])
        
        if must_download == True:
            song.connect("cover-downloaded", self.on_song_cover_downloaded)
            song.download_cover()
            
    def on_song_cover_downloaded(self, song):
        image = self.create_cornered_image(song.get_cover_pixbuf())
        if song.equals(self.get_playing_song()) == True:
            self.get_song_row(song)[2] = self.create_composited_image(image)
        else:
            self.get_song_row(song)[2] = image
        
    def create_loading_track_icon(self):
        default = gtk.gdk.pixbuf_new_from_file("%s/data/loading.png" % env().BASEPATH)
        return self.create_cornered_image(default)

    def create_cornered_image(self, pixbuf):
        corners = gtk.gdk.pixbuf_new_from_file("%s/data/corners.png" % env().BASEPATH)
        corners.composite(pixbuf, 0, 0, pixbuf.props.width, pixbuf.props.height,
                          0, 0, 1.0, 1.0, gtk.gdk.INTERP_HYPER, 255)
        return pixbuf
    
    def create_composited_image(self, pixbuf):
        pixbuf = pixbuf.copy()
        play = gtk.gdk.pixbuf_new_from_file('%s/data/play.png' % env().BASEPATH)
        play.composite(pixbuf, 0, 0, pixbuf.props.width, pixbuf.props.height,
                          0, 0, 1.0, 1.0, gtk.gdk.INTERP_HYPER, 255)
        return pixbuf
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.