Changeset 713

Show
Ignore:
Timestamp:
07/09/08 15:29:54 (5 months ago)
Author:
sylvain
Message:

Refactored the ContactModel? class so that it handles correctly item positioning (notably due to the support of groups)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • oss/jlib/jlib/core/contact.py

    r712 r713  
    158158 
    159159class jlibContactItem(QtGui.QStandardItem): 
    160     def __init__(self, parent=None): 
    161         QtGui.QStandardItem.__init__(self, parent) 
     160    def __init__(self, contact): 
     161        QtGui.QStandardItem.__init__(self) 
     162        self.contact = contact 
    162163 
    163164    def type(self): 
    164165        return QtGui.QStandardItem.UserType + 101 
     166 
     167class jlibContactGroupItem(QtGui.QStandardItem): 
     168    def __init__(self, group): 
     169        QtGui.QStandardItem.__init__(self) 
     170        self.group = group 
     171 
     172    def type(self): 
     173        return QtGui.QStandardItem.UserType + 102 
    165174 
    166175class jlibContactModel(QtGui.QStandardItemModel): 
    167176    def __init__(self, parent=None): 
    168177        QtGui.QStandardItemModel.__init__(self, parent) 
    169         self.roster = None 
    170         self.groups = {} 
     178        self.contact_items = {} 
     179        self.group_items = {} 
    171180 
    172181    def setRoster(self, roster): 
    173         self.roster = roster 
    174182        for nodeid in roster.items: 
    175183            contact = roster.items[nodeid] 
     184            for group in contact.groups: 
     185                self.setGroup(group) 
    176186            self.setContact(contact) 
    177187 
     188    def isAContactItem(self, index): 
     189        return self.itemFromIndex(index).type() == QtGui.QStandardItem.UserType + 101 
     190 
    178191    def getContact(self, index): 
    179         return self.roster.items.values()[index.row()] 
    180  
    181     def updateContact(self, contact): 
    182         for contact in self.roster.items: 
    183             if unicode(contact.jid) == unicode(contact_jid): 
    184                 self.roster.items 
    185  
    186     def getContactByJID(self, contact_jid): 
    187         return self.roster.items.get(str(contact_jid), None) 
    188      
     192        return self.itemFromIndex(index).contact 
     193 
    189194    def hasContact(self, contact_jid): 
    190         return str(contact_jid) in self.roster.items 
    191      
     195        return str(contact_jid) in self.contact_items 
     196         
    192197    def getContactIndex(self, contact): 
    193198        if self.hasContact(contact.jid): 
    194             row = self.roster.items.keys().index(str(contact.jid)) 
    195             return self.index(row, 0) 
     199            return self.contact_items[str(contact.jid)].index() 
    196200 
    197201        return QtCore.QModelIndex() 
    198202 
    199203    def getGroupIndex(self, group): 
    200         if group in self.groups: 
    201             return self.groups[group] 
     204        if group in self.group_items: 
     205            return self.group_items[group].index() 
     206 
     207        return QtCore.QModelIndex() 
     208 
     209    def getGroup(self, group): 
     210        if group in self.group_items: 
     211            return self.group_items[group] 
    202212 
    203213    def deleteContact(self, contact): 
    204         index = None 
    205         for nodeid in self.roster.items: 
    206             if str(contact.jid) == nodeid: 
    207                 index = self.index(self.roster.items.keys().index(nodeid), 0) 
    208                 del self.roster.items[nodeid] 
    209                 break 
    210  
    211         if index: 
     214        index = self.getContactIndex(contact) 
     215 
     216        if index.isValid(): 
     217            del self.contact_items[str(contact.jid)] 
    212218            self.removeRow(index.row(), index.parent()) 
    213219         
    214220    def setContact(self, contact): 
    215         item = self.itemFromIndex(self.getContactIndex(contact)) 
    216         if not item: 
    217             item = QtGui.QStandardItem() 
    218             if contact.groups: 
    219                 for group in contact.groups: 
    220                     groupItem = self.setGroup(group) 
    221                     groupItem.appendRow(item) 
    222             else: 
    223                 self.appendRow(item) 
     221        rootItem = self.invisibleRootItem() 
     222 
     223        index = self.getContactIndex(contact) 
     224        if index.isValid(): 
     225            item = self.contact_items[str(contact.jid)] 
     226        else: 
     227            item = jlibContactItem(contact) 
     228         
     229        if contact.groups: 
     230            for group in contact.groups: 
     231                groupItem = self.getGroup(group) 
     232                groupItem.appendRow(item) 
     233        else: 
     234            rootItem.appendRow(item) 
     235         
    224236        nodejid = str(contact.jid) 
    225         self.setData(self.index(item.row(), 0),  
    226                      QtCore.QVariant(QtCore.QString(nodejid))) 
    227         self.roster.items[nodejid] = contact 
     237        item.setData(QtCore.QVariant(QtCore.QString(nodejid)), 
     238                     QtCore.Qt.DisplayRole) 
     239 
     240        self.contact_items[nodejid] = item 
     241 
    228242        return item 
    229243 
    230244    def setGroup(self, group): 
    231245        index = self.getGroupIndex(group) 
    232         item = self.itemFromIndex(index) 
    233         if not item: 
    234             item = QtGui.QStandardItem(QtCore.QString(group)) 
    235             self.appendRow(item) 
    236         self.groups[group] = index 
     246        if index.isValid(): 
     247            item = self.group_items[group] 
     248        else: 
     249            item = jlibContactGroupItem(group) 
     250            rootItem = self.invisibleRootItem() 
     251            rootItem.appendRow(item) 
     252            item.setData(QtCore.QVariant(QtCore.QString(str(group))), 
     253                         QtCore.Qt.DisplayRole)  
     254        self.group_items[group] = item 
    237255        return item 
    238256 
  • oss/jlib/jlib/gui/contact.py

    r712 r713  
    5959 
    6060    def clicked(self, index): 
    61         contact = index.model().getContact(index) 
    62         self.emit(QtCore.SIGNAL("contactClicked(PyQt_PyObject)"), contact) 
     61        if index.model().isAContactItem(index): 
     62            contact = index.model().getContact(index) 
     63            self.emit(QtCore.SIGNAL("contactClicked(PyQt_PyObject)"), contact) 
    6364 
    6465    def requestedSubscription(self, presence): 
     
    9899    def contextMenuEvent(self, event): 
    99100        menu = QtGui.QMenu(self.parent()) 
     101 
    100102        actionUnsub = QtGui.QAction(QtCore.QString("Unsubscribe"), self.parent()) 
    101103        QtCore.QObject.connect(actionUnsub, QtCore.SIGNAL("triggered()"), self.unsubscribeFromContact) 
    102104        menu.addAction(actionUnsub) 
     105 
    103106        actionSub = QtGui.QAction(QtCore.QString("Subscribe to contact..."), self.parent()) 
    104107        QtCore.QObject.connect(actionSub, QtCore.SIGNAL("triggered()"), self.subscribeToContact) 
    105108        menu.addAction(actionSub) 
     109 
    106110        menu.exec_(event.globalPos()) 
    107111