On 09.04.07 20:20:54, Detlev Offenbach wrote:
> On Montag, 9. April 2007, Andreas Pakulat wrote:
> > The one I posted seems to work fine (I didn't test everything though),
> > the one I currently have implemented (without the self.dict_items list,
> > as references to the objects are already kept in the children list -
> > afaik) doesn't work when I try to open a file (i.e. show its classes).
> > I'm also not 100% sure I tested this with the posted patch... So I guess
> > you should first try it out a bit, before adding it permanently.
> >
> Ok, I'll play with it tomorrow.

Ok, now that running eric4 inside eric3 works right this was easy to
find: The asserts I put into the code to make sure that internalPointer
returned a valid object were a bit wrong :) So attached is a diff where
the Models have

a) the item_dict removed
b) use internalPointer all the time
c) have nearly no errors pointed out by the ModelTest

One thing I couldn't fix is the assert on line 415 in modeltest.py, it
tests that all valid indexes return a valid QVariant for DisplayRole.
The problem is that the items under a given parent may have different
number of columnCount(), so I decided to return QVariant("") in that
case (see line 80 of BrowserModel.py) as a workaround.

Andreas

-- 
You will be singled out for promotion in your work.
diff -u -ur eric4-snapshot-20070402.old/eric/Project/ProjectBrowserModel.py eric4-snapshot-20070402/eric/Project/ProjectBrowserModel.py
--- eric4-snapshot-20070402.old/eric/Project/ProjectBrowserModel.py	2007-02-10 21:34:26.000000000 +0100
+++ eric4-snapshot-20070402/eric/Project/ProjectBrowserModel.py	2007-04-09 23:33:22.000000000 +0200
@@ -200,8 +200,6 @@
         """
         QAbstractItemModel.__init__(self, parent)
         
-        self.item_dict = {}
-        
         rootData = QVariant(self.trUtf8("Name"))
         self.rootItem = BrowserItem(None, rootData)
         self.rootItem.itemData.append(QVariant(self.trUtf8("VCS Status")))
@@ -253,13 +251,13 @@
         if role == Qt.TextColorRole:
             if index.column() == 0:
                 try:
-                    return self.item_dict[index.internalId()].getTextColor()
+                    return index.internalPointer().getTextColor()
                 except AttributeError:
                     return QVariant()
         elif role == Qt.BackgroundColorRole:
             try:
                 col = self.itemBackgroundColors[\
-                    self.item_dict[index.internalId()].vcsState]
+                    index.internalPointer().vcsState]
                 if col.isValid():
                     return QVariant(col)
                 else:
@@ -337,7 +335,6 @@
         Public method called after a project has been closed.
         """
         self.rootItem.removeChildren()
-        self.item_dict = {}
         self.reset()
         
         # reset the module parser cache
@@ -465,8 +462,7 @@
         if parentItem is None:
             parentItem = self.rootItem
         
-        for childId in parentItem.children():
-            itm = self.item_dict[childId]
+        for itm in parentItem.children():
             if QString(itm.data(column)) == QString(text):
                 return itm
         
@@ -629,28 +625,22 @@
         childItem = self.findChildItem(fname, 0, parentItem)
         if childItem is not None:
             self.beginRemoveRows(parentIndex, childItem.row(), childItem.row())
-            # step 1: remove all children of childItem
-            ids = self.__getChildIds(childItem)
-            for id_ in ids:
-                del self.item_dict[id_]
-            # step 2: remove the childItem
-            parentItem.removeChild(id(childItem))
-            del self.item_dict[id(childItem)]
+            parentItem.removeChild(childItem)
             self.endRemoveRows()
         
