From eec411a3c67d077768bb443daf20f5c9dd12780c Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Fri, 22 Jan 2021 11:09:59 +0800 Subject: [PATCH] fixup Flowchart for PyQt6 --- pyqtgraph/flowchart/Node.py | 39 ++++++++++++++++------------- pyqtgraph/flowchart/Terminal.py | 36 +++++++++++++++----------- pyqtgraph/flowchart/library/Data.py | 28 ++++++++++++--------- 3 files changed, 60 insertions(+), 43 deletions(-) diff --git a/pyqtgraph/flowchart/Node.py b/pyqtgraph/flowchart/Node.py index dcda62d7..eebbfac4 100644 --- a/pyqtgraph/flowchart/Node.py +++ b/pyqtgraph/flowchart/Node.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from ..Qt import QtCore, QtGui +from ..Qt import QtCore, QtGui, QtWidgets from ..graphicsItems.GraphicsObject import GraphicsObject from .. import functions as fn from .Terminal import * @@ -436,6 +436,24 @@ class Node(QtCore.QObject): t.disconnectAll() +class TextItem(QtWidgets.QGraphicsTextItem): + def __init__(self, text, parent, on_update): + super().__init__(text, parent) + self.on_update = on_update + + def focusOutEvent(self, ev): + super().focusOutEvent(ev) + if self.on_update is not None: + self.on_update() + + def keyPressEvent(self, ev): + if ev.key() == QtCore.Qt.Key_Enter or ev.key() == QtCore.Qt.Key_Return: + if self.on_update is not None: + self.on_update() + return + super().keyPressEvent(ev) + + #class NodeGraphicsItem(QtGui.QGraphicsItem): class NodeGraphicsItem(GraphicsObject): def __init__(self, node): @@ -461,16 +479,13 @@ class NodeGraphicsItem(GraphicsObject): self.setFlags(flags) self.bounds = QtCore.QRectF(0, 0, 100, 100) - self.nameItem = QtGui.QGraphicsTextItem(self.node.name(), self) + self.nameItem = TextItem(self.node.name(), self, self.labelChanged) self.nameItem.setDefaultTextColor(QtGui.QColor(50, 50, 50)) self.nameItem.moveBy(self.bounds.width()/2. - self.nameItem.boundingRect().width()/2., 0) self.nameItem.setTextInteractionFlags(QtCore.Qt.TextEditorInteraction) self.updateTerminals() #self.setZValue(10) - self.nameItem.focusOutEvent = self.labelFocusOut - self.nameItem.keyPressEvent = self.labelKeyPress - self.menu = None self.buildMenu() @@ -481,16 +496,6 @@ class NodeGraphicsItem(GraphicsObject): #item.setZValue(z+1) #GraphicsObject.setZValue(self, z) - def labelFocusOut(self, ev): - QtGui.QGraphicsTextItem.focusOutEvent(self.nameItem, ev) - self.labelChanged() - - def labelKeyPress(self, ev): - if ev.key() == QtCore.Qt.Key_Enter or ev.key() == QtCore.Qt.Key_Return: - self.labelChanged() - else: - QtGui.QGraphicsTextItem.keyPressEvent(self.nameItem, ev) - def labelChanged(self): newName = str(self.nameItem.toPlainText()) if newName != self.node.name(): @@ -574,7 +579,7 @@ class NodeGraphicsItem(GraphicsObject): def mouseClickEvent(self, ev): #print "Node.mouseClickEvent called." - if int(ev.button()) == int(QtCore.Qt.LeftButton): + if ev.button() == QtCore.Qt.LeftButton: ev.accept() #print " ev.button: left" sel = self.isSelected() @@ -587,7 +592,7 @@ class NodeGraphicsItem(GraphicsObject): self.update() #return ret - elif int(ev.button()) == int(QtCore.Qt.RightButton): + elif ev.button() == QtCore.Qt.RightButton: #print " ev.button: right" ev.accept() #pos = ev.screenPos() diff --git a/pyqtgraph/flowchart/Terminal.py b/pyqtgraph/flowchart/Terminal.py index 7c512dda..c2e60a55 100644 --- a/pyqtgraph/flowchart/Terminal.py +++ b/pyqtgraph/flowchart/Terminal.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from ..Qt import QtCore, QtGui +from ..Qt import QtCore, QtGui, QtWidgets import weakref from ..graphicsItems.GraphicsObject import GraphicsObject from .. import functions as fn @@ -273,6 +273,25 @@ class Terminal(object): """ return self._name < other._name + +class TextItem(QtWidgets.QGraphicsTextItem): + def __init__(self, text, parent, on_update): + super().__init__(text, parent) + self.on_update = on_update + + def focusOutEvent(self, ev): + super().focusOutEvent(ev) + if self.on_update is not None: + self.on_update() + + def keyPressEvent(self, ev): + if ev.key() == QtCore.Qt.Key_Enter or ev.key() == QtCore.Qt.Key_Return: + if self.on_update is not None: + self.on_update() + return + super().keyPressEvent(ev) + + class TerminalGraphicsItem(GraphicsObject): def __init__(self, term, parent=None): @@ -280,27 +299,16 @@ class TerminalGraphicsItem(GraphicsObject): GraphicsObject.__init__(self, parent) self.brush = fn.mkBrush(0,0,0) self.box = QtGui.QGraphicsRectItem(0, 0, 10, 10, self) - self.label = QtGui.QGraphicsTextItem(self.term.name(), self) + on_update = self.labelChanged if self.term.isRenamable() else None + self.label = TextItem(self.term.name(), self, on_update) self.label.scale(0.7, 0.7) self.newConnection = None self.setFiltersChildEvents(True) ## to pick up mouse events on the rectitem if self.term.isRenamable(): self.label.setTextInteractionFlags(QtCore.Qt.TextEditorInteraction) - self.label.focusOutEvent = self.labelFocusOut - self.label.keyPressEvent = self.labelKeyPress self.setZValue(1) self.menu = None - def labelFocusOut(self, ev): - QtGui.QGraphicsTextItem.focusOutEvent(self.label, ev) - self.labelChanged() - - def labelKeyPress(self, ev): - if ev.key() == QtCore.Qt.Key_Enter or ev.key() == QtCore.Qt.Key_Return: - self.labelChanged() - else: - QtGui.QGraphicsTextItem.keyPressEvent(self.label, ev) - def labelChanged(self): newName = str(self.label.toPlainText()) if newName != self.term.name(): diff --git a/pyqtgraph/flowchart/library/Data.py b/pyqtgraph/flowchart/library/Data.py index b133b159..6edd3a80 100644 --- a/pyqtgraph/flowchart/library/Data.py +++ b/pyqtgraph/flowchart/library/Data.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- from ..Node import Node -from ...Qt import QtGui, QtCore +from ...Qt import QtGui, QtCore, QtWidgets import numpy as np import sys from .common import * @@ -173,6 +173,20 @@ class RegionSelectNode(CtrlNode): self.update() +class TextEdit(QtWidgets.QTextEdit): + def __init__(self, on_update): + super().__init__() + self.on_update = on_update + self.lastText = None + + def focusOutEvent(self, ev): + text = str(self.toPlainText()) + if text != self.lastText: + self.lastText = text + self.on_update() + super().focusOutEvent(ev) + + class EvalNode(Node): """Return the output of a string evaluated/executed by the python interpreter. The string may be either an expression or a python script, and inputs are accessed as the name of the terminal. @@ -190,15 +204,12 @@ class EvalNode(Node): self.ui = QtGui.QWidget() self.layout = QtGui.QGridLayout() - self.text = QtGui.QTextEdit() + self.text = TextEdit(self.update) self.text.setTabStopWidth(30) self.text.setPlainText("# Access inputs as args['input_name']\nreturn {'output': None} ## one key per output terminal") self.layout.addWidget(self.text, 1, 0, 1, 2) self.ui.setLayout(self.layout) - self.text.focusOutEvent = self.focusOutEvent - self.lastText = None - def ctrlWidget(self): return self.ui @@ -221,13 +232,6 @@ class EvalNode(Node): def code(self): return self.text.toPlainText() - def focusOutEvent(self, ev): - text = str(self.text.toPlainText()) - if text != self.lastText: - self.lastText = text - self.update() - return QtGui.QTextEdit.focusOutEvent(self.text, ev) - def process(self, display=True, **args): l = locals() l.update(args)