Skip to content

Several code smells, improvements, and clean-up #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions node_editor/gui/connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide6 import QtWidgets, QtGui, QtCore
from PySide6 import QtCore, QtGui, QtWidgets


class Connection(QtWidgets.QGraphicsPathItem):
Expand Down Expand Up @@ -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)

Expand Down
22 changes: 7 additions & 15 deletions node_editor/gui/node.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide6 import QtWidgets, QtGui, QtCore
from PySide6 import QtCore, QtGui, QtWidgets

from node_editor.gui.port import Port

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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(),
}

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()

Expand Down
40 changes: 14 additions & 26 deletions node_editor/gui/node_editor.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -152,20 +142,18 @@ 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()
self.connection = None
self.port = None
return True

return super(NodeEditor, self).eventFilter(watched, event)
return super().eventFilter(watched, event)
6 changes: 3 additions & 3 deletions node_editor/gui/node_list.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -27,4 +27,4 @@ def mousePressEvent(self, event):
drag.setPixmap(pixmap)
drag.exec_()

super(NodeList, self).mousePressEvent(event)
super().mousePressEvent(event)
2 changes: 1 addition & 1 deletion node_editor/gui/node_type_editor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide6 import QtWidgets, QtGui
from PySide6 import QtWidgets


class NodeTypeEditor(QtWidgets.QDialog):
Expand Down
8 changes: 4 additions & 4 deletions node_editor/gui/node_widget.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down
27 changes: 9 additions & 18 deletions node_editor/gui/port.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide6 import QtWidgets, QtGui, QtCore
from PySide6 import QtCore, QtGui, QtWidgets


class Port(QtWidgets.QGraphicsPathItem):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
13 changes: 6 additions & 7 deletions node_editor/gui/view.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PySide6
pyqtdarktheme
PySide6