CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ConnectableWidgetOwner.py
Go to the documentation of this file.
1 from PyQt4.QtCore import QCoreApplication, QEvent, Qt
2 from PyQt4.QtGui import QMouseEvent,QWidget, QCursor
3 
4 import logging
5 
6 from Vispa.Gui.VispaWidgetOwner import VispaWidgetOwner
7 from Vispa.Gui.PortConnection import PortConnection
8 from Vispa.Gui.ConnectableWidget import ConnectableWidget
9 from Vispa.Gui.MenuWidget import MenuWidget
10 
11 class ConnectableWidgetOwner(VispaWidgetOwner):
12  """ Interface for classes containing ConnectableWidgets
13 
14  Only makes sense if implementing class also inherits QWidget or class inheriting QWidget.
15  """
16 
17  def getWidgetByName(self, name):
18  """ Returns module with given name or None if there is no such one.
19  """
20  for widget in [child for child in self.children() if hasattr(child, 'title')]:
21  if widget.title() == name:
22  return widget
23  return None
24 
25  def updateConnections(self):
26  """ Updates all connection.
27  """
28  #logging.debug(self.__class__.__name__ +": updateConnections()")
29  for child in self.children():
30  if isinstance(child, ConnectableWidgetOwner):
31  child.updateConnections()
32  if isinstance(child, PortConnection):
33  child.updateConnection()
34 
36  """ Deletes all selected connections.
37  """
38  for connection in [child for child in self.children() if isinstance(child, PointToPointConnection)]:
39  if connection.isSelected():
40  connection.delete()
41 
42  def portConnection(self, port1, port2=None):
43  """ Returns the PortConnection if there is a connection in this ConnectableWidgetOwner that is attached to the given port.
44 
45  Otherwise None will be returned.
46  """
47  for connection in [child for child in self.children() if isinstance(child, PortConnection)]:
48  if connection.attachedToPort(port1) and (not port2 or connection.attachedToPort(port2)):
49  return connection
50  return None
51 
52  def propagateEventUnderConnectionWidget(self, connection, event):
53  """ This function propagates an event to one of it's children.
54 
55  If a connection widget is clicked in an area where it does not draw the connection line, the event should be forwarded to the underlying widget if there is such one.
56  However the default behavior of Qt is to propagate the event to the connection's parent. This should be an ConnectableWidgetOwner object.
57  This function is a workaround searching for any child widget at event.pos() which is not the initial connection.
58  If it finds such a widget a new event with correct position in the new widget's own frame is created and sent to the widget.
59  This function calls grabMouse() on the found child. The child should make sure releaseMouse() will be called e.g. in mouseReleaseEvent().
60 
61  Currently supported events: QEvent.MouseButtonPress, QEvent.MouseButtonDblClick.
62  """
63  logging.debug("%s: propagateEventUnderConnectionWidget() - %s" % (self.__class__.__name__, str(event.type())))
64 
65  workspacePos = connection.mapToParent(event.pos())
66  for child in reversed(self.children()):
67  if not child==connection and isinstance(child,QWidget) and child.geometry().contains(workspacePos):
68  # do not forward event to connections which do not cross the mouse click point, this is important to prevent infinite loop error
69  if isinstance(child,PortConnection) and not child.belongsToRoute(workspacePos):
70  continue
71 # if event.type() == QEvent.MouseButtonDblClick or \
72 # event.type() == QEvent.MouseButtonPress or \
73 # event.type() == QEvent.MouseButtonRelease or \
74 # event.type() == QEvent.MouseMove or \
75 # event.type() == QEvent.DragEnter or \
76 # event.type() == QEvent.Drop:
77 
78  childPos = child.mapFromParent(workspacePos)
79  grandChild = child.childAt(childPos)
80  if grandChild:
81  child = grandChild
82  childPos = child.mapFromParent(childPos)
83  if event.type() == QEvent.MouseButtonPress:
84  child.grabMouse(QCursor(Qt.ClosedHandCursor))
85  child.setFocus()
86  newEvent = QMouseEvent(event.type(), childPos, event.button(), event.buttons(), event.modifiers())
87  QCoreApplication.instance().sendEvent(child, newEvent)
88  return True
89  return False
90 
91  def hideMenuWidgets(self):
92  for child in self.children():
93  if isinstance(child, MenuWidget):
94  child.hide()
bool contains(EventRange const &lh, EventID const &rh)
Definition: EventRange.cc:38