This file is indexed.

/usr/share/pyshared/PyMca/PeakTableWidget.py is in pymca 4.5.0-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#/*##########################################################################
# Copyright (C) 2004-2012 European Synchrotron Radiation Facility
#
# This file is part of the PyMCA X-ray Fluorescence Toolkit developed at
# the ESRF by the Beamline Instrumentation Software Support (BLISS) group.
#
# This toolkit is free software; you can redistribute it and/or modify it 
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option) 
# any later version.
#
# PyMCA is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# PyMCA; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307, USA.
#
# PyMCA follows the dual licensing model of Trolltech's Qt and Riverbank's PyQt
# and cannot be used as a free plugin for a non-free program. 
#
# Please contact the ESRF industrial unit (industry@esrf.fr) if this license 
# is a problem for you.
#############################################################################*/
__revision__ = "$Revision: 1.8 $"
__author__="V.A. Sole - ESRF BLISS Group"
import sys
import PyMcaQt as qt
if hasattr(qt, "QStringList"):
    QStringList = qt.QStringList
else:
    def QStringList():
        return []
if hasattr(qt, "QString"):
    QString = qt.QString
else:
    QString = str
if qt.qVersion() < '3.0.0':
    import Myqttable as qttable
elif qt.qVersion() < '4.0.0':
    import qttable
import string
import Elements

DEBUG=0

if qt.qVersion() < '4.0.0':
    class QTable(qttable.QTable):
        def __init__(self, parent=None, name=""):
            qttable.QTable.__init__(self, parent, name)
            self.rowCount    = self.numRows
            self.columnCount = self.numCols
            self.setRowCount = self.setNumRows
            self.setColumnCount = self.setNumCols
    QComboTableItem = qttable.QComboTableItem
else:
    QTable = qt.QTableWidget
    class QComboTableItem(qt.QComboBox):
        def __init__(self, parent=None, row = None, col = None):
            self._row = row
            self._col = col
            qt.QComboBox.__init__(self,parent)
            self.connect(self, qt.SIGNAL('activated(int)'), self._cellChanged)

        def _cellChanged(self, idx):
            if DEBUG:
                print("cell changed",idx)
            self.emit(qt.SIGNAL('cellChanged(int,int)'), self._row, self._col)

    class QCheckBoxItem(qt.QCheckBox):
        def __init__(self, parent=None, row = None, col = None):
            self._row = row
            self._col = col
            qt.QCheckBox.__init__(self,parent)
            self.connect(self, qt.SIGNAL('clicked()'), self._cellChanged)

        def _cellChanged(self):
            self.emit(qt.SIGNAL('cellChanged(int, int)'), self._row, self._col)


