python - PyQt Node interface - Parrent to ItemIsMovable object -
so building node based interface using pyqt project working on , having issues getting objects belong base not follow in space. when user drags base node, child objects (inputs , output boxes) follow it. have drag-able node works child objects not following properly. ideas?
#!/usr/bin/python # -*- coding: utf-8 -*- """ base py file gui """ import sys pyqt4 import qtgui, qtcore array import * """ base class node. contains initialization, drawing, , containing inputs , outputs """ class node(): width = 100 height = 100 color = 1 x = 90 y = 60 inputs=[] outputs=[] def __init__(self, nwidth, nheight): self.width = nwidth self.height = nheight self.ininodedata() """ inputs , outputs created """ def ininodedata(self): j in range(5): = self x = input(this,90, 0+(j*10)) self.inputs.append(x) """draw node input , output objects""" def draw(self, drawobj): item = drawobj.addrect(self.x, self.y, self.width, self.height) item.setflag(qtgui.qgraphicsitem.itemismovable) curinput in self.inputs: curinput.draw(drawobj) print("(", self.x, ",", self.y, ")") """ nodes evaluate last node first node, therefore inputs evaluted """ class input(): currentconnectednode = none currentconnectedoutput = none parentnode = none width = 10 height = 10 x = 1 y = 1 color = 1 def __init__(self, pnode, posx, posy): self.parentnode = pnode self.x = posx self.y = posy self.color = 1 def draw(self, drawobj): item = drawobj.addrect(self.x+self.parentnode.x, self.y+self.parentnode.y, self.width, self.height) class output(): parentnode = none class mainwindow(qtgui.qgraphicsview): nodes = [] def __init__(self): super(mainwindow, self).__init__() self.initui() def initui(self): j in range(1): x = node(100,100) self.nodes.append(x) self.setscene(qtgui.qgraphicsscene()) self.setwindowtitle('ris rib generator') self.setgeometry(800, 600, 800, 850) self.initnodes() self.show() def initnodes(self): curnode in self.nodes: curnode.draw(self.scene()) def main(): app = qtgui.qapplication(sys.argv) mainwindow = mainwindow() mainwindow.show() app.exec_() if __name__ == '__main__': main()
ok after week figured out. need few things.
ensure flags correct:
self.setflag(qtgui.qgraphicsitem.itemismovable, true) self.setflag(qtgui.qgraphicsitem.itemisselectable, true)
once set can use built in event handelers these on ride original ones. inside user defined one, need call event handeler base class. example:
def mousepressevent(self, e): print("square got mouse press event") print("event came accepted: %s"%(e.isaccepted(),)) qtgui.qgraphicsrectitem.mousepressevent(self, e)
here working example of progress.
#!/usr/bin/python # -*- coding: utf-8 -*- """ base py file gui todo list ----------------- - pop menu adding new nodes - node connectivity - create data structure storing """ import sys pyqt4 import qtgui, qtcore array import * """ base class node. contains inilization, drawing, , containing inputs , outputs """ class node(qtgui.qgraphicsrectitem): width = 100 height = 100 color = 1 x = 90 y = 60 inputs=[] outputs=[] viewobj = none def __init__(self, n_x, n_y, n_width,n_height): qtgui.qgraphicsrectitem.__init__(self, n_x, n_y, n_width, n_height) self.width = n_width self.height = n_height self.x = n_x self.y = n_y self.setflag(qtgui.qgraphicsitem.itemismovable, true) self.setflag(qtgui.qgraphicsitem.itemisselectable, true) self.ininodedata() def mousepressevent(self, e): print("square got mouse press event") print("event came accepted: %s"%(e.isaccepted(),)) qtgui.qgraphicsrectitem.mousepressevent(self, e) def mousereleaseevent(self, e): print("square got mouse release event") print("event came accepted: %s"%(e.isaccepted(),)) qtgui.qgraphicsrectitem.mousereleaseevent(self, e) """ inputs , outputs created based on node type """ def ininodedata(self): print('making node data') j in range(5): = self x = input(this,0, 0+(j*10)) self.inputs.append(x) k in range(5): = self x = output(this,self.x+self.width, self.y+(k*10)) self.outputs.append(x) def mousemoveevent(self, event): print('dragging@') qtgui.qgraphicsrectitem.mousemoveevent(self, event) def mousepressevent(self, event): print('moving!') """ nodes evaluate last node first node, therefore inputs evaluted """ class input(qtgui.qgraphicsrectitem): currentconnectednode = none currentconnectedoutput = none parentnode = none width = 10 height = 10 x = 1 y = 1 color = 1 drawitem = none def __init__(self, pnode, posx, posy): self.parentnode = pnode self.x = posx self.y = posy self.color = 1 qtgui.qgraphicsrectitem.__init__(self, self.x+self.parentnode.x, self.y+self.parentnode.y, self.width, self.height, self.parentnode) ''' output value node ''' class output(node): parentnode = none width = 10 height = 10 x = 1 y = 1 def __init__(self, pnode, posx, posy): self.parentnode = pnode self.x = posx self.y = posy self.color = 1 qtgui.qgraphicsrectitem.__init__(self, self.x-self.width, self.y, self.width, self.height, self.parentnode) ''' check click events on scene object ''' class scene(qtgui.qgraphicsscene): nodes = [] def mousepressevent(self, e): print("scene got mouse press event") print("event came accepted: %s"%(e.isaccepted(),)) qtgui.qgraphicsscene.mousepressevent(self, e) def mousereleaseevent(self, e): print("scene got mouse release event") print("event came accepted: %s"%(e.isaccepted(),)) qtgui.qgraphicsscene.mousereleaseevent(self, e) def dragmoveevent(self, e): print('scene got drag move event') def addnode(self): newnode = self.additem(node(250,250,100,150)) self.nodes.append(newnode) ''' main window object ''' class mainwindowui(qtgui.qmainwindow): def __init__(self): qtgui.qmainwindow.__init__(self) self.setwindowtitle('ris rib generator') self.scene = scene(0, 0, 800, 850, self) self.view = qtgui.qgraphicsview() self.setcentralwidget(self.view) self.view.setscene(self.scene) exitaction = qtgui.qaction(qtgui.qicon('exit24.png'), 'exit', self) exitaction.setshortcut('ctrl+q') exitaction.setstatustip('exit application') exitaction.triggered.connect(self.close) newnodeaction = qtgui.qaction(qtgui.qicon('exit24.png'), 'new node', self) newnodeaction.setstatustip('add blank node') newnodeaction.triggered.connect(self.scene.addnode) self.statusbar() menubar = self.menubar() filemenu = menubar.addmenu('&file') filemenu.addaction(newnodeaction) filemenu.addaction(exitaction) ''' start point ''' if __name__ == '__main__': app = qtgui.qapplication(sys.argv) win = mainwindowui() win.show() sys.exit(app.exec_())
Comments
Post a Comment