def __init__(self): QMainWindow.__init__(self) self.ui_main = Ui_MainWindow() self.data = DataManager() #self.maya = self.ui_main.MayaviQwidget self.DIG = DIG3D() self.calibration_window = Calibration_Window(self) self.new = new_subject_dialog(self) self.prompt_text = "" self.dig_text = "" self.timer_main = QTimer(self) self.timer_calibration = QTimer(self) #self.timer_progress = QTimer(self) self.ui_main.setupUi(self) #self.ui_main.progressBar.setMinimum(0) #self.ui_main.progressBar.setMaximum(100) #self.progressBar_value = 0 self.interface_before_new() self.callback_register()
class MainWindow(QMainWindow,Ui_MainWindow): def __init__(self): QMainWindow.__init__(self) self.ui_main = Ui_MainWindow() self.data = DataManager() #self.maya = self.ui_main.MayaviQwidget self.DIG = DIG3D() self.calibration_window = Calibration_Window(self) self.new = new_subject_dialog(self) self.prompt_text = "" self.dig_text = "" self.timer_main = QTimer(self) self.timer_calibration = QTimer(self) #self.timer_progress = QTimer(self) self.ui_main.setupUi(self) #self.ui_main.progressBar.setMinimum(0) #self.ui_main.progressBar.setMaximum(100) #self.progressBar_value = 0 self.interface_before_new() self.callback_register() #------------------register callbacks def callback_register(self): self.ui_main.pushButton_new.clicked.connect(self.new_subject) self.new.pushButton_ok.clicked.connect(self.check_new) self.new.pushButton_cancel.clicked.connect(self.give_up_new) self.ui_main.pushButton_back.clicked.connect(self.delete_last_point) self.ui_main.pushButton_finish.clicked.connect(self.finish) self.ui_main.pushButton_load.clicked.connect(self.load) self.ui_main.pushButton_navigation.clicked.connect(self.navigation_start) self.ui_main.pushButton_save.clicked.connect(self.save_data) self.ui_main.pushButton_DIG3D.clicked.connect(self.DIG3D_reconnect) #self.timer_check.timeout.connect(self.DIG3D_chck) self.timer_calibration.timeout.connect(self.calibration_data) self.timer_main.timeout.connect(self.get_digitizer_point) #self.timer_progress.timeout.connect(self.add_progress_value) self.ui_main.pushButton_calibration.clicked.connect(self.start_calibration) self.calibration_window.pushButton_OK.clicked.connect(self.stop_calibration_OK) self.calibration_window.pushButton_restart.clicked.connect(self.calibration_window.restart_calibration) self.calibration_window.finished.connect(self.stop_calibration_close) #------- functions about interface --------- def interface_before_new(self): #set up gui self.ui_main.pushButton_new.setEnabled(True) self.ui_main.pushButton_load.setEnabled(False) self.ui_main.pushButton_navigation.setEnabled(False) self.ui_main.pushButton_save.setEnabled(False) self.ui_main.pushButton_back.setEnabled(False) self.ui_main.pushButton_finish.setEnabled(False) self.ui_main.pushButton_DIG3D.setEnabled(True) self.ui_main.pushButton_calibration.setEnabled(True) #self.ui_main.progressBar.hide() if self.DIG.connection(): self.dig_text = "3d digitizer is connected by USB! " self.prompt_text = "Please click new subject head to continue" self.ui_main.statusbar.showMessage(self.dig_text + self.prompt_text) else: self.dig_text = "3d digitizer is not connected! " self.prompt_text = "Please check your device then click plug logo to reconnect." self.ui_main.statusbar.showMessage(self.dig_text + self.prompt_text) def interface_after_new(self): #set up gui self.ui_main.pushButton_new.setEnabled(True) self.ui_main.pushButton_load.setEnabled(False) self.ui_main.pushButton_navigation.setEnabled(False) self.ui_main.pushButton_save.setEnabled(False) self.ui_main.pushButton_back.setEnabled(False) self.ui_main.pushButton_finish.setEnabled(False) self.ui_main.pushButton_DIG3D.setEnabled(True) self.ui_main.pushButton_calibration.setEnabled(True) self.ui_main.MayaviQwidget.visualization.show_head() self.ui_main.MayaviQwidget.visualization.point_to_obtain = self.data.model_ref self.ui_main.MayaviQwidget.visualization.obtain_turn = 1 self.ui_main.MayaviQwidget.visualization.show_obtain_points() self.data.step = 0 self.timer_main.start(100) def interface_before_S3R(self): self.ui_main.pushButton_finish.setEnabled(True) def interface_after_finish_obtain(self): #set up gui self.ui_main.pushButton_new.setEnabled(True) self.ui_main.pushButton_load.setEnabled(False) self.ui_main.pushButton_navigation.setEnabled(False) self.ui_main.pushButton_save.setEnabled(False) self.ui_main.pushButton_back.setEnabled(False) self.ui_main.pushButton_finish.setEnabled(False) self.ui_main.pushButton_DIG3D.setEnabled(False) self.ui_main.pushButton_calibration.setEnabled(False) self.timer_main.stop() def interface_before_load(self): #set up gui self.ui_main.pushButton_new.setEnabled(True) self.ui_main.pushButton_load.setEnabled(True) self.ui_main.pushButton_navigation.setEnabled(False) self.ui_main.pushButton_save.setEnabled(False) self.ui_main.pushButton_back.setEnabled(False) self.ui_main.pushButton_finish.setEnabled(False) self.ui_main.pushButton_DIG3D.setEnabled(False) self.ui_main.pushButton_calibration.setEnabled(False) def interface_after_load(self): #set up gui self.ui_main.pushButton_new.setEnabled(True) self.ui_main.pushButton_load.setEnabled(True) self.ui_main.pushButton_navigation.setEnabled(True) self.ui_main.pushButton_save.setEnabled(False) self.ui_main.pushButton_back.setEnabled(False) self.ui_main.pushButton_finish.setEnabled(False) self.ui_main.pushButton_DIG3D.setEnabled(False) self.ui_main.pushButton_calibration.setEnabled(False) def interface_before_navigation(self): #set up gui self.ui_main.pushButton_new.setEnabled(True) self.ui_main.pushButton_load.setEnabled(False) self.ui_main.pushButton_navigation.setEnabled(False) self.ui_main.pushButton_save.setEnabled(False) self.ui_main.pushButton_back.setEnabled(False) self.ui_main.pushButton_finish.setEnabled(True) self.ui_main.pushButton_DIG3D.setEnabled(True) self.ui_main.pushButton_calibration.setEnabled(True) self.data.step = 1 self.data.number_points[1] = 0 def interface_after_navigation(self): #set up gui self.ui_main.pushButton_new.setEnabled(True) self.ui_main.pushButton_load.setEnabled(False) self.ui_main.pushButton_navigation.setEnabled(False) self.ui_main.pushButton_save.setEnabled(True) self.ui_main.pushButton_back.setEnabled(True) self.ui_main.pushButton_finish.setEnabled(False) self.ui_main.pushButton_DIG3D.setEnabled(True) self.ui_main.pushButton_calibration.setEnabled(True) def interface_before_save(self): #set up gui self.ui_main.pushButton_new.setEnabled(True) self.ui_main.pushButton_load.setEnabled(False) self.ui_main.pushButton_navigation.setEnabled(False) self.ui_main.pushButton_save.setEnabled(True) self.ui_main.pushButton_back.setEnabled(False) self.ui_main.pushButton_finish.setEnabled(False) self.ui_main.pushButton_DIG3D.setEnabled(False) self.ui_main.pushButton_calibration.setEnabled(False) self.timer_main.stop() @Slot() #------ functions for new subject --------- def new_subject(self): if self.timer_main.isActive(): self.timer_main.stop() self.new.show() def check_new(self): subject_name = self.new.lineEdit.text() if subject_name == "": QMessageBox.warning(self,"Error","Please input subject's name or ID",QMessageBox.Ok) else: self.data.subjectName = subject_name print self.data.subjectName self.new.close() self.interface_after_new() def give_up_new(self): self.new.close() #------ function to load reference data, including reference subject's 1020 system points and four plate points ------ @Slot() def load(self): filename,selectedFilter = QFileDialog.getOpenFileName(filter="*.mat") if filename == '' and selectedFilter == '': return file_handle = sio.loadmat(file_name = filename) try: ref_1020 = file_handle['l1020'] ref_plate = file_handle['plate'] except: QMessageBox.warning(self,"Error","The .mat file is not contain reference subject's 1020 system points and plate information",QMessageBox.Ok) return self.data.ref_1020 = ref_1020 self.data.ref_plate = ref_plate self.data.compute_navigation_target() navigation_target_mesh = self.data.navigation_target_mesh self.ui_main.MayaviQwidget.visualization.navigation_target = self.data.navigation_target self.ui_main.MayaviQwidget.visualization.navigation_target_mesh = navigation_target_mesh self.ui_main.MayaviQwidget.visualization.sub_1020_mesh = self.data.sub_1020_mesh self.ui_main.MayaviQwidget.visualization.hide_head() self.ui_main.MayaviQwidget.visualization.show_target() self.interface_after_load() #------- functions for navigation @Slot() def navigation_start(self): self.interface_before_navigation() if self.timer_main.isActive() == False: self.timer_main.start(100) #------- functions about calibration --------- @Slot() def start_calibration(self): #stop the main timer self.timer_main.stop() #show the calibration window self.calibration_window.show() self.calibration_window.setModal(True) #set up calibration data self.calibration_window.first_point = None self.calibration_window.second_point = None self.calibration_window.start_calibration() self.timer_calibration.start(100) @Slot() def calibration_data(self): cite_data = self.DIG.readPDIData() if cite_data == None: return point = cite_data[0,0:3] first = self.calibration_window.first_point second = self.calibration_window.second_point #print str(point) #print "first"+str(first) #print "second"+str(second) if first == None: self.calibration_window.first_point = point self.calibration_window.proc_calibration() #print "after first"+str(self.calibration_window.first_point) elif second == None: self.calibration_window.second_point = point self.calibration_window.proc_calibration() #print "after second"+str(self.calibration_window.second_point) else: return @Slot() def stop_calibration_OK(self): self.timer_calibration.stop() self.calibration_window.close() self.timer_main.start(100) @Slot() def stop_calibration_close(self): self.timer_calibration.stop() self.calibration_window.close() self.timer_main.start(100) #------- functions about 3d digitizer --------- @Slot() def DIG3D_reconnect(self): self.DIG.unconnection() time.sleep(1) if self.DIG.connection(): self.dig_text = "3d digitizer is connected by USB! " self.prompt_text = "Please click new subject head to continue" self.ui_main.statusbar.showMessage(self.dig_text + self.prompt_text) else: self.dig_text = "3d digitizer is not connected! " self.prompt_text = "Please check your device then click plug logo to reconnect." self.ui_main.statusbar.showMessage(self.dig_text + self.prompt_text) #------- functions for data obtain or delete point ------- @Slot() def get_digitizer_point(self): cite_data = self.DIG.readPDIData() if cite_data == None: return self.data.push_point(cite_data) step = self.data.step index = self.data.number_points[step] #-update the mayaview if step == 0: self.ui_main.pushButton_back.setEnabled(True) self.ui_main.MayaviQwidget.visualization.obtain_turn = index+1 self.ui_main.MayaviQwidget.visualization.show_obtain_points() if index < 26 : return elif index == 26: self.interface_before_S3R() return if step == 1: #-updata the mayaview target_turn = self.data.number_points[1] print "target turn"+str(target_turn) target_point = self.data.navigation_target[target_turn] distance = np.linalg.norm(target_point-self.data.navigation_process) self.ui_main.MayaviQwidget.visualization.target_distance = distance self.ui_main.MayaviQwidget.visualization.navigation_current_mesh = self.data.navigation_process_mesh self.ui_main.MayaviQwidget.visualization.navigation_current = self.data.navigation_process self.ui_main.MayaviQwidget.visualization.navigation_flash() @Slot() def delete_last_point(self): self.data.pop_point() step = self.data.step index = self.data.number_points[step] if step == 0: self.ui_main.MayaviQwidget.visualization.obtain_turn = index+1 self.ui_main.MayaviQwidget.visualization.show_obtain_points() if index == 0 : self.ui_main.pushButton_back.setEnabled(False) if index < 26: self.ui_main.pushButton_finish.setEnabled(False) elif step == 1: self.ui_main.MayaviQwidget.visualization.target_turn -= 1 index = self.data.number_points[1] self.ui_main.MayaviQwidget.visualization.show_target() self.ui_main.pushButton_finish.setEnabled(True) self.ui_main.pushButton_save.setEnabled(False) if index == 0 : self.ui_main.pushButton_back.setEnabled(False) if self.timer_main.isActive() == False: self.timer_main.start(100) @Slot() def finish(self): step = self.data.step index = self.data.number_points[step] if step == 0 : #self.ui_main.progressBar.setVisible(True) #self.timer_progress.start() self.interface_after_finish_obtain() self.data.S3Ralgo_l1020() ############这里不能简单乘5就算了,要计算一下 self.ui_main.MayaviQwidget.visualization.sub_1020 = (self.data.sub_1020) self.ui_main.MayaviQwidget.visualization.sub_GheadR = (self.data.sub_GheadR[::3]) self.ui_main.MayaviQwidget.visualization.show_ghead_and_1020() #self.timer_progress.stop() #self.ui_main.progressBar.hide() self.interface_before_load() elif step == 1: self.data.navigation_result[index,:] = self.data.navigation_process self.data.navigation_result_mesh[index,:] = self.data.mesh_point_mapping(self.data.sub_landmark, self.data.navigation_result[index,:]) self.data.number_points[1] += 1 self.ui_main.MayaviQwidget.visualization.target_turn += 1 index = self.data.number_points[1] if index < 4: self.ui_main.MayaviQwidget.visualization.show_target() elif index == 4 : #把板的结果显示出来 self.timer_main.stop() self.ui_main.MayaviQwidget.visualization.navigation_result_mesh = self.data.navigation_result_mesh self.ui_main.MayaviQwidget.visualization.show_navigation_result() self.interface_after_navigation() self.ui_main.pushButton_back.setEnabled(True) @Slot() def save_data(self): self.interface_before_save() path_name = QFileDialog.getExistingDirectory() # check reference subjectID = self.data.subjectName subject_1020 = self.data.sub_1020 subject_plate = self.data.navigation_result if path_name ==u"": return try: sio.savemat(path_name+u"\\"+ "rewear"+ subjectID,{'l1020':subject_1020, 'plate':subject_plate}) except: QMessageBox.warning(self,"Error","Error occured when saving data!",QMessageBox.Ok) return QMessageBox.information(self,"Notification","Saving completed!",QMessageBox.Ok) return #def add_progress_value(self): #if self.progressBar_value < 100: #self.progressBar_value = self.progressBar_value +1 #self.progressBar.setValue(self.progressBar_value) #else: #return def exit_app(self): pass def close_window(self): pass def closeEvent(self,event): #-stop timers self.timer_calibration.stop() self.timer_main.stop() #-unconnect DIG3D self.DIG.unconnection() #-disconnect the TCP link self.DIG.TCP_disconnect() #print("exit callback!") event.accept()