00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 """GUI sets parameters for intensity monitors"""
00011
00012
00013
00014
00015 __version__ = "$Revision: 9892 $"
00016
00017
00018
00019
00020
00021 import sys
00022 import os
00023
00024 from PyQt4 import QtGui, QtCore
00025
00026
00027
00028
00029
00030
00031 from ConfigParametersCorAna import confpars as cp
00032 from Logger import logger
00033 from FileNameManager import fnm
00034 from PlotArray import *
00035 import GlobalUtils as gu
00036
00037
00038
00039 class GUIIntensityMonitors ( QtGui.QWidget ) :
00040 """GUI sets parameters for intensity monitors"""
00041
00042 def __init__ ( self, parent=None ) :
00043 QtGui.QWidget.__init__(self, parent)
00044 self.setGeometry(100, 200, 850, 300)
00045 self.setWindowTitle('GUI for Intensity Monitors')
00046 self.setFrame()
00047
00048 self.list_of_dicts = []
00049
00050 self.grid = QtGui.QGridLayout()
00051 self.grid_row = 1
00052 self.setTitleBar()
00053
00054 self.tit_title = QtGui.QLabel('Intensity Monitors')
00055 self.grid.addWidget(self.tit_title, 0, 0, 1, 10)
00056
00057 self.rad_nonorm = QtGui.QRadioButton('No norm.')
00058 self.rad_sele_grp = QtGui.QButtonGroup()
00059 self.rad_sele_grp.addButton(self.rad_nonorm)
00060 self.connect(self.rad_nonorm, QtCore.SIGNAL('clicked()'), self.onRadio)
00061
00062 for i, (name, ch1, ch2, ch3, ch4, norm, sele, sele_min, sele_max, norm_ave, short_name) in enumerate(cp.imon_pars_list) :
00063
00064 self.guiSection(name, ch1, ch2, ch3, ch4, norm, sele, sele_min, sele_max, norm_ave, short_name)
00065
00066 self.grid.addWidget(self.rad_nonorm, self.grid_row, 6, 1, 3)
00067
00068 self.setLayout(self.grid)
00069
00070 self.showToolTips()
00071 self.setStyle()
00072
00073 self.initRadio()
00074 self.setStyleEdiFields()
00075
00076
00077
00078
00079
00080 def showToolTips(self):
00081 msg = 'Use this GUI to set partitions.'
00082 self.setToolTip(msg)
00083
00084 def setFrame(self):
00085 self.frame = QtGui.QFrame(self)
00086 self.frame.setFrameStyle( QtGui.QFrame.Box | QtGui.QFrame.Sunken )
00087 self.frame.setLineWidth(0)
00088 self.frame.setMidLineWidth(1)
00089 self.frame.setGeometry(self.rect())
00090
00091
00092 def setStyle(self):
00093 self.setMinimumSize(600,360)
00094
00095
00096 self.setStyleSheet(cp.styleBkgd)
00097 self.rad_nonorm.setStyleSheet(cp.styleLabel)
00098 self.tit_title.setStyleSheet(cp.styleTitleBold)
00099 self.tit_title.setAlignment(QtCore.Qt.AlignCenter)
00100
00101 def setTitleBar(self) :
00102 self.list_of_titles = ['Intensity Monitor', 'Ch.1', 'Ch.2', 'Ch.3', 'Ch.4',
00103 'Plot', 'Norm', 'Select', 'Imin', 'Imax', 'Iave']
00104 list_of_sizes = [170, 50, 50, 50, 50, 80, 50, 50, 60, 60, 60]
00105
00106 for i,t in enumerate(self.list_of_titles) :
00107 label = QtGui.QLabel(t)
00108 label.setStyleSheet(cp.styleLabel)
00109 label.setFixedHeight(10)
00110 label.setFixedWidth(list_of_sizes[i])
00111 self.grid.addWidget(label, self.grid_row, i)
00112 self.grid_row += 1
00113
00114
00115 def guiSection(self, name, cbch1=None, cbch2=None, cbch3=None, cbch4=None,
00116 norm=None, sele=None, sele_min=None, sele_max=None, norm_ave=None, short_name=None) :
00117 edi = QtGui.QLineEdit( str(short_name.value()) )
00118 but = QtGui.QPushButton('Browse')
00119
00120
00121
00122 cb1 = QtGui.QCheckBox(' +', self)
00123 cb2 = QtGui.QCheckBox(' +', self)
00124 cb3 = QtGui.QCheckBox(' +', self)
00125 cb4 = QtGui.QCheckBox(' =', self)
00126
00127 rad = QtGui.QRadioButton(' ')
00128 if norm.value() : rad.setChecked(True)
00129
00130 self.rad_sele_grp.addButton(rad)
00131
00132 cbs = QtGui.QCheckBox('', self)
00133 mis = QtGui.QLineEdit( str(sele_min.value()) )
00134 mas = QtGui.QLineEdit( str(sele_max.value()) )
00135 ave = QtGui.QLineEdit( str(norm_ave.value()) )
00136
00137 sec_dict = { 0:(edi,short_name),
00138 1:(cb1,cbch1),
00139 2:(cb2,cbch2),
00140 3:(cb3,cbch3),
00141 4:(cb4,cbch4),
00142 5:(but,None),
00143 6:(rad,norm),
00144 7:(cbs,sele),
00145 8:(mis,sele_min),
00146 9:(mas,sele_max),
00147 10:(ave,norm_ave)
00148 }
00149
00150 self.list_of_dicts.append( sec_dict )
00151
00152 for col,(fld, par) in sec_dict.iteritems() :
00153 self.grid.addWidget(fld, self.grid_row, col)
00154 if col>0 and col<5 or col==7 :
00155 fld.setChecked( par.value() )
00156 self.connect(fld, QtCore.SIGNAL('stateChanged(int)'), self.onCBox )
00157
00158 self.grid_row += 1
00159 self.connect(rad, QtCore.SIGNAL('clicked()'), self.onRadio)
00160
00161
00162 edi.setReadOnly( True )
00163 edi.setToolTip('Edit number in this field\nor click on "Browse"\nto select the file.')
00164 but.setToolTip('Click on this button\nand select the file.')
00165
00166
00167 edi .setStyleSheet (cp.styleEditInfo)
00168
00169 but .setStyleSheet (cp.styleButton)
00170 edi .setAlignment (QtCore.Qt.AlignLeft)
00171 mis .setStyleSheet (cp.styleEdit)
00172 mas .setStyleSheet (cp.styleEdit)
00173 ave .setStyleSheet (cp.styleEditInfo)
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 width = 60
00184 but .setFixedWidth(width)
00185 edi .setFixedWidth(150)
00186 mis .setFixedWidth(width)
00187 mas .setFixedWidth(width)
00188 ave .setFixedWidth(width)
00189
00190 self.connect(but, QtCore.SIGNAL('clicked()'), self.onBut )
00191 self.connect(mis, QtCore.SIGNAL('editingFinished()'), self.onEdit )
00192 self.connect(mas, QtCore.SIGNAL('editingFinished()'), self.onEdit )
00193
00194
00195 def setParent(self,parent) :
00196 self.parent = parent
00197
00198 def resizeEvent(self, e):
00199
00200 self.frame.setGeometry(self.rect())
00201
00202 def moveEvent(self, e):
00203
00204
00205 pass
00206
00207 def closeEvent(self, event):
00208 logger.debug('closeEvent', __name__)
00209 try : del cp.guiintensitymonitors
00210 except : pass
00211
00212 def onClose(self):
00213 logger.debug('onClose', __name__)
00214 self.close()
00215
00216 def onApply(self):
00217 logger.debug('onApply - is already applied...', __name__)
00218
00219 def onShow(self):
00220 logger.debug('onShow - is not implemented yet...', __name__)
00221
00222
00223 def onCBox(self) :
00224 for row, sec_dict in enumerate(self.list_of_dicts) :
00225 for col in [1,2,3,4,7] :
00226 cbx, par = sec_dict[col]
00227 if cbx.hasFocus() :
00228 cbx_status = cbx.isChecked()
00229 msg = 'onCBox - set status %s of checkbox in row:%s col:%s' % (cbx_status, row, col)
00230 par.setValue( cbx_status )
00231 logger.info(msg, __name__ )
00232
00233 if col == 7 :
00234 self.setStyleEdiFieldsForRow(row)
00235
00236 elif cp.plotarray_is_on :
00237 self.redrawArray(row)
00238 return
00239
00240
00241 def setStyleEdiFields(self):
00242 for row, sec_dict in enumerate(self.list_of_dicts) :
00243 self.setStyleEdiFieldsForRow(row)
00244
00245
00246 def setStyleEdiFieldsForRow(self, row):
00247 sec_dict = self.list_of_dicts[row]
00248 rad, par_rad = sec_dict[6]
00249 cbx, par_cbx = sec_dict[7]
00250
00251 is_selected = cbx.isChecked()
00252
00253 for col in [8,9] :
00254 edi, par = sec_dict[col]
00255 edi.setReadOnly(not is_selected)
00256 if is_selected :
00257 edi.setStyleSheet (cp.styleEdit)
00258 else :
00259 edi.setStyleSheet (cp.styleEditInfo)
00260
00261 if is_selected : self.checkEdiLimits(row)
00262
00263
00264 def setAverageForRow(self, row):
00265 sec_dict = self.list_of_dicts[row]
00266 mis, par_mis = sec_dict[8]
00267 mas, par_mas = sec_dict[9]
00268 ave, par_ave = sec_dict[10]
00269 par_ave.setValue( (par_mis.value() + par_mas.value())/2 )
00270 ave.setText(str(par_ave.value()))
00271 ave.setReadOnly(True)
00272
00273
00274 def checkEdiLimits(self, row):
00275 sec_dict = self.list_of_dicts[row]
00276 mis, par_mis = sec_dict[8]
00277 mas, par_mas = sec_dict[9]
00278 Imin, Imax = par_mis.value(), par_mas.value()
00279 if Imin >= Imax :
00280 logger.warning('Check limits: Imin:' + str(Imin) + ' >= Imax:' + str(Imax), __name__)
00281 mis.setStyleSheet (cp.styleEditBad)
00282 mas.setStyleSheet (cp.styleEditBad)
00283
00284 if par_mis.value() == par_mis.value_def() :
00285 logger.warning('Imin is equal to the default value: ' + str(par_mis.value()), __name__)
00286 mis.setStyleSheet (cp.styleEditBad)
00287
00288 if par_mas.value() == par_mas.value_def() :
00289 logger.warning('Imax is equal to the default value: ' + str(par_mas.value()), __name__)
00290 mas.setStyleSheet (cp.styleEditBad)
00291
00292
00293 def initRadio(self):
00294 for row, sec_dict in enumerate(self.list_of_dicts) :
00295 rad, par = sec_dict[6]
00296 if par.value() :
00297 rad.setChecked(True)
00298 logger.info('initRadio: row' + str(row) + ' status:' + str(par.value()), __name__)
00299 return
00300 self.rad_nonorm.setChecked(True)
00301
00302
00303 def onRadio(self):
00304 isOn = False
00305 for row, sec_dict in enumerate(self.list_of_dicts) :
00306 rad, par = sec_dict[6]
00307 if rad.isChecked() :
00308 par.setValue(True)
00309 isOn = True
00310 logger.info('onRadio in row:' + str(row), __name__)
00311 else :
00312 par.setValue(False)
00313
00314 if not isOn : logger.info('onRadio - Normalization is OFF', __name__)
00315
00316
00317
00318
00319 def onEdit(self):
00320 logger.debug('onEdit', __name__)
00321 for row, sec_dict in enumerate(self.list_of_dicts) :
00322
00323 for col in [8,9] :
00324 edi, par = sec_dict[col]
00325
00326 if edi.isModified() :
00327 edi.setModified(False)
00328 par.setValue( str(edi.displayText()) )
00329 msg = 'row:' + str(row) + ' set ' + \
00330 self.list_of_titles[col] + ' = ' + str(par.value())
00331 logger.info(msg, __name__ )
00332
00333 self.setStyleEdiFieldsForRow(row)
00334 self.setAverageForRow(row)
00335
00336
00337 def onBut(self):
00338 logger.debug('onBut', __name__)
00339 for row, sec_dict in enumerate(self.list_of_dicts) :
00340 edi, name = sec_dict[0]
00341 but, empt = sec_dict[5]
00342 if but.hasFocus() :
00343 msg = 'onBut - click on button %s in row %s, plot for %s' % (str(but.text()), row, name.value())
00344 logger.info(msg, __name__ )
00345 self.plotIMon(row)
00346 return
00347
00348
00349 def plotIMon(self,imon):
00350 logger.debug('plotIMon', __name__)
00351 arr = self.getArray(imon)
00352 try :
00353 cp.plotarray.close()
00354 try : del cp.plotarray
00355 except : pass
00356 except :
00357 if arr is None : return
00358 cp.plotarray = PlotArray(None, arr,
00359 ofname=fnm.path_data_mons_plot(),
00360 title=self.titleForIMon(imon))
00361 cp.plotarray.move(self.parentWidget().pos().__add__(QtCore.QPoint(850,20)))
00362 cp.plotarray.show()
00363
00364
00365 def titleForIMon(self,imon):
00366 return cp.imon_short_name_list[imon].value() + \
00367 ': sum of channels: ' + \
00368 self.strMaskForIMonChannels(imon)
00369
00370
00371 def redrawArray(self,imon):
00372 logger.debug('plotIMon', __name__)
00373 arr = self.getArray(imon)
00374 if arr is None : return
00375 try :
00376 cp.plotarray.set_array(arr, title=self.titleForIMon(imon))
00377 except :
00378 pass
00379
00380
00381 def boolMaskForIMonChannels(self,imon):
00382 return [cp.imon_ch1_list[imon].value(),
00383 cp.imon_ch2_list[imon].value(),
00384 cp.imon_ch3_list[imon].value(),
00385 cp.imon_ch4_list[imon].value()]
00386
00387
00388 def npIntMaskForIMonChannels(self,imon):
00389 return np.array(self.boolMaskForIMonChannels(imon),dtype=int)
00390
00391
00392 def strMaskForIMonChannels(self,imon):
00393 mask = self.boolMaskForIMonChannels(imon)
00394 str = ''
00395 for i,v in enumerate(mask) :
00396 if v : str += '%s+' % (i+1)
00397 return str.rstrip('+')
00398
00399
00400 def getArray(self,imon):
00401 logger.debug('getArray for imon: '+str(imon), __name__)
00402 arr_all = gu.get_array_from_file(fnm.path_data_scan_monitors_data())
00403 if arr_all is None : return None
00404 logger.debug('Array shape: ' + str(arr_all.shape), __name__)
00405
00406 ibase = 1+imon*4
00407 arr_imon = arr_all[:,ibase:ibase+4]
00408
00409
00410
00411
00412
00413 npmask = self.npIntMaskForIMonChannels(imon)
00414
00415 size = arr_imon.shape[0]
00416 npcol1 = np.ones(size)
00417
00418 X,Y = np.meshgrid(npmask,npcol1)
00419 arr_prod = (arr_imon * X)
00420 arr_sum = arr_prod.sum(1)
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 return arr_sum
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445 if __name__ == "__main__" :
00446
00447 app = QtGui.QApplication(sys.argv)
00448
00449 widget = GUIIntensityMonitors ()
00450 widget.show()
00451
00452
00453
00454
00455 app.exec_()
00456
00457