CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EventBrowserTabController.py
Go to the documentation of this file.
1 import logging
2 import os.path
3 import sys
4 
5 from PyQt4.QtCore import QCoreApplication,SIGNAL
6 from PyQt4.QtGui import QInputDialog
7 
8 from Vispa.Main.Application import Application
9 from Vispa.Plugins.Browser.BrowserTabController import BrowserTabController
10 from Vispa.Plugins.EventBrowser.EventFileAccessor import EventFileAccessor
11 from Vispa.Share.ThreadChain import ThreadChain
12 
14  """ The EventBrowserTabController supplies functionality for browsing objects in an EventBrowserTab and navigating through events.
15  """
16  def __init__(self, plugin):
17  logging.debug(__name__ + ": __init__")
18  BrowserTabController.__init__(self, plugin)
19 
20  self.setEditable(False)
21  self._thread=None
22  self._navigateTo=None
23  self.connect(self,SIGNAL("navigate"),self.navigate)
24 
25  def close(self):
26  """ Close data file.
27  """
28  result=BrowserTabController.close(self)
29  if result:
30  self.dataAccessor().close()
31  return result
32 
33  def setDataAccessor(self, accessor):
34  if not isinstance(accessor, EventFileAccessor):
35  raise TypeError(__name__ + " requires data accessor of type EventFileAccessor.")
36  BrowserTabController.setDataAccessor(self, accessor)
37 
38  def activated(self):
39  """ Show event menu when tab is shown.
40  """
41  BrowserTabController.activated(self)
43  if not self.isEditable():
44  self.plugin().application().showPluginMenu(self.plugin().navigateMenu())
45  self.plugin().application().showPluginToolBar(self.plugin().navigateToolBar())
46 
47  def refresh(self):
48  eventNum=self._dataAccessor.eventNumber()
49  self._fileModifcationTimestamp = os.path.getmtime(self._filename)
50  self.navigate(0)
51  self.navigate(eventNum)
52 
53  def readFile(self, filename):
54  """ Reads in the file in a separate thread.
55  """
56  self.cancel()
57  self._thread = ThreadChain(self.dataAccessor().open, filename)
58  while self._thread.isRunning():
59  if not Application.NO_PROCESS_EVENTS:
60  QCoreApplication.instance().processEvents()
61  if self._thread.returnValue():
63  return True
64  return False
65 
66  def isBusy(self):
67  return BrowserTabController.isBusy(self) or\
68  (self._thread and self._thread.isRunning())
69 
70  def navigate(self,to):
71  # remember if navigation is ongoing
72  navigating=self._navigateTo
73  # set where to navigate
74  self._navigateTo=to
75  # if navigation is ongoing return
76  if navigating!=None:
77  return
78  # if window is busy navigate later
79  if self.isBusy():
80  self.emit(SIGNAL("navigate"),to)
81  return
82  update=False
83  while self._navigateTo!=None:
84  current=self._navigateTo
85  if self._navigateTo==0:
86  statusMessage = self.plugin().application().startWorking("Reopening file")
87  self.dataAccessor().close()
88  self.readFile(self._filename)
89  else:
90  statusMessage = self.plugin().application().startWorking("Navigate in file")
91  if self._dataAccessor.goto(self._navigateTo):
92  update=True
93  if current==self._navigateTo:
94  self._navigateTo=None
95  if update:
96  self.updateContent()
98  self.plugin().application().stopWorking(statusMessage)
99 
100  def first(self):
101  """ Navigate and to first event.
102  """
103  logging.debug(__name__ + ": first")
104  self.cancel()
105  currentEvent=self.dataAccessor().eventNumber()
106  if currentEvent>1:
107  self.navigate(1)
108 
109  def previous(self):
110  """ Navigate and to previous event.
111  """
112  logging.debug(__name__ + ": previous")
113  self.cancel()
114  currentEvent=self.dataAccessor().eventNumber()
115  if currentEvent>1:
116  self.navigate(currentEvent-1)
117 
118  def next(self):
119  """ Navigate and to next event.
120  """
121  logging.debug(__name__ + ": next")
122  self.cancel()
123  currentEvent=self.dataAccessor().eventNumber()
124  allEvents=self.dataAccessor().numberOfEvents()
125  if allEvents==None:
126  allEvents=sys.maxsize
127  if currentEvent<allEvents:
128  self.navigate(currentEvent+1)
129 
130  def last(self):
131  """ Navigate and to last event.
132  """
133  logging.debug(__name__ + ": last")
134  self.cancel()
135  currentEvent=self.dataAccessor().eventNumber()
136  allEvents=self.dataAccessor().numberOfEvents()
137  if allEvents==None:
138  allEvents=sys.maxsize
139  if currentEvent<allEvents:
140  self.navigate(allEvents)
141 
142  def goto(self, number=None):
143  """ Ask event number in dialog and navigate and to event.
144  """
145  logging.debug(__name__ + ": goto")
146  if self._dataAccessor.numberOfEvents():
147  max = self._dataAccessor.numberOfEvents()
148  else:
149  max = sys.maxsize
150  if number!=None:
151  ok=(number>=1, number<=max)
152  else:
153  if hasattr(QInputDialog, "getInteger"):
154  # Qt 4.3
155  (number, ok) = QInputDialog.getInteger(self.plugin().application().mainWindow(), "Goto...", "Enter event number:", self._dataAccessor.eventNumber(), 1, max)
156  else:
157  # Qt 4.5
158  (number, ok) = QInputDialog.getInt(self.plugin().application().mainWindow(), "Goto...", "Enter event number:", self._dataAccessor.eventNumber(), 1, max)
159  if ok:
160  self.cancel()
161  currentEvent=self.dataAccessor().eventNumber()
162  if currentEvent!=number:
163  self.navigate(number)
164 
166  eventDisplayString = str(self._dataAccessor.eventNumber()) + "/"
167  if self._dataAccessor.numberOfEvents():
168  eventDisplayString += str(self._dataAccessor.numberOfEvents())
169  else:
170  eventDisplayString += "?"
171  self.plugin().eventNumberDisplay().setText(eventDisplayString)