def __init__(self, flow, items): super(RemoveComponents_Command, self).__init__() self.flow = flow self.items = items self.broken_connections = [ ] # the connections that go beyond the removed ports and need to be restored in undo self.node_instances = [] for i in self.items: if find_type_in_object(i, NodeInstance): self.node_instances.append(i) self.connected_node_instances_indices_not_in_del_selection = [] for n in self.node_instances: for i in n.inputs: for cpi in i.connected_port_instances: cni = cpi.parent_node_instance if cni not in self.node_instances: self.broken_connections.append({cpi: i}) for o in n.outputs: for cpi in o.connected_port_instances: cni = cpi.parent_node_instance if cni not in self.node_instances: self.broken_connections.append({o: cpi})
def dropEvent(self, event): text = event.mimeData().text() item: QListWidgetItem = event.mimeData() Debugger.debug('drop received in Flow:', text) j_obj = None type = '' try: j_obj = json.loads(text) type = j_obj['type'] except Exception: return if type == 'variable': self.show_node_choice_widget(event.pos(), # only show get_var and set_var nodes [n for n in self.all_nodes if find_type_in_object(n, GetVariable_Node) or find_type_in_object(n, SetVariable_Node)])
def tabletEvent(self, event): """tabletEvent gets called by stylus operations. LeftButton: std, no button pressed RightButton: upper button pressed""" # if in edit mode and not panning or starting a pan, pass on to std mouseEvent handlers above if self.stylus_mode == 'edit' and not self.panning and not \ (event.type() == QTabletEvent.TabletPress and event.button() == Qt.RightButton): return # let the mousePress/Move/Release-Events handle it scaled_event_pos: QPointF = event.posF() / self.current_scale if event.type() == QTabletEvent.TabletPress: self.ignore_mouse_event = True if event.button() == Qt.LeftButton: if self.stylus_mode == 'comment': view_pos = self.mapToScene(self.viewport().pos()) new_drawing = self.create_and_place_drawing__cmd( view_pos + scaled_event_pos, config={ **self.stylus_modes_widget.get_pen_settings(), 'viewport pos': view_pos }) self.current_drawing = new_drawing self.drawing = True elif event.button() == Qt.RightButton: self.panning = True self.pan_last_x = event.x() self.pan_last_y = event.y() elif event.type() == QTabletEvent.TabletMove: self.ignore_mouse_event = True if self.panning: self.pan(event.pos()) elif event.pointerType() == QTabletEvent.Eraser: if self.stylus_mode == 'comment': for i in self.items(event.pos()): if find_type_in_object(i, DrawingObject): self.remove_drawing(i) break elif self.stylus_mode == 'comment' and self.drawing: if self.current_drawing.append_point(scaled_event_pos): self.current_drawing.stroke_weights.append( event.pressure()) self.current_drawing.update() self.viewport().update() elif event.type() == QTabletEvent.TabletRelease: if self.panning: self.panning = False if self.stylus_mode == 'comment' and self.drawing: Debugger.debug('drawing obj finished') self.current_drawing.finished() self.current_drawing = None self.drawing = False
def tabletEvent(self, event): """tabletEvent gets called by stylus operations. LeftButton: std, no button pressed RightButton: upper button pressed""" # if in edit mode and not panning or starting a pan, pass on to std mouseEvent handlers above if self.stylus_mode == 'edit' and not self.panning and not \ (event.type() == QTabletEvent.TabletPress and event.button() == Qt.RightButton): return # let the mousePress/Move/Release-Events handle it if event.type() == QTabletEvent.TabletPress: self.tablet_press_pos = event.pos() self.ignore_mouse_event = True if event.button() == Qt.LeftButton: if self.stylus_mode == 'comment': new_drawing = self.create_and_place_drawing__cmd(self.mapToScene(self.tablet_press_pos), config=self.stylus_modes_widget.get_pen_settings()) self.current_drawing = new_drawing self.drawing = True elif event.button() == Qt.RightButton: self.panning = True self.pan_last_x = event.x() self.pan_last_y = event.y() elif event.type() == QTabletEvent.TabletMove: self.ignore_mouse_event = True if self.panning: self.pan(event.pos()) elif event.pointerType() == QTabletEvent.Eraser: if self.stylus_mode == 'comment': for i in self.items(event.pos()): if find_type_in_object(i, DrawingObject): self.remove_drawing(i) break elif self.stylus_mode == 'comment' and self.drawing: mapped = self.mapToScene(QPoint(event.posF().x(), event.posF().y())) # rest = QPointF(event.posF().x()%1, event.posF().y()%1) # exact = QPointF(mapped.x()+rest.x()%1, mapped.y()+rest.y()%1) # TODO: use exact position (event.posF() ). Problem: mapToScene() only uses QPoint, not QPointF. The # calculation above didn't work if self.current_drawing.try_to_append_point(mapped): self.current_drawing.stroke_weights.append(event.pressure()) self.current_drawing.update() self.viewport().update() elif event.type() == QTabletEvent.TabletRelease: if self.panning: self.panning = False if self.stylus_mode == 'comment' and self.drawing: Debugger.debug('drawing obj finished') self.current_drawing.finished() self.current_drawing = None self.drawing = False
def update_variable_usages(self, v): get_var_NIs = [] for ni in self.flow.all_node_instances: if find_type_in_object(ni, GetVar_NodeInstance): get_var_NIs.append(ni) for ni in get_var_NIs: if ni.get_current_var_name() == v.name: ni.update()
def contextMenuEvent(self, event): QGraphicsView.contextMenuEvent(self, event) # in the case of the menu already being shown by a widget under the mouse, the event is accepted here if event.isAccepted(): return for i in self.items(event.pos()): if find_type_in_object(i, NodeInstance): ni: NodeInstance = i menu: QMenu = ni.get_context_menu() menu.exec_(event.globalPos()) event.accept()
def mouseReleaseEvent(self, event): # there might be a proxy widget meant to receive the event instead of the flow QGraphicsView.mouseReleaseEvent(self, event) if self.ignore_mouse_event or \ (event.button() == Qt.LeftButton and not self.left_mouse_pressed_in_flow): self.ignore_mouse_event = False return elif event.button() == Qt.MidButton: self.panning = False # connection dropped over specific gate if self.dragging_connection and self.itemAt(event.pos()) and \ find_type_in_object(self.itemAt(event.pos()), PortInstanceGate): self.connect_gates__cmd(self.gate_selected, self.itemAt(event.pos())) # connection dropped over NodeInstance - auto connect elif self.dragging_connection and find_type_in_objects( self.items(event.pos()), NodeInstance): # find node instance ni_under_drop = None for item in self.items(event.pos()): if find_type_in_object(item, NodeInstance): ni_under_drop = item break # connect self.try_conn_gate_and_ni(self.gate_selected, ni_under_drop) # connection dropped somewhere else - show node choice widget elif self.dragging_connection: self.auto_connection_gate = self.gate_selected self.show_node_choice_widget(event.pos()) self.left_mouse_pressed_in_flow = False self.dragging_connection = False self.gate_selected = None self.viewport().repaint()
def mousePressEvent(self, event): Debugger.debug('mouse press event received, point:', event.pos()) # to catch tablet events (for some reason, it results in a mousePrEv too) if self.ignore_mouse_event: self.ignore_mouse_event = False return # there might be a proxy widget meant to receive the event instead of the flow QGraphicsView.mousePressEvent(self, event) # to catch any Proxy that received the event. Checking for event.isAccepted() or what is returned by # QGraphicsView.mousePressEvent(...) both didn't work so far, so I do it manually if self.ignore_mouse_event: self.ignore_mouse_event = False return if event.button() == Qt.LeftButton: if self.node_choice_proxy.isVisible(): self.hide_node_choice_widget() else: if find_type_in_object(self.itemAt(event.pos()), PortInstanceGate): self.gate_selected = self.itemAt(event.pos()) self.dragging_connection = True self.left_mouse_pressed_in_flow = True elif event.button() == Qt.RightButton: if len(self.items(event.pos())) == 0: self.node_choice_widget.reset_list() self.show_node_choice_widget(event.pos()) elif event.button() == Qt.MidButton: self.panning = True self.pan_last_x = event.x() self.pan_last_y = event.y() event.accept() self.mouse_press_pos = self.mapToScene(event.pos())
def rebuild_class_selection(self, obj): # clear layout for i in range(self.class_selection_layout.count()): item = self.class_selection_layout.itemAt(0) widget = item.widget() widget.hide() self.class_selection_layout.removeItem(item) self.buttons_obj_dict = {} self.active_class_index = -1 if find_type_in_object(obj, NodeInstance): # NI class node_inst_class_RB = QRadioButton('NodeInstance') node_inst_class_RB.toggled.connect(self.class_RB_toggled) self.buttons_obj_dict[node_inst_class_RB] = obj self.class_selection_layout.addWidget(node_inst_class_RB, 0, 0) # main_widget class if obj.main_widget is not None: main_widget_class_RB = QRadioButton('MainWidget') main_widget_class_RB.toggled.connect(self.class_RB_toggled) self.buttons_obj_dict[main_widget_class_RB] = obj.main_widget self.class_selection_layout.addWidget(main_widget_class_RB, 1, 0) # data input widgets row_count = 0 for inp in obj.inputs: if inp.widget is not None: inp_widget_class_RB = QRadioButton( 'Input ' + str(obj.inputs.index(inp))) inp_widget_class_RB.toggled.connect(self.class_RB_toggled) self.buttons_obj_dict[inp_widget_class_RB] = inp.widget self.class_selection_layout.addWidget( inp_widget_class_RB, row_count, 1) row_count += 1 node_inst_class_RB.setChecked(True)
def add_component(self, e): if find_type_in_object(e, NodeInstance): self.add_node_instance(e) elif find_type_in_object(e, DrawingObject): self.add_drawing(e)
def selected_drawings(self): selected_drawings = [] for i in self.scene().selectedItems(): if find_type_in_object(i, DrawingObject): selected_drawings.append(i) return selected_drawings
def selected_node_instances(self): selected_NIs = [] for i in self.scene().selectedItems(): if find_type_in_object(i, NodeInstance): selected_NIs.append(i) return selected_NIs
def find_NI_in_object(obj): return find_type_in_object(obj, NodeInstance)