/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 avatarsbox.py

  • Committer: zedtux
  • Date: 2011-03-15 16:46:35 UTC
  • mto: This revision was merged to the branch mainline in revision 723.
  • Revision ID: zedtux@zedroot.org-20110315164635-4dwbd5pl0o25gg71
Moved Avatar and AvatarBox classes into avatarsbox file

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import gtk
18
18
 
19
 
from bzrlib.plugins.gtk.avatar import Avatar
20
 
from bzrlib.plugins.gtk.avatarbox import AvatarBox
 
19
from bzrlib.plugins.gtk import _i18n
 
20
from bzrlib.config import parse_username
21
21
from bzrlib.plugins.gtk.avatarproviders import AvatarProviderGravatar
22
22
from bzrlib.plugins.gtk.avatardownloaderworker import AvatarDownloaderWorker
23
23
 
 
24
 
 
25
class Avatar(gtk.Box):
 
26
    """ Author or committer avatar """
 
27
    
 
28
    def __init__(self, apparent_username):
 
29
        """ Constructor """
 
30
        gtk.Box.__init__(self)
 
31
        
 
32
        self.apparent_username = apparent_username
 
33
        self.username, self.email = parse_username(apparent_username)
 
34
        self.image = None
 
35
    
 
36
    def __eq__(self, other):
 
37
        """
 
38
        Return True if attributes of the given avatar
 
39
        match to current object attributes otherwise return False
 
40
        """
 
41
        return self.apparent_username == other.apparent_username and \
 
42
               self.name == other.name and \
 
43
               self.email == other.email
 
44
    
 
45
    # ~~~~~ Public methods ~~~~~
 
46
    def show_spinner(self):
 
47
        """
 
48
        Replace the current content of the Avatar with a gtk.Spinner
 
49
        if an email address has been parsed. If not, show an gtk.Label with
 
50
        the translatable 'No email' text.
 
51
        """
 
52
        if not self.email is "":
 
53
            spinner = gtk.Spinner()
 
54
            spinner.start()
 
55
            self.pack_start(spinner, False)
 
56
            spinner.set_tooltip_text(_i18n("Retrieving avatar for %s...") % self.email)
 
57
            spinner.set_size_request(20, 20)
 
58
            spinner.show()
 
59
        else:
 
60
            no_email = gtk.Label(_i18n("No email"))
 
61
            self.pack_start(no_email)
 
62
            self.set_tooltip_text(self.apparent_username)
 
63
            no_email.show()
 
64
    
 
65
    def show_image(self):
 
66
        """ Replace the current content of the Avatar with the gtk.Image """
 
67
        if not self.email is "" and self.image:
 
68
            self.remove(self.get_children()[0])
 
69
            self.pack_start(self.image)
 
70
            self.image.set_tooltip_text(self.apparent_username)
 
71
            self.image.show()
 
72
 
 
73
 
 
74
class AvatarBox(gtk.HBox):
 
75
    """ Improved gtk.HBox """
 
76
    
 
77
    def __init__(self, homogeneous=False, spacing=0):
 
78
        """ Constructor """
 
79
        gtk.HBox.__init__(self, homogeneous, spacing)
 
80
        self.__avatars = {}
 
81
        self.avatar = None
 
82
        self.__displaying = None
 
83
    
 
84
    
 
85
    # ~~~~~ Public methods ~~~~~
 
86
    def reset_view(self):
 
87
        """ Remove current avatars from the gtk box """
 
88
        for child in self.get_children():
 
89
            self.remove(child)
 
90
        self.__displaying = None
 
91
    
 
92
    def have_avatar(self, avatar):
 
93
        """
 
94
        Return True if this box has registered given avatar,
 
95
        otherwise return False
 
96
        """
 
97
        return avatar.email in self.__avatars
 
98
    
 
99
    def showing(self, avatar):
 
100
        """
 
101
        Return True if the displaying avatar is the same
 
102
        as the given one otherwise return False
 
103
        """
 
104
        return self.__displaying and self.__displaying == avatar
 
105
    
 
106
    def append_avatars_with(self, avatar):
 
107
        """
 
108
        Append avatars collection with the given one if not already registed
 
109
        otherwise render it back.
 
110
        When an avatar is added this method True, otherwise, if the avatar
 
111
        was in the collection, return False.
 
112
        """
 
113
        if not avatar.email is "" and not avatar.email in self.__avatars:
 
114
            self.__avatars[avatar.email] = avatar
 
115
            self._new_cell_for_avatar(avatar)
 
116
            return True
 
117
        else:
 
118
            self.and_avatar_email(avatar.email).come_back_to_gui()
 
119
        return False
 
120
    
 
121
    def and_avatar_email(self, email):
 
122
        """
 
123
        Select the avatar from avatars collection
 
124
        in order to apply future actions
 
125
        """
 
126
        self.avatar = None
 
127
        if not email is "" and email in self.__avatars:
 
128
            self.avatar = self.__avatars[email]
 
129
        else:
 
130
            self.avatar = None
 
131
        return self
 
132
    
 
133
    def update_avatar_image_from_pixbuf_loader(self, loader):
 
134
        """ Replace the gtk.Spinner with the given loader """
 
135
        if self.avatar:
 
136
            self.avatar.image = gtk.Image()
 
137
            self.avatar.image.set_from_pixbuf(loader.get_pixbuf())
 
138
            self.avatar.show_image()
 
139
            self.__displaying = self.avatar
 
140
    
 
141
    def come_back_to_gui(self):
 
142
        """ Render back avatar in the GUI """
 
143
        if self.avatar:
 
144
            self.pack_start(self.avatar)
 
145
            self.__displaying = self.avatar
 
146
        else:
 
147
            self._show_no_email()
 
148
    
 
149
    
 
150
    # ~~~~~ Private methods ~~~~~~
 
151
    def _new_cell_for_avatar(self, avatar):
 
152
        """ Create a new cell in this box with a gtk.Spinner """
 
153
        avatar.show_spinner()
 
154
        self.pack_start(avatar)
 
155
        avatar.show()
 
156
        self.__displaying = avatar
 
157
    
 
158
    def _show_no_email(self):
 
159
        """ Show a gtk.Label with test 'No email' """
 
160
        no_email = gtk.Label(_i18n("No email"))
 
161
        self.pack_start(no_email)
 
162
        no_email.show()
 
163
 
 
164
 
24
165
class AvatarsBox(gtk.HBox):
25
166
    """ GTK container for authors and committers avatars """
26
167