/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to avatarbox.py

  • Committer: zedtux
  • Date: 2011-03-11 14:59:04 UTC
  • mto: This revision was merged to the branch mainline in revision 723.
  • Revision ID: zedtux@zedroot.org-20110311145904-g0cq6drvweriy9hz
Added committers and authors avatars from Gravatar (urllib2 version)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011 by Guillaume Hain (zedtux) <zedtux@zedroot.org>
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
import gtk
 
18
 
 
19
from bzrlib.plugins.gtk import _i18n
 
20
 
 
21
class AvatarBox(gtk.HBox):
 
22
    """ Improved gtk.HBox """
 
23
    
 
24
    def __init__(self, homogeneous=False, spacing=0):
 
25
        """ Constructor """
 
26
        gtk.HBox.__init__(self, homogeneous, spacing)
 
27
        self.__avatars = {}
 
28
        self.__current_avatar = None
 
29
        self.__displaying = None
 
30
    
 
31
    
 
32
    # ~~~~~ Properties ~~~~~
 
33
    # Avatar
 
34
    def get_avatar(self):
 
35
        return self.__current_avatar
 
36
    def set_avatar(self, avatar):
 
37
        self.__current_avatar = avatar
 
38
    avatar = property(get_avatar, set_avatar)
 
39
    
 
40
    
 
41
    # ~~~~~ Public methods ~~~~~
 
42
    def reset_view(self):
 
43
        """ Remove current avatars from the gtk box """
 
44
        for child in self.get_children():
 
45
            self.remove(child)
 
46
        self.__displaying = None
 
47
    
 
48
    def have_avatar(self, avatar):
 
49
        """
 
50
        Return True if this box has registered given avatar,
 
51
        otherwise return False
 
52
        """
 
53
        return avatar.email in self.__avatars
 
54
    
 
55
    def showing(self, avatar):
 
56
        """
 
57
        Return True if the displaying avatar is the same
 
58
        as the given one otherwise return False
 
59
        """
 
60
        return self.__displaying and self.__displaying.is_identical(avatar)
 
61
    
 
62
    def append_avatars_with(self, avatar):
 
63
        """
 
64
        Append avatars collection with the given one if not already registed
 
65
        otherwise render it back.
 
66
        When an avatar is added this method True, otherwise, if the avatar
 
67
        was in the collection, return False.
 
68
        """
 
69
        if not avatar.email is "" and not avatar.email in self.__avatars:
 
70
            self.__avatars[avatar.email] = avatar
 
71
            self._new_cell_for_avatar(avatar)
 
72
            return True
 
73
        else:
 
74
            self.and_avatar_email(avatar.email).come_back_to_gui()
 
75
        return False
 
76
    
 
77
    def and_avatar_email(self, email):
 
78
        """
 
79
        Select the avatar from avatars collection
 
80
        in order to apply future actions
 
81
        """
 
82
        self.avatar = None
 
83
        if not email is "":
 
84
            self.avatar = self.__avatars[email] if email in self.__avatars else None
 
85
        return self
 
86
    
 
87
    def update_avatar_image_from_pixbuf_loader(self, loader):
 
88
        """ Replace the gtk.Spinner with the given loader """
 
89
        if self.avatar:
 
90
            self.avatar.image = gtk.Image()
 
91
            self.avatar.image.set_from_pixbuf(loader.get_pixbuf())
 
92
            self.avatar.show_image()
 
93
            self.__displaying = self.avatar
 
94
    
 
95
    def come_back_to_gui(self):
 
96
        """ Render back avatar in the GUI """
 
97
        if self.avatar:
 
98
            self.pack_start(self.avatar)
 
99
            self.__displaying = self.avatar
 
100
        else:
 
101
            self._show_no_email()
 
102
    
 
103
    
 
104
    # ~~~~~ Private methods ~~~~~~
 
105
    def _new_cell_for_avatar(self, avatar):
 
106
        """ Create a new cell in this box with a gtk.Spinner """
 
107
        avatar.show_spinner()
 
108
        self.pack_start(avatar)
 
109
        avatar.show()
 
110
        self.__displaying = avatar
 
111
    
 
112
    def _show_no_email(self):
 
113
        """ Show a gtk.Label with test 'No email' """
 
114
        no_email = gtk.Label(_i18n("No email"))
 
115
        self.pack_start(no_email)
 
116
        no_email.show()