-    def __getChildIds(self, item):
-        """
-        Private method to get the ids of all children, grandchildren, ...
-        
-        @param item reference to the item (BrowserItem)
-        """
-        ids = []
-        
-        for id_ in item.children():
-            ids += self.__getChildIds(self.item_dict[id_])
-        
-        ids += item.children()
-        return ids
+##    def __getChildIds(self, item):
+##        """
+##        Private method to get the ids of all children, grandchildren, ...
+##        
+##        @param item reference to the item (BrowserItem)
+##        """
+##        ids = []
+##        
+##        for id_ in item.children():
+##            ids += self.__getChildIds(id_)
+##        
+##        ids += item.children()
+##        return ids
         
     def repopulateItem(self, name):
         """
@@ -668,9 +658,6 @@
         
         index = self.createIndex(itm.row(), 0, itm)
         self.beginRemoveRows(index, 0, itm.childCount() - 1)
-        ids = self.__getChildIds(itm)
-        for id_ in ids:
-            del self.item_dict[id_]
         itm.removeChildren()
         self.endRemoveRows()
         Utilities.ModuleParser.resetParsedModule(os.path.join(self.project.ppath, name))
@@ -776,8 +763,8 @@
             if itm:
                 state = " "
                 for id_ in itm.children():
-                    if state < self.item_dict[id_].vcsState:
-                        state = self.item_dict[id_].vcsState
+                    if state < id_.vcsState:
+                        state = id_.vcsState
                 if state != itm.vcsState:
                     itm.setVcsState(state)
                     index1 = self.createIndex(itm.row(), 0, itm)
@@ -798,7 +785,7 @@
                 continue
             
             self.itemBackgroundColors[code] = color
-            for itm in self.item_dict.values():
+            for itm in self.item_dict:
                 if itm.vcsState == code:
                     index1 = self.createIndex(itm.row(), 0, itm)
                     index2 = self.createIndex(itm.row(), 
@@ -811,7 +798,7 @@
         color = Preferences.getProjectBrowserColour("Highlighted")
         if self.highLightColor.name() != color.name():
             self.highLightColor = color
-            for itm in self.item_dict.values():
+            for itm in self.item_dict:
                 if itm.bold:
                     index1 = self.createIndex(itm.row(), 0, itm)
                     index2 = self.createIndex(itm.row(), 
diff -u -ur eric4-snapshot-20070402.old/eric/UI/BrowserModel.py eric4-snapshot-20070402/eric/UI/BrowserModel.py
--- eric4-snapshot-20070402.old/eric/UI/BrowserModel.py	2007-03-18 14:08:11.000000000 +0100
+++ eric4-snapshot-20070402/eric/UI/BrowserModel.py	2007-04-10 00:46:37.000000000 +0200
@@ -41,8 +41,6 @@
         """
         QAbstractItemModel.__init__(self, parent)
         
-        self.item_dict = {}
-        
         rootData = QVariant(self.trUtf8("Name"))
         self.rootItem = BrowserItem(None, rootData)
         
@@ -57,7 +55,12 @@
         @param parent index of parent item (QModelIndex)
         @return number of columns (integer)
         """
-        return self.rootItem.columnCount() + 1
+        if parent.isValid():
+            item = parent.internalPointer()
+        else:
+            item = self.rootItem
+        
+        return item.columnCount()
     
     def data(self, index, role):
         """
@@ -71,12 +74,15 @@
             return QVariant()
         
         if role == Qt.DisplayRole:
-            item = self.item_dict[index.internalId()]
+            item = index.internalPointer()
             if index.column() < item.columnCount():
                 return QVariant(item.data(index.column()))
+            elif index.column() == item.columnCount() and index.column() < self.columnCount(self.parent(index)):
+               # This is for the case where an item under a multi-column parent doesn't have a value for all the columns
+                return QVariant("")
         elif role == Qt.DecorationRole:
             if index.column() == 0:
-                return QVariant(self.item_dict[index.internalId()].getIcon())
+                return QVariant(index.internalPointer().getIcon())
 ##        elif role == Qt.ToolTipRole:
 ##            if index.column() == 0:
 ##                return QVariant(self.item_dict[index.internalId()].data(index.column()))
