def mouseReleaseEvent(self, event): QtGui.QGraphicsScene.mouseReleaseEvent(self, event) if event.isAccepted() == False: if event.button() == QtCore.Qt.LeftButton: from connectionHook import ConnectionHook if ConnectionHook._outstandingDraggingConnection: ConnectionHook._removeOutstandingDraggingConnection() elif event.button() == QtCore.Qt.RightButton: self._parentNodeUi().showRightClickMenu() self._selectionChanged()
def __init__(self, startHook, endHook = None): QtGui.QGraphicsItem.__init__(self, None, startHook.scene()) self._rect = QtCore.QRectF(0, 0, 0, 0) self._startHook = weakref.ref(startHook) self._endHook = None self._color = startHook.colorRef() self._pen = QtGui.QPen(self._color) self._pen.setWidth(1) self._isTempConnection = False self._path = QtGui.QPainterPath() startHook.addConnection(self) if endHook: self._endHook = weakref.ref(endHook) endHook.addConnection(self) else: # creating a dummy endHook for temporary connection dragging self._endHook = weakref.ref(ConnectionHook(startHook.parentAttributeUi(), parentItem = self)) self._endHook().boundingRect().setSize(QtCore.QSizeF(2.0, 2.0)) self._isTempConnection = True self.updateStartPos() self.setZValue(-1.0)
def __init__(self, coralAttribute, parentNodeUi): QtGui.QGraphicsWidget.__init__(self, parentNodeUi) self._coralAttribute = weakref.ref(coralAttribute) self._coralAttributeId = coralAttribute.id() self._parentNodeUi = weakref.ref(parentNodeUi) self._spacerConstant = 5.0 self._label = QtGui.QGraphicsSimpleTextItem(self) self._inputHook = None self._outputHook = None self._proxy = None self._attributeSpecializedObserver = Observer() self._disconnectedInputObserver = Observer() self._nameChangedObserver = Observer() self._labelSuffix = "" self._labelText = "" if coralAttribute.isInput(): self._inputHook = ConnectionHook(self, isInput=True) else: self._outputHook = ConnectionHook(self, isOutput=True) self._label.setBrush(parentNodeUi.labelsColor()) self._labelText = coralAttribute.name().split(":")[-1] self._label.setText(self._labelText) self.setHooksColor( self.hooksColor(self._coralAttribute().allowedSpecialization())) coralApp.addDisconnectedInputObserver(self._disconnectedInputObserver, coralAttribute, self._disconnected) coralApp.addAttributeSpecializedObserver( self._attributeSpecializedObserver, coralAttribute, self.specialized) coralApp.addNameChangedObserver(self._nameChangedObserver, coralAttribute, self._coralAttributeNameChanged) if coralAttribute.name().startswith("_"): self.setVisible(False) if parentNodeUi.attributesProxyEnabled(): self._enableProxy() self.specialized()
def __init__(self, attributeUi): QtGui.QGraphicsWidget.__init__(self) self._parentNodeUi = weakref.ref(attributeUi.parentNodeUi()) self._attributeUi = weakref.ref(attributeUi) self._shapePen = self._parentNodeUi().shapePen() self._inputHook = None self._outputHook = None self._label = QtGui.QGraphicsSimpleTextItem(self) self._spacerConstant = 5.0 self._currentMagnifyFactor = 0.0 self._zValue = self.zValue() hookColor = QtGui.QColor(100, 100, 100) if attributeUi.outputHook(): hookColor = attributeUi.outputHook().color() elif attributeUi.inputHook(): hookColor = attributeUi.inputHook().color() if attributeUi.inputHook(): self._outputHook = ConnectionHook(attributeUi, parentItem=self, isOutput=True) self._outputHook.setColor(hookColor) self._outputHook.setFlags( QtGui.QGraphicsItem.ItemSendsScenePositionChanges) else: self._inputHook = ConnectionHook(attributeUi, parentItem=self, isInput=True) self._inputHook.setColor(hookColor) self._inputHook.setFlags( QtGui.QGraphicsItem.ItemSendsScenePositionChanges) self.setFlag(QtGui.QGraphicsItem.ItemIsMovable) self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable) self._label.setBrush(attributeUi.labelColor()) self._label.setText(attributeUi.coralAttribute().name()) self._shapePen.setStyle(QtCore.Qt.NoPen) self.setAcceptHoverEvents(True) self.updateLayout()
def __init__(self, coralAttribute, parentNodeUi): QtGui.QGraphicsWidget.__init__(self, parentNodeUi) self._coralAttribute = weakref.ref(coralAttribute) self._coralAttributeId = coralAttribute.id() self._parentNodeUi = weakref.ref(parentNodeUi) self._spacerConstant = 5.0 self._label = QtGui.QGraphicsSimpleTextItem(self) self._inputHook = None self._outputHook = None self._proxy = None self._attributeSpecializedObserver = Observer() self._disconnectedInputObserver = Observer() self._nameChangedObserver = Observer() self._labelSuffix = "" self._labelText = "" if coralAttribute.isInput(): self._inputHook = ConnectionHook(self, isInput = True) else: self._outputHook = ConnectionHook(self, isOutput = True) self._label.setBrush(parentNodeUi.labelsColor()) self._labelText = coralAttribute.name().split(":")[-1] self._label.setText(self._labelText) self.setHooksColor(self.hooksColor(self._coralAttribute().allowedSpecialization())) coralApp.addDisconnectedInputObserver(self._disconnectedInputObserver, coralAttribute, self._disconnected) coralApp.addAttributeSpecializedObserver(self._attributeSpecializedObserver, coralAttribute, self.specialized) coralApp.addNameChangedObserver(self._nameChangedObserver, coralAttribute, self._coralAttributeNameChanged) if coralAttribute.name().startswith("_"): self.setVisible(False) if parentNodeUi.attributesProxyEnabled(): self._enableProxy() self.specialized()
def __init__(self, attributeUi): QtGui.QGraphicsWidget.__init__(self) self._parentNodeUi = weakref.ref(attributeUi.parentNodeUi()) self._attributeUi = weakref.ref(attributeUi) self._shapePen = self._parentNodeUi().shapePen() self._inputHook = None self._outputHook = None self._label = QtGui.QGraphicsSimpleTextItem(self) self._spacerConstant = 5.0 self._currentMagnifyFactor = 0.0 self._zValue = self.zValue() hookColor = QtGui.QColor(100, 100, 100) if attributeUi.outputHook(): hookColor = attributeUi.outputHook().color() elif attributeUi.inputHook(): hookColor = attributeUi.inputHook().color() if attributeUi.inputHook(): self._outputHook = ConnectionHook(attributeUi, parentItem = self, isOutput = True) self._outputHook.setColor(hookColor) self._outputHook.setFlags(QtGui.QGraphicsItem.ItemSendsScenePositionChanges) else: self._inputHook = ConnectionHook(attributeUi, parentItem = self, isInput = True) self._inputHook.setColor(hookColor) self._inputHook.setFlags(QtGui.QGraphicsItem.ItemSendsScenePositionChanges) self.setFlag(QtGui.QGraphicsItem.ItemIsMovable) self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable) self._label.setBrush(attributeUi.labelColor()) self._label.setText(attributeUi.coralAttribute().name()) self._shapePen.setStyle(QtCore.Qt.NoPen) self.setAcceptHoverEvents(True) self.updateLayout()
class AttributeUiProxy(QtGui.QGraphicsWidget): def __init__(self, attributeUi): QtGui.QGraphicsWidget.__init__(self) self._parentNodeUi = weakref.ref(attributeUi.parentNodeUi()) self._attributeUi = weakref.ref(attributeUi) self._shapePen = self._parentNodeUi().shapePen() self._inputHook = None self._outputHook = None self._label = QtGui.QGraphicsSimpleTextItem(self) self._spacerConstant = 5.0 self._currentMagnifyFactor = 0.0 self._zValue = self.zValue() hookColor = QtGui.QColor(100, 100, 100) if attributeUi.outputHook(): hookColor = attributeUi.outputHook().color() elif attributeUi.inputHook(): hookColor = attributeUi.inputHook().color() if attributeUi.inputHook(): self._outputHook = ConnectionHook(attributeUi, parentItem = self, isOutput = True) self._outputHook.setColor(hookColor) self._outputHook.setFlags(QtGui.QGraphicsItem.ItemSendsScenePositionChanges) else: self._inputHook = ConnectionHook(attributeUi, parentItem = self, isInput = True) self._inputHook.setColor(hookColor) self._inputHook.setFlags(QtGui.QGraphicsItem.ItemSendsScenePositionChanges) self.setFlag(QtGui.QGraphicsItem.ItemIsMovable) self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable) self._label.setBrush(attributeUi.labelColor()) self._label.setText(attributeUi.coralAttribute().name()) self._shapePen.setStyle(QtCore.Qt.NoPen) self.setAcceptHoverEvents(True) self.updateLayout() def _magnifyAnimStep(self, frame): step = frame / nodeView.NodeView._animSteps invStep = 1.0 - step self.setScale((self.scale() * invStep) + ((1.0 * self._currentMagnifyFactor) * step)) if self._inputHook: self._inputHook.updateWorldPos() elif self._outputHook: self._outputHook.updateWorldPos() self.scene().update() def _magnify(self, factor): self._currentMagnifyFactor = factor timer = QtCore.QTimeLine(nodeView.NodeView._animSpeed, self) timer.setFrameRange(0, nodeView.NodeView._animSteps) self.connect(timer, QtCore.SIGNAL("frameChanged(int)"), self._magnifyAnimStep); timer.start() def hoverEnterEvent(self, event): if nodeView.NodeView._lastHoveredItem is not self: if nodeView.NodeView._lastHoveredItem: nodeView.NodeView._lastHoveredItem.hoverLeaveEvent(None) zoom = self.scene().zoom() if zoom < 0.6: factor = 0.7 / zoom self.setTransformOriginPoint(self.rect().center()) self._magnify(factor) nodeView.NodeView._lastHoveredItem = self self.setZValue(9999999) def hoverLeaveEvent(self, event): if nodeView.NodeView._lastHoveredItem is self: self._magnify(1.0) self.setZValue(self._zValue) nodeView.NodeView._lastHoveredItem = None def deleteInputConnection(self): if self._inputHook: if self._inputHook._connections: self._inputHook._connections[0].deleteIt() def onChangedScene(self, scene): pass def deleteIt(self): if self._outputHook: self._outputHook.deleteIt() if self._inputHook: self._inputHook.deleteIt() self.scene().removeItem(self) def coralAttribute(self): return self._attributeUi().coralAttribute() def inputHook(self): return self._inputHook def outputHook(self): return self._outputHook def specialized(self): attributeUi = self._attributeUi() hookColor = QtGui.QColor(100, 100, 100) hookMixedColor = hookColor if attributeUi.outputHook(): hook = attributeUi.outputHook() hookColor = hook.color() hookMixedColor = hook.mixedColor() elif attributeUi.inputHook(): hook = attributeUi.inputHook() hookColor = hook.color() hookMixedColor = hook.mixedColor() hook = None if self._outputHook: hook = self._outputHook elif self._inputHook: hook = self._inputHook hook.setColor(hookColor) hook.setMixedColor(hookMixedColor) self._label.setText(attributeUi._labelText + attributeUi._labelSuffix) self.updateLayout() self.update() def itemChange(self, change, value): if change == QtGui.QGraphicsItem.ItemSelectedHasChanged: if self.isSelected(): self._shapePen.setStyle(QtCore.Qt.SolidLine) else: self._shapePen.setStyle(QtCore.Qt.NoPen) return value def updateLayout(self): labelHeight = self._label.boundingRect().height() height = self._spacerConstant + labelHeight + self._spacerConstant inputHookOffset = 0 if self._inputHook: hookY = (height - self._inputHook.boundingRect().height()) / 2.0 self._inputHook.setPos(self._spacerConstant, hookY) inputHookOffset = self._inputHook.boundingRect().width() + self._inputHook.pos().x() self._label.setPos(inputHookOffset + self._spacerConstant, (height - labelHeight) / 2.0) outputHookOffset = self._label.pos().x() + self._label.boundingRect().width() if self._outputHook: hookY = (height - self._outputHook.boundingRect().height()) / 2.0 self._outputHook.setPos(outputHookOffset + self._spacerConstant, hookY) outputHookOffset = self._outputHook.pos().x() + self._outputHook.boundingRect().width() self.resize(QtCore.QSizeF(outputHookOffset + self._spacerConstant, height)) def paint(self, painter, option, widget): attributeUi = self._attributeUi() parentNodeUi = self._parentNodeUi() shape = QtGui.QPainterPath() shape.addRoundedRect(self.rect(), 2, 2) painter.setPen(self._shapePen) painter.setBrush(parentNodeUi.color()) painter.drawPath(shape) def data(self): data = {"name": self.coralAttribute().name(), "pos": [self.pos().x(), self.pos().y()]} return data def setData(self, data): if data.has_key("pos"): pos = data["pos"] self.setPos(pos[0], pos[1])
class AttributeUi(QtGui.QGraphicsWidget): def __init__(self, coralAttribute, parentNodeUi): QtGui.QGraphicsWidget.__init__(self, parentNodeUi) self._coralAttribute = weakref.ref(coralAttribute) self._coralAttributeId = coralAttribute.id() self._parentNodeUi = weakref.ref(parentNodeUi) self._spacerConstant = 5.0 self._label = QtGui.QGraphicsSimpleTextItem(self) self._inputHook = None self._outputHook = None self._proxy = None self._attributeSpecializedObserver = Observer() self._disconnectedInputObserver = Observer() self._nameChangedObserver = Observer() self._labelSuffix = "" self._labelText = "" if coralAttribute.isInput(): self._inputHook = ConnectionHook(self, isInput=True) else: self._outputHook = ConnectionHook(self, isOutput=True) self._label.setBrush(parentNodeUi.labelsColor()) self._labelText = coralAttribute.name().split(":")[-1] self._label.setText(self._labelText) self.setHooksColor( self.hooksColor(self._coralAttribute().allowedSpecialization())) coralApp.addDisconnectedInputObserver(self._disconnectedInputObserver, coralAttribute, self._disconnected) coralApp.addAttributeSpecializedObserver( self._attributeSpecializedObserver, coralAttribute, self.specialized) coralApp.addNameChangedObserver(self._nameChangedObserver, coralAttribute, self._coralAttributeNameChanged) if coralAttribute.name().startswith("_"): self.setVisible(False) if parentNodeUi.attributesProxyEnabled(): self._enableProxy() self.specialized() def labelColor(self): return self._label.brush().color() def _coralAttributeNameChanged(self): newName = self.coralAttribute().name().split(":")[-1] self._labelText = newName self._label.setText(self._labelText + self._labelSuffix) self.parentNodeUi().updateLayout() parentNodeScene = self.parentNodeUi().scene() if parentNodeScene: parentNodeScene.update() if self._proxy: self._proxy()._label.setText(self._labelText + self._labelSuffix) self._proxy().updateLayout() self._proxy().scene().update() def label(self): return self._label ''' Override this method to return a more detailed toolTip. ''' def toolTip(self): return self.coralAttribute().shortDebugInfo() def _disconnected(self): if self._inputHook: if self._inputHook._connections: self._inputHook._connections[0].deleteIt() elif self._proxy: self._proxy().deleteInputConnection() def onChangedScene(self, scene): if self._outputHook: self._outputHook.onChangedScene(scene) if self._inputHook: self._inputHook.onChangedScene(scene) if self._proxy: self._proxy().onChangedScene(scene) def connectTo(self, attributeUi): startHook = self._outputHook endHook = attributeUi.inputHook() if endHook is None: proxy = attributeUi.proxy() if proxy: endHook = proxy.inputHook() if startHook is None: if self._proxy: startHook = self._proxy().outputHook() Connection(startHook, endHook) def _enableProxy(self): attr = self._coralAttribute() if attr.isInput() or attr.isPassThrough(): proxy = AttributeUiProxy(self) self._proxy = weakref.ref(proxy) self.parentNodeUi().containedScene().addItem(proxy) def proxy(self): retProxy = None if self._proxy: retProxy = self._proxy() return retProxy def hooksColor(self, specialization): return QtGui.QColor(255, 255, 255) def _updateLabel(self): attr = self.coralAttribute() val = attr.outValue() if attr.isPassThrough(): source = attr.connectedNonPassThrough() if source: val = source.outValue() self._labelSuffix = "" if hasattr(val, "isArray"): if val.isArray(): self._labelSuffix = "[]" self.label().setText(self._labelText + self._labelSuffix) def labelSuffix(self): return self._labelSuffix def specialized(self): self._updateLabel() specialization = self.coralAttribute().specialization() color = self.hooksColor(specialization) mixedColor = False if len(specialization) > 1: mixedColor = True self.setHooksColor(color, mixedColor) self.parentNodeUi().updateLayout() if self._outputHook: for conn in self._outputHook._connections: conn.update() elif self._inputHook: for conn in self._inputHook._connections: conn.update() if self._proxy: self._proxy().specialized() def setHooksColor(self, color, mixedColor=False): hook = None if self._outputHook: hook = self._outputHook else: hook = self._inputHook hook.setColor(color) hook.setMixedColor(mixedColor) def coralAttribute(self): return self._coralAttribute() def parentNodeUi(self): parent = None if self._parentNodeUi: parent = self._parentNodeUi() return parent def allowConnectionTo(self, targetAttributeUi, errorObject): allow = NetworkManager.allowConnection( self._coralAttribute(), targetAttributeUi.coralAttribute(), errorObject) return allow def inputHook(self): return self._inputHook def outputHook(self): return self._outputHook def updateLayout(self): height = self._label.boundingRect().height() hookY = 0 if self._outputHook: hookY = (height - self._outputHook.boundingRect().height()) / 2.0 elif self._inputHook: hookY = (height - self._inputHook.boundingRect().height()) / 2.0 inputHookWidth = self._spacerConstant * 2.0 if self._inputHook: self._inputHook.setPos(0.0, hookY) self._label.setPos(inputHookWidth + self._spacerConstant, 0) if self._outputHook: self._outputHook.setPos( self._label.pos().x() + self._label.boundingRect().width() + self._spacerConstant, hookY) self.resize( self._outputHook.pos().x() + self._outputHook.boundingRect().width(), height) else: self.resize( self._label.pos().x() + self._label.boundingRect().width(), height) def setParentNodeUi(self, nodeUi): self.setParentItem(None) if self.scene(): if self in self.scene().items(): self.scene().removeItem(self) if self._parentNodeUi: parentNodeUi = self._parentNodeUi() if self in parentNodeUi._attributeUis: del parentNodeUi._attributeUis[ parentNodeUi._attributeUis.index(self)] if nodeUi: self._parentNodeUi = weakref.ref(nodeUi) self.setParentItem(nodeUi) if self not in nodeUi._attributeUis: nodeUi._attributeUis.append(self) else: self._parentNodeUi = None def deleteIt(self): if self._outputHook: self._outputHook.deleteIt() elif self._inputHook: self._inputHook.deleteIt() if self._proxy: self._proxy().deleteIt() self.setParentNodeUi(None) if nodeEditor.NodeEditor._attributeUis.has_key(self._coralAttributeId): del nodeEditor.NodeEditor._attributeUis[self._coralAttributeId]
class AttributeUi(QtGui.QGraphicsWidget): def __init__(self, coralAttribute, parentNodeUi): QtGui.QGraphicsWidget.__init__(self, parentNodeUi) self._coralAttribute = weakref.ref(coralAttribute) self._coralAttributeId = coralAttribute.id() self._parentNodeUi = weakref.ref(parentNodeUi) self._spacerConstant = 5.0 self._label = QtGui.QGraphicsSimpleTextItem(self) self._inputHook = None self._outputHook = None self._proxy = None self._attributeSpecializedObserver = Observer() self._disconnectedInputObserver = Observer() self._nameChangedObserver = Observer() self._labelSuffix = "" self._labelText = "" if coralAttribute.isInput(): self._inputHook = ConnectionHook(self, isInput = True) else: self._outputHook = ConnectionHook(self, isOutput = True) self._label.setBrush(parentNodeUi.labelsColor()) self._labelText = coralAttribute.name().split(":")[-1] self._label.setText(self._labelText) self.setHooksColor(self.hooksColor(self._coralAttribute().allowedSpecialization())) coralApp.addDisconnectedInputObserver(self._disconnectedInputObserver, coralAttribute, self._disconnected) coralApp.addAttributeSpecializedObserver(self._attributeSpecializedObserver, coralAttribute, self.specialized) coralApp.addNameChangedObserver(self._nameChangedObserver, coralAttribute, self._coralAttributeNameChanged) if coralAttribute.name().startswith("_"): self.setVisible(False) if parentNodeUi.attributesProxyEnabled(): self._enableProxy() self.specialized() def labelColor(self): return self._label.brush().color() def _coralAttributeNameChanged(self): newName = self.coralAttribute().name().split(":")[-1] self._labelText = newName self._label.setText(self._labelText + self._labelSuffix) self.parentNodeUi().updateLayout() parentNodeScene = self.parentNodeUi().scene() if parentNodeScene: parentNodeScene.update() if self._proxy: self._proxy()._label.setText(self._labelText + self._labelSuffix) self._proxy().updateLayout() self._proxy().scene().update() def label(self): return self._label ''' Override this method to return a more detailed toolTip. ''' def toolTip(self): return self.coralAttribute().shortDebugInfo() def _disconnected(self): if self._inputHook: if self._inputHook._connections: self._inputHook._connections[0].deleteIt() elif self._proxy: self._proxy().deleteInputConnection() def onChangedScene(self, scene): if self._outputHook: self._outputHook.onChangedScene(scene) if self._inputHook: self._inputHook.onChangedScene(scene) if self._proxy: self._proxy().onChangedScene(scene) def connectTo(self, attributeUi): startHook = self._outputHook endHook = attributeUi.inputHook() if endHook is None: proxy = attributeUi.proxy() if proxy: endHook = proxy.inputHook() if startHook is None: if self._proxy: startHook = self._proxy().outputHook() Connection(startHook, endHook) def _enableProxy(self): attr = self._coralAttribute() if attr.isInput() or attr.isPassThrough(): proxy = AttributeUiProxy(self) self._proxy = weakref.ref(proxy) self.parentNodeUi().containedScene().addItem(proxy) def proxy(self): retProxy = None if self._proxy: retProxy = self._proxy() return retProxy def hooksColor(self, specialization): return QtGui.QColor(255, 255, 255) def _updateLabel(self): attr = self.coralAttribute() val = attr.outValue() if attr.isPassThrough(): source = attr.connectedNonPassThrough() if source: val = source.outValue() self._labelSuffix = "" if hasattr(val, "isArray"): if val.isArray(): self._labelSuffix = "[]" self.label().setText(self._labelText + self._labelSuffix) def labelSuffix(self): return self._labelSuffix def specialized(self): self._updateLabel() specialization = self.coralAttribute().specialization() color = self.hooksColor(specialization) mixedColor = False if len(specialization) > 1: mixedColor = True self.setHooksColor(color, mixedColor) self.parentNodeUi().updateLayout() if self._outputHook: for conn in self._outputHook._connections: conn.update() elif self._inputHook: for conn in self._inputHook._connections: conn.update() if self._proxy: self._proxy().specialized() def setHooksColor(self, color, mixedColor = False): hook = None if self._outputHook: hook = self._outputHook else: hook = self._inputHook hook.setColor(color) hook.setMixedColor(mixedColor) def coralAttribute(self): return self._coralAttribute() def parentNodeUi(self): parent = None if self._parentNodeUi: parent = self._parentNodeUi() return parent def allowConnectionTo(self, targetAttributeUi, errorObject): allow = NetworkManager.allowConnection(self._coralAttribute(), targetAttributeUi.coralAttribute(), errorObject) return allow def inputHook(self): return self._inputHook def outputHook(self): return self._outputHook def updateLayout(self): height = self._label.boundingRect().height() hookY = 0 if self._outputHook: hookY = (height - self._outputHook.boundingRect().height()) / 2.0 elif self._inputHook: hookY = (height - self._inputHook.boundingRect().height()) / 2.0 inputHookWidth = self._spacerConstant * 2.0 if self._inputHook: self._inputHook.setPos(0.0, hookY) self._label.setPos(inputHookWidth + self._spacerConstant, 0) if self._outputHook: self._outputHook.setPos(self._label.pos().x() + self._label.boundingRect().width() + self._spacerConstant, hookY) self.resize(self._outputHook.pos().x() + self._outputHook.boundingRect().width(), height) else: self.resize(self._label.pos().x() + self._label.boundingRect().width(), height) def setParentNodeUi(self, nodeUi): self.setParentItem(None) if self.scene(): if self in self.scene().items(): self.scene().removeItem(self) if self._parentNodeUi: parentNodeUi = self._parentNodeUi() if self in parentNodeUi._attributeUis: del parentNodeUi._attributeUis[parentNodeUi._attributeUis.index(self)] if nodeUi: self._parentNodeUi = weakref.ref(nodeUi) self.setParentItem(nodeUi) if self not in nodeUi._attributeUis: nodeUi._attributeUis.append(self) else: self._parentNodeUi = None def deleteIt(self): if self._outputHook: self._outputHook.deleteIt() elif self._inputHook: self._inputHook.deleteIt() if self._proxy: self._proxy().deleteIt() self.setParentNodeUi(None) if nodeEditor.NodeEditor._attributeUis.has_key(self._coralAttributeId): del nodeEditor.NodeEditor._attributeUis[self._coralAttributeId]
class AttributeUiProxy(QtGui.QGraphicsWidget): def __init__(self, attributeUi): QtGui.QGraphicsWidget.__init__(self) self._parentNodeUi = weakref.ref(attributeUi.parentNodeUi()) self._attributeUi = weakref.ref(attributeUi) self._shapePen = self._parentNodeUi().shapePen() self._inputHook = None self._outputHook = None self._label = QtGui.QGraphicsSimpleTextItem(self) self._spacerConstant = 5.0 self._currentMagnifyFactor = 0.0 self._zValue = self.zValue() hookColor = QtGui.QColor(100, 100, 100) if attributeUi.outputHook(): hookColor = attributeUi.outputHook().color() elif attributeUi.inputHook(): hookColor = attributeUi.inputHook().color() if attributeUi.inputHook(): self._outputHook = ConnectionHook(attributeUi, parentItem=self, isOutput=True) self._outputHook.setColor(hookColor) self._outputHook.setFlags( QtGui.QGraphicsItem.ItemSendsScenePositionChanges) else: self._inputHook = ConnectionHook(attributeUi, parentItem=self, isInput=True) self._inputHook.setColor(hookColor) self._inputHook.setFlags( QtGui.QGraphicsItem.ItemSendsScenePositionChanges) self.setFlag(QtGui.QGraphicsItem.ItemIsMovable) self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable) self._label.setBrush(attributeUi.labelColor()) self._label.setText(attributeUi.coralAttribute().name()) self._shapePen.setStyle(QtCore.Qt.NoPen) self.setAcceptHoverEvents(True) self.updateLayout() def _magnifyAnimStep(self, frame): step = frame / nodeView.NodeView._animSteps invStep = 1.0 - step self.setScale((self.scale() * invStep) + ((1.0 * self._currentMagnifyFactor) * step)) if self._inputHook: self._inputHook.updateWorldPos() elif self._outputHook: self._outputHook.updateWorldPos() self.scene().update() def _magnify(self, factor): self._currentMagnifyFactor = factor timer = QtCore.QTimeLine(nodeView.NodeView._animSpeed, self) timer.setFrameRange(0, nodeView.NodeView._animSteps) self.connect(timer, QtCore.SIGNAL("frameChanged(int)"), self._magnifyAnimStep) timer.start() def hoverEnterEvent(self, event): if nodeView.NodeView._lastHoveredItem is not self: if nodeView.NodeView._lastHoveredItem: nodeView.NodeView._lastHoveredItem.hoverLeaveEvent(None) zoom = self.scene().zoom() if zoom < 0.6: factor = 0.7 / zoom self.setTransformOriginPoint(self.rect().center()) self._magnify(factor) nodeView.NodeView._lastHoveredItem = self self.setZValue(9999999) def hoverLeaveEvent(self, event): if nodeView.NodeView._lastHoveredItem is self: self._magnify(1.0) self.setZValue(self._zValue) nodeView.NodeView._lastHoveredItem = None def deleteInputConnection(self): if self._inputHook: if self._inputHook._connections: self._inputHook._connections[0].deleteIt() def onChangedScene(self, scene): pass def deleteIt(self): if self._outputHook: self._outputHook.deleteIt() if self._inputHook: self._inputHook.deleteIt() self.scene().removeItem(self) def coralAttribute(self): return self._attributeUi().coralAttribute() def inputHook(self): return self._inputHook def outputHook(self): return self._outputHook def specialized(self): attributeUi = self._attributeUi() hookColor = QtGui.QColor(100, 100, 100) hookMixedColor = hookColor if attributeUi.outputHook(): hook = attributeUi.outputHook() hookColor = hook.color() hookMixedColor = hook.mixedColor() elif attributeUi.inputHook(): hook = attributeUi.inputHook() hookColor = hook.color() hookMixedColor = hook.mixedColor() hook = None if self._outputHook: hook = self._outputHook elif self._inputHook: hook = self._inputHook hook.setColor(hookColor) hook.setMixedColor(hookMixedColor) self._label.setText(attributeUi._labelText + attributeUi._labelSuffix) self.updateLayout() self.update() def itemChange(self, change, value): if change == QtGui.QGraphicsItem.ItemSelectedHasChanged: if self.isSelected(): self._shapePen.setStyle(QtCore.Qt.SolidLine) else: self._shapePen.setStyle(QtCore.Qt.NoPen) return value def updateLayout(self): labelHeight = self._label.boundingRect().height() height = self._spacerConstant + labelHeight + self._spacerConstant inputHookOffset = 0 if self._inputHook: hookY = (height - self._inputHook.boundingRect().height()) / 2.0 self._inputHook.setPos(self._spacerConstant, hookY) inputHookOffset = self._inputHook.boundingRect().width( ) + self._inputHook.pos().x() self._label.setPos(inputHookOffset + self._spacerConstant, (height - labelHeight) / 2.0) outputHookOffset = self._label.pos().x() + self._label.boundingRect( ).width() if self._outputHook: hookY = (height - self._outputHook.boundingRect().height()) / 2.0 self._outputHook.setPos(outputHookOffset + self._spacerConstant, hookY) outputHookOffset = self._outputHook.pos().x( ) + self._outputHook.boundingRect().width() self.resize( QtCore.QSizeF(outputHookOffset + self._spacerConstant, height)) def paint(self, painter, option, widget): attributeUi = self._attributeUi() parentNodeUi = self._parentNodeUi() shape = QtGui.QPainterPath() shape.addRoundedRect(self.rect(), 2, 2) painter.setPen(self._shapePen) painter.setBrush(parentNodeUi.color()) painter.drawPath(shape) def data(self): data = { "name": self.coralAttribute().name(), "pos": [self.pos().x(), self.pos().y()] } return data def setData(self, data): if data.has_key("pos"): pos = data["pos"] self.setPos(pos[0], pos[1])