def handle_capture(window, capture): email, ok = QInputDialog.getText( window, 'Email', 'Enter your employee ID or email:', flags=Qt.FramelessWindowHint | Qt.Popup) while ok and not get_email(email): email, ok = QInputDialog.getText( window, 'Email', 'Enter a valid employee ID or email:', flags=Qt.FramelessWindowHint | Qt.Popup) if ok: print('Send email to %s' % email) pb = None try: pb = QProgressDialog("Sending...", "", 0, 0, window, Qt.FramelessWindowHint | Qt.Popup) pb.setWindowModality(Qt.WindowModal) pb.setRange(0, 0) pb.setMinimumDuration(0) pb.setCancelButton(None) pb.show() nongui(send_image)(email, capture, cache_dir=IMAGE_CACHE_DIR) except Exception: import traceback traceback.print_exc() msg = QMessageBox(window) msg.setIcon(QMessageBox.Critical) msg.setText('Error sending email.') msg.setWindowFlags(Qt.FramelessWindowHint | Qt.Popup) msg.exec_() finally: if pb: pb.close()
def process_data(self): # get settings buffer_length = int(float(self.buffer_settings['buffer_length']) * self.SR) sample_length = int(float(self.buffer_settings['sample_length']) * self.SR) valid_thresh = [int(x['validity'] / 100 * sample_length) for x in self.buffer_settings['electrode_settings'].values()] bar = QProgressDialog('Processing', 'Stop', 0, len(self.depths), None) bar.setWindowModality(Qt.WindowModal) bar.show() # loop through each comment time and check whether segment is long enough for idx, (time, depth) in enumerate(zip(self.depth_times[:-1], self.depths[:-1])): bar.setValue(idx) bar.setLabelText(self.f_name + " " + str(depth)) if bar.wasCanceled(): break if self.depth_times[idx + 1] - time >= sample_length: # get sample first, if doesn't pass validation, move forward until it does # or until buffer_length is elapsed. data = self.reader.get_analogsignal_chunk(i_start=time, i_stop=time + sample_length).T valid = self.validate_data_sample(data) t_offset = 0 while not all(np.sum(valid, axis=1) > valid_thresh) and \ t_offset + sample_length < buffer_length and \ time + t_offset + sample_length < self.depth_times[idx + 1]: t_offset += 300 # roughly a 100 Hz update rate. data = self.reader.get_analogsignal_chunk(i_start=time + t_offset, i_stop=time + t_offset + sample_length).T valid = self.validate_data_sample(data) # send to db self.db_wrapper.save_depth_datum(depth=depth, data=data, is_good=np.sum(valid, axis=1) > valid_thresh, group_info=self.channels, start_time=self.rec_start_time + datetime.timedelta(seconds=(time + t_offset) / self.SR), stop_time=self.rec_start_time + datetime.timedelta(seconds=(time + t_offset + sample_length) / self.SR)) bar.close()
class QtProgressInformation(object): def __init__(self, parent): self._parent = parent self._progressDialog = QProgressDialog(self._parent) self._progressDialog.setMinimumWidth(300) self._progressDialog.setWindowModality(Qt.ApplicationModal) self._progressDialog.setMinimumDuration(0) self._progressDialog.hide() # self.progress_iterator = lambda seq, text = "Working... Please wait", allow_cancel = True, self = self : self.QtProgressIterator(self, seq, text, allow_cancel) def progress_iterator(self, sequence, text="Working... Please wait", allow_cancel=True, always_refresh=True): return self.QtProgressIterator(self, sequence, text, allow_cancel) def __show(self, text, allow_cancel, max_value=0): if not hasattr(self, '_progressDialog'): raise Exception ("%s inheriting QtProgressInformation must call QtProgressInformation.__init__" % self.__class__.__name__) self._progressDialog.reset() #reset cancel flag self._progressDialog.setWindowTitle(text) self._progressDialog.setMaximum(max_value) cancel_text = (None, "Cancel")[allow_cancel] self._progressDialog.setCancelButtonText(cancel_text) self._progressDialog self._progressDialog.show() QApplication.processEvents() def __hide(self): self._progressDialog.hide() self._progressDialog.close() QApplication.processEvents() def start_blocking_task(self, text): self.__show(text, False) def end_blocking_task(self): self.__hide() class QtProgressIterator(collections.Iterator): def __init__(self, QtProgressInformation, seq, text, allow_cancel=True): self.QtProgressInformation = QtProgressInformation self.generator = iter(list(seq)) self.allow_cancel = allow_cancel self.n = 0 max_value = self.generator.__length_hint__() self.update_every = max(1, max_value // 100) self.QtProgressInformation._QtProgressInformation__show(text, allow_cancel, max_value) def __del__(self): self.QtProgressInformation._QtProgressInformation__hide() pass def __iter__(self): return self def next(self): # required by python 2 return self.__next__() def __next__(self): if self.n % self.update_every == 0: if self.allow_cancel and self.QtProgressInformation._progressDialog.wasCanceled(): raise CancelWarning() self.QtProgressInformation._progressDialog.setValue(self.n) QApplication.processEvents() self.n += 1 return next(self.generator) # try: # return self.generator.__next__() # except AttributeError: # return self.generator.next() #in python 2, iterators __next__ is named next def _callback(self, current, maximum): if self._progressDialog.maximum() != maximum: self._progressDialog.setMaximum(maximum) self._progressDialog.setValue(current) if self._progressDialog.wasCanceled(): raise CancelWarning() def exec_long_task_with_callback(self, text, allow_cancel, task, *args, **kwargs): self.__show(text, allow_cancel) try: res = task(*args, **kwargs) except Exception as e: raise e finally: # self.taskThread = TaskThread(task, *args, **kwargs) # self.taskThread.cancel_event = threading.Event() # self.taskThread.start() # while self.taskThread.is_alive(): # time.sleep(0.1) # if allow_cancel and self._progressDialog.wasCanceled(): # self.taskThread.cancel_event.set() # self.taskThread.join() # #raise CancelWarning # QApplication.processEvents() # self.taskThread.join() self.__hide() return res # def progress_iterator(self, sequence, text="Working... Please wait", allow_cancel=True): # it = iter(sequence) # if it.__length_hint__() > 0: # self.__show(text, allow_cancel, it.__length_hint__()) # for n, v in enumerate(it): # if allow_cancel and self._progressDialog.wasCanceled(): # raise CancelWarning() # self._progressDialog.setValue(n) # yield(v) # self._progressDialog.hide() # QApplication.processEvents() def exec_long_task(self, text, allow_cancel, task, *args, **kwargs): # class TaskQThread(QThread): # result = None # def __init__(self, _parent, task, *args, **kwargs): # QThread.__init__(self) # self.task = lambda: task(*args, **kwargs) # # def run(self): # self.result = self.task() # t = TaskQThread(self, task, *args, **kwargs) # t.start() # while t.isRunning(): # time.sleep(0.1) # QApplication.processEvents() self.__show(text, allow_cancel) if allow_cancel: self.taskThread = CancelableTaskThread(task, *args, **kwargs) else: self.taskThread = TaskThread(task, *args, **kwargs) self.taskThread.start() while self.taskThread.is_alive(): time.sleep(0.1) if allow_cancel and self._progressDialog.wasCanceled(): self.taskThread.cancel_event.set() self.taskThread.join() raise CancelWarning QApplication.processEvents() self.taskThread.join() self.__hide() if isinstance(self.taskThread.result, Exception): raise self.taskThread.result return self.taskThread.result def exec_long_guitask(self, text, task, *args, **kwargs): self.__show(text, False) QApplication.processEvents() task(*args, **kwargs) self.__hide() return self.taskThread.result