1
# -*- coding: utf-8 -*-
2
# Copyright 2004-2005 Joe Wreschnig, Michael Urman, Iñigo Serna
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License version 2 as
6
# published by the Free Software Foundation
8
# $Id: cover.py 4047 2007-04-30 03:49:58Z piman $
15
class BigCenteredImage(gtk.Window):
16
"""Load an image and display it, scaling down to 1/2 the screen's
17
dimensions if necessary.
19
This might leak memory, but it could just be Python's GC being dumb."""
21
def __init__(self, title, filename):
22
super(BigCenteredImage, self).__init__()
23
width = gtk.gdk.screen_width() / 2
24
height = gtk.gdk.screen_height() / 2
25
pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
27
x_rat = pixbuf.get_width() / float(width)
28
y_rat = pixbuf.get_height() / float(height)
29
if x_rat > 1 or y_rat > 1:
30
if x_rat > y_rat: height = int(pixbuf.get_height() / x_rat)
31
else: width = int(pixbuf.get_width() / y_rat)
32
pixbuf = pixbuf.scale_simple(
33
width, height, gtk.gdk.INTERP_BILINEAR)
36
self.set_decorated(False)
37
self.set_position(gtk.WIN_POS_CENTER)
41
self.child.set_shadow_type(gtk.SHADOW_OUT)
42
self.child.add(gtk.EventBox())
43
self.child.child.add(gtk.Image())
44
self.child.child.child.set_from_pixbuf(pixbuf)
46
self.child.child.connect('button-press-event', self.__destroy)
47
self.child.child.connect('key-press-event', self.__destroy)
50
def __destroy(self, *args): self.destroy()
52
class CoverImage(gtk.Frame):
57
def __init__(self, size=None, song=None):
58
super(CoverImage, self).__init__()
59
self.add(gtk.EventBox())
60
self.child.add(gtk.Image())
61
self.__size = size or [100, 71]
62
self.child.connect('button-press-event', self.__show_cover)
65
if self.__no_album is None:
67
CoverImage.__no_album = gtk.gdk.pixbuf_new_from_file_at_size(
68
stock.NO_ALBUM, *self.__size)
72
self.set_song(self, song)
74
def set_song(self, activator, song):
77
self.child.child.set_from_pixbuf(None)
81
cover = song.find_cover()
84
self.child.child.set_from_pixbuf(self.__no_album)
85
elif cover.name != self.__albumfn:
87
pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(
88
cover.name, *self.__size)
89
except gobject.GError:
92
self.child.child.set_from_pixbuf(pixbuf)
93
self.__albumfn = cover.name
98
super(CoverImage, self).show()
100
def __nonzero__(self):
101
return bool(self.__albumfn)
103
def __reset_bci(self, bci):
104
self.__current_bci = None
106
def __show_cover(self, box, event):
107
"""Show the cover as a detached BigCenteredImage.
108
If one is already showing, destroy it instead"""
109
if (self.__song and event.button == 1 and
110
event.type == gtk.gdk.BUTTON_PRESS):
111
if self.__current_bci is None:
112
# We're not displaying it yet; display it.
113
cover = self.__song.find_cover()
115
self.__current_bci = BigCenteredImage(
116
self.__song.comma("album"), cover.name)
117
self.__current_bci.connect('destroy', self.__reset_bci)
119
# We're displaying it; destroy it.
120
self.__current_bci.destroy()