def waitForSignal(signal, message="", timeout=0): """Waits (max timeout msecs if given) for a signal to be emitted. It the waiting lasts more than 2 seconds, a progress dialog is displayed with the message. Returns True if the signal was emitted. Return False if the wait timed out or the dialog was canceled by the user. """ loop = QEventLoop() dlg = QProgressDialog(minimum=0, maximum=0, labelText=message) dlg.setWindowTitle(appinfo.appname) dlg.setWindowModality(Qt.ApplicationModal) QTimer.singleShot(2000, dlg.show) dlg.canceled.connect(loop.quit) if timeout: QTimer.singleShot(timeout, dlg.cancel) stop = lambda: loop.quit() signal.connect(stop) loop.exec_() signal.disconnect(stop) dlg.hide() dlg.deleteLater() return not dlg.wasCanceled()
def transferPoints(self, other): params = parameters.instance current_data = self.current_data items = self.selectedItems() if len(items) == 0: items = [it for it in self.points.values() if it.arrow is None and it.link is None] new_pt_ids = [] new_pt_pos = [] move_pt_ids = [] move_pt_new_pos = [] if params.estimate: progress = QProgressDialog("Estimating position of the points...", "Abort", 0, len(items), self.parent()) progress.setMinimumDuration(1) size = (params.filter_size, params.filter_size) im1 = image_cache.cache.numpy_array(self.image_path, size) im2 = image_cache.cache.numpy_array(other.image_path, size) for i, it in enumerate(items): pos = self.findPoint(im1, im2, other, it) id = it.pt_id if id in other.points: move_pt_ids.append(id) move_pt_new_pos.append(pos) else: new_pt_ids.append(id) new_pt_pos.append(pos) progress.setValue(i+1) if progress.wasCanceled(): progress.hide() break else: for it in items: id = it.pt_id pos = current_data[id] if id in other.points: move_pt_ids.append(id) move_pt_new_pos.append(pos) else: new_pt_ids.append(id) new_pt_pos.append(pos) if new_pt_ids or move_pt_ids: self.undo_stack.beginMacro("Transfer point(s) from %s to %s" % (self.image_name, other.image_name)) if new_pt_ids: other.planAddPoints(new_pt_ids, new_pt_pos) if move_pt_ids: other.planMovePoints(move_pt_ids, move_pt_new_pos) self.undo_stack.endMacro()
class SimulationTab(QMdiArea): def __init__(self, simulation_window, simulation_file=None, parent=None): QMdiArea.__init__(self, parent) if simulation_file: self._configuration = Configuration(simulation_file) else: self._configuration = Configuration() self.worker = None self._model = None self._gantt = None self._logs = None self._editor = None self._metrics_window = None self._model_window = None self._progress_bar = None self._documentation = None self._simulation_window = simulation_window self._configuration.configurationChanged.connect( self.configuration_changed) self._configuration.configurationSaved.connect( self.configuration_saved) self.showModelWindow() @property def simulation_file(self): return self._configuration.simulation_file @property def configuration(self): return self._configuration def configuration_saved(self): simulation_file = self._configuration.simulation_file self._simulation_window.setTabText(self, os.path.split(simulation_file)[1]) self._simulation_window.updateMenus() def configuration_changed(self): simulation_file = self._configuration.simulation_file if not simulation_file: simulation_file = "Unsaved" self._simulation_window.setTabText( self, '* ' + os.path.split(simulation_file)[1]) def showGantt(self): if not self._gantt and self._model: self._gantt = create_gantt_window(self._model) if self._gantt: self.addSubWindow(self._gantt) if self._gantt: self._gantt.parent().show() def showModelWindow(self): if not self._model_window and self._configuration: self._model_window = ModelWindow(self._configuration, self) self.addSubWindow(self._model_window) if self._model_window: self._model_window.parent().show() def showResults(self): if not self._metrics_window and self._model and self._model.results: self._metrics_window = ResultsWindow(self._model.results) self.addSubWindow(self._metrics_window) if self._metrics_window: self._metrics_window.parent().show() def _reinit_simu(self): self._model = None if self._gantt: self.removeSubWindow(self._gantt.parent()) if self._logs: self.removeSubWindow(self._logs.parent()) if self._metrics_window: self.removeSubWindow(self._metrics_window.parent()) self._gantt = None self._logs = None self._metrics_window = None def save(self): self._configuration.save() def save_as(self, filename): self._configuration.save(filename) def run(self): if self._configuration: try: self._configuration.check_all() self._reinit_simu() self._progress_bar = QProgressDialog("Simulating...", "Abort", 0, 100) self._progress_bar.canceled.connect(self.abort) self._progress_bar.show() self.worker = RunSimulation() self._model = Model(self._configuration, callback=self.worker.updateProgressBar) self.worker.set_model(self._model) self.worker.finished.connect(self.runFinished) self.worker.start() QObject.connect(self.worker, SIGNAL("updateProgressBar"), self.updateProgressBar) except Exception as msg: QMessageBox.warning(self, "Configuration error", str(msg)) self._reinit_simu() self.runFinished() def abort(self): self._progress_bar = None self._model.stopSimulation() self._simulation_window.updateMenus() # Ultimate killer. self.worker.terminate() def runFinished(self): if self._progress_bar: self._progress_bar.hide() self._simulation_window.updateMenus() self.showResults() if self.worker and self.worker.error: QMessageBox.critical(None, "Exception during simulation", ''.join(self.worker.get_error()), QMessageBox.Ok | QMessageBox.Default, QMessageBox.NoButton) def updateProgressBar(self, value): if self._progress_bar: duration = self._configuration.duration p = 100.0 * value / duration self._progress_bar.setValue(int(p)) def close(self): if not self._configuration.is_saved(): ret = QMessageBox.warning( self, "Close without saving?", "The configuration of the " "simulation is not saved and the modifications will be lost.\n" "Are you sure you want to quit?", QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Cancel) if ret == QMessageBox.Cancel: return False return True
class specread: def __init__(self, specfile, startLineNum=0, endScanNum=0, beamline='APS-15IDC', det='CCD', data={}, par={}): self.Data = data self.Par = par self.specfile = specfile if beamline == 'APS-15IDC': self.APS_15IDC(startLineNum=startLineNum, endScanNum=endScanNum, det=det) if beamline == 'APS-9IDC': self.APS_9IDC(startLineNum=startLineNum, endScanNum=endScanNum, det=det) def updateProgress(self): self.progressDialog.setValue(self.progressDialog.value() + 1) def APS_15IDC(self, startLineNum=0, endScanNum=0, det='CCD'): """ Function to read a complete spec File collected at APS 15IDC """ self.progressDialog = QProgressDialog('Reading scans form SPEC File:', 'Abort', 0, 100) self.progressDialog.setWindowModality(Qt.WindowModal) self.progressDialog.setWindowTitle('Wait') self.progressDialog.setAutoClose(True) self.progressDialog.setAutoReset(True) self.progressDialog.setMinimum(1) self.Data['YCol'] = 'Apex2' self.Data['NCol'] = 'Monc' fid = open(self.specfile) fdata = fid.readlines() self.SpecFileFull = fdata fid.close() if fdata[0][:2] != '#F': self.Data['NumOfScans'] = 0 self.Data['Message'] = 'The file is not a valid specfile!!' print 'Error:: The file is not a valid specfile!!' else: startScanLineNums = [ i for i in range(startLineNum, len(fdata)) if fdata[i][:2] == '#S' ] self.progressDialog.setMaximum(len(startScanLineNums)) self.progressDialog.show() if startLineNum > 0: startScanLineNums = sorted(startScanLineNums) numOfScans = len(startScanLineNums) scanLines = [fdata[i] for i in startScanLineNums] if startLineNum == 0: tmp = 0 self.Data['NumOfScans'] = 0 #numOfScans self.Data['ScanNums'] = [] offset = 0 # self.Data['ScanLines']=[]#scanLines # self.Data['StartScanLineNums']=[]#startScanLineNums else: tmp = self.Data['NumOfScans'] self.Data[ 'NumOfScans'] = self.Data['NumOfScans'] - 1 #+numOfScans offset = 1 # self.Data['ScanLines']=self.Data['ScanLines'][:-1]#+scanLines[1:] # self.Data['StartScanLineNums']=self.Data['StartScanLineNums'][:-1]#+startScanLineNums if int(scanLines[-1].split() [1]) != len(startScanLineNums) + endScanNum - offset: print len( startScanLineNums), scanLines[-1].split()[1], endScanNum self.Data['Error'] = True self.Data['Message'] = 'There are identical scans in the file' else: self.Data['Error'] = False for i in range(numOfScans): start = startScanLineNums[i] + 1 line = fdata[start] num = int(fdata[startScanLineNums[i]].split()[1]) i = i + tmp self.Data[num] = {} self.Par[num] = {} if fdata[start - 1].split()[2] == 'getandsave_mca': self.Par[num]['Detector'] = 'Vortex' self.Data[num]['ScanVar'] = 'Empty' else: self.Par[num]['Detector'] = 'Monitor' tmpdata = [] while line[:2] != '\n' and line[:2] != '#C': if line[:2] == '#P': parName = line[4:].split() start = start + 1 parValue = map(eval, fdata[start][1:].split()) for j in range(len(parName)): self.Par[num][parName[j]] = parValue[j] if line[:2] == '#W': tmppar = line[2:].split() self.Par[num]['Wavelength'] = eval(tmppar[1]) if line[:3] == '#G0': self.Par[num]['g_l1'] = float(line[4:].split()[5]) self.Par[num]['g_l2'] = float(line[4:].split()[6]) self.Par[num]['g_l3'] = float(line[4:].split()[7]) if line[:2] == '#A': tmppar = line[2:].split() self.Par[num]['Absorber'] = eval(tmppar[1]) if line[:2] == '#Q': tmppar = line[2:].split() self.Par[num]['Q'] = map(eval, tmppar) if line[:2] == '#V': self.Par[num]['Detector'] = 'Vortex' if line[:3] == '#B0': tmppar = line[3:].split('.') self.Par[num]['ImageNumber'] = len( line[3:].split('_')[-1].split('.')[0]) if tmppar[1] == 'tif\n': self.Par[num]['Detector'] = 'Pilatus' elif tmppar[1] == 'sfrm\n': self.Par[num]['Detector'] = 'Bruker' if line[:3] == '#B1': try: tmppar = map(eval, line[3:].split()) except: tmppar = map(eval, line[3:].split()[:-1]) self.Par[num]['DBPos'] = tmppar[:2] self.Par[num]['S2D_Dist'] = tmppar[2] self.Par[num]['S7D_Dist'] = tmppar[3] if line[:3] == '#B2': tmppar = map(eval, line[3:].split()) self.Par[num]['DBPos'] = tmppar[:2] self.Par[num]['S2D_Dist'] = tmppar[2] self.Par[num]['S7D_Dist'] = tmppar[3] if line[:2] == '#L': scanVar = line[3:-1].split() self.Data[num]['ScanVar'] = scanVar if line[0] != '#': try: tmpdata.append(map(eval, line.split())) except: self.Data[num][ 'Message'] = 'Something wrong with Scan Number %d', num, '.Please check the the scan in the specfile.' print 'Something wrong with Scan Number %d', num start = start + 1 try: line = fdata[start] except: break if self.Data[num]['ScanVar'] != 'Empty': for j in range(len(scanVar)): try: self.Data[num][scanVar[j]] = np.array( tmpdata, dtype='float')[:, j] except: self.Data[num][scanVar[j]] = None if len(self.Par[num]) == 1: self.Par[num]['Message'] = 'No parameters!!' self.progressDialog.setLabelText('Reading Scan #' + str(num)) self.updateProgress() self.Data['NumOfScans'] = num # self.Data['ScanLines']=self.Data['ScanLines']+[scanLines[num-tmp]] self.Data[num]['ScanLine'] = fdata[startScanLineNums[i - tmp]] self.Data[num]['StartScanLineNum'] = startScanLineNums[i - tmp] self.endLineNum = startScanLineNums[i - tmp] self.Data['ScanNums'].append(num) if self.progressDialog.wasCanceled() == True: break self.progressDialog.hide() def APS_9IDC(self, startLineNum=0, endScanNum=0, det='CCD'): """ Function to read a complete spec File collected at APS 9IDC """ self.progressDialog = QProgressDialog('Reading scans form SPEC File:', 'Abort', 0, 100) self.progressDialog.setWindowModality(Qt.WindowModal) self.progressDialog.setWindowTitle('Wait') self.progressDialog.setAutoClose(True) self.progressDialog.setAutoReset(True) self.progressDialog.setMinimum(1) self.Data['YCol'] = 'Bicron1' self.Data['NCol'] = 'i2' fid = open(self.specfile) fdata = fid.readlines() self.SpecFileFull = fdata fid.close() if fdata[0][:2] != '#F': self.Data['NumOfScans'] = 0 self.Data['Message'] = 'The file is not a valid specfile!!' print 'Error:: The file is not a valid specfile!!' else: startScanLineNums = [ i for i in range(startLineNum, len(fdata)) if fdata[i][:2] == '#S' ] self.progressDialog.setMaximum(len(startScanLineNums)) self.progressDialog.show() self.endLineNum = startScanLineNums[-1] if startLineNum > 0: startScanLineNums = sorted(startScanLineNums) self.Data['StartScanLineNums'] = startScanLineNums numOfScans = len(self.Data['StartScanLineNums']) scanLines = [fdata[i] for i in startScanLineNums] if startLineNum == 0: tmp = 0 self.Data['NumOfScans'] = 0 #numOfScans self.Data['ScanLines'] = [] #scanLines self.Data['StartScanLineNums'] = [] #startScanLineNums self.Par['ParName'] = [] for i in range(startScanLineNums[0]): line = fdata[i].split() if fdata[i][:2] == '#O': self.Par['ParName'] = self.Par['ParName'] + line[1:] else: tmp = self.Data['NumOfScans'] self.Data[ 'NumOfScans'] = self.Data['NumOfScans'] - 1 #+numOfScans self.Data['ScanLines'] = self.Data[ 'ScanLines'][:-1] #+scanLines[1:] self.Data['StartScanLineNums'] = self.Data[ 'StartScanLineNums'][:-1] #+startScanLineNums for i in range(numOfScans): start = startScanLineNums[i] + 1 line = fdata[start] i = i + tmp self.Data[i] = {} self.Par[i] = {} if fdata[start - 1].split()[2] == 'getandsave_mca' or fdata[ start - 1].split()[2] == 'MCAscanpt': self.Par[i]['Mca'] = 1 self.Data[i]['ScanVar'] = 'Empty' else: self.Par[i]['Mca'] = 0 self.Par[i]['CCD'] = 0 tmpdata = [] pstart = 0 while line[:2] != '\n' and line[:2] != '#C': if line[:2] == '#P': parValue = map(eval, fdata[start].split()[1:]) for j in range(len(parValue)): self.Par[i][self.Par['ParName'] [pstart]] = parValue[j] pstart = pstart + 1 if line[:2] == '#Q': tmppar = line[2:].split() self.Par[i]['Q'] = map(eval, tmppar) if line[:2] == '#L': scanVar = line[3:-1].split() self.Data[i]['ScanVar'] = scanVar if line[0] != '#': tmpdata.append(map(eval, line.split())) start = start + 1 line = fdata[start] for j in range(len(scanVar)): try: self.Data[i][scanVar[j]] = np.array(tmpdata)[:, j] except: self.Data[i][scanVar[j]] = None if len(self.Par[i]) == 1: self.Par[i]['Message'] = 'No parameters!!' self.progressDialog.setLabelText( 'Reading scans form SPEC File: ' + str(i + 1)) self.updateProgress() self.Data['NumOfScans'] = i self.Data['ScanLines'] = self.Data['ScanLines'] + [ scanLines[i - tmp] ] self.Data['StartScanLineNums'] = self.Data[ 'StartScanLineNums'] + [startScanLineNums[i - tmp]] self.endLineNum = startScanLineNums[i - tmp] if self.progressDialog.wasCanceled() == True: break self.progressDialog.hide()