def group_unserialize(s): # 返回的不是group,是List[ShellEN/ShellIN] l = eval(s) neurons = [] lconnects = [] # 表中存表 for flag, p, value, connects in l: neuron = (ShellEN if flag == 'EN' else ShellIN)(QPointF(*p), value) neurons.append(neuron) lconnects.append(connects) for neuron, connects in zip(neurons, lconnects): neuron.connects = [ withpack(Neuron.Connect(k, t, neurons[id]), actpacks, backpacks) for id, k, t, actpacks, backpacks in connects ] return neurons
def __init__(self): super().__init__() Ui_MainWindow.setupUi(self, self) # model/state(scene) n1 = ShellEN(QPoint(200, 200), False) n2 = ShellIN(QPoint(300, 400), False) n1.connects.append(Neuron.Connect(10, 1, n2)) group = Group([n1, n2]) #~ s = group.serialize() #~ print(Group.unserialize(s)) self.scene = MyScene(group, True) self.button_design.hide() self.button_pause.hide() self.mod = None # 统一初始化,是个麻烦的问题 self.d_or_t = 0 # design self.canvas.setScene(self.scene) self.t = 0 self.toolbar = self.addToolBar('mytoolbar') self.toolbar.setMovable(False) serializeAction = QAction('&储存', self) # QAction放哪都诡异 serializeAction.setShortcut('Ctrl+S') serializeAction.setStatusTip('Serialize application') self.toolbar.addAction(serializeAction) serializeAction.triggered.connect(self.scene.serialize) unserializeAction = QAction('&读取', self) unserializeAction.setShortcut('Ctrl+O') unserializeAction.setStatusTip('Unserialize application') unserializeAction.triggered.connect(self.scene.unserialize) self.toolbar.addAction(unserializeAction) self.label.hide() # 这些按钮是首屏不显示的 self.label_2.hide() self.button_next.hide() self.button_continue.hide() self.choose_button(0)
def mouseReleaseEvent(self, event): # 也未使用表驱动 mod = self.mod if mod == 0: # move self.choosed = None self._dp = None elif mod == 4: # connect temp_connect = self.allconnect.temp_connect self.allconnect.temp_connect = None l = self.choosed.connects if Qt.ControlModifier != event.modifiers(): # 添加模式 if temp_connect: other = temp_connect[1] if all(other is not connect.to for connect in l): # 有连接 l.append(Neuron.Connect(10, 1, other)) self.update() else: #删除模式 if temp_connect: other = temp_connect[1] for i, connect in enumerate(l): # 找不到删则什么都不做 if connect.to is other: del l[i] self.update() break