class PeakTableWidget(QTable):
    def __init__(self, *args,**kw):
        QTable.__init__(self, *args)
        self.setRowCount(0)
        self.labels=['Peak','Channel','Element','Line',
                     'Energy','Use','Calc. Energy']
        self.setColumnCount(len(self.labels))
        if 'labels' in kw:
            self.labels = kw['labels']
        if qt.qVersion() < '4.0.0':
            i=0
            for label in self.labels:
                qt.QHeader.setLabel(self.horizontalHeader(),i,label)
                i=i+1
        else:
            for i in range(len(self.labels)):
                item = self.horizontalHeaderItem(i)
                if item is None:
                    item = qt.QTableWidgetItem(self.labels[i],
                                               qt.QTableWidgetItem.Type)
                item.setText(self.labels[i])
                self.setHorizontalHeaderItem(i,item)
                
        self.peaks={}
        self.peaklist=[]
        if 'peaklist' in kw:
            self.peaklist = kw['peaklist']
        self.build()
        if qt.qVersion() < '4.0.0':
            self.connect(self,qt.SIGNAL("valueChanged(int,int)"),self.myslot)
        else:
            self.connect(self,qt.SIGNAL("cellChanged(int,int)"),self.myslot)

        if qt.qVersion() > '4.0.0':
            rheight = self.horizontalHeader().sizeHint().height()
            for idx in range(self.rowCount()):
                self.setRowHeight(idx, rheight)
        

    def build(self):
        line = 1
        oldlist=list(self.peaklist)
        self.peaklist=[]
        for peak in oldlist:
            self.newpeakline(peak,line)
            line=line+1
        if qt.qVersion() < '4.0.0':
            self.adjustColumn(0)    
            self.adjustColumn(1)    
            self.adjustColumn(2)    
            self.adjustColumn(5)
        else:
            self.resizeColumnToContents(0)
            #self.resizeColumnToContents(1)
            #self.resizeColumnToContents(2)
            self.resizeColumnToContents(5)

    def clearPeaks(self):
        self.peaks = {}
        self.peaklist = []
        self.setRowCount(0)
        
    def newpeakline(self,peak,line):
        #get current number of lines
        nlines=self.rowCount()
        #if the number of lines is smaller than line resize table
        if (line > nlines):
            self.setRowCount(line)
        linew=line-1
        self.peaks[peak]={ 'line':linew,
                           'fields':['number',
                                    'channel',
                                    'element',
                                    'elementline',
                                    'setenergy',
                                    'use',
                                    'calenergy'],
                          'number':     QString('1'),          
                          'channel':    QString('0'),
                          'element':    QString('-'),
                          'elementline':QString('-'),
                          'setenergy':  QString('0'),
                          'use':        0,
                          'calenergy':  QString()}
        self.peaklist.append(peak)
        self.setReadWrite(peak,'setenergy')
        self.setReadWrite(peak,'channel')
        self.setReadOnly (peak,['number','line','calenergy'])
        col = self.peaks[peak]['fields'].index('element')
        self.peaks[peak]['element_item']=QPeriodicComboTableItem(self,
                                        row = linew, col= col)
        if qt.qVersion() < '4.0.0':
            self.setItem(linew,
                         col,
                         self.peaks[peak]['element_item'])
        else:
            self.setCellWidget(linew,
                               col,
                               self.peaks[peak]['element_item'])
            self.connect(self.peaks[peak]['element_item'],
                         qt.SIGNAL('cellChanged(int,int)'), self.myslot)
                         #qt.SIGNAL('activated(int)'), self.myslot)
        a = QStringList()
        a.append('-')
        col = self.peaks[peak]['fields'].index('elementline')
        if qt.qVersion() < '4.0.0':
            self.peaks[peak]['elementline_item']= QComboTableItem(self,a)
            self.setItem(linew,
                         col,
                         self.peaks[peak]['elementline_item'])
        else:
           self.peaks[peak]['elementline_item']= QComboTableItem(self,
                                                                 row = linew,
                                                                 col = col)
           self.peaks[peak]['elementline_item'].addItems(a)
           self.setCellWidget(linew,
                         col,
                         self.peaks[peak]['elementline_item'])
           self.connect(self.peaks[peak]['elementline_item'],
                         qt.SIGNAL('cellChanged(int,int)'), self.myslot)
        
        col = self.peaks[peak]['fields'].index('use')
        if qt.qVersion() < '4.0.0':
            self.peaks[peak]['use_item']    = qttable.QCheckTableItem(self,"")
            self.setItem(linew, col,
                     self.peaks[peak]['use_item'])
        else:
            self.peaks[peak]['use_item']    = QCheckBoxItem(self,
                                                            row = linew,
                                                            col = col)
            self.peaks[peak]['use_item'].setText("")
            self.setCellWidget(linew, col,
                     self.peaks[peak]['use_item'])
            self.connect(self.peaks[peak]['use_item'],
                         qt.SIGNAL('cellChanged(int,int)'), self.myslot)

        self.peaks[peak]['use_item'].setChecked(self.peaks[peak]['use'])
        #Not supported below 3.0
        #self.setColumnReadOnly(self.parameters[param]['fields'].index('name'),1)
        #self.setColumnReadOnly(self.parameters[param]['fields'].index('fitresult'),1)
        #self.setColumnReadOnly(self.parameters[param]['fields'].index('sigma'),1)

    def myslot(self,row,col):
        if DEBUG:
            print("Passing by myslot",
                  self.peaks[self.peaklist[row]]['fields'][col])
        peak=self.peaklist[row]
        field=self.peaks[peak]['fields'][col]
        if (field == "element") or (field == "elementline"):
            key = field+"_item"
            newvalue=self.peaks[peak][key].currentText()
        elif field == "use":
            pass
        else:
            if qt.qVersion() < '4.0.0':
                newvalue=QString(self.text(row,col))
            else:
                newvalue = self.item(row, col).text()
        if field == "element":
            if str(newvalue) == '-':
                #no element
                #set line to -
                options  = QStringList()
                options.append('-')
                if qt.qVersion() < '4.0.0':
                    self.peaks[peak]["elementline_item"].setStringList(options)
                    self.peaks[peak]["elementline_item"].setCurrentItem(0)
                else:
                    self.peaks[peak]["elementline_item"].insertItems(0, options)
                    self.peaks[peak]["elementline_item"].setCurrentIndex(0)
            else:
                #get the emission energies
                ele = str(newvalue).split()[0]
                options  = QStringList()
                energies = QStringList()
                options.append('-') 
                energies.append('0.000')
                emax = 0.0
                for rays in Elements.Element[ele]['rays']:
                    for transition in Elements.Element[ele][rays]:
                        options.append("%s (%.5f)" % (transition,
                                    Elements.Element[ele][transition]['rate']))
                        energies.append("%.5f " % (Elements.Element[ele][transition]['energy']))
                        emax = max(emax,Elements.Element[ele][transition]['rate'])
                energies[0] = "%.5f " % emax
                #lineitem=qttable.QComboTableItem(self,options)
                if qt.qVersion() < '4.0.0':
                    self.peaks[peak]["elementline_item"].setStringList(options)
                    self.peaks[peak]["elementline_item"].setCurrentItem(0)
                else:
                    self.peaks[peak]["elementline_item"].insertItems(0, options)
                    self.peaks[peak]["elementline_item"].setCurrentIndex(0)
                #self.setItem(row,
                #             col+1,
                #             lineitem)
            self.peaks[peak][field] = newvalue
        if field == "elementline":
            if str(newvalue) == '-':
                #no element
                #set energy to rw
                self.setReadWrite(peak,'setenergy')
            else:
                #get the element energy
                #newvalue=QString(self.text(row,col-1))
                elevalue=self.peaks[peak]["element_item"].currentText()
                ele = str(elevalue).split()[0]
                energy = "0.0"
                for rays in Elements.Element[ele]['rays']:
                    for transition in Elements.Element[ele][rays]:
                        option = QString("%s (%.5f)" % (transition,
                                    Elements.Element[ele][transition]['rate']))
                        if option == newvalue:
                            energy = "%.5f " % (Elements.Element[ele][transition]['energy'])
                            break
                if energy == "0.0":
                    print("Something is wrong")
                else:
                    self.configure(name=peak,setenergy=energy)
                self.setReadOnly(peak,'setenergy')
            self.peaks[peak][field] = newvalue
        if field == "setenergy":
            oldvalue = self.peaks[peak]["setenergy"]
            try:
                value = float(str(newvalue))
            except:
                print("taking old value")
                if qt.qVersion() < '4.0.0':
                    self.setText(row,col,oldvalue)
                else:
                    item = self.item(row, col)
                    item.setText("%s" % oldvalue)
                value = float(str(oldvalue))
            self.peaks[peak][field] = value 
            ddict={}
            ddict['event'] = 'use'
            if qt.qVersion() < '4.0.0':
                self.emit(qt.PYSIGNAL('PeakTableWidgetSignal'),(ddict,))
            else:
                self.emit(qt.SIGNAL('PeakTableWidgetSignal'), (ddict))

        if field == "channel":
            oldvalue = self.peaks[peak]["channel"]
            try:
                value = float(str(newvalue))
            except:
                print("taking old value")
                if qt.qVersion() < '4.0.0':
                    self.setText(row,col,oldvalue)
                else:
                    item = self.item(row, col)
                    item.setText("%s" % oldvalue)
                value = float(str(oldvalue))
            self.peaks[peak][field] = value 
            ddict={}
            ddict['event'] = 'use'
            if qt.qVersion() < '4.0.0':
                self.emit(qt.PYSIGNAL('PeakTableWidgetSignal'),(ddict,))
            else:
                self.emit(qt.SIGNAL('PeakTableWidgetSignal'), (ddict))

        if field == "use":
            if self.peaks[peak][field+"_item"].isChecked():
                self.peaks[peak][field] = 1
            else:
                self.peaks[peak][field] = 0
            ddict={}
            ddict['event'] = 'use'
            if qt.qVersion() < '4.0.0':
                self.emit(qt.PYSIGNAL('PeakTableWidgetSignal'),(ddict,))
            else:
                self.emit(qt.SIGNAL('PeakTableWidgetSignal'), (ddict))

    def setReadOnly(self,parameter,fields):
        if DEBUG:
            print("peak ",parameter,"fields = ",fields,"asked to be read only")
        if qt.qVersion() < '4.0.0':
            self.setfield(parameter,fields,qttable.QTableItem.Never)
        else:
            self.setfield(parameter, fields,
                          qt.Qt.ItemIsSelectable|qt.Qt.ItemIsEnabled)

        
    def setReadWrite(self,parameter,fields):
        if DEBUG:
            print("peak ",parameter,"fields = ",fields,"asked to be read write")
        if qt.qVersion() < '4.0.0':
            self.setfield(parameter,fields,qttable.QTableItem.OnTyping)
        else:
            self.setfield(parameter, fields,
                          qt.Qt.ItemIsEditable|qt.Qt.ItemIsSelectable|qt.Qt.ItemIsEnabled)
                            
    def setfield(self,peak,fields,EditType):
        if DEBUG:
            print("setfield. peak =",peak,"fields = ",fields)
        if type(peak) == type (()) or \
           type(peak) == type ([]):
            peaklist=peak
        else:
            peaklist=[peak]
        if type(fields) == type (()) or \
           type(fields) == type ([]):
            fieldlist=fields
        else:
            fieldlist=[fields]
        for peak in peaklist:
            if peak in self.peaklist:            
                try:
                    row=self.peaklist.index(peak)
                except ValueError:
                    row=-1
                if row >= 0:
                    for field in fieldlist:
                        if field in self.peaks[peak]['fields']:
                            col=self.peaks[peak]['fields'].index(field)
                            if (field != 'element') and (field != 'elementline'):
                                key=field+"_item"
                                if qt.qVersion() < '4.0.0':
                                    self.peaks[peak][key]=qttable.QTableItem(self,
                                                            EditType,
                                                        self.peaks[peak][field])
                                    self.setItem(row,col,self.peaks[peak][key])
                                else:
                                    item = self.item(row, col)
                                    text = "%s" % self.peaks[peak][field]
                                    if item is None:
                                        item = qt.QTableWidgetItem(text,
                                                   qt.QTableWidgetItem.Type)
                                        self.setItem(row, col, item)
                                    else:                                        
                                        item.setText(str(text))
                                    item.setFlags(EditType)
                                    

    def configure(self,*vars,**kw):
        if DEBUG:
            print("configure called with **kw = ",kw)
            print("configure called with *vars = ",vars)
        name=None
        error=0
        if 'name' in kw:
            name=kw['name']        
        elif 'number' in kw:
            name=kw['number']
        else:
            return 1
        
        keylist = []
        if "channel" in kw:
            keylist=["channel"]
        for key in kw.keys():
            if key != "setenergy":
                if key not in keylist:
                    keylist.append(key)
        if "setenergy"  in kw.keys():
            keylist.append("setenergy")    

        if name in self.peaks:
            row=self.peaks[name]['line']
            for key in keylist:
              if key is not 'name':
                if key in self.peaks[name]['fields']:
                    col=self.peaks[name]['fields'].index(key)
                    oldvalue=self.peaks[name][key]
                    if key is 'code':
                        newvalue = QString(str(kw[key]))
                    elif key is 'element':
                        newvalue = str(kw[key]).split()[0]
                        if newvalue == "-":
                            if qt.qVersion() < '4.0.0':
                                self.peaks[name][key+"_item"].setCurrentItem(0)
                            else:
                                self.peaks[name][key+"_item"].setCurrentIndex(0)
                        else:
                            self.peaks[name][key+"_item"].setSelection(newvalue)
                        try:
                            self.myslot(row,col)
                        except:
                            print("Error setting element") 
                    elif key is 'elementline':
                        try:
                            if qt.qVersion() < '4.0.0':
                                self.peaks[name][key+"_item"].setCurrentItem(kw[key])
                            else:
                                iv = self.peaks[name][key+"_item"].findText(QString(kw[key]))
                                self.peaks[name][key+"_item"].setCurrentIndex(iv)
                        except:
                            print("Error setting elementline")
                    elif key is 'use':
                        if kw[key]:
                            self.peaks[name][key] = 1
                        else:
                            self.peaks[name][key] = 0
                        self.peaks[name][key+"_item"].setChecked(self.peaks[name][key])  
                    elif key == 'number':
                        if len(str(kw[key])):
                            newvalue=float(str(kw[key]))
                            newvalue= QString("%3d" % newvalue)
                            self.peaks[name][key]=newvalue
                        else:
                            self.peaks[name][key]=oldvalue
                        if qt.qVersion() < '4.0.0':
                            self.setText(row,col,self.peaks[name][key])
                        else:
                            text = self.peaks[name][key]
                            item = self.item(row, col)
                            if item is None:
                                item = qt.QTableWidgetItem(text,
                                                qt.QTableWidgetItem.Type)
                                self.setItem(row, col, item)
                            else:
                                item.setText(text)
                    elif key == 'channel':
                        if DEBUG:
                            print("setting channel in configure")
                        if len(str(kw[key])):
                            newvalue=float(str(kw[key]))
                            newvalue= QString("%.3f" % newvalue)
                            self.peaks[name][key]=newvalue
                        else:
                            self.peaks[name][key]=oldvalue
                        if qt.qVersion() < '4.0.0':
                            self.setText(row,col,self.peaks[name][key])
                        else:
                            text = self.peaks[name][key]
                            item = self.item(row, col)
                            if item is None:
                                item = qt.QTableWidgetItem(text,
                                                qt.QTableWidgetItem.Type)
                                self.setItem(row, col, item)
                            else:
                                item.setText(text)
                    elif (key == 'setenergy') or (key == 'calenergy'):
                        if len(str(kw[key])):
                            newvalue=float(str(kw[key]))
                            newvalue= QString("%.4f" % newvalue)
                            self.peaks[name][key]=newvalue
                        else:
                            self.peaks[name][key]=oldvalue
                        if qt.qVersion() < '4.0.0':
                            self.setText(row,col,self.peaks[name][key])
                        else:
                            text = self.peaks[name][key]
                            item = self.item(row, col)
                            if item is None:
                                item = qt.QTableWidgetItem(text,
                                                qt.QTableWidgetItem.Type)
                                self.setItem(row, col, item)
                            else:
                                item.setText(text)
                        #self.myslot(row,col)
                    else:
                        if len(str(kw[key])):
                            newvalue=float(str(kw[key]))
                            if key is 'sigma':
                                newvalue= "%6.3g" % newvalue
                            else:
                                newvalue= "%8g" % newvalue
                        else:
                            newvalue=""
                        newvalue=QString(newvalue)
        return error


    def validate(self,name,key,oldvalue,newvalue):
        if (key == 'setenergy') or (key == 'number') or (key == 'calcenergy'):
            try:
                float(str(newvalue))
            except:
                return 0
        return 1

    def getdict(self,*var):
        dict={}
        if len(var) == 0:
            #asked for the dict of dicts
            for peak in self.peaks.keys():
                dict[peak] = {}
                dict[peak]['number']      = float(str(self.peaks[peak]['number']))
                dict[peak]['channel']     = float(str(self.peaks[peak]['channel']))
                dict[peak]['element']     = str(self.peaks[peak]['element'])
                dict[peak]['elementline'] = str(self.peaks[peak]['elementline'])
                dict[peak]['setenergy']   = float(str(self.peaks[peak]['setenergy']))
                dict[peak]['use']         = self.peaks[peak]['use']
                if len(str(self.peaks[peak]['calenergy'])):
                    dict[peak]['calenergy']   = float(str(self.peaks[peak]['calenergy']))
                else:
                    dict[peak]['calenergy']   = ""
        else:
            peak=var[0]              
            if peak in self.peaks.keys():        
                dict['number']      = float(str(self.peaks[peak]['number']))
                dict['channel']     = float(str(self.peaks[peak]['channel']))
                dict['element']     = str(self.peaks[peak]['element'])
                dict['elementline'] = str(self.peaks[peak]['elementline'])
                dict['setenergy']   = float(str(self.peaks[peak]['setenergy']))
                dict['use']         = self.peaks[peak]['use']
                if len(str(self.peaks[peak]['calenergy'])):
                    dict['calenergy']   = float(str(self.peaks[peak]['calenergy']))
                else:
                    dict['calenergy']   = ""
        return dict
        
