From 86069cc0a1f2ab7af5ac5a9a6252ea6d3e4308f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Sat, 18 Mar 2023 15:57:13 +0100 Subject: [PATCH] Several code smells, improvements, and clean-up --- main.py | 21 ++++++--------- node_editor/gui/connection.py | 4 +-- node_editor/gui/node.py | 22 +++++----------- node_editor/gui/node_editor.py | 40 ++++++++++------------------- node_editor/gui/node_list.py | 6 ++--- node_editor/gui/node_type_editor.py | 2 +- node_editor/gui/node_widget.py | 8 +++--- node_editor/gui/port.py | 27 +++++++------------ node_editor/gui/view.py | 13 +++++----- requirements.txt | 3 ++- 10 files changed, 56 insertions(+), 90 deletions(-) diff --git a/main.py b/main.py index 2c6ac27..97e9856 100644 --- a/main.py +++ b/main.py @@ -10,29 +10,20 @@ Repo: https://github.com/bhowiebkr/simple-node-editor """ -import sys - -from PySide6 import QtWidgets, QtCore, QtGui - import logging -import os -from node_editor.gui.node_widget import NodeWidget +from PySide6 import QtCore, QtGui, QtWidgets + from node_editor.gui.node_list import NodeList from node_editor.gui.node_type_editor import NodeTypeEditor +from node_editor.gui.node_widget import NodeWidget logging.basicConfig(level=logging.DEBUG) -import importlib - -# Import qdarktheme if you have it. If not install it with pip. Dark Themese are great! -if importlib.util.find_spec("qdarktheme") is not None: - import qdarktheme - class NodeEditor(QtWidgets.QMainWindow): def __init__(self, parent=None): - super(NodeEditor, self).__init__(parent) + super().__init__(parent) self.settings = None icon = QtGui.QIcon("resources\\app.ico") @@ -106,6 +97,10 @@ def closeEvent(self, event): if __name__ == "__main__": + import sys + + import qdarktheme + app = QtWidgets.QApplication(sys.argv) app.setWindowIcon(QtGui.QIcon("resources\\app.ico")) qdarktheme.setup_theme() diff --git a/node_editor/gui/connection.py b/node_editor/gui/connection.py index f3555d0..ba0f7f4 100644 --- a/node_editor/gui/connection.py +++ b/node_editor/gui/connection.py @@ -1,4 +1,4 @@ -from PySide6 import QtWidgets, QtGui, QtCore +from PySide6 import QtCore, QtGui, QtWidgets class Connection(QtWidgets.QGraphicsPathItem): @@ -27,7 +27,7 @@ class Connection(QtWidgets.QGraphicsPathItem): """ def __init__(self, parent): - super(Connection, self).__init__(parent) + super().__init__(parent) self.setFlag(QtWidgets.QGraphicsPathItem.ItemIsSelectable) diff --git a/node_editor/gui/node.py b/node_editor/gui/node.py index d396af0..b46fe1f 100644 --- a/node_editor/gui/node.py +++ b/node_editor/gui/node.py @@ -1,4 +1,4 @@ -from PySide6 import QtWidgets, QtGui, QtCore +from PySide6 import QtCore, QtGui, QtWidgets from node_editor.gui.port import Port @@ -35,7 +35,7 @@ class Node(QtWidgets.QGraphicsPathItem): """ def __init__(self): - super(Node, self).__init__() + super().__init__() self.setFlag(QtWidgets.QGraphicsPathItem.ItemIsMovable) self.setFlag(QtWidgets.QGraphicsPathItem.ItemIsSelectable) @@ -84,10 +84,9 @@ def paint(self, painter, option=None, widget=None): if self.isSelected(): painter.setPen(QtGui.QPen(QtGui.QColor(241, 175, 0), 2)) - painter.setBrush(self.node_color) else: painter.setPen(self.node_color.lighter()) - painter.setBrush(self.node_color) + painter.setBrush(self.node_color) painter.drawPath(self.path()) painter.setPen(QtCore.Qt.NoPen) @@ -137,7 +136,6 @@ def build(self): self.misc_path = QtGui.QPainterPath() # a bunch of other stuff total_width = 0 - total_height = 0 path = QtGui.QPainterPath() # The main path # The fonts what will be used @@ -152,7 +150,7 @@ def build(self): } title_type_dim = { - "w": QtGui.QFontMetrics(title_type_font).horizontalAdvance("(" + self._type_text + ")"), + "w": QtGui.QFontMetrics(title_type_font).horizontalAdvance(f"({self._type_text})"), "h": QtGui.QFontMetrics(title_type_font).height(), } @@ -162,8 +160,7 @@ def build(self): total_width = dim # Add both the title and type height together for the total height - for dim in [title_dim["h"], title_type_dim["h"]]: - total_height += dim + total_height = sum([title_dim["h"], title_type_dim["h"]]) port_dim = None # Add the heigth for each of the ports @@ -198,7 +195,7 @@ def build(self): -title_type_dim["w"] / 2, (-total_height / 2) + title_dim["h"] + title_type_dim["h"], title_type_font, - "(" + self._type_text + ")", + f"({self._type_text})", ) if port_dim: @@ -280,12 +277,7 @@ def delete(self): None """ - to_delete = [] - - for port in self._ports: - if port.connection: - to_delete.append(port.connection) - + to_delete = [port.connection for port in self._ports if port.connection] for connection in to_delete: connection.delete() diff --git a/node_editor/gui/node_editor.py b/node_editor/gui/node_editor.py index 357453c..75af872 100644 --- a/node_editor/gui/node_editor.py +++ b/node_editor/gui/node_editor.py @@ -1,4 +1,6 @@ -from PySide6 import QtWidgets, QtCore +from contextlib import suppress + +from PySide6 import QtCore, QtWidgets from node_editor.gui.connection import Connection from node_editor.gui.node import Node @@ -27,7 +29,7 @@ def __init__(self, parent): :type parent: QWidget """ - super(NodeEditor, self).__init__(parent) + super().__init__(parent) self.connection = None self.port = None self.scene = None @@ -55,10 +57,7 @@ def item_at(self, position): """ items = self.scene.items(QtCore.QRectF(position - QtCore.QPointF(1, 1), QtCore.QSizeF(3, 3))) - - if items: - return items[0] - return None + return items[0] if items else None def eventFilter(self, watched, event): """ @@ -87,7 +86,7 @@ def eventFilter(self, watched, event): self.connection.update_path() return True - elif isinstance(item, Connection): + if isinstance(item, Connection): self.connection = Connection(None) self.connection.start_pos = item.start_pos self.scene.addItem(self.connection) @@ -96,24 +95,15 @@ def eventFilter(self, watched, event): self.connection.update_start_and_end_pos() # to fix the offset return True - elif isinstance(item, Node): - if self._last_selected: - # If we clear the scene, we loose the last selection - try: - self._last_selected.select_connections(False) - except RuntimeError: - pass + if self._last_selected: + # If we clear the scene, we loose the last selection + with suppress(RuntimeError): + self._last_selected.select_connections(False) + if isinstance(item, Node): item.select_connections(True) self._last_selected = item - else: - try: - if self._last_selected: - self._last_selected.select_connections(False) - except RuntimeError: - pass - self._last_selected = None elif event.button() == QtCore.Qt.RightButton: @@ -152,15 +142,13 @@ def eventFilter(self, watched, event): item.clear_connection() self.connection.start_port = self.port - self.connection.end_port = item - self.connection.update_start_and_end_pos() - self.connection = None else: print("Deleting connection") self.connection.delete() - self.connection = None + + self.connection = None if self.connection: self.connection.delete() @@ -168,4 +156,4 @@ def eventFilter(self, watched, event): self.port = None return True - return super(NodeEditor, self).eventFilter(watched, event) + return super().eventFilter(watched, event) diff --git a/node_editor/gui/node_list.py b/node_editor/gui/node_list.py index 1085d2f..85d8541 100644 --- a/node_editor/gui/node_list.py +++ b/node_editor/gui/node_list.py @@ -1,9 +1,9 @@ -from PySide6 import QtWidgets, QtCore, QtGui +from PySide6 import QtCore, QtGui, QtWidgets class NodeList(QtWidgets.QListWidget): def __init__(self, parent=None): - super(NodeList, self).__init__(parent) + super().__init__(parent) for node in ["Input", "Output", "And", "Not", "Nor", "Empty"]: item = QtWidgets.QListWidgetItem(node) @@ -27,4 +27,4 @@ def mousePressEvent(self, event): drag.setPixmap(pixmap) drag.exec_() - super(NodeList, self).mousePressEvent(event) + super().mousePressEvent(event) diff --git a/node_editor/gui/node_type_editor.py b/node_editor/gui/node_type_editor.py index 74177cb..78ea850 100644 --- a/node_editor/gui/node_type_editor.py +++ b/node_editor/gui/node_type_editor.py @@ -1,4 +1,4 @@ -from PySide6 import QtWidgets, QtGui +from PySide6 import QtWidgets class NodeTypeEditor(QtWidgets.QDialog): diff --git a/node_editor/gui/node_widget.py b/node_editor/gui/node_widget.py index 7c0da2b..9005a76 100644 --- a/node_editor/gui/node_widget.py +++ b/node_editor/gui/node_widget.py @@ -1,8 +1,8 @@ -from PySide6 import QtWidgets, QtGui +from PySide6 import QtGui, QtWidgets -from node_editor.gui.view import View from node_editor.gui.node import Node from node_editor.gui.node_editor import NodeEditor +from node_editor.gui.view import View def create_input(): @@ -69,7 +69,7 @@ def dragEnterEvent(self, e): def dropEvent(self, e): # find item at these coordinates item = self.itemAt(e.scenePos()) - if item.setAcceptDrops == True: + if item.setAcceptDrops: # pass on event to item at the coordinates item.dropEvent(e) @@ -94,7 +94,7 @@ def __init__(self, parent): Args: parent (QWidget): The parent widget. """ - super(NodeWidget, self).__init__(parent) + super().__init__(parent) main_layout = QtWidgets.QVBoxLayout() main_layout.setContentsMargins(0, 0, 0, 0) self.setLayout(main_layout) diff --git a/node_editor/gui/port.py b/node_editor/gui/port.py index f0bae7b..2db842d 100644 --- a/node_editor/gui/port.py +++ b/node_editor/gui/port.py @@ -1,4 +1,4 @@ -from PySide6 import QtWidgets, QtGui, QtCore +from PySide6 import QtCore, QtGui, QtWidgets class Port(QtWidgets.QGraphicsPathItem): @@ -32,7 +32,7 @@ class Port(QtWidgets.QGraphicsPathItem): """ def __init__(self, parent, scene): - super(Port, self).__init__(parent) + super().__init__(parent) self.radius_ = 5 self.margin = 2 @@ -66,15 +66,12 @@ def set_name(self, name): if self._is_output: x = -self.radius_ - self.margin - self.port_text_width - y = self.port_text_height / 4 - - self.text_path.addText(x, y, self.font, nice_name) - else: x = self.radius_ + self.margin - y = self.port_text_height / 4 - self.text_path.addText(x, y, self.font, nice_name) + y = self.port_text_height / 4 + + self.text_path.addText(x, y, self.font, nice_name) def set_node(self, node): self.m_node = node @@ -114,19 +111,13 @@ def can_connect_to(self, port): if port.node() == self.node(): return False - if self._is_output == port._is_output: - return False - - return True + return self._is_output != port._is_output def is_connected(self): - if self.connection: - return True - return False + return bool(self.connection) def itemChange(self, change, value): - if change == QtWidgets.QGraphicsItem.ItemScenePositionHasChanged: - if self.connection: - self.connection.update_start_and_end_pos() + if change == QtWidgets.QGraphicsItem.ItemScenePositionHasChanged and self.connection: + self.connection.update_start_and_end_pos() return value diff --git a/node_editor/gui/view.py b/node_editor/gui/view.py index c6ae2e3..f2ac036 100644 --- a/node_editor/gui/view.py +++ b/node_editor/gui/view.py @@ -1,6 +1,5 @@ -from PySide6 import QtCore, QtGui, QtWidgets, QtOpenGLWidgets +from PySide6 import QtCore, QtGui, QtOpenGLWidgets, QtWidgets -from node_editor.gui.connection import Connection from node_editor.gui.node import Node @@ -22,7 +21,7 @@ class View(QtWidgets.QGraphicsView): request_node = QtCore.Signal(str) def __init__(self, parent): - super(View, self).__init__(parent) + super().__init__(parent) self.setRenderHint(QtGui.QPainter.Antialiasing) self._manipulationMode = 0 @@ -144,7 +143,7 @@ def drawBackground(self, painter, rect): y += self._grid_size_course painter.drawLines(gridLines) - return super(View, self).drawBackground(painter, rect) + return super().drawBackground(painter, rect) def contextMenuEvent(self, event): """ @@ -199,7 +198,7 @@ def mousePressEvent(self, event): self._pan_start_y = event.y() self.setCursor(QtCore.Qt.ClosedHandCursor) - return super(View, self).mousePressEvent(event) + return super().mousePressEvent(event) def mouseReleaseEvent(self, event): """ @@ -210,7 +209,7 @@ def mouseReleaseEvent(self, event): self._pan = False self.setCursor(QtCore.Qt.ArrowCursor) - return super(View, self).mouseReleaseEvent(event) + return super().mouseReleaseEvent(event) def mouseMoveEvent(self, event): """ @@ -225,4 +224,4 @@ def mouseMoveEvent(self, event): self._pan_start_x = event.x() self._pan_start_y = event.y() - return super(View, self).mouseMoveEvent(event) + return super().mouseMoveEvent(event) diff --git a/requirements.txt b/requirements.txt index e3f31e2..8a16d08 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -PySide6 \ No newline at end of file +pyqtdarktheme +PySide6