@@ -121,22 +127,21 @@
         @param parent index of parent item (QModelIndex)
         @return index object (QModelIndex)
         """
+        # The model/view framework considers negative values out-of-bounds, however in python
+        # they work when indexing into lists. So make sure we return an invalid index for out-of-bounds row/col
+        if row < 0 or column < 0 or row >= self.rowCount(parent) or column >= self.columnCount(parent):
+            return QModelIndex()
         if not parent.isValid():
             parentItem = self.rootItem
         else:
-            try:
-                parentItem = self.item_dict[parent.internalId()]
-            except KeyError:
-                return QModelIndex()
+            parentItem = parent.internalPointer()
         
         try:
             if not parentItem.isPopulated():
                 self.populateItem(parentItem)
-            childItem = self.item_dict[parentItem.child(row)]
+            childItem = parentItem.child(row)
         except IndexError:
             childItem = None
-        except KeyError:
-            childItem = None
         if childItem:
             return self.createIndex(row, column, childItem)
         else:
@@ -152,7 +157,8 @@
         if not index.isValid():
             return QModelIndex()
         
-        childItem = self.item_dict[index.internalId()]
+        childItem = index.internalPointer()
+        
         parentItem = childItem.parent()
         
         if parentItem == self.rootItem:
@@ -167,10 +173,13 @@
         @param parent index of parent item (QModelIndex)
         @return number of rows (integer)
         """
+        # Only the first column should have children
+        if parent.column() > 0:
+            return 0
         if not parent.isValid():
             parentItem = self.rootItem
         else:
-            parentItem = self.item_dict[parent.internalId()]
+            parentItem = parent.internalPointer()
             if not parentItem.isPopulated():    # lazy population
                 self.populateItem(parentItem)
         
@@ -186,19 +195,24 @@
         @param parent index of parent item (QModelIndex)
         @return flag indicating the presence of child items (boolean)
         """
+        # Only the first column should have children
+        if parent.column() > 0:
+            return 0
+            
         if not parent.isValid():
             return self.rootItem.childCount() > 0
-        elif self.item_dict[parent.internalId()].isLazyPopulated():
+        
+                
+        if parent.internalPointer().isLazyPopulated():
             return True
         else:
-            return self.item_dict[parent.internalId()].childCount() > 0
+            return parent.internalPointer().childCount() > 0
 
     def clear(self):
         """
         Public method to clear the model.
         """
         self.rootItem.removeChildren()
-        self.item_dict = {}
         self.reset()
     
     def item(self, index):
@@ -210,8 +224,8 @@
         """
         if not index.isValid():
             return None
-        
-        return self.item_dict[index.internalId()]
+            
+        return index.internalPointer()
     
     def __populateModel(self):
         """
@@ -243,11 +257,10 @@
                 return
             
             # remove old entry
-            id_ = id(self.progDir)
+            id_ = self.progDir
             self.beginRemoveRows(QModelIndex(), self.progDir.row(), self.progDir.row())
             self.rootItem.removeChild(id_)
             self.endRemoveRows()
-            del self.item_dict[id_]
             self.progDir = None
             
         itm = BrowserDirectoryItem(self.rootItem, dirname)
@@ -273,14 +286,13 @@
         """
         if not index.isValid():
             return
+        item = index.internalPointer()
         
-        id_ = index.internalId()
         self.beginRemoveRows(index.parent(), index.row(), index.row())
-        self.rootItem.removeChild(id_)
+        self.rootItem.removeChild(item)
         self.endRemoveRows()
         
-        self.toplevelDirs.removeAll(self.item_dict[id_].dirName())
-        del self.item_dict[id_]
+        self.toplevelDirs.removeAll(item.dirName())
         
     def saveToplevelDirs(self):
         """
@@ -296,9 +308,7 @@
         @param itm reference to item to add (BrowserItem)
         @param parentItem reference to item to add to (BrowserItem)
         """
-        id_ = id(itm)
-        self.item_dict[id_] = itm
-        parentItem.appendChild(id_)
+        parentItem.appendChild( itm )
     
     def addItem(self, itm, parent = QModelIndex()):
         """
@@ -310,7 +320,8 @@
         if not parent.isValid():
             parentItem = self.rootItem
         else:
-            parentItem = self.item_dict[parent.internalId()]
+            parentItem = parent.internalPointer()
+            
         cnt = parentItem.childCount()
         self.beginInsertRows(parent, cnt, cnt)
         self._addItem(itm, parentItem)
@@ -604,7 +615,7 @@
         
         @return row number (integer)
         """
-        return self.parentItem.childItems.index(id(self))
+        return self.parentItem.childItems.index(self)
         
     def type(self):
         """
_______________________________________________
Eric mailing list
[email protected]
http://www.riverbankcomputing.com/mailman/listinfo/eric

Reply via email to