44
55from PyQt4 import QtGui
66from PyQt4 .QtCore import Qt
7- from PyQt4 .QtGui import QGroupBox , QVBoxLayout , QHBoxLayout , QTableWidget
7+ from PyQt4 .QtGui import QGroupBox , QVBoxLayout , QHBoxLayout
88
99from dls_barcode .plate import NOT_FOUND_SLOT_SYMBOL , EMPTY_SLOT_SYMBOL
1010
@@ -15,16 +15,15 @@ class BarcodeTable(QGroupBox):
1515 def __init__ (self , options ):
1616 super (BarcodeTable , self ).__init__ ()
1717
18- self ._barcodes = []
19-
2018 self ._options = options
2119
2220 self .setTitle ("Plate Barcodes" )
2321 self ._init_ui ()
22+ self .clear ()
2423
2524 def _init_ui (self ):
26- # Create record table - lists all the records in the store
27- self ._table = QTableWidget ()
25+ # Plate being displayed
26+ self ._plate_lbl = QtGui . QLabel ()
2827
2928 # Create barcode table - lists all the barcodes in a record
3029 self ._table = QtGui .QTableWidget ()
@@ -35,70 +34,85 @@ def _init_ui(self):
3534 self ._table .setHorizontalHeaderLabels (['Barcode' ])
3635 self ._table .setColumnWidth (0 , 100 )
3736
38-
3937 # Clipboard button - copy the selected barcodes to the clipboard
4038 self ._btn_clipboard = QtGui .QPushButton ('Copy To Clipboard' )
4139 self ._btn_clipboard .setToolTip ('Copy barcodes for the selected record to the clipboard' )
4240 self ._btn_clipboard .resize (self ._btn_clipboard .sizeHint ())
43- self ._btn_clipboard .clicked .connect (self .copy_selected_to_clipboard )
44- self ._btn_clipboard .setEnabled (False )
41+ self ._btn_clipboard .clicked .connect (self .copy_to_clipboard )
4542
4643 hbox = QHBoxLayout ()
4744 hbox .setSpacing (10 )
4845 hbox .addWidget (self ._btn_clipboard )
4946 hbox .addStretch (1 )
5047
5148 vbox = QVBoxLayout ()
49+ vbox .addWidget (self ._plate_lbl )
5250 vbox .addWidget (self ._table )
5351 vbox .addLayout (hbox )
5452
5553 self .setLayout (vbox )
5654
57- def populate (self , barcodes ):
58- """ Called when a new row is selected on the record table. Displays all of the
59- barcodes from the selected record in the barcode table. By default, valid barcodes are
55+ def populate (self , holder_barcode , barcodes ):
56+ """ Called when a new row is selected on the record table.
57+ """
58+ self ._holder_barcode = holder_barcode
59+ self ._barcodes = barcodes [:]
60+ self ._update_state ()
61+
62+ def clear (self ):
63+ self ._holder_barcode = None
64+ self ._barcodes = []
65+ self ._update_state ()
66+
67+ def _update_state (self ):
68+ self ._populate_table ()
69+ self ._update_button_state ()
70+ self ._update_plate_label ()
71+
72+ def _populate_table (self ):
73+ """Displays all of the barcodes from the selected record in the barcode table. By default, valid barcodes are
6074 highlighted green, invalid barcodes are highlighted red, and empty slots are grey.
6175 """
62- num_slots = len (barcodes )
6376 self ._table .clearContents ()
64- self ._table .setRowCount (num_slots )
77+ self ._table .setRowCount (len ( self . _barcodes ) )
6578
66- for index , barcode in enumerate (barcodes ):
67- # Select appropriate background color
79+ for index , barcode in enumerate (self ._barcodes ):
6880 if barcode == NOT_FOUND_SLOT_SYMBOL :
69- color = self ._options .col_bad ()
81+ cell_color = self ._options .col_bad ()
7082 elif barcode == EMPTY_SLOT_SYMBOL :
71- color = self ._options .col_empty ()
83+ cell_color = self ._options .col_empty ()
7284 else :
73- color = self ._options .col_ok ()
85+ cell_color = self ._options .col_ok ()
7486
75- color .a = 192
87+ cell_color .a = 192
7688
7789 # Set table item
7890 barcode = QtGui .QTableWidgetItem (barcode )
79- barcode .setBackgroundColor (color .to_qt ())
91+ barcode .setBackgroundColor (cell_color .to_qt ())
8092 barcode .setFlags (Qt .ItemIsSelectable | Qt .ItemIsEnabled )
8193 self ._table .setItem (index , 0 , barcode )
8294
83- self ._barcodes = barcodes [:]
84- for i , barcode in enumerate (self ._barcodes ):
85- if barcode in [NOT_FOUND_SLOT_SYMBOL , EMPTY_SLOT_SYMBOL ]:
86- self ._barcodes [i ] = ""
87-
88- self ._update_button_state ()
89-
9095 def _update_button_state (self ):
91- if self ._barcodes is None or len (self ._barcodes ) == 0 :
92- self ._btn_clipboard .setEnabled (False )
93- else :
94- self ._btn_clipboard .setEnabled (True )
96+ self ._btn_clipboard .setEnabled (self ._has_barcodes ())
9597
96- def copy_selected_to_clipboard (self ):
98+ def copy_to_clipboard (self ):
9799 """ Called when the copy to clipboard button is pressed. Copies the list/s of
98100 barcodes for the currently selected records to the clipboard so that the user
99101 can paste it elsewhere.
100102 """
103+ clipboard_barcodes = [self ._holder_barcode ] + self ._barcodes [:]
104+ for i , barcode in enumerate (clipboard_barcodes ):
105+ if barcode in [NOT_FOUND_SLOT_SYMBOL , EMPTY_SLOT_SYMBOL ]:
106+ clipboard_barcodes [i ] = ""
107+
101108 sep = os .linesep
102- if self . _barcodes :
109+ if clipboard_barcodes :
103110 import pyperclip
104- pyperclip .copy (sep .join (self ._barcodes ))
111+ pyperclip .copy (sep .join (clipboard_barcodes ))
112+
113+ def _update_plate_label (self ):
114+ text = "Plate : " + str (self ._holder_barcode ) if self ._has_barcodes () else "Plate:"
115+ self ._plate_lbl .setText (text )
116+
117+ def _has_barcodes (self ):
118+ return self ._barcodes is not None and len (self ._barcodes ) > 0
0 commit comments