class DomItem:<br> def __init__(self, node, row, parent=None):<br> self.domNode = node<br> # Record the item's location within its parent.<br> self.rowNumber = row<br> self.parentItem = parent
<br> self.childItems = {}<br><br> def node(self):<br> return self.domNode<br><br> def parent(self):<br> return self.parentItem<br><br> def child(self, i):<br> if self.childItems.has_key
(i):<br> return self.childItems[i]<br><br> if i >= 0 and i < self.domNode.childNodes().count():<br> childNode = self.domNode.childNodes().item(i)<br> childItem = DomItem(childNode, i, self)
<br> self.childItems[i] = childItem<br> return childItem<br><br> return 0<br><br> def row(self):<br> return self.rowNumber<br><br> <br><br><br>class DomModel(QtCore.QAbstractItemModel
):<br> def __init__(self, document, parent = None):<br> QtCore.QAbstractItemModel.__init__(self, parent)<br><br> self.domDocument = document<br><br> self.rootItem = DomItem(self.domDocument, 0)<br>
<br> self.idList=[]<br> self.gotoList=[]<br> self.errorList=[]<br><br> def columnCount(self, parent):<br> return 3<br><br> def data(self, index, role):<br> if not index.isValid():<br>
return QtCore.QVariant()<br><br> if role != QtCore.Qt.DisplayRole:<br> return QtCore.QVariant()<br><br> item = index.internalPointer()<br><br> node = item.node()<br> attributes =
QtCore.QStringList()<br> attributeMap = node.attributes()<br><br> if index.column() == 0:<br> return QtCore.QVariant(node.nodeName())<br> <br> elif index.column() == 1:<br> for i in range(0,
attributeMap.count()):<br> attribute = attributeMap.item(i)<br> attributes.append(attribute.nodeName() + "=\"" + \<br> attribute.nodeValue() + "\"")
<br><br> return QtCore.QVariant(attributes.join(" "))<br> elif index.column() == 2:<br> return QtCore.QVariant(node.nodeValue().split("\n").join(" "))<br> else:
<br> return QtCore.QVariant()<br><br> def flags(self, index):<br> if not index.isValid():<br> return QtCore.Qt.ItemIsEnabled<br><br> return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
<br><br> def headerData(self, section, orientation, role):<br> if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:<br> if section == 0:<br> return QtCore.QVariant(
self.tr("Name"))<br> elif section == 1:<br> return QtCore.QVariant(self.tr("Attributes"))<br> elif section == 2:<br> return QtCore.QVariant(self.tr("Value"))
<br> else:<br> return QtCore.QVariant()<br><br> return QtCore.QVariant()<br><br> def index(self, row, column, parent):<br> if not parent.isValid():<br> parentItem = self.rootItem
<br> else:<br> parentItem = parent.internalPointer()<br><br> childItem = parentItem.child(row)<br> if childItem:<br> return self.createIndex(row, column, childItem)<br> else:
<br> return QtCore.QModelIndex()<br><br> def parent(self, child):<br> if not child.isValid():<br> return QtCore.QModelIndex()<br><br> childItem = child.internalPointer()<br> parentItem =
childItem.parent()<br><br> if not parentItem or parentItem == self.rootItem:<br> return QtCore.QModelIndex()<br><br> return self.createIndex(parentItem.row(), 0, parentItem)<br><br> def rowCount(self, parent):
<br> if not parent.isValid():<br> parentItem = self.rootItem<br> else:<br> parentItem = parent.internalPointer()<br><br> return parentItem.node().childNodes().count()<br><br><br>
def findNodeByTagNameID(self,tagName,id):<br> element = self.domDocument.elementsByTagName(tagName)<br> for e in range(element.length()) :<br> tmp=element.item(e).attributes().namedItem("id").nodeValue()
<br> if tmp==id:<br> return element.item(e)<br><br> return None <br><br> def removeRows(self,arow,count,parent):<br> print "removeRow"<br> self.beginRemoveRows
(parent,arow,arow+count-1)<br> for ii in range(count): <br> nodoIndex=self.index(arow+ii,0,parent)<br> NomeNodo= nodoIndex.data().toString()<br> IdNodo=self.index(arow+ii,1,parent).data().toString()
<br><br> if IdNodo.contains("id"):<br> i=IdNodo.indexOf("id")<br> i=IdNodo.indexOf("\"",i)<br> f=IdNodo.indexOf("\"",i+1)
<br> IdNodo=IdNodo.mid(i+1,f-i-1)<br> else:<br> return False<br> <br> nodo=self.findNodeByTagNameID(NomeNodo,IdNodo)<br> nodo.parentNode
().removeChild(nodo)<br> <br> self.endRemoveRows()<br> return True<br> <br> def validateDom(self,nodo):<br><br> if nodo.isText():<br> return<br><br> if nodo.nodeName
()=="text":<br> return <br><br> ##IDLIST<br> if nodo.hasAttributes()==True and nodo.attributes().contains("id")==True :<br> if nodo.attributes().namedItem("goToSequence"):
<br> goto=nodo.attributes().namedItem("goToSequence").nodeValue()<br> else:<br> goto="none"<br> <br> tmp=MyListElem(nodo.nodeName(),nodo.attributes
().namedItem("id").nodeValue(),goto )<br> self.idList.append(tmp)<br> ##----------------------------------------------<br> <br> ##GOTOLIST<br> if nodo.hasAttributes()==True and
nodo.attributes().contains("goToSequence")==True :<br> tmp=MyListElem(nodo.nodeName(),nodo.attributes().namedItem("id").nodeValue(),nodo.attributes().namedItem("goToSequence").nodeValue())
<br> if tmp.goto!="none":<br> self.gotoList.append(tmp)<br> ##----------------------------------------------<br><br> <br> ##ERRORLIST<br>## if
nodo.hasAttributes()==True and nodo.attributes().contains("question")==True :<br>## if nodo.attributes().namedItem("question").nodeValue()==False and nodo.lastChildElement("answer") != None:
<br>## self.errorList.append(MyListElem(nodo.nodeName(),nodo.attributes().namedItem("id").nodeValue(),nodo.attributes().namedItem("goToSequence").nodeValue()))<br> if nodo.nodeName
()=="dialog":<br> if nodo.attributes().contains("actor")==False or nodo.attributes().contains("id")==False :<br> self.errorList.append(MyListElem(nodo.nodeName(),
nodo.attributes().namedItem("id").nodeValue(),nodo.attributes().namedItem("goToSequence").nodeValue()))<br><br> if nodo.nodeName()=="sentence":<br> if nodo.parentNode().nodeName()!= "dialog":
<br> self.errorList.append(MyListElem(nodo.nodeName(),nodo.attributes().namedItem("id").nodeValue(),nodo.attributes().namedItem("goToSequence").nodeValue()))<br> elif nodo.attributes
().contains("goToSequence")==False or nodo.attributes().contains("lifetime")==False or nodo.attributes().contains("id")==False or nodo.attributes().contains("question")==False:<br>
self.errorList.append(MyListElem(nodo.nodeName(),nodo.attributes().namedItem("id").nodeValue(),nodo.attributes().namedItem("goToSequence").nodeValue()))<br> elif nodo.attributes().namedItem("question").nodeValue()==False and
nodo.lastChildElement("answer") != None:<br> self.errorList.append(MyListElem(nodo.nodeName(),nodo.attributes().namedItem("id").nodeValue(),nodo.attributes().namedItem("goToSequence").nodeValue()))
<br> <br><br> if nodo.nodeName()=="answer":<br> if nodo.parentNode().nodeName()!= "sentence":<br> self.errorList.append(MyListElem(nodo.nodeName(),nodo.attributes
().namedItem("id").nodeValue(),nodo.attributes().namedItem("goToSequence").nodeValue()))<br> elif nodo.attributes().contains("goToSequence")==False or nodo.attributes().contains("execute")==False or
nodo.attributes().contains("id")==False :<br> self.errorList.append(MyListElem(nodo.nodeName(),nodo.attributes().namedItem("id").nodeValue(),nodo.attributes().namedItem("goToSequence").nodeValue()))
<br> <br> ##----------------------------------------------<br> <br> if nodo.hasChildNodes():<br> for count in range(nodo.childNodes().length()):<br> self.validateDom
(nodo.childNodes().at(count))<br><br><br><br><br><br><br><br><br><br>HERE IS WHERE CALL MODEL::REMOVEROWS()<br><br>class Ui_MainWindow(object):<br> def setupUi(self, MainWindow):<br> .<br> .<br> .<br> .<br>
<br><br>def cancellaNodo(self):<br>print self.view.model().removeRows(self.view.selectedIndexes()[0].row(),1,self.view.selectedIndexes()[0].parent())<br> print self.view.model().domDocument.toString() <br><br><br><br><br>
<br><br><div><span class="gmail_quote">2006/9/8, Andreas Pakulat <<a href="mailto:apaku@gmx.de">apaku@gmx.de</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 08.09.06 15:23:50, Oscar Cossu wrote:<br>> def removeRows(self,arow,count,parent):<br>><br>> self.beginRemoveRows(parent,arow,arow+count-1)<br>><br>> for ii in range(count):<br>
> ##code for removing a row from QDomDocument here<br>><br>> self.endRemoveRows()<br>> return True<br>><br>> This is my model class. When i remove a row the qtreeview repaint the tree
<br>> but the row deleted isn't the one i've selected.<br>> example:<br>><br>> document<br>> element1<br>> element2<br>> element3<br>><br>> pick elements2 for removing.<br>> treeview update after removing but show:
<br>><br>> document<br>> element1<br>> element2<br>><br>> Now, if i do:<br>> print qtreeview.model().domDocument.toString()<br>> the result is:<br>><br>> document<br>> element1<br>
> element3<br><br>Your removeRows looks ok. I guess you're either telling the treeview (by<br>false row number) that it should remove the last row, or your data<br>method doesn't fetch the proper row from the document.
<br><br>As you use QDomDocument, your complete model is probably not that large,<br>can you post a complete example, or at least the complete model<br>including QDomItem.<br><br>Andreas<br><br>--<br>You'll feel devilish tonight. Toss dynamite caps under a flamenco dancer's
<br>heel.<br><br>_______________________________________________<br>PyKDE mailing list <a href="mailto:PyKDE@mats.imk.fraunhofer.de">PyKDE@mats.imk.fraunhofer.de</a><br><a href="http://mats.imk.fraunhofer.de/mailman/listinfo/pykde">
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde</a><br></blockquote></div><br>