def classForItem(cls, item): """ Returns the class implementing the given item. :type item: Item :rtype: class """ item = Item.forValue(item) if item is Item.AttributeNode: return AttributeNode if item is Item.ComplementNode: return ComplementNode if item is Item.ConceptNode: return ConceptNode if item is Item.DatatypeRestrictionNode: return DatatypeRestrictionNode if item is Item.DisjointUnionNode: return DisjointUnionNode if item is Item.DomainRestrictionNode: return DomainRestrictionNode if item is Item.EnumerationNode: return EnumerationNode if item is Item.FacetNode: return FacetNode if item is Item.IndividualNode: return IndividualNode if item is Item.IntersectionNode: return IntersectionNode if item is Item.PropertyAssertionNode: return PropertyAssertionNode if item is Item.RangeRestrictionNode: return RangeRestrictionNode if item is Item.RoleNode: return RoleNode if item is Item.RoleChainNode: return RoleChainNode if item is Item.RoleInverseNode: return RoleInverseNode if item is Item.UnionNode: return UnionNode if item is Item.ValueDomainNode: return ValueDomainNode if item is Item.InclusionEdge: return InclusionEdge if item is Item.EquivalenceEdge: return EquivalenceEdge if item is Item.InputEdge: return InputEdge if item is Item.MembershipEdge: return MembershipEdge raise RuntimeError('unknown item type ({0})'.format(item))
def dropEvent(self, dropEvent): """ Executed when a dragged element is dropped on the diagram. :type dropEvent: QGraphicsSceneDragDropEvent """ super().dropEvent(dropEvent) if dropEvent.mimeData().hasFormat('text/plain'): snapToGrid = self.session.action('toggle_grid').isChecked() node = self.factory.create(Item.forValue(dropEvent.mimeData().text())) node.setPos(snap(dropEvent.scenePos(), Diagram.GridSize, snapToGrid)) self.session.undostack.push(CommandNodeAdd(self, node)) self.sgnItemInsertionCompleted.emit(node, dropEvent.modifiers()) dropEvent.setDropAction(QtCore.Qt.CopyAction) dropEvent.accept() else: dropEvent.ignore()
def mousePressEvent(self, mouseEvent): """ Executed when a mouse button is clicked on the scene. :type mouseEvent: QGraphicsSceneMouseEvent """ mouseModifiers = mouseEvent.modifiers() mouseButtons = mouseEvent.buttons() mousePos = mouseEvent.scenePos() if mouseButtons & QtCore.Qt.LeftButton: if self.mode is DiagramMode.NodeAdd: ############################################# # NODE INSERTION ################################# snapToGrid = self.session.action('toggle_grid').isChecked() node = self.factory.create(Item.forValue(self.modeParam)) node.setPos(snap(mousePos, Diagram.GridSize, snapToGrid)) self.session.undostack.push(CommandNodeAdd(self, node)) self.sgnItemInsertionCompleted.emit(node, mouseEvent.modifiers()) elif self.mode is DiagramMode.EdgeAdd: ############################################# # EDGE INSERTION ################################# node = first(self.items(mousePos, edges=False)) if node: edge = self.factory.create(Item.forValue(self.modeParam), source=node) edge.updateEdge(target=mousePos) self.mp_Edge = edge self.addItem(edge) else: # Execute super at first since this may change the diagram # mode: some actions are directly handle by graphics items # (i.e: edge breakpoint move, edge anchor move, node shape # resize) and we need to check whether any of them is being # performed before handling the even locally. super().mousePressEvent(mouseEvent) if self.mode is DiagramMode.Idle: if mouseModifiers & QtCore.Qt.ShiftModifier: ############################################# # LABEL MOVE ################################# item = first(self.items(mousePos, nodes=False, edges=False, labels=True)) if item and item.isMovable(): self.clearSelection() self.mp_Label = item self.mp_LabelPos = item.pos() self.mp_Pos = mousePos self.setMode(DiagramMode.LabelMove) else: ############################################# # ITEM SELECTION ################################# item = first(self.items(mousePos, labels=True)) if item: if item.isLabel(): # If we are hitting a label, check whether the label # is overlapping it's parent item and such item is # also intersecting the current mouse position: if so, # use the parent item as placeholder for the selection. parent = item.parentItem() items = self.items(mousePos) item = parent if parent in items else None if item: if mouseModifiers & QtCore.Qt.ControlModifier: # CTRL => support item multi selection. item.setSelected(not item.isSelected()) else: if self.selectedItems(): # Some elements have been already selected in the # diagram, during a previous mouse press event. if not item.isSelected(): # There are some items selected but we clicked # on a node which is not currently selected, so # make this node the only selected one. self.clearSelection() item.setSelected(True) else: # No item (nodes or edges) is selected and we just # clicked on one so make sure to select this item and # because selectedItems() filters out item Label's, # clear out the selection on the diagram. self.clearSelection() item.setSelected(True) # If we have some nodes selected we need to prepare data for a # possible item move operation: we need to make sure to retrieve # the node below the mouse cursor that will act as as mouse grabber # to compute delta movements for each component in the selection. selected = self.selectedNodes() if selected: self.mp_Node = first(self.items(mousePos, edges=False)) if self.mp_Node: self.mp_NodePos = self.mp_Node.pos() self.mp_Pos = mousePos self.mp_Data = { 'nodes': { node: { 'anchors': {k: v for k, v in node.anchors.items()}, 'pos': node.pos(), } for node in selected}, 'edges': {} } # Figure out if the nodes we are moving are sharing edges: # if that's the case, move the edge together with the nodes # (which actually means moving the edge breakpoints). for node in self.mp_Data['nodes']: for edge in node.edges: if edge not in self.mp_Data['edges']: if edge.other(node).isSelected(): self.mp_Data['edges'][edge] = edge.breakpoints[:]