CalibManager/src/GUIDarkList.py

Go to the documentation of this file.
00001 #--------------------------------------------------------------------------
00002 # File and Version Information:
00003 #  $Id: GUIDarkList.py 11469 2016-03-08 01:29:03Z dubrovin@SLAC.STANFORD.EDU $
00004 #
00005 # Description:
00006 #   GUIDarkList ...
00007 #------------------------------------------------------------------------
00008 __version__ = "$Revision: 11469 $"
00009 #--------------------------------
00010 
00011 #import os
00012 
00013 from PyQt4 import QtGui, QtCore
00014 
00015 from ConfigParametersForApp import cp
00016 from Logger                 import logger
00017 from FileNameManager        import fnm
00018 from GUIDarkListItem        import *
00019 import GlobalUtils          as     gu
00020 import RegDBUtils           as     ru
00021 from BatchLogScanParser     import blsp
00022 
00023 from time import time
00024 
00025 #------------------------------
00026 
00027 class GUIDarkList(QtGui.QWidget) :
00028     """GUI for the list of widgers"""
00029 
00030     def __init__(self, parent=None) :
00031 
00032         self.parent = parent
00033         self.dark_list_run_min     = cp.dark_list_run_min 
00034         self.dark_list_run_max     = cp.dark_list_run_max 
00035         self.dark_list_show_runs   = cp.dark_list_show_runs
00036         self.dark_list_show_dets   = cp.dark_list_show_dets
00037         self.list_of_show_dets     = cp.list_of_show_dets
00038         self.instr_name            = cp.instr_name
00039         self.exp_name              = cp.exp_name
00040         self.det_name              = cp.det_name
00041         self.str_run_number        = cp.str_run_number
00042         self.list_of_det_pars      = cp.list_of_det_pars
00043         self.list_of_dets_selected = cp.list_of_dets_selected
00044         self.dict_guidarklistitem  = cp.dict_guidarklistitem
00045         self.list_of_visible_records = []
00046 
00047         self.click_counter = 0
00048         self.update_counter = 0
00049 
00050         #self.calib_dir      = cp.calib_dir
00051         #self.det_name       = cp.det_name
00052 
00053         #QtGui.QGroupBox.__init__(self, 'Runs', parent)
00054         QtGui.QWidget.__init__(self, parent)
00055 
00056         self.setGeometry(100, 100, 800, 300)
00057         self.setWindowTitle('List of dark runs')
00058         #self.setTitle('My status')
00059         #try : self.setWindowIcon(cp.icon_help)
00060         #except : pass
00061 
00062         #self.list = QtGui.QListWidget(parent=self)
00063         # Use singleton object
00064         if cp.dark_list is None : self.list = cp.dark_list = QtGui.QListWidget()
00065         else                    : self.list = cp.dark_list
00066 
00067         self.updateList()
00068 
00069         vbox = QtGui.QVBoxLayout()
00070         vbox.addWidget(self.list)
00071         self.setLayout(vbox)
00072 
00073         self.list.itemClicked.connect(self.onItemClick)
00074         self.list.itemDoubleClicked.connect(self.onItemDoubleClick)
00075 
00076         #self.connect(self.list.horizontalHeader(),
00077         #             QtCore.SIGNAL('sectionClicked (int)'),
00078         #             self.random_function)
00079  
00080         self.showToolTips()
00081         self.setStyle()
00082 
00083         self.connectToThreadWorker()
00084 
00085         cp.guidarklist = self
00086 
00087 
00088     def connectToThreadWorker(self):
00089         try : self.connect(cp.thread_check_new_xtc_files, QtCore.SIGNAL('update(QString)'), self.signalReciever)
00090         except : logger.warning('connectToThreadWorker is failed', __name__)
00091 
00092 
00093     def disconnectFromThreadWorker(self):
00094         try : self.disconnect(cp.thread_check_new_xtc_files, QtCore.SIGNAL('update(QString)'), self.signalReciever)
00095         except : pass
00096 
00097 
00098     def signalReciever(self, text):
00099         msg = 'Signal received: new xtc file is available, msg: %s' % text
00100         #print msg 
00101         logger.info(msg, __name__)
00102         self.updateList()
00103         self.scrollDown()
00104 
00105 
00106     def scrollDown(self):
00107         #self.list.moveCursor(QtGui.QTextCursor.End)
00108         #self.list.repaint()
00109         last_run_num = self.list_of_runs[-1]
00110         item, widg = self.dict_guidarklistitem[last_run_num]
00111         self.list.scrollToItem (item) #, hint=QAbstractItemView.EnsureVisible)
00112 
00113 
00114     def updateList(self, clearList=False) :
00115 
00116         self.update_counter += 1
00117 
00118         self.t0_sec = time()
00119 
00120         if clearList :
00121             self.removeItemWidgets()            
00122             self.list.clear()
00123             self.dict_guidarklistitem.clear()
00124             #msg = 'Consumed time to clean list (sec) = %7.3f' % (time()-self.t0_sec)        
00125             #print msg
00126 
00127         self.setItemsHidden()
00128 
00129         self.list_of_visible_records = []
00130 
00131         if self.instr_name.value() == self.instr_name.value_def() : return
00132         if self.exp_name  .value() == self.exp_name  .value_def() : return
00133 
00134         str_exp_name = self.exp_name.value()
00135 
00136         msg = 'Begin to update the list of runs. It is slow procedure that takes ~0.1s/run, stay calm and wait.'
00137         logger.info(msg, __name__)
00138         #print msg
00139         # Get run records from RegDB
00140         self.dict_run_recs = ru.calibration_runs (self.instr_name.value(), self.exp_name.value())
00141         #print 'self.dict_run_recs = ', self.dict_run_recs
00142 
00143         self.list_of_run_strs_in_dir = fnm.get_list_of_xtc_runs()   # ['0001', '0202', '0203',...]
00144         self.list_of_run_nums_in_dir = gu.list_of_int_from_list_of_str(self.list_of_run_strs_in_dir) # [1, 202, 203, 204,...]
00145         self.list_of_run_nums_in_regdb = ru.list_of_runnums(self.instr_name.value(), self.exp_name.value())
00146 
00147         #print 'list_of_run_nums_in_dir  :', self.list_of_run_nums_in_dir
00148         #print 'list_of_run_nums_in_regdb:', self.list_of_run_nums_in_regdb
00149         #print '\nA. Consumed time (sec) =', time()-self.t0_sec
00150         #print 'Begin to construct the list of items for %s' % self.exp_name.value()
00151 
00152         
00153         if self.list_of_run_nums_in_regdb == [] : self.list_of_runs = self.list_of_run_nums_in_dir
00154         else                                    : self.list_of_runs = self.list_of_run_nums_in_regdb
00155 
00156 
00157         for run_num in self.list_of_runs :
00158 
00159             str_run_num = '%04d' % run_num
00160             self.str_run_number.setValue(str_run_num)
00161 
00162             if not self.isSelectedRun            (run_num) :
00163                 #print 'not self.isSelectedRun', run_num 
00164                 continue
00165 
00166             if not self.hasSelectedDetectorsInRun(run_num) :
00167                 #print 'not self.hasSelectedDetectorsInRun', run_num 
00168                 continue
00169 
00170             if not run_num in self.list_of_run_nums_in_dir :
00171                 self.comment = 'xtc file IS NOT on disk!'
00172                 self.xtc_in_dir = False
00173             else :
00174                 self.xtc_in_dir = True
00175                 #self.type    = 'N/A'
00176 
00177             item, widg = self.create_or_use_guidarklistitem(run_num)
00178 
00179             self.list.setItemHidden (item, False)
00180 
00181             record = run_num, item, widg
00182             self.list_of_visible_records.append(record)
00183 
00184 
00185         self.list.sortItems(QtCore.Qt.AscendingOrder)
00186 
00187         msg = 'Consumed time to generate list of files (sec) = %7.3f' % (time()-self.t0_sec)        
00188         logger.info(msg, __name__)
00189 
00190 
00191 
00192     def create_or_use_guidarklistitem(self, run_num) :
00193         """Creates QListWidgetItem and GUIDarkListItem objects for the 1st time and add them to self.list or use existing from the dictionary
00194         """
00195         if run_num in self.dict_guidarklistitem.keys() :
00196             #print 'Use existing GUIDarkListItem object for run %d' % run_num
00197             item, widg = self.dict_guidarklistitem[run_num]
00198             widg.updateButtons(self.type, self.comment, self.xtc_in_dir)
00199             return item, widg
00200         else :
00201             #print 'Create new GUIDarkListItem object for run %d' % run_num
00202             str_run_num = '%04d'%run_num
00203             item = QtGui.QListWidgetItem(str_run_num, self.list)
00204             widg = GUIDarkListItem(self, str_run_num, self.type, self.comment, self.xtc_in_dir)  
00205             self.dict_guidarklistitem[run_num] = [item, widg]
00206             item.setTextColor(QtGui.QColor(0, 0, 0, alpha=0)) # set item text invisible. All pars in the range [0,255]
00207             item.setFlags ( QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable  | QtCore.Qt.ItemIsUserCheckable )
00208             item.setSizeHint(widg.size())
00209             self.list.setItemWidget(item, widg)
00210             return item, widg
00211 
00212 
00213     def setItemsHidden(self) :        
00214         for run, (item, widg) in self.dict_guidarklistitem.iteritems() :
00215             self.list.setItemHidden (item, True)
00216             #print 'Hide item for run %d' % run
00217  
00218 
00219     def removeItemWidgets(self) :     
00220         for run, (item, widg) in self.dict_guidarklistitem.iteritems() :
00221             self.list.removeItemWidget(item)
00222             widg.close()
00223 
00224 
00225     def isSelectedRun(self, run_num, type_to_select = 'dark') :
00226         # Unpack RegDB info
00227         if self.dict_run_recs != {} : ### and run_num in self.dict_run_recs.keys() : ###!!!!!!!!!!!!!!!!!!!
00228             run_rec = self.dict_run_recs[run_num]
00229             list_of_calibs = run_rec['calibrations']
00230             self.comment   = run_rec['comment']
00231             self.is_found_type = type_to_select in list_of_calibs
00232             if self.is_found_type : self.type = type_to_select
00233             else                  : self.type = ''
00234         else :
00235             self.is_found_type = False
00236             self.comment = ''
00237             self.type = ''
00238 
00239         if self.dark_list_show_runs.value() == 'in range' : # self.list_of_show_runs[0]
00240             if   run_num > self.dark_list_run_max.value() : return False
00241             elif run_num < self.dark_list_run_min.value() : return False
00242             else                                          : return True
00243 
00244         #if self.dark_list_show_runs.value() == 'dark' : # self.list_of_show_runs[1]
00245         if self.is_found_type :
00246             return True
00247 
00248         if self.dark_list_show_runs.value() == 'all' : # self.list_of_show_runs[2]
00249             return True
00250 
00251         return False
00252 
00253 
00254     def det_is_in_list_of_sources(self, det_name, list_of_srcs) :
00255 
00256         pattern = det_name.lower() + '.'
00257         for src in list_of_srcs :
00258             if src.lower().find(pattern) != -1 :
00259                 return True
00260         return False
00261 
00262 
00263     def hasSelectedDetectorsInRun(self, run_num) :
00264 
00265         if self.dark_list_show_dets.value() == self.dark_list_show_dets.value_def() : # 'all' - For all detectors
00266             return True
00267 
00268         if self.det_name.value() == '' : # If detector(s) were not selected
00269             logger.warning('Detector is not selected !!!', __name__)
00270             return False
00271 
00272         list_of_srcs = blsp.get_list_of_sources()
00273 
00274         if self.dark_list_show_dets.value() == self.list_of_show_dets[1] : # 'selected any' - For any of selected detectors in run
00275             for det in self.list_of_dets_selected() :
00276                 if self.det_is_in_list_of_sources(det, list_of_srcs) : return True
00277             return False
00278 
00279         if self.dark_list_show_dets.value() == self.list_of_show_dets[2] : # 'selected all' - For all selected detectors in run
00280             for det in self.list_of_dets_selected() :
00281                 if not self.det_is_in_list_of_sources(det, list_of_srcs) : return False
00282             return True
00283 
00284         return True
00285 
00286 
00287     def setFieldsEnabled(self, is_enabled=False):
00288         #for run, (item, widg) in self.dict_guidarklistitem.iteritems() :
00289         for (run, item, widg) in self.list_of_visible_records :
00290             #print '  run:', run          
00291             widg.setFieldsEnabled(is_enabled)
00292 
00293 
00294     def getRunAndItemForWidget(self, widg_active):
00295         #for run, (item, widg) in self.dict_guidarklistitem.iteritems() :
00296         for (run, item, widg) in self.list_of_visible_records :
00297             if widg == widg_active :
00298                 return '%04d'%run, item
00299         return None, None
00300 
00301 
00302     def showToolTips(self):
00303         #self           .setToolTip('This GUI is intended for run control and monitoring.')
00304         #self.but_close .setToolTip('Close this window.')
00305         pass
00306 
00307 
00308     def setStyle(self):
00309         self.setMinimumSize(760,80)
00310         self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
00311         self.setStyleSheet(cp.styleBkgd)
00312         self.setContentsMargins(QtCore.QMargins(-9,-9,-9,-9))
00313 
00314         #self.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)
00315         #self.list.adjustSize()
00316         #print 'self.list.size():',  self.list.size()
00317         #self.setMinimumSize(self.list.size())
00318 
00319         #self.tit_status.setStyleSheet (cp.styleTitle)
00320         #self.tit_status.setStyleSheet (cp.styleDefault)
00321         #self.tit_status.setStyleSheet (cp.styleTitleInFrame)
00322         #self.lab_txt   .setReadOnly   (True)
00323         #self.lab_txt   .setStyleSheet (cp.styleWhiteFixed) 
00324         #self.lab_txt   .setStyleSheet (cp.styleBkgd)
00325         
00326 
00327     #def resizeEvent(self, e):
00328         #logger.debug('resizeEvent', __name__) 
00329         #self.lab_txt.setGeometry(self.contentsRect())
00330         #print 'self.rect():', str(self.rect())
00331         #pass
00332 
00333         
00334     #def moveEvent(self, e):
00335         #logger.debug('moveEvent', __name__) 
00336         #cp.posGUIMain = (self.pos().x(),self.pos().y())
00337         #pass
00338 
00339 
00340     def closeEvent(self, event):
00341         logger.debug('closeEvent', __name__)
00342 
00343         self.disconnectFromThreadWorker()
00344 
00345         #self.saveLogTotalInFile() # It will be saved at closing of GUIMain
00346 
00347         #try    : cp.guimain.butLogger.setStyleSheet(cp.styleButtonBad)
00348         #except : pass
00349 
00350         #self.lab_txt.close()
00351 
00352         #try    : del cp.guidarklist # GUIDarkList
00353         #except : pass
00354 
00355         # DO NOT CLOSE THESE WIDGETS!
00356         #for run, (item, widg) in self.dict_guidarklistitem.iteritems() :
00357         #    widg.close()
00358 
00359 
00360     def onClose(self):
00361         logger.debug('onClose', __name__)
00362         self.close()
00363 
00364         
00365     def onItemExpand(self, widg):
00366         run, item = self.getRunAndItemForWidget(widg)
00367         logger.debug('Expand widget for run %s' % run, __name__)
00368         item.setSizeHint(widg.size())
00369 
00370 
00371     def onItemShrink(self, widg):
00372         run, item = self.getRunAndItemForWidget(widg)
00373         logger.debug('Shrink widget for run %s' % run, __name__)
00374         item.setSizeHint(widg.size())
00375 
00376 
00377     def onItemClick(self, item):
00378         #print 'onItemClick' # , isChecked: ', str(item.checkState())
00379 
00380         if item.isSelected(): item.setSelected(False)
00381         #else               : item.setSelected(True)
00382 
00383         self.list_of_run_strs_in_dir = fnm.get_list_of_xtc_runs()   # ['0001', '0202', '0203',...]
00384         self.list_of_run_nums_in_dir = gu.list_of_int_from_list_of_str(self.list_of_run_strs_in_dir) # [1, 202, 203, 204,...]
00385 
00386         widg = self.list.itemWidget(item)
00387         run_num = widg.getRunNum()
00388 
00389         run_rec = self.dict_run_recs[run_num]
00390         self.comment   = run_rec['comment']
00391         list_of_calibs = run_rec['calibrations']
00392         self.type = ', '.join(list_of_calibs)
00393 
00394         if not run_num in self.list_of_run_nums_in_dir :
00395             self.comment = 'xtc file IS NOT on disk!'
00396             self.xtc_in_dir = False
00397         else :
00398             self.xtc_in_dir = True
00399 
00400         widg.updateButtons(self.type, self.comment, self.xtc_in_dir)
00401 
00402         msg = 'onItemClick - update button status for run %s with calib_type: %s, comment: %s, xtc_in_dir %s' \
00403               % (widg.str_run_num, self.type, self.comment, self.xtc_in_dir) 
00404         logger.info(msg, __name__)
00405 
00406 
00407     def onItemDoubleClick(self, item):
00408         logger.debug('onItemDoubleClick', __name__)
00409         #print 'onItemDoubleClick' #, isChecked: ', str(item.checkState())
00410 
00411 #-----------------------------
00412 
00413 if __name__ == "__main__" :
00414     import sys
00415     app = QtGui.QApplication(sys.argv)
00416     w = GUIDarkList()
00417     #w.setStatusMessage('Test of GUIDarkList...')
00418     #w.statusOfDir('./')
00419     w.show()
00420     app.exec_()
00421 
00422 #-----------------------------

Generated on 19 Dec 2016 for PSDMSoftware by  doxygen 1.4.7