Changeset 2659 for trunk/src/dialogs.py

Show
Ignore:
Timestamp:
07/31/05 21:21:11 (3 years ago)
Author:
dkirov
Message:

tooltip positioning... base tooltip class...
custom tooltip in notif. area

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/dialogs.py

    r2652 r2659  
    517517                        [ [ gtk.STOCK_OK, gtk.RESPONSE_OK ] ] 
    518518                ) 
    519  
    520 class RosterTooltip: 
    521         def __init__(self, plugin): 
    522                 self.plugin = plugin 
    523                  
    524                 self.image = gtk.Image() 
    525                 self.image.set_alignment(0.5, 0.025) 
    526                 self.account = None 
    527                 self.table = None 
    528                  
    529                 self.current_row = 1 
     519class BaseTooltip: 
     520        ''' Base Tooltip . Usage: 
     521                tooltip = BaseTooltip() 
     522                ....  
     523                tooltip.show_tooltip('', window_postions, widget_postions) 
     524                .... 
     525                if tooltip.timeout != 0: 
     526                        tooltip.hide_tooltip() 
     527        ''' 
     528        def __init__(self): 
    530529                self.timeout = 0 
    531530                self.prefered_position = [0, 0] 
    532                 self.path = None 
     531                self.win = None 
     532                self.id = None 
     533                 
     534        def populate(self, data): 
     535                ''' this method must be overridenby all extenders ''' 
    533536                self.create_window() 
     537                self.win.add(gtk.Label(data)) 
    534538                 
    535539        def create_window(self): 
    536540                ''' create a popup window each time tooltip is requested ''' 
    537541                self.win = gtk.Window(gtk.WINDOW_POPUP) 
     542                self.win.set_border_width(3) 
    538543                self.win.set_resizable(False) 
    539544                self.win.set_name('gtk-tooltips') 
    540                 self.hbox = gtk.HBox() 
    541                 self.hbox.set_border_width(6) 
    542                 self.hbox.set_homogeneous(False) 
    543                 self.win.add(self.hbox) 
    544                 self.hbox.pack_start(self.image, False, False) 
     545                 
    545546                 
    546547                self.win.set_events(gtk.gdk.POINTER_MOTION_MASK) 
     
    562563                else: 
    563564                        self.prefered_position[0] -= half_width  
     565                        screen.get_height() 
    564566                if self.prefered_position[1] + requisition.height > screen.get_height(): 
    565                         self.prefered_position[1] -= requisition.height  + 25 
     567                        # flip tooltip up 
     568                        self.prefered_position[1] -= requisition.height  + self.widget_height + 8 
    566569                if self.prefered_position[1] < 0: 
    567570                        self.prefered_position[1] = 0 
     
    581584                return True 
    582585         
    583         def show_tooltip(self, contact, pointer_position, win_size): 
    584                 self.populate(contact) 
    585                 new_x = win_size[0] + pointer_position[0]  
    586                 new_y = win_size[1] + pointer_position[1] + 35 
     586        def show_tooltip(self, data, widget_pos, win_size): 
     587                self.populate(data) 
     588                new_x = win_size[0] + widget_pos[0]  
     589                new_y = win_size[1] + widget_pos[1] + 4 
    587590                self.prefered_position = [new_x, new_y] 
     591                self.widget_height = widget_pos[1] 
    588592                self.win.ensure_style() 
    589593                self.win.show_all() 
     
    593597                        gobject.source_remove(self.timeout) 
    594598                        self.timeout = 0 
    595                 self.win.destroy() 
    596                 self.path = None 
    597  
    598  
    599         def add_status(self, file_path, resource, priority, show, status): 
     599                if self.win: 
     600                        self.win.destroy() 
     601                        self.win = None 
     602                self.id = None 
     603 
     604class StatusTable: 
     605        ''' Contains methods for creating status table. This  
     606        is used in Roster and NotificationArea tooltips ''' 
     607        def __init__(self): 
     608                self.current_row = 1 
     609                self.table = None 
     610                self.text_lable = None 
     611                 
     612        def create_table(self): 
     613                self.table = gtk.Table(3, 1) 
     614                self.table.set_property('column-spacing', 6) 
     615                self.text_lable = gtk.Label() 
     616                self.text_lable.set_line_wrap(True) 
     617                self.text_lable.set_alignment(0, 0) 
     618                self.text_lable.set_selectable(False) 
     619                self.table.attach(self.text_lable, 1, 4, 1, 2) 
     620                 
     621        def get_status_info(self, resource, priority, show, status): 
     622                str_status = resource + ' ('+str(priority)+')' 
     623                if status: 
     624                        status = status.strip() 
     625                        if status != '': 
     626                                str_status += ' - ' + status 
     627                return gtkgui_helpers.escape_for_pango_markup(str_status) 
     628                 
     629        def add_status_row(self, file_path, show, str_status): 
    600630                ''' appends a new row with status icon to the table ''' 
    601631                self.current_row += 1 
     
    606636                image = gtk.Image() 
    607637                image.set_from_pixbuf(None) 
    608                 spacer = gtk.Label('    ') 
     638                spacer = gtk.Label('   ') 
    609639                for file in files: 
    610640                        if os.path.exists(file): 
     
    617647                        self.current_row + 1, 0, 0, 3, 0) 
    618648                image.set_alignment(0.01, 1) 
    619                 str_status = resource + ' ('+str(priority)+')' 
    620                 if status: 
    621                         status = status.strip() 
    622                         if status != '': 
    623                                 str_status += ' - ' + status 
    624                 status_label = gtk.Label(str_status) 
     649                status_label = gtk.Label() 
     650                status_label.set_markup(str_status) 
    625651                status_label.set_alignment(00, 0) 
    626652                self.table.attach(status_label, 3, 4, self.current_row, 
    627653                        self.current_row + 1, gtk.EXPAND | gtk.FILL, 0, 0, 0) 
     654         
     655class NotificationAreaTooltip(BaseTooltip, StatusTable): 
     656        ''' Tooltip that is shown in the notification area ''' 
     657        def __init__(self, plugin): 
     658                self.plugin = plugin 
     659                BaseTooltip.__init__(self) 
     660                StatusTable.__init__(self) 
     661                 
     662                 
     663        def populate(self, data): 
     664                self.create_window() 
     665                self.create_table() 
     666                self.hbox = gtk.HBox() 
     667                self.table.set_property('column-spacing', 1) 
     668                text, single_line, accounts = '', '', [] 
     669                if gajim.contacts: 
     670                        for account in gajim.contacts.keys(): 
     671                                status_idx = gajim.connections[account].connected 
     672                                # uncomment the following to hide offline accounts 
     673                                # if status_idx == 0: continue 
     674                                from common.connection import STATUS_LIST 
     675                                status = STATUS_LIST[status_idx] 
     676                                message = gajim.connections[account].status 
     677                                single_line = helpers.get_uf_show(status) 
     678                                if message is None: 
     679                                        message = '' 
     680                                else: 
     681                                        message = message.strip() 
     682                                if message != '': 
     683                                        single_line += ': ' + message 
     684                                else: 
     685                                        message = helpers.get_uf_show(status) 
     686 
     687                                accounts.append({'name':account, 'status_line':single_line,  
     688                                                'show':status, 'message':message}) 
     689                unread_messages_no = self.plugin.roster.nb_unread 
     690                if unread_messages_no > 1: 
     691                        text = _('Gajim - %s unread messages') % unread_messages_no 
     692                elif unread_messages_no == 1: 
     693                        text = _('Gajim - 1 unread message') 
     694                elif len(accounts) > 1: 
     695                        text = _('Gajim') 
     696                        self.current_row = 1 
     697                        self.table.resize(2,1) 
     698                        iconset = gajim.config.get('iconset') 
     699                        if not iconset: 
     700                                iconset = 'sun' 
     701                        file_path = os.path.join(gajim.DATA_DIR, 'iconsets', iconset, '16x16') 
     702                        for acct in accounts: 
     703                                self.add_status_row(file_path, acct['show'], '<span weight="bold">' +  
     704                                        gtkgui_helpers.escape_for_pango_markup(acct['name']) + '</span>'  
     705                                        + ' - ' + gtkgui_helpers.escape_for_pango_markup(acct['message'])) 
     706                                         
     707                elif len(accounts) == 1: 
     708                        text = _('Gajim - %s') % accounts[0]['status_line'] 
     709                else: 
     710                        text = _('Gajim - %s') % helpers.get_uf_show('offline') 
     711                self.text_lable.set_markup(text) 
     712                self.hbox.add(self.table) 
     713                self.win.add(self.hbox) 
     714                 
     715class RosterTooltip(BaseTooltip, StatusTable): 
     716        ''' Tooltip that is shown in the roster treeview ''' 
     717        def __init__(self, plugin): 
     718                self.account = None 
     719                self.plugin = plugin 
     720                 
     721                self.image = gtk.Image() 
     722                self.image.set_alignment(0.5, 0.025) 
     723                BaseTooltip.__init__(self) 
     724                StatusTable.__init__(self) 
    628725                 
    629726        def populate(self, contacts): 
     
    631728                        return 
    632729                self.create_window() 
    633                 self.table = gtk.Table(3, 1) 
    634                 self.table.set_property('column-spacing', 6) 
    635                 self.account = gtk.Label() 
    636                 self.account.set_line_wrap(True) 
    637                 self.account.set_alignment(0, 0) 
    638                 self.account.set_selectable(False) 
    639                 self.table.attach(self.account, 1, 4, 1, 2) 
    640                 self.hbox.pack_start(self.table, True, True) 
    641                 # default resource of the contact 
     730                self.hbox = gtk.HBox() 
     731                #~ self.hbox.set_border_width(6) 
     732                self.hbox.set_homogeneous(False) 
     733                self.create_table() 
    642734                prim_contact = None # primary contact 
    643735                for contact in contacts: 
     
    666758                                self.image.set_from_file(file) 
    667759                                break 
    668  
     760                 
    669761                info = '<span size="large" weight="bold">' + prim_contact.jid + '</span>' 
    670762                info += '\n<span weight="bold">' + _('Name: ') + '</span>' + \ 
     
    694786                        for contact in contacts: 
    695787                                if contact.resource: 
    696                                         self.add_status(file_path, contact.resource, contact.priority,  
     788                                        status_line = self.get_status_info(contact.resource, contact.priority,  
    697789                                                contact.show, contact.status) 
     790                                        self.add_status_row(file_path, contact.show, status_line) 
    698791                                         
    699792                else: # only one resource 
     
    711804                                                info += ' - ' + gtkgui_helpers.escape_for_pango_markup(status) 
    712805                 
    713                 self.account.set_markup(info) 
     806                self.text_lable.set_markup(info) 
     807                self.hbox.pack_start(self.image, False, False) 
     808                self.hbox.pack_start(self.table, True, True) 
     809                self.win.add(self.hbox) 
    714810 
    715811class InputDialog: