from graph.directed_acyclic_graph.DAGLayout import DAGGraph from graph.DataType import Arrow, Point from typing import List from PyQt5.QtWidgets import QWidget, QApplication, QGraphicsView, QGraphicsScene, QGraphicsItem from PyQt5.QtCore import QRectF, Qt from PyQt5.QtGui import QFont, QBrush import sys from enum import Enum class StoryFragmentNode(QGraphicsItem): """ 故事情节名称节点 """ def __init__(self, name:str, font: QFont, parent): QGraphicsItem.__init__(self, parent) self.fragment_name = name self.font_bind = font self.data_bind: object = None pass def boundingRect(self) -> QRectF: size = self.font_bind.pixelSize() return QRectF(0, 0, size * len(self.fragment_name) + 10, size + 10) def paint(self, painter, option, widget = ...): outline = self.boundingRect() text_rect = QRectF(outline.x()+5, outline.y()+5, outline.width()-10, outline.height() - 10) painter.save() painter.fillRect(outline, QBrush(Qt.gray)) painter.drawRect(outline) painter.setFont(self.font_bind) painter.drawText(text_rect, self.fragment_name) painter.restore() # 添加自定义功能函数 def attach_data(self, data_inst: object): self.data_bind = data_inst pass def get_data(self) -> object: return self.data_bind class Direction(Enum): RankLR = 0, RankTB = 1, class DAGActiveView(QGraphicsView): def __init__(self, orie: Direction, parent): QGraphicsView.__init__(self, parent) self.orientation = orie self.scene_bind = QGraphicsScene(self) self.setScene(self.scene_bind) font_global = QFont() font_global.setPixelSize(20) self.scene_bind.addItem(StoryFragmentNode("实例节点:示例情节", font_global, None)) pass def update_with_edges(self, arrows: List[Arrow]) -> None: tools = DAGGraph() tools.rebuild_from_edges(arrows) tools.graph_layout() total_nodes = tools.nodes_with_layout pass if __name__ == "__main__": app = QApplication(sys.argv) view = DAGActiveView(Direction.RankLR, None) view.show() app.exec()