CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PortWidget.py
Go to the documentation of this file.
1 import logging
2 
3 from PyQt4.QtCore import *
4 from PyQt4.QtGui import *
5 
6 from Vispa.Gui.VispaWidget import VispaWidget
7 from Vispa.Gui.PortConnection import PortConnection,PointToPointConnection
8 
10  """ This widget is used to dispay sink and source port of ConnectableWidget.
11  """
12  # Overridden attibutes
13  WIDTH = 6
14  HEIGHT = WIDTH
15  BACKGROUND_SHAPE = 'CIRCLE'
16  TITLEFIELD_FONTSIZE = 10
17  SELECTABLE_FLAG = False
18 
19  # New attributes
20  PORT_TYPE = ''
21  CONNECTIONPOINT_X = 3
22  CONNECTIONPOINT_Y = 3
23  CONNECTION_DIRECTION = PointToPointConnection.ConnectionDirection.UNDEFINED # Default
24  DRAG_PORT_TEXT = 'connect port' # Text shown while dragging. The drop event is verified by this string, so should not be empty.
25 
26  def __init__(self, parent=None, name='default'):
27  """ Constructor.
28  """
29  VispaWidget.__init__(self, parent)
30  self.setName(name)
32  self._startDragPosition = None
33  self.setDragable(True)
34  self._aimConnection = None
36 
37  def attachConnection(self, connection):
38  self._attachedConnections.append(connection)
39 
40  def detachConnection(self, connection):
41  if connection in self._attachedConnections:
42  self._attachedConnections.remove(connection)
43  else:
44  logging.error("%s: detachConnection(): Tried to detach a connection that was not attached ot this port. Aborting..." % self.__class__.__name__)
45 
47  for connection in self._attachedConnections:
48  connection.updateConnection()
49 
51  for connection in self._attachedConnections:
52  connection.delete()
53 
55  return self._attachedConnections
56 
57  def setDragable(self,dragable, recursive=False):
58  """ Set whether user can grab the port and connect it to others.
59  """
60  VispaWidget.setDragable(self,False, recursive)
61  self._dragablePort=dragable
62  self.setAcceptDrops(dragable)
63 
64  def isDragable(self):
65  return self._dragablePort
66 
67  def setName(self, name):
68  """ Sets name of port.
69 
70  Name will be shown as tooltip unless a descriptions is set.
71  See setDescription().
72  """
73  #self._initTitleField()
74  #self.titleField().setAutoscale(True, False)
75  self.setTitle(name)
76  self.setToolTip(name)
77 
78  def name(self):
79  """ Returns name of this port.
80  """
81  if self.titleIsSet():
82  return self.title()
83  return ''
84 
85  def setDescription(self, description):
86  """ Sets description text of this port.
87 
88  Description will be shown as tooltip.
89  """
90  self.setText(description)
91 
92  def description(self):
93  """ Returns description text.
94  """
95  if self.textFieldIsSet():
96  return self.textField.getText()
97  return ''
98 
99  def portType(self):
100  """ Return type of this port.
101 
102  The value has to be set by inheriting classes.
103  """
104  return self.PORT_TYPE
105 
106  def connectionPoint(self, frame="workspace"):
107  """ Returns point within this port from which attached connections should start.
108 
109  Possible values for the optional frame argument are 'workspace' (default or invalid value), 'widget' and 'port'.
110  This value of this argument decides in which frame the coordinates of the returned point are measured.
111  """
112  point = QPoint(self.CONNECTIONPOINT_X, self.CONNECTIONPOINT_Y) * self.scale()
113 
114  if frame == "port":
115  return point
116  if frame == "widget":
117  return self.pos() + point
118 
119  return self.parent().mapToParent(self.pos() + point)
120 
122  """ Returns the direction in which an attached connection should start.
123  """
124  return self._connectionDirection
125 
126  def moduleParent(self):
127  """ Returns parent of this port's parent.
128 
129  As the port should belong to a ConnectableWidget the function returns the QWidget in which the ConnectableWidget lives.
130  """
131  # Port belongs to module
132  # Think of better solution. Port shall not create PortConnection.
133  if self.parent():
134  return self.parent().parent()
135  else:
136  return None
137 
138  def drawTitle(self, painter):
139  """ Overwrite VispaWidget.drawTitle()
140 
141  Do not show title, instead ConnectableWidget will show title/name.
142  """
143  pass
144 
145  def drawTextField(self, painter):
146  """ Overwrite VispaWidget.drawTextField()
147 
148  Do not show text field, just using the text for tooltip.
149  """
150  pass
151 
152  def mousePressEvent(self, event):
153  """ Registers position for starting drag.
154  """
155  logging.debug("%s: mousePressEvent()" % self.__class__.__name__)
156  if self._dragablePort and event.button() == Qt.LeftButton:
157  self._startDragPosition = QPoint(event.pos())
158  VispaWidget.mousePressEvent(self, event)
159 
160  def mouseMoveEvent(self, event):
161  """ If minimum distance from mousePressEvent is reached initiates dragging.
162  """
163  #logging.debug(self.__class__.__name__ +": mouseMoveEvent()")
164  if self._dragablePort and self._startDragPosition and bool(event.buttons() & Qt.LeftButton):
165  if not self._aimConnection and (event.pos() - self._startDragPosition).manhattanLength() >= QApplication.startDragDistance():
166  self._aimConnection = PointToPointConnection(self.moduleParent(), self.connectionPoint(), self.mapTo(self.moduleParent(), event.pos()))
167  self._aimConnection.setSourceDirection(self.CONNECTION_DIRECTION)
168  self.connect(self._aimConnection, SIGNAL("connectionDeleted"), self.resetAimConnection)
169 
170  if self.CONNECTION_DIRECTION == PointToPointConnection.ConnectionDirection.RIGHT:
171  self._aimConnection.setTargetDirection(PointToPointConnection.ConnectionDirection.LEFT)
172  elif self.CONNECTION_DIRECTION == PointToPointConnection.ConnectionDirection.LEFT:
173  self._aimConnection.setTargetDirection(PointToPointConnection.ConnectionDirection.RIGHT)
174 
175  self._aimConnection.show()
176  elif self._aimConnection:
177  self._aimConnection.updateTargetPoint(self.mapTo(self.moduleParent(), event.pos()))
178  VispaWidget.mouseMoveEvent(self, event)
179 
180  def mouseReleaseEvent(self, event):
181  """ Calls realeseMouse() to make sure the widget does not grab the mouse.
182 
183  Necessary because ConnectableWidgetOwner.propagateEventUnderConnectionWidget() may call grabMouse() on this widget.
184  """
185  logging.debug(self.__class__.__name__ +": mouseReleaseEvent()")
186  self.releaseMouse()
187  if self._dragablePort:
188  if self._aimConnection:
189  self._aimConnection.hide() # don't appear as childWidget()
190  self._aimConnection.delete()
191  self._aimConnection = None
192 
193  moduleParentPosition = self.mapTo(self.moduleParent(), event.pos())
194  #widget = self.moduleParent().childAt(moduleParentPosition)
195  widget = None
196  for child in reversed(self.moduleParent().children()):
197  if isinstance(child,QWidget) and child.isVisible() and child.geometry().contains(moduleParentPosition) and not isinstance(child, PortConnection):
198  widget = child
199  break
200 
201  if hasattr(widget, "dropAreaPort"):
202  localPosition = widget.mapFrom(self.moduleParent(), moduleParentPosition)
203  self.moduleParent().addPortConnection(self, widget.dropAreaPort(localPosition))
204  elif isinstance(widget, PortWidget):
205  self.moduleParent().addPortConnection(self, widget)
206 
207  VispaWidget.mouseReleaseEvent(self, event)
208 
210  logging.debug("%s: resetAimConnection()" % self.__class__.__name__)
211  self._aimConnection = None
212 
214  """ Class for sink port of ConnectableWidgets.
215 
216  Sets colors, port type and connection direction.
217  """
218 
219  PEN_COLOR = QColor()
220  FILL_COLOR1 = QColor(0, 150, 0)
221  FILL_COLOR2 = QColor(0, 100, 0)
222  PORT_TYPE = 'sink'
223  CONNECTION_DIRECTION = PointToPointConnection.ConnectionDirection.LEFT
224  TITLE_COLOR = FILL_COLOR1
225 
226 # def __init__(self, parent=None, name='default'):
227 # """ Constructor.
228 # """
229 # PortWidget.__init__(self, parent, name)
230 
231 
233  """ Class for sink port of ConnectableWidgets.
234 
235  Sets colors, port type and connection direction.
236 
237  """
238 
239  PEN_COLOR = QColor()
240  FILL_COLOR1 = QColor(150, 0, 0)
241  FILL_COLOR2 = QColor(100, 0, 0)
242  PORT_TYPE = 'source'
243  CONNECTION_DIRECTION = PointToPointConnection.ConnectionDirection.RIGHT
244  TITLE_COLOR = FILL_COLOR1
245 
246 # def __init__(self, parent=None, name='default'):
247 # """ Constructor.
248 # """
249 # PortWidget.__init__(self, parent, name)
250 
bool contains(EventRange const &lh, EventID const &rh)
Definition: EventRange.cc:38