Skip to content

Commit

Permalink
Addressing review -- Code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
irgolic committed Aug 2, 2018
1 parent 57380e0 commit 28fe821
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
5 changes: 0 additions & 5 deletions Orange/canvas/canvas/items/linkitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class LinkCurveItem(QGraphicsPathItem):
"""
def __init__(self, parent):
super().__init__(parent)
self.__parent = parent

self.setAcceptedMouseButtons(Qt.NoButton)
self.setAcceptHoverEvents(True)

Expand Down Expand Up @@ -93,9 +91,6 @@ def setPath(self, path):
self.__shape = None
super().setPath(path)

def parent(self):
return self.__parent

def __update(self):
shadow_enabled = self.__hover
if self.shadow.isEnabled() != shadow_enabled:
Expand Down
21 changes: 4 additions & 17 deletions Orange/canvas/document/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

from AnyQt.QtWidgets import QUndoCommand

from Orange.canvas.scheme import SchemeLink


class AddNodeCommand(QUndoCommand):
def __init__(self, scheme, node, parent=None):
Expand Down Expand Up @@ -72,23 +70,12 @@ def undo(self):


class InsertNodeCommand(QUndoCommand):
def __init__(self, scheme, link, new_node, parent=None):
QUndoCommand.__init__(self, "Remove link", parent)
def __init__(self, scheme, new_node, old_link, new_links, parent=None):
QUndoCommand.__init__(self, "Insert widget into link", parent)
self.scheme = scheme
self.original_link = link
self.inserted_widget = new_node

possible_links = (self.scheme.propose_links(link.source_node, new_node),
self.scheme.propose_links(new_node, link.sink_node))

if not possible_links[0] or not possible_links[1]:
raise ValueError("Cannot insert widget: links not possible")

self.new_links = (
SchemeLink(link.source_node, link.source_channel,
new_node, possible_links[0][0][1]), # first link, first entry, output (1)
SchemeLink(new_node, possible_links[1][0][0], # second link, first entry, input (0)
link.sink_node, link.sink_channel))
self.original_link = old_link
self.new_links = new_links

def redo(self):
self.scheme.add_node(self.inserted_widget)
Expand Down
39 changes: 32 additions & 7 deletions Orange/canvas/document/schemeedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,11 +878,11 @@ def removeLink(self, link):
command = commands.RemoveLinkCommand(self.__scheme, link)
self.__undoStack.push(command)

def insertNode(self, link, new_node):
def insertNode(self, new_node, old_link, new_links):
"""
Insert a node in-between two linked nodes.
"""
command = commands.InsertNodeCommand(self.__scheme, link, new_node)
command = commands.InsertNodeCommand(self.__scheme, new_node, old_link, new_links)
self.__undoStack.push(command)

def onNewLink(self, func):
Expand Down Expand Up @@ -1054,7 +1054,20 @@ def tryInsertNode(self, link, new_node_desc, pos):
if nodes_are_compatible(source_node.description, new_node_desc) and \
nodes_are_compatible(new_node_desc, sink_node.description):
new_node = self.newNodeHelper(new_node_desc, position=(pos.x(), pos.y()))
self.insertNode(link, new_node)

possible_links = (self.scheme().propose_links(source_node, new_node),
self.scheme().propose_links(new_node, sink_node))

if not possible_links[0] or not possible_links[1]:
raise ValueError("Cannot insert widget: links not possible")

new_links = (
SchemeLink(source_node, link.source_channel,
new_node, possible_links[0][0][1]), # first link, first entry, output
SchemeLink(new_node, possible_links[1][0][0], # second link, first entry, input
sink_node, link.sink_channel))

self.insertNode(new_node, link, new_links)
else:
self.createNewNode(new_node_desc, position=(pos.x(), pos.y()))

Expand Down Expand Up @@ -1083,9 +1096,9 @@ def eventFilter(self, obj, event):
log.error("Unknown qualified name '%s'", qname)
else:
pos = event.scenePos()
item = self.__scene.item_at(event.scenePos())
if item and isinstance(item, items.LinkCurveItem):
link = self.__scene.link_for_item(item.parent())
item = self.__scene.item_at(event.scenePos(), items.LinkItem)
if item:
link = self.__scene.link_for_item(item)
self.tryInsertNode(link, desc, pos)
else:
self.createNewNode(desc, position=(pos.x(), pos.y()))
Expand Down Expand Up @@ -1667,7 +1680,19 @@ def filterFunc(index):
else:
return

self.insertNode(original_link, new_node)
possible_links = (self.scheme().propose_links(source_node, new_node),
self.scheme().propose_links(new_node, sink_node))

if not possible_links[0] or not possible_links[1]:
raise ValueError("Cannot insert widget: links not possible")

new_links = (
SchemeLink(source_node, original_link.source_channel,
new_node, possible_links[0][0][1]), # first link, first entry, output
SchemeLink(new_node, possible_links[1][0][0], # second link, first entry, input
sink_node, original_link.sink_channel))

self.insertNode(new_node, original_link, new_links)

def __duplicateSelected(self):
"""
Expand Down

0 comments on commit 28fe821

Please sign in to comment.