test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
WidgetView.py
Go to the documentation of this file.
1 import logging
2 
3 from PyQt4.QtCore import SIGNAL,Qt
4 from PyQt4.QtGui import QWidget
5 
6 from Vispa.Views.AbstractView import AbstractView
7 from Vispa.Gui.ZoomableScrollableWidgetOwner import ZoomableScrollableWidgetOwner
8 from Vispa.Gui.ZoomableScrollArea import ZoomableScrollArea
9 
11  """ Area for drawing connectable widgets.
12 
13  New widgets have to be added by using the addWidget function.
14  The WidgetView takes of the selection of widgets.
15  """
16 
17  LABEL = "&Widget View"
18 
19  def __init__(self, parent=None):
20  logging.debug(__name__ + ": __init__")
21  AbstractView.__init__(self)
22  ZoomableScrollableWidgetOwner.__init__(self, parent)
23 
24  self._widgetDict = {}
25  self._selection = None
26  self._updatingFlag = 0
27 
28  def widgets(self):
29  return self._widgetDict.values()
30 
31  def widgetSelected(self, widget, multiSelect=False):
32  """ Emits signal widgetSelected that the TabController can connect to.
33  """
34  logging.debug(__name__ + ": widgetSelected")
35  ZoomableScrollableWidgetOwner.widgetSelected(self, widget, multiSelect)
36  if not self._updatingFlag and hasattr(widget, "object"):
37  if hasattr(widget, "positionName"):
38  self._selection = widget.positionName
39  self.emit(SIGNAL("selected"), widget.object)
40  else:
41  self.emit(SIGNAL("selected"), widget.object())
42 
43  def deselectAllWidgets(self, exception=None):
44  """ Deselect all widget in view.
45  """
46  ZoomableScrollableWidgetOwner.deselectAllWidgets(self, exception)
47  if not self._updatingFlag:
48  self._selection = None
49  if not exception:
50  self.emit(SIGNAL("selected"), None)
51 
52  def widgetByObject(self, object):
53  widgets = []
54  for positionName, widget in self._widgetDict.items():
55  if widget.object == object:
56  widgets += [(positionName, widget)]
57  if len(widgets) > 0:
58  return sorted(widgets)[0][1]
59  else:
60  return None
61 
62  def select(self, object, offset=5):
63  """ Mark an object as selected.
64  """
65  widget = self.widgetByObject(object)
66  if widget!=None:
67  self._selection = widget.positionName
68  self._updatingFlag +=1
69  widget.select()
70  if self.parent() and isinstance(self.parent().parent(), ZoomableScrollArea):
71  self.parent().parent().ensureWidgetVisible(widget,offset,offset)
72  self._updatingFlag -=1
73  else:
74  self._selection = None
75  self._updatingFlag +=1
76  self.deselectAllWidgets()
77  self._updatingFlag -=1
78 
79  def restoreSelection(self,offset=5):
80  """ Restore selection.
81  """
82  if self._selection in self._widgetDict.keys():
83  widget = self._widgetDict[self._selection]
84  self._updatingFlag +=1
85  widget.select()
86  if self.parent() and isinstance(self.parent().parent(), ZoomableScrollArea):
87  self.parent().parent().ensureWidgetVisible(widget,offset,offset)
88  self._updatingFlag -=1
89 
90  def selection(self):
91  """ Return the currently selected object.
92  """
93  if self._selection in self._widgetDict.keys():
94  return self._widgetDict[self._selection].object
95  else:
96  return None
97 
98  def clear(self):
99  """ Deletes all boxes in the WidgetView
100  """
101  # logging statement here can be very useful for debugging / improving performance
102  logging.debug(__name__ + ": clear")
103  self._widgetDict = {}
104  for w in self.children():
105  if isinstance(w,QWidget):
106  w.setParent(None)
107  w.deleteLater()
108 
109  def addWidget(self, widget, object=None, positionName=0):
110  """ Add widget to the view and map it to an id.
111  """
112  if str(positionName)+"("+self.dataAccessor().label(object)+")" in self._widgetDict.keys():
113  positionName = 0
114  while positionName in self._widgetDict.keys():
115  positionName += 1
116  widget.positionName = str(positionName)+"("+self.dataAccessor().label(object)+")"
117  widget.object = object
118  self._widgetDict[widget.positionName] = widget
119 
120  def closeEvent(self, event):
121  self.clear()
122  event.accept()
123 
124  def setDataObjects(self, objects):
125  AbstractView.setDataObjects(self, objects)
126  self.clear()
127 
128  def mousePressEvent(self,event):
129  if event.button()==Qt.RightButton:
130  self.emit(SIGNAL("mouseRightPressed"), event.globalPos())
131  ZoomableScrollableWidgetOwner.mousePressEvent(self,event)
132 
133  def isBusy(self):
134  return self._updatingFlag>0
135 
136  def widgetDoubleClicked(self, widget):
137  """ Emits signal doubleClicked that the TabController can connect to.
138  """
139  logging.debug(__name__ + ": widgetDoubleClicked")
140  ZoomableScrollableWidgetOwner.widgetDoubleClicked(self, widget)
141  if hasattr(widget, "object"):
142  if hasattr(widget, "positionName"):
143  self._selection = widget.positionName
144  self.emit(SIGNAL("doubleClicked"), widget.object)
145  else:
146  self.emit(SIGNAL("doubleClicked"), widget.object())