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