00001
00002
00003
00004
00005
00006
00007
00008 __version__ = "$Revision: 11469 $"
00009
00010
00011
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
00051
00052
00053
00054 QtGui.QWidget.__init__(self, parent)
00055
00056 self.setGeometry(100, 100, 800, 300)
00057 self.setWindowTitle('List of dark runs')
00058
00059
00060
00061
00062
00063
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
00077
00078
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
00101 logger.info(msg, __name__)
00102 self.updateList()
00103 self.scrollDown()
00104
00105
00106 def scrollDown(self):
00107
00108
00109 last_run_num = self.list_of_runs[-1]
00110 item, widg = self.dict_guidarklistitem[last_run_num]
00111 self.list.scrollToItem (item)
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
00125
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
00139
00140 self.dict_run_recs = ru.calibration_runs (self.instr_name.value(), self.exp_name.value())
00141
00142
00143 self.list_of_run_strs_in_dir = fnm.get_list_of_xtc_runs()
00144 self.list_of_run_nums_in_dir = gu.list_of_int_from_list_of_str(self.list_of_run_strs_in_dir)
00145 self.list_of_run_nums_in_regdb = ru.list_of_runnums(self.instr_name.value(), self.exp_name.value())
00146
00147
00148
00149
00150
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
00164 continue
00165
00166 if not self.hasSelectedDetectorsInRun(run_num) :
00167
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
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
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
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))
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
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
00227 if self.dict_run_recs != {} :
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' :
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
00245 if self.is_found_type :
00246 return True
00247
00248 if self.dark_list_show_runs.value() == 'all' :
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() :
00266 return True
00267
00268 if self.det_name.value() == '' :
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] :
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] :
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
00289 for (run, item, widg) in self.list_of_visible_records :
00290
00291 widg.setFieldsEnabled(is_enabled)
00292
00293
00294 def getRunAndItemForWidget(self, widg_active):
00295
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
00304
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
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 def closeEvent(self, event):
00341 logger.debug('closeEvent', __name__)
00342
00343 self.disconnectFromThreadWorker()
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
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
00379
00380 if item.isSelected(): item.setSelected(False)
00381
00382
00383 self.list_of_run_strs_in_dir = fnm.get_list_of_xtc_runs()
00384 self.list_of_run_nums_in_dir = gu.list_of_int_from_list_of_str(self.list_of_run_strs_in_dir)
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
00410
00411
00412
00413 if __name__ == "__main__" :
00414 import sys
00415 app = QtGui.QApplication(sys.argv)
00416 w = GUIDarkList()
00417
00418
00419 w.show()
00420 app.exec_()
00421
00422