class MyForm(QtGui.QMainWindow):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_mainWindow()
        self.ui.setupUi(self)
        self.timer = QtCore.QTimer()

        #initialize objects
        self.connection = Connection()
        
        self.air_meter = Meter(self.connection)
        self.fuel_meter = Meter(self.connection)       
        self.air = Air()
        self.fuel = Fuel()
        self.burner = Burner()
        self.flow = Flow(self.air_meter, self.fuel_meter, self.air, self.fuel, self.burner)
        self.record = Record(self.flow, self.ui)

        self.update_thread = Update_thread(self.air_meter,self.fuel_meter)

        self.switchedMode = False
        self.Re_setPoint = 0
        self.TotalMass_setPoint = 0
        self.airSLPM_setPoint = 0
        self.fuelSLPM_setPoint = 0
        self.phi_setPoint = 0
        self.Re_step = 5
        self.TotalMass_step = 0.1
        self.phi_step = 0.1
        self.airSLPM_step = 0.1
        self.fuelSLPM_step = 0.1

        self.ui.dial_flowRate_Air_incr.setTracking(False)
        self.ui.dial_flowRate_Fuel_incr.setTracking(False)
        self.signal_from_button = False

        self.control_enabled = False
        self.doneIgniting = False

        self.SI_to_displayUnits = 60000 # Kg/s -> g/min

        # here we connect signals with our slots
        QtCore.QObject.connect(self.ui.Button_flowrate_Air_up,QtCore.SIGNAL("clicked()"), self.Button_A_Up)
        QtCore.QObject.connect(self.ui.Button_flowrate_Air_down,QtCore.SIGNAL("clicked()"), self.Button_A_Down)
        QtCore.QObject.connect(self.ui.Button_flowrate_Fuel_up,QtCore.SIGNAL("clicked()"), self.Button_B_Up)
        QtCore.QObject.connect(self.ui.Button_flowrate_Fuel_down,QtCore.SIGNAL("clicked()"), self.Button_B_Down)
        QtCore.QObject.connect(self.ui.button_openCloseSerial,QtCore.SIGNAL("clicked()"), self.openCloseSerial)
        QtCore.QObject.connect(self.ui.pushButton_apply_all_settings,QtCore.SIGNAL("clicked()"), self.apply_all_settings)
        QtCore.QObject.connect(self.ui.button_reset,QtCore.SIGNAL("clicked()"), self.reset_meters)
        QtCore.QObject.connect(self.ui.button_record_set,QtCore.SIGNAL("clicked()"), self.add_record)
        QtCore.QObject.connect(self.ui.button_new_record_set,QtCore.SIGNAL("clicked()"), self.new_record_set)
        QtCore.QObject.connect(self.ui.delete_record_button,QtCore.SIGNAL("clicked()"), self.delete_record)
        QtCore.QObject.connect(self.ui.pushButton_start,QtCore.SIGNAL("clicked()"), self.start_update)
        QtCore.QObject.connect(self.update_thread,QtCore.SIGNAL("update_ui()"), self.update_meter_data)
        QtCore.QObject.connect(self.ui.pushButton_save_button,QtCore.SIGNAL("clicked()"), self.record.save_to_file)
        QtCore.QObject.connect(self.ui.button_abort,QtCore.SIGNAL("clicked()"), self.abort)
        QtCore.QObject.connect(self.ui.button_ignition,QtCore.SIGNAL("clicked()"), self.ignition_set_point)
        QtCore.QObject.connect(self.ui.pushButton_clear_records,QtCore.SIGNAL("clicked()"), self.record.clear_records)
        
        self.ui.record_list.itemSelectionChanged.connect(self.item_selected)
        self.ui.dial_flowRate_Air_incr.valueChanged.connect(self.SLPM_Air_slide)
        self.ui.dial_flowRate_Fuel_incr.valueChanged.connect(self.SLPM_Fuel_slide)
        self.ui.comboBox_fuel.currentIndexChanged.connect(self.fuel_changed)

        QtCore.QObject.connect(self.ui.radioButton_re_phi,QtCore.SIGNAL("toggled(bool)"),self.switchControlMode)
        QtCore.QObject.connect(self.ui.radioButton_air_fuel_SLPM,QtCore.SIGNAL("toggled(bool)"),self.switchControlMode)
          
        QtCore.QObject.connect(self.ui.flowrate_Air_incr,QtCore.SIGNAL("valueChanged(double)"),self.update_step)
        QtCore.QObject.connect(self.ui.flowrate_Fuel_incr,QtCore.SIGNAL("valueChanged(double)"),self.update_step)


    def fuel_changed(self):
        if self.ui.comboBox_fuel.currentText() == 'CH4':
            #self.ui.lineEdit_fuel_name.setValue('CH4')
            self.ui.lineEdit_fuel_viscosity.setText('1.0997e-5')
            self.ui.lineEdit_fuel_density.setText('0.6569')
            self.ui.lineEdit_fuel_A_F.setText('17.195')
            self.ui.lineEdit_fuel_SCP.setText('55.5')
            self.ui.lineEdit_fuel_molweight.setText('16.044')
            self.ui.lineEdit_fuel_gas_ID.setText('2')
        elif self.ui.comboBox_fuel.currentText() == 'C3H8':
            #self.ui.lineEdit_fuel_name.setValue('C3H8')
            self.ui.lineEdit_fuel_viscosity.setText('8.01e-5')
            self.ui.lineEdit_fuel_density.setText('1.8316')
            self.ui.lineEdit_fuel_A_F.setText('15.246')
            self.ui.lineEdit_fuel_SCP.setText('50.35')
            self.ui.lineEdit_fuel_molweight.setText('44.10')
            self.ui.lineEdit_fuel_gas_ID.setText('12')
            
    def item_selected(self):
        print 'signal'
        self.record.set_current_record()

    def start_update(self):
        if not self.update_thread.isRunning:
            self.update_thread.isRunning = True
            self.update_thread.start()
            self.ui.pushButton_start.setText('Stop')
            #self.ui.pushButton_start.setDown(True)
        else:
            self.update_thread.isRunning = False
            self.ui.pushButton_start.setText('Run')
            #self.ui.pushButton_start.setDown(False)
        
    def add_record(self):
        self.record.add_record()

    def new_record_set(self):
        self.record.new_record_set()

    def delete_record(self):
        self.record.delete_record()

    def apply_all_settings(self):
        if self.ui.pushButton_apply_all_settings.isEnabled():
            self.setMixtureSettings()
            self.set_meters_settings()
            self.set_burner_settings()
            self.ui.button_openCloseSerial.setEnabled(True)
        else:
            message = QtGui.QMessageBox.information(self,'Warning!','Before aplly changes, RESET the meter and CLOSE the connection.') 

    def enable_control_ui(self, state):
        if not self.control_enabled:
            self.control_enabled = state
            self.ui.button_reset.setEnabled(state)
            self.ui.button_ignition.setEnabled(state)
            self.ui.button_abort.setEnabled(state)
            self.ui.pushButton_start.setEnabled(state)
            self.ui.button_record_set.setEnabled(state)
            self.ui.Button_flowrate_Air_up.setEnabled(state)
            self.ui.Button_flowrate_Air_down.setEnabled(state)
            self.ui.dial_flowRate_Air_incr.setEnabled(state)
            self.ui.Button_flowrate_Fuel_up.setEnabled(state)
            self.ui.Button_flowrate_Fuel_down.setEnabled(state)
            self.ui.dial_flowRate_Fuel_incr.setEnabled(state) 

    def setMixtureSettings(self):
        # air
        self.air.din_viscosity = float(self.ui.lineEdit_air_viscosity.text())
        self.air.density = float(self.ui.lineEdit_air_density.text())
        self.air.molecular_weight = float(self.ui.lineEdit_air_molweight.text())

        # fuel
        self.fuel.fuel_name = self.ui.comboBox_fuel.currentText()
        self.fuel.din_viscosity = float(self.ui.lineEdit_fuel_viscosity.text())
        self.fuel.density = float(self.ui.lineEdit_fuel_density.text())
        self.fuel.A_F = float(self.ui.lineEdit_fuel_A_F.text())
        self.fuel.SCP = float(self.ui.lineEdit_fuel_SCP.text())
        self.fuel.molecular_weight = float(self.ui.lineEdit_fuel_molweight.text())
        

    def set_meters_settings(self):
        self.connection.port = str(self.ui.input_connection_port.text())
        self.connection.baudrate = 19200
        self.connection.timeout = 0.1

        fuel_ID = self.ui.input_fuelControllerID.text()
        max_fuel_meter = self.ui.input_fuelControllerMaxCapac.text()
        air_ID = self.ui.input_airControllerID.text()
        max_air_meter = self.ui.input_airControllerMaxCapac.text()

        self.fuel_meter.set_meter_settings(fuel_ID,max_fuel_meter,float(self.ui.lineEdit_fuel_gas_ID.text()))
        self.air_meter.set_meter_settings(air_ID,max_air_meter,0)
        
    def set_burner_settings(self):
        self.burner.dim_a = float(self.ui.input_burnerDim_a.text())
        self.burner.dim_b = float(self.ui.input_burnerDim_b.text())
        self.burner.no_holes = int(self.ui.input_burnerDim_no.text())
        self.burner.isHole = self.ui.radio_button_burnerHoles.isChecked()

    def update_meter_data(self):
        if self.ui.radioButton_re_phi.isChecked():
            self.ui.lcd_flowrate_Air.display(self.flow.get_Reynolds_flow())
            self.ui.lcd_flowrate_Fuel.display(self.flow.get_phi_flow())
        elif self.ui.radioButton_massflow.isChecked():
            self.ui.lcd_flowrate_Air.display(self.flow.get_totalMassFlow() * self.SI_to_displayUnits)
            self.ui.lcd_flowrate_Fuel.display(self.flow.get_phi_flow())
        elif self.ui.radioButton_air_fuel_SLPM.isChecked():
            self.ui.lcd_flowrate_Air.display(self.flow.get_airSLPMFlow())
            self.ui.lcd_flowrate_Fuel.display(self.flow.get_fuelSLPMFlow())

        self.ui.lcd_pressure_Air.display(self.air_meter.get_pressure())
