UI/BrowserModel.py

changeset 5656
9c21b2746218
parent 5624
cdd346d8858b
child 5708
9b01b4004314
equal deleted inserted replaced
5654:d75dfc0d10f2 5656:9c21b2746218
66 rootData = QCoreApplication.translate("BrowserModel", "Name") 66 rootData = QCoreApplication.translate("BrowserModel", "Name")
67 self.rootItem = BrowserItem(None, rootData) 67 self.rootItem = BrowserItem(None, rootData)
68 68
69 self.__populateModel() 69 self.__populateModel()
70 70
71 def columnCount(self, parent=QModelIndex()): 71 def columnCount(self, parent=None):
72 """ 72 """
73 Public method to get the number of columns. 73 Public method to get the number of columns.
74 74
75 @param parent index of parent item (QModelIndex) 75 @param parent index of parent item (QModelIndex)
76 @return number of columns (integer) 76 @return number of columns (integer)
77 """ 77 """
78 if parent is None:
79 parent = QModelIndex()
80
78 if parent.isValid(): 81 if parent.isValid():
79 item = parent.internalPointer() 82 item = parent.internalPointer()
80 else: 83 else:
81 item = self.rootItem 84 item = self.rootItem
82 85
141 else: 144 else:
142 return self.rootItem.data(section) 145 return self.rootItem.data(section)
143 146
144 return None 147 return None
145 148
146 def index(self, row, column, parent=QModelIndex()): 149 def index(self, row, column, parent=None):
147 """ 150 """
148 Public method to create an index. 151 Public method to create an index.
149 152
150 @param row row number of the new index (integer) 153 @param row row number of the new index (integer)
151 @param column column number of the new index (integer) 154 @param column column number of the new index (integer)
152 @param parent index of parent item (QModelIndex) 155 @param parent index of parent item (QModelIndex)
153 @return index object (QModelIndex) 156 @return index object (QModelIndex)
154 """ 157 """
158 if parent is None:
159 parent = QModelIndex()
160
155 # The model/view framework considers negative values out-of-bounds, 161 # The model/view framework considers negative values out-of-bounds,
156 # however in python they work when indexing into lists. So make sure 162 # however in python they work when indexing into lists. So make sure
157 # we return an invalid index for out-of-bounds row/col 163 # we return an invalid index for out-of-bounds row/col
158 if row < 0 or column < 0 or \ 164 if row < 0 or column < 0 or \
159 row >= self.rowCount(parent) or column >= self.columnCount(parent): 165 row >= self.rowCount(parent) or column >= self.columnCount(parent):
191 if parentItem == self.rootItem: 197 if parentItem == self.rootItem:
192 return QModelIndex() 198 return QModelIndex()
193 199
194 return self.createIndex(parentItem.row(), 0, parentItem) 200 return self.createIndex(parentItem.row(), 0, parentItem)
195 201
196 def rowCount(self, parent=QModelIndex()): 202 def rowCount(self, parent=None):
197 """ 203 """
198 Public method to get the number of rows. 204 Public method to get the number of rows.
199 205
200 @param parent index of parent item (QModelIndex) 206 @param parent index of parent item (QModelIndex)
201 @return number of rows (integer) 207 @return number of rows (integer)
202 """ 208 """
209 if parent is None:
210 parent = QModelIndex()
211
203 # Only the first column should have children 212 # Only the first column should have children
204 if parent.column() > 0: 213 if parent.column() > 0:
205 return 0 214 return 0
206 215
207 if not parent.isValid(): 216 if not parent.isValid():
211 if not parentItem.isPopulated(): # lazy population 220 if not parentItem.isPopulated(): # lazy population
212 self.populateItem(parentItem) 221 self.populateItem(parentItem)
213 222
214 return parentItem.childCount() 223 return parentItem.childCount()
215 224
216 def hasChildren(self, parent=QModelIndex()): 225 def hasChildren(self, parent=None):
217 """ 226 """
218 Public method to check for the presence of child items. 227 Public method to check for the presence of child items.
219 228
220 We always return True for normal items in order to do lazy 229 We always return True for normal items in order to do lazy
221 population of the tree. 230 population of the tree.
222 231
223 @param parent index of parent item (QModelIndex) 232 @param parent index of parent item (QModelIndex)
224 @return flag indicating the presence of child items (boolean) 233 @return flag indicating the presence of child items (boolean)
225 """ 234 """
235 if parent is None:
236 parent = QModelIndex()
237
226 # Only the first column should have children 238 # Only the first column should have children
227 if parent.column() > 0: 239 if parent.column() > 0:
228 return 0 240 return 0
229 241
230 if not parent.isValid(): 242 if not parent.isValid():
481 @param itm reference to item to add (BrowserItem) 493 @param itm reference to item to add (BrowserItem)
482 @param parentItem reference to item to add to (BrowserItem) 494 @param parentItem reference to item to add to (BrowserItem)
483 """ 495 """
484 parentItem.appendChild(itm) 496 parentItem.appendChild(itm)
485 497
486 def addItem(self, itm, parent=QModelIndex()): 498 def addItem(self, itm, parent=None):
487 """ 499 """
488 Public slot to add an item. 500 Public slot to add an item.
489 501
490 @param itm item to add (BrowserItem) 502 @param itm item to add (BrowserItem)
491 @param parent index of parent item (QModelIndex) 503 @param parent index of parent item (QModelIndex)
492 """ 504 """
505 if parent is None:
506 parent = QModelIndex()
507
493 if not parent.isValid(): 508 if not parent.isValid():
494 parentItem = self.rootItem 509 parentItem = self.rootItem
495 else: 510 else:
496 parentItem = parent.internalPointer() 511 parentItem = parent.internalPointer()
497 512

eric ide

mercurial