~ubuntu-branches/debian/squeeze/quodlibet/squeeze

« back to all changes in this revision

Viewing changes to qltk/cover.py

  • Committer: Bazaar Package Importer
  • Author(s): Christine Spang
  • Date: 2009-02-16 07:09:42 UTC
  • mfrom: (2.1.17 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090216070942-bd2iy1qva7fmpf4m
Upload to unstable now that Lenny has been released (woo!).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
# Copyright 2004-2005 Joe Wreschnig, Michael Urman, Iñigo Serna
3
 
#
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
7
 
#
8
 
# $Id: cover.py 4047 2007-04-30 03:49:58Z piman $
9
 
 
10
 
import gobject
11
 
import gtk
12
 
 
13
 
import stock
14
 
 
15
 
class BigCenteredImage(gtk.Window):
16
 
    """Load an image and display it, scaling down to 1/2 the screen's
17
 
    dimensions if necessary.
18
 
 
19
 
    This might leak memory, but it could just be Python's GC being dumb."""
20
 
 
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)
26
 
 
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)
34
 
 
35
 
        self.set_title(title)
36
 
        self.set_decorated(False)
37
 
        self.set_position(gtk.WIN_POS_CENTER)
38
 
        self.set_modal(False)
39
 
        self.set_icon(pixbuf)
40
 
        self.add(gtk.Frame())
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)
45
 
 
46
 
        self.child.child.connect('button-press-event', self.__destroy)
47
 
        self.child.child.connect('key-press-event', self.__destroy)
48
 
        self.show_all()
49
 
 
50
 
    def __destroy(self, *args): self.destroy()
51
 
 
52
 
class CoverImage(gtk.Frame):
53
 
    __albumfn = None
54
 
    __current_bci = None
55
 
    __no_album = None
56
 
 
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)
63
 
        self.child.show_all()
64
 
 
65
 
        if self.__no_album is None:
66
 
            try:
67
 
                CoverImage.__no_album = gtk.gdk.pixbuf_new_from_file_at_size(
68
 
                    stock.NO_ALBUM, *self.__size)
69
 
            except RuntimeError:
70
 
                pass
71
 
 
72
 
        self.set_song(self, song)
73
 
 
74
 
    def set_song(self, activator, song):
75
 
        self.__song = song
76
 
        if song is None:
77
 
            self.child.child.set_from_pixbuf(None)
78
 
            self.__albumfn = None
79
 
            self.hide()
80
 
        else:
81
 
            cover = song.find_cover()
82
 
            if cover is None:
83
 
                self.__albumfn = None
84
 
                self.child.child.set_from_pixbuf(self.__no_album)
85
 
            elif cover.name != self.__albumfn:
86
 
                try:
87
 
                    pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(
88
 
                        cover.name, *self.__size)
89
 
                except gobject.GError:
90
 
                    self.hide()
91
 
                else:
92
 
                    self.child.child.set_from_pixbuf(pixbuf)
93
 
                    self.__albumfn = cover.name
94
 
                    self.show()
95
 
 
96
 
    def show(self):
97
 
        if self.__albumfn:
98
 
            super(CoverImage, self).show()
99
 
 
100
 
    def __nonzero__(self):
101
 
        return bool(self.__albumfn)
102
 
 
103
 
    def __reset_bci(self, bci):
104
 
        self.__current_bci = None
105
 
 
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()
114
 
                if cover:
115
 
                    self.__current_bci = BigCenteredImage(
116
 
                        self.__song.comma("album"), cover.name)
117
 
                    self.__current_bci.connect('destroy', self.__reset_bci)
118
 
            else:
119
 
                # We're displaying it; destroy it.
120
 
                self.__current_bci.destroy()