##        self.ui.progressBar_pressure
        self.ui.lcd_temperature_Air.display(self.air_meter.get_temperature())
##        self.ui.progressBar_temperature

        self.ui.lcd_mean_velocity.display(self.flow.get_mean_velocity())
        self.ui.lcd_power.display(self.flow.get_power())

    def switchControlMode(self):
        self.switchedMode = True
        self.update_setPoints()
        if self.ui.radioButton_re_phi.isChecked():
            self.ui.label_flowRate_A_mode.setText('Re')
            self.ui.label_18.setText('Re set point')
            self.ui.label_flowRate_B_mode.setText('phi')
            self.ui.label_19.setText('phi set point')
            self.ui.lcd_flowRate_setPoint_Air.display(self.Re_setPoint)
            self.ui.lcd_flowRate_setPoint_Fuel.display(self.phi_setPoint)
            self.ui.flowrate_Air_incr.setDecimals(1)
            self.ui.flowrate_Air_incr.setSingleStep(5)
            self.ui.flowrate_Air_incr.setValue(self.Re_step)
            self.ui.flowrate_Fuel_incr.setDecimals(2)
            self.ui.flowrate_Fuel_incr.setSingleStep(0.1)
            self.ui.flowrate_Fuel_incr.setValue(self.phi_step)

        elif self.ui.radioButton_massflow.isChecked():
            self.ui.label_flowRate_A_mode.setText('total Mass Flow g/min')
            self.ui.label_18.setText('g/min set point')
            self.ui.label_flowRate_B_mode.setText('phi')
            self.ui.label_19.setText('phi set point')
            self.ui.lcd_flowRate_setPoint_Air.display(self.TotalMass_setPoint)
            self.ui.lcd_flowRate_setPoint_Fuel.display(self.phi_setPoint)
            self.ui.flowrate_Air_incr.setDecimals(2)
            self.ui.flowrate_Air_incr.setSingleStep(0.1)
            self.ui.flowrate_Air_incr.setValue(self.TotalMass_step)
            self.ui.flowrate_Fuel_incr.setDecimals(2)
            self.ui.flowrate_Fuel_incr.setSingleStep(0.1)
            self.ui.flowrate_Fuel_incr.setValue(self.phi_step)

        elif self.ui.radioButton_air_fuel_SLPM.isChecked():
            self.ui.label_flowRate_A_mode.setText('air SLPM')
            self.ui.label_18.setText('SLPM set point')
            self.ui.label_flowRate_B_mode.setText('fuel SLPM')
            self.ui.label_19.setText('SLPM set point')
            self.ui.lcd_flowRate_setPoint_Air.display(self.airSLPM_setPoint)
            self.ui.lcd_flowRate_setPoint_Fuel.display(self.fuelSLPM_setPoint)
            self.ui.flowrate_Air_incr.setDecimals(2)
            self.ui.flowrate_Air_incr.setSingleStep(0.1)
            self.ui.flowrate_Air_incr.setValue(self.airSLPM_step)
            self.ui.flowrate_Fuel_incr.setDecimals(2)
            self.ui.flowrate_Fuel_incr.setSingleStep(0.1)
            self.ui.flowrate_Fuel_incr.setValue(self.fuelSLPM_step)

        self.switchedMode = False

    def update_step(self):
        if not self.switchedMode:
            print 'update step'
            A_step = float(self.ui.flowrate_Air_incr.value())
            B_step = float(self.ui.flowrate_Fuel_incr.value())
            if self.ui.radioButton_re_phi.isChecked():
                self.Re_step = A_step
                self.phi_step = B_step
            elif self.ui.radioButton_massflow.isChecked():
                self.TotalMass_step = A_step
                self.phi_step = B_step
            elif self.ui.radioButton_air_fuel_SLPM.isChecked():
                self.airSLPM_step = A_step
                self.fuelSLPM_step = B_step

    def update_setPoints(self):       
        if self.ui.radioButton_re_phi.isChecked():
            self.Re_setPoint = self.flow.convert_massFlow_to_Reynolds(self.flow.get_totalMassFlow_setPoint())
            self.phi_setPoint = self.flow.get_phi_setPoint()
            self.ui.lcd_flowRate_setPoint_Air.display(self.Re_setPoint)
            self.ui.lcd_flowRate_setPoint_Fuel.display(self.phi_setPoint)
        elif self.ui.radioButton_massflow.isChecked():
            self.TotalMass_setPoint = self.flow.get_totalMassFlow_setPoint() * self.SI_to_displayUnits
            self.phi_setPoint = self.flow.get_phi_setPoint()
            self.ui.lcd_flowRate_setPoint_Air.display(self.TotalMass_setPoint)
            self.ui.lcd_flowRate_setPoint_Fuel.display(self.phi_setPoint)
        elif self.ui.radioButton_air_fuel_SLPM.isChecked():
            self.airSLPM_setPoint = self.air_meter.get_setPoint_SLPM()
            self.fuelSLPM_setPoint = self.fuel_meter.get_setPoint_SLPM()
            self.ui.lcd_flowRate_setPoint_Air.display(self.airSLPM_setPoint)
            self.ui.lcd_flowRate_setPoint_Fuel.display(self.fuelSLPM_setPoint) 

        air_meter_capacity = self.air_meter.meter_capacity()
        fuel_meter_capacity = self.fuel_meter.meter_capacity()
        self.ui.progressBar_air_meter_scale.setValue(air_meter_capacity)
        self.ui.progressBar_fuel_meter_scale.setValue(fuel_meter_capacity)
        
        if self.signal_from_button:
            self.ui.dial_flowRate_Air_incr.setValue(self.air_meter.get_setPoint_percent())
            self.ui.dial_flowRate_Fuel_incr.setValue(self.fuel_meter.get_setPoint_percent())
            time.sleep(.01)
            self.signal_from_button = False

    def SLPM_Air_slide(self):
        if not self.signal_from_button:
            self.air_meter.set_setPoint_percent(self.ui.dial_flowRate_Air_incr.value())
            self.update_setPoints()

    def SLPM_Fuel_slide(self):
        if not self.signal_from_button:
            self.fuel_meter.set_setPoint_percent(self.ui.dial_flowRate_Fuel_incr.value())
            self.update_setPoints()

    def Button_A_Up(self):
        self.signal_from_button = True
        if self.ui.radioButton_re_phi.isChecked():
            self.Re_Up()
        elif self.ui.radioButton_massflow.isChecked():
            self.TotalMass_Up()
        elif self.ui.radioButton_air_fuel_SLPM.isChecked():
            self.airSLPM_Up()        

    def Button_A_Down(self):
        self.signal_from_button = True
        if self.ui.radioButton_re_phi.isChecked():
            self.Re_Down()
        elif self.ui.radioButton_massflow.isChecked():
            self.TotalMass_Down()
        elif self.ui.radioButton_air_fuel_SLPM.isChecked():
            self.airSLPM_Down()

    def Button_B_Up(self):
        self.signal_from_button = True       
        if self.ui.radioButton_re_phi.isChecked() or self.ui.radioButton_massflow.isChecked():
            self.phi_Up()
        elif self.ui.radioButton_air_fuel_SLPM.isChecked():
            self.fuelSLPM_Up()

    def Button_B_Down(self):
        self.signal_from_button = True
        if self.ui.radioButton_re_phi.isChecked() or self.ui.radioButton_massflow.isChecked():
            self.phi_Down()
        elif self.ui.radioButton_air_fuel_SLPM.isChecked():
            self.fuelSLPM_Down()

    def Re_Up(self):
        if not self.air_meter.at_max:
            self.flow.set_Reynolds_flow(self.Re_setPoint + self.Re_step)
        self.update_setPoints()
        
    def Re_Down(self):
        self.flow.set_Reynolds_flow(self.Re_setPoint - self.Re_step)
        self.update_setPoints()

    def TotalMass_Up(self):
        if not self.air_meter.at_max:
            self.flow.set_totalMassFlow(( self.TotalMass_setPoint + self.TotalMass_step ) / self.SI_to_displayUnits )
        self.update_setPoints()
        
    def TotalMass_Down(self):
        self.flow.set_totalMassFlow(( self.TotalMass_setPoint - self.TotalMass_step ) / self.SI_to_displayUnits )
        self.update_setPoints()

    def phi_Up(self):
        if not self.air_meter.at_max:
            self.flow.set_phi_flow(self.phi_setPoint + self.phi_step)
        self.update_setPoints()

    def phi_Down(self):
        self.flow.set_phi_flow(self.phi_setPoint - self.phi_step)
        self.update_setPoints()

    def airSLPM_Up(self):
        self.flow.set_airSLPMFlow(self.airSLPM_setPoint + self.airSLPM_step)
        self.update_setPoints()
        
    def airSLPM_Down(self):
        self.flow.set_airSLPMFlow(self.airSLPM_setPoint - self.airSLPM_step)
        self.update_setPoints()

    def fuelSLPM_Up(self):
        if not self.air_meter.at_max:
            self.flow.set_fuelSLPMFlow(self.fuelSLPM_setPoint + self.fuelSLPM_step)
        self.update_setPoints()
        
    def fuelSLPM_Down(self):
        self.flow.set_fuelSLPMFlow(self.fuelSLPM_setPoint - self.fuelSLPM_step)
        self.update_setPoints()

    def reset_meters(self):
        print 'Reseting flowmeters'
        # Air
        self.air_meter.set_setPoint_SLPM(0)
        # Fuel
        self.fuel_meter.set_setPoint_SLPM(0)
        self.update_setPoints()
        if not self.update_thread.isRunning:
            self.air_meter.update_meter()
            self.fuel_meter.update_meter()
            self.update_meter_data()

    def closeEvent(self,Event):
        if self.connection.isOpen():
            self.reset_meters()
            time.sleep(.5)
            print 'Exiting GUI and closing all ports and threads'
            self.update_thread.isRunning = False
            self.connection.close()

    def abort(self):
        print 'Abort:: air meter full open and fuel meter closed'
        self.air_meter.set_setPoint_percent(100)
        self.fuel_meter.set_setPoint_percent(0)
        self.update_setPoints()
                                     

    def ignition_set_point(self):
        if not self.doneIgniting:
            self.flow.set_totalMassFlow(float(self.ui.ignition_massflow_set_point.text()) / self.SI_to_displayUnits)
            self.flow.set_phi_flow(float(self.ui.ignition_phi_set_point.text()))
            self.ui.lcd_flowRate_setPoint_Air.display('-')
            self.ui.lcd_flowRate_setPoint_Fuel.display('-')
            self.ui.button_ignition.setText('Stop Igniting')
            self.ui.button_ignition.setDown(True)
            self.doneIgniting = True
        else:
            print 'ignition finished'
            self.ui.button_ignition.setText('Ignition')
            self.ui.button_ignition.setDown(False)
            if self.ui.radioButton_re_phi.isChecked():
                self.flow.set_Reynolds_flow(self.Re_setPoint)
                self.flow.set_phi_flow(self.phi_setPoint)
            elif self.ui.radioButton_massflow.isChecked():
                self.flow.set_totalMassFlow(self.TotalMass_setPoint)
                self.flow.set_phi_flow(self.phi_setPoint)
            elif self.ui.radioButton_air_fuel_SLPM.isChecked():
                self.flow.set_airSLPMFlow(self.airSLPM_setPoint)
                self.flow.set_fuelSLPMFlow(self.fuelSLPM_setPoint)

            self.update_setPoints()
            self.doneIgniting = False
        
        
    def openCloseSerial(self):
        if self.connection.isOpen() == False:
            self.connection.open()
            self.fuel_meter.setup_meter()
            #change button message do "close connection"
            self.ui.button_openCloseSerial.setText("Close connection")
            self.ui.pushButton_apply_all_settings.setEnabled(False)
        else:
            self.reset_meters()
            time.sleep(.1)
            self.connection.close()
            #change button message do "close open"
            self.ui.button_openCloseSerial.setText("Open connection")
            self.ui.pushButton_apply_all_settings.setEnabled(True)
            
        print self.connection

        if not self.control_enabled:
            self.enable_control_ui(True)
        else:
            self.enable_control_ui(False)
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_mainWindow()
        self.ui.setupUi(self)
        self.timer = QtCore.QTimer()

        #initialize objects
        self.connection = Connection()
        
        self.air_meter = Meter(self.connection)
        self.fuel_meter = Meter(self.connection)       
        self.air = Air()
        self.fuel = Fuel()
        self.burner = Burner()
        self.flow = Flow(self.air_meter, self.fuel_meter, self.air, self.fuel, self.burner)
        self.record = Record(self.flow, self.ui)

        self.update_thread = Update_thread(self.air_meter,self.fuel_meter)

        self.switchedMode = False
        self.Re_setPoint = 0
        self.TotalMass_setPoint = 0
        self.airSLPM_setPoint = 0
        self.fuelSLPM_setPoint = 0
        self.phi_setPoint = 0
        self.Re_step = 5
        self.TotalMass_step = 0.1
        self.phi_step = 0.1
        self.airSLPM_step = 0.1
        self.fuelSLPM_step = 0.1

        self.ui.dial_flowRate_Air_incr.setTracking(False)
        self.ui.dial_flowRate_Fuel_incr.setTracking(False)
        self.signal_from_button = False

        self.control_enabled = False
        self.doneIgniting = False

        self.SI_to_displayUnits = 60000 # Kg/s -> g/min

        # here we connect signals with our slots
        QtCore.QObject.connect(self.ui.Button_flowrate_Air_up,QtCore.SIGNAL("clicked()"), self.Button_A_Up)
        QtCore.QObject.connect(self.ui.Button_flowrate_Air_down,QtCore.SIGNAL("clicked()"), self.Button_A_Down)
        QtCore.QObject.connect(self.ui.Button_flowrate_Fuel_up,QtCore.SIGNAL("clicked()"), self.Button_B_Up)
        QtCore.QObject.connect(self.ui.Button_flowrate_Fuel_down,QtCore.SIGNAL("clicked()"), self.Button_B_Down)
        QtCore.QObject.connect(self.ui.button_openCloseSerial,QtCore.SIGNAL("clicked()"), self.openCloseSerial)
        QtCore.QObject.connect(self.ui.pushButton_apply_all_settings,QtCore.SIGNAL("clicked()"), self.apply_all_settings)
        QtCore.QObject.connect(self.ui.button_reset,QtCore.SIGNAL("clicked()"), self.reset_meters)
        QtCore.QObject.connect(self.ui.button_record_set,QtCore.SIGNAL("clicked()"), self.add_record)
        QtCore.QObject.connect(self.ui.button_new_record_set,QtCore.SIGNAL("clicked()"), self.new_record_set)
        QtCore.QObject.connect(self.ui.delete_record_button,QtCore.SIGNAL("clicked()"), self.delete_record)
        QtCore.QObject.connect(self.ui.pushButton_start,QtCore.SIGNAL("clicked()"), self.start_update)
        QtCore.QObject.connect(self.update_thread,QtCore.SIGNAL("update_ui()"), self.update_meter_data)
        QtCore.QObject.connect(self.ui.pushButton_save_button,QtCore.SIGNAL("clicked()"), self.record.save_to_file)
        QtCore.QObject.connect(self.ui.button_abort,QtCore.SIGNAL("clicked()"), self.abort)
        QtCore.QObject.connect(self.ui.button_ignition,QtCore.SIGNAL("clicked()"), self.ignition_set_point)
        QtCore.QObject.connect(self.ui.pushButton_clear_records,QtCore.SIGNAL("clicked()"), self.record.clear_records)
        
        self.ui.record_list.itemSelectionChanged.connect(self.item_selected)
        self.ui.dial_flowRate_Air_incr.valueChanged.connect(self.SLPM_Air_slide)
        self.ui.dial_flowRate_Fuel_incr.valueChanged.connect(self.SLPM_Fuel_slide)
        self.ui.comboBox_fuel.currentIndexChanged.connect(self.fuel_changed)

        QtCore.QObject.connect(self.ui.radioButton_re_phi,QtCore.SIGNAL("toggled(bool)"),self.switchControlMode)
        QtCore.QObject.connect(self.ui.radioButton_air_fuel_SLPM,QtCore.SIGNAL("toggled(bool)"),self.switchControlMode)
          
        QtCore.QObject.connect(self.ui.flowrate_Air_incr,QtCore.SIGNAL("valueChanged(double)"),self.update_step)
        QtCore.QObject.connect(self.ui.flowrate_Fuel_incr,QtCore.SIGNAL("valueChanged(double)"),self.update_step)