CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
VispaWidgetOwner.py
Go to the documentation of this file.
1 from PyQt4.QtCore import QObject, QRect, QPoint, Qt
2 from PyQt4.QtGui import QApplication
3 
4 import logging
5 
7  """ Interface for classes containing VispaWidgets
8 
9  Only makes sense if implementing class also inherits QWidget or class inheriting QWidget.
10  """
11 
12  def enableMultiSelect(self, multiSelect=True):
13  self._multiSelectEnabledFlag = multiSelect
14 
15  def multiSelectEnabled(self):
16  return hasattr(self, "_multiSelectEnabledFlag") and self._multiSelectEnabledFlag
17 
18  def selectedWidgets(self):
19  """ Returns a list of all selected widgets.
20  """
21  if hasattr(self, "_selectedWidgets"):
22  return self._selectedWidgets
23  return [child for child in self.children() if hasattr(child, "isSelected") and child.isSelected()]
24 
25  def widgetSelected(self, widget, multiSelect=False):
26  """ Forward selection information to super class if it is a VispaWidgetOwner.
27  """
28  logging.debug(self.__class__.__name__ +": widgetSelected()")
29 
30  if isinstance(self, QObject):
31  if not hasattr(self, "_selectedWidgets"):
32  self._selectedWidgets = []
33 
34  if not multiSelect or not self.multiSelectEnabled():
35  self.deselectAllWidgets(widget)
36  self._selectedWidgets = []
37 
38  if widget.parent() == self and not widget in self._selectedWidgets:
39  self._selectedWidgets.append(widget)
40 
41  for widget in [child for child in self._selectedWidgets if hasattr(child, "isSelected") and not child.isSelected()]:
42  self._selectedWidgets.remove(widget)
43 
44  if isinstance(self.parent(), VispaWidgetOwner):
45  self.parent().widgetSelected(widget)
46 
47  def widgetDoubleClicked(self, widget):
48  """ Forward selection information to super class if it is a VispaWidgetOwner.
49  """
50  #logging.debug(self.__class__.__name__ +": widgetDoubleClicked()")
51  if isinstance(self, QObject):
52  if isinstance(self.parent(), VispaWidgetOwner):
53  self.parent().widgetDoubleClicked(widget)
54 
55  def initWidgetMovement(self, widget):
57  if self.multiSelectEnabled():
58  pos = widget.pos()
59  for child in self.children():
60  if child != widget and hasattr(child, "isSelected") and child.isSelected():
61  child.setDragReferencePoint(pos - child.pos())
62 
63  def widgetDragged(self, widget):
64  """ Tell parent widget has moved.
65 
66  Only informs parent if it is a VispaWidgetOwner, too.
67  """
68  if isinstance(self.parent(), VispaWidgetOwner):
69  self.parent().widgetDragged(widget)
70 
71  if hasattr(self, "_lastMovedWidgets"):
72  self._lastMovedWidgets.append(widget)
73 
74  if self.multiSelectEnabled():
75  for child in self.children():
76  if hasattr(child, "dragReferencePoint") and child != widget and hasattr(child, "isSelected") and child.isSelected():
77  if hasattr(child, "setPreviousDragPosition"):
78  child.setPreviousDragPosition(child.pos())
79  child.move(widget.pos() - child.dragReferencePoint())
80  self._lastMovedWidgets.append(child)
81 
82 # apparently unused feature (2010-07-02), remove if really unnecessary
83 # also see self._lastMovedWidget definition above
84  def lastMovedWidgets(self):
85  if hasattr(self, "_lastMovedWidgets"):
86  return self._lastMovedWidgets
87  return None
88 
89  def widgetAboutToDelete(self, widget):
90  """ This function is called from the delete() function of VispaWidget.
91  """
92  pass
93 
94  def keyPressEvent(self, event):
95  """ Calls delete() method of selected child widgets if multi-select is activated.
96  """
97  if self.multiSelectEnabled() and ( event.key() == Qt.Key_Backspace or event.key() == Qt.Key_Delete ):
98  selection = self.selectedWidgets()[:]
99  for widget in selection:
100  widget.delete()
101 
102  def deselectAllWidgets(self, exception=None):
103  """ Deselects all widgets except the widget given as exception.
104  """
105  #logging.debug(self.__class__.__name__ +": deselectAllWidgets()")
106  self._selectedWidgets = []
107  for child in self.children():
108  if child != exception:
109  if hasattr(child, 'select'):
110  child.select(False)
111  else:
112  self._selectedWidgets.append(child)
113 
114  if isinstance(child, VispaWidgetOwner):
115  child.deselectAllWidgets(exception)
116  self.update()
117 
118  def mousePressEvent(self, event):
119  """ Calls deselectAllWidgets.
120  """
121  multiSelectEnabled = self.multiSelectEnabled()
122  if event.modifiers() != Qt.ControlModifier:
123  self.deselectAllWidgets()
124  if multiSelectEnabled:
125  self._selectionRectStartPos = QPoint(event.pos())
126  self._selectionRect = None
127 
128  def mouseMoveEvent(self, event):
129  if self.multiSelectEnabled() and self._selectionRectStartPos and (event.pos() - self._selectionRectStartPos).manhattanLength() >= QApplication.startDragDistance():
130  eventX = event.pos().x()
131  eventY = event.pos().y()
132  startX = self._selectionRectStartPos.x()
133  startY = self._selectionRectStartPos.y()
134  oldRect = self._selectionRect
135  self._selectionRect = QRect(min(startX, eventX), min(startY, eventY), abs(eventX - startX), abs(eventY - startY))
136  if oldRect:
137  self.update(self._selectionRect.united(oldRect).adjusted(-5, -5, 5, 5))
138  else:
139  self.update(self._selectionRect)
140 
141  # dynamically update selection statur
142  # currently bad performance (2010-07-07)
143  # TODO: improve selection mechanism
144 # for child in self.children():
145 # if hasattr(child, "select") and hasattr(child, "isSelected"):
146 # child.select(self._selectionRect.contains(child.geometry()), True) # select, mulitSelect
147 
148  def mouseReleaseEvent(self, event):
149  if hasattr(self, "_selectionRect") and self._selectionRect and self.multiSelectEnabled():
150  for child in self.children():
151  if hasattr(child, "select") and hasattr(child, "isSelected") and self._selectionRect.contains(child.geometry()) and not child.isSelected():
152  child.select(True, True) # select, mulitSelect
153  self.update(self._selectionRect.adjusted(-5, -5, 5, 5))
154  self._selectionRect = None
155  self._selectionRectStartPos = None
156 
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
list object
Definition: dbtoconf.py:77
Definition: DDAxes.h:10