Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
menu.setFilterFunc(None)
action = menu.exec_(pos, search_text)
if action:
item = action.property("item")
desc = item.data(QtWidgetRegistry.WIDGET_DESC_ROLE)
# Get the scene position
view = self.document.view()
pos = view.mapToScene(view.mapFromGlobal(pos))
node = self.document.newNodeHelper(desc, position=(pos.x(), pos.y()))
self.document.addNode(node)
return node
class RectangleSelectionAction(UserInteraction):
"""
Select items in the scene using a Rectangle selection
"""
def __init__(self, document, *args, **kwargs):
UserInteraction.__init__(self, document, *args, **kwargs)
# The initial selection at drag start
self.initial_selection = None
# Selection when last updated in a mouseMoveEvent
self.last_selection = None
# A selection rect (`QRectF`)
self.selection_rect = None
# Keyboard modifiers
self.modifiers = 0
def mousePressEvent(self, event):
class UserCanceledError(ValueError):
pass
def reversed_arguments(func):
"""
Return a function with reversed argument order.
"""
def wrapped(*args):
return func(*reversed(args))
return wrapped
class NewLinkAction(UserInteraction):
"""
User drags a new link from an existing `NodeAnchorItem` to create
a connection between two existing nodes or to a new node if the release
is over an empty area, in which case a quick menu for new node selection
is presented to the user.
"""
# direction of the drag
FROM_SOURCE = 1
FROM_SINK = 2
def __init__(self, document, *args, **kwargs):
UserInteraction.__init__(self, document, *args, **kwargs)
self.source_item = None
self.sink_item = None
link.sink_channel,
)
add_keys = list(map(link_key, links_to_add))
remove_keys = list(map(link_key, links_to_remove))
duplicate_keys = set(add_keys).intersection(remove_keys)
def not_duplicate(link):
return link_key(link) not in duplicate_keys
links_to_add = list(filter(not_duplicate, links_to_add))
links_to_remove = list(filter(not_duplicate, links_to_remove))
return links_to_add, links_to_remove
class NewNodeAction(UserInteraction):
"""
Present the user with a quick menu for node selection and
create the selected node.
"""
def mousePressEvent(self, event):
if event.button() == Qt.RightButton:
self.create_new(event.screenPos())
self.end()
def create_new(self, pos, search_text=""):
"""
Create a new widget with a `QuickMenu` at `pos` (in screen
coordinates).
def __init__(self, document, *args, **kwargs):
UserInteraction.__init__(self, document, *args, **kwargs)
self.item = None
self.annotation = None
self.control = None
self.savedFramePen = None
self.savedRect = None
if not duplicate:
commands.AddLinkCommand(self.scheme, link, parent=self.macro)
except scheme.IncompatibleChannelTypeError:
log.info("Cannot connect: invalid channel types.")
self.cancel()
except scheme.SchemeTopologyError:
log.info("Cannot connect: connection creates a cycle.")
self.cancel()
except NoPossibleLinksError:
log.info("Cannot connect: no possible links.")
self.cancel()
except UserCanceledError:
log.info("User canceled a new link action.")
self.cancel(UserInteraction.UserCancelReason)
except Exception:
log.error(
"An error occurred during the creation of a new link.", exc_info=True
)
self.cancel()
finally:
self.force_link_dialog = False
def cancel(self, reason=UserInteraction.OtherReason):
self.cleanup()
UserInteraction.cancel(self, reason)
def cancel(self, reason=UserInteraction.OtherReason):
self.cleanup()
UserInteraction.cancel(self, reason)
self.scene.removeItem(self.control)
self.control = None
self.down_pos = None
self.annotation_item = None
self.annotation = None
self.document.view().setCursor(Qt.ArrowCursor)
# Clear the help tip
helpevent = QuickHelpTipEvent("", "")
QCoreApplication.postEvent(self.document, helpevent)
UserInteraction.end(self)
class ResizeTextAnnotation(UserInteraction):
"""
Resize a Text Annotation interaction handler.
"""
def __init__(self, document, *args, **kwargs):
UserInteraction.__init__(self, document, *args, **kwargs)
self.item = None
self.annotation = None
self.control = None
self.savedFramePen = None
self.savedRect = None
def mousePressEvent(self, event):
pos = event.scenePos()
if event.button() & Qt.LeftButton and self.item is None:
item = self.scene.item_at(pos, items.TextAnnotation)
def mousePressEvent(self, event):
pos = event.scenePos()
if self.item is None:
item = self.scene.item_at(pos, items.ArrowAnnotation)
if item is not None and not item.hasFocus():
self.editItem(item)
return False
return UserInteraction.mousePressEvent(self, event)
def end(self):
if self.control is not None:
self.scene.removeItem(self.control)
if self.item is not None:
self.item.setFramePen(self.savedFramePen)
self.item = None
self.annotation = None
self.control = None
UserInteraction.end(self)
class ResizeArrowAnnotation(UserInteraction):
"""
Resize an Arrow Annotation interaction handler.
"""
def __init__(self, document, *args, **kwargs):
UserInteraction.__init__(self, document, *args, **kwargs)
self.item = None
self.annotation = None
self.control = None
self.savedLine = None
def mousePressEvent(self, event):
pos = event.scenePos()
if self.item is None:
item = self.scene.item_at(pos, items.ArrowAnnotation)
if item is not None and not item.hasFocus():