class QPeriodicComboTableItem(QComboTableItem):
    """ Periodic Table Combo List to be used in a QTable
        Init options:
            table (mandatory)= parent QTable
            addnone= 1 (default) add "-" in the list to provide possibility
                        to select no specific element.
                 0 only element list.
            detailed= 1 (default) display element symbol, Z and name
                  0 display only element symbol and Z
        Public methods:
            setSelection(eltsymbol):
                Set the element selected given its symbol
            getSelection():
                Return symbol of element selected

        Signals:
            No specific signals in Qt3. Use signals from QTable
            SIGNAL("valueChanged(int,int)") for example.
    """
    def __init__(self, table=None, addnone=1, detailed=0, row=None, col=None):
        strlist = QStringList()
        self.addnone= (addnone==1)
        if self.addnone: strlist.append("-")
        for (symbol, Z, x, y, name, mass, density) in Elements.ElementsInfo:
            if detailed:    txt= "%2s (%d) - %s"%(symbol, Z, name)
            else:       txt= "%2s (%d)"%(symbol, Z)
            strlist.append(txt)
        if row is None: row = 0
        if col is None: col = 0
        self._row = row
        self._col = col
        if qt.qVersion() < '4.0.0':
            qttable.QComboTableItem.__init__(self, table, strlist)
        else:
            qt.QComboBox.__init__(self)
            self.addItems(strlist)
            self.connect(self, qt.SIGNAL('activated(int)'), self._cellChanged)

    if qt.qVersion() > "4.0.0":
        def _cellChanged(self, idx):
            self.emit(qt.SIGNAL('cellChanged(int, int)'), self._row, self._col)

    def setSelection(self, symbol=None):
        if symbol is None:
            if self.addnone:
                if qt.qVersion() < '4.0.0':
                    self.setCurrentItem(0)
                else:
                    self.setCurrentIndex(0)
        else:
            idx= self.addnone+Elements.getz(symbol)-1
            if qt.qVersion() < '4.0.0':
                self.setCurrentItem(idx)
            else:
                self.setCurrentIndex(idx)

    def getSelection(self):
        if qt.qVersion() < '4.0.0':
            id = self.currentItem()
        else:
            id = self.currentIndex()
        if self.addnone and not id: return None
        else: return Elements.ElementList[id-self.addnone]

def main(args):
    app=qt.QApplication(args)
    win=qt.QMainWindow()
    #tab = Parameters(labels=['Parameter','Estimation','Fit Value','Sigma',
    #                    'Restrains','Min/Parame','Max/Factor/Delta/'],
    #               paramlist=['Height','Position','FWHM'])
    tab = PeakTableWidget(labels= ['Peak','Channel','Element','Line','Set Energy','Use',
                            'Cal. Energy'],
                            peaklist=['1'])
    tab.showGrid()
    tab.configure(name='1',number=24,channel='1234',use=1,
                  setenergy=12.5,calenergy=24.0)    
    tab.show()
    if qt.qVersion() < '4.0.0':
        app.setMainWidget( tab )
        app.exec_loop()
    else:
        app.exec_()
        

                            

if __name__=="__main__":
    main(sys.argv)