def timer_factory(self, func=None, period=150): """ reuse timer if func is the same """ timer = self.timer if func is None: func = self._inprogress_update if timer is None: self._not_moving_count = 0 timer = Timer(period, func, delay=250) elif timer.func == func: if timer.isActive(): self.debug('reusing old timer') else: self._not_moving_count = 0 timer = Timer(period, func, delay=250) else: timer.stop() self._not_moving_count = 0 time.sleep(period / 1000.) timer = Timer(period, func) timer.set_interval(period) return timer
def timer_factory(self, func=None, period=150): """ reuse timer if func is the same """ timer = self.timer if func is None: func = self._inprogress_update if timer is None: self._not_moving_count = 0 timer = Timer(period, func, delay=250) elif timer.func == func: if timer.isActive(): self.debug('reusing old timer') else: self._not_moving_count = 0 timer = Timer(period, func, delay=250) else: timer.stop() self._not_moving_count = 0 time.sleep(period / 1000.) timer = Timer(period, func) timer.set_interval(period) return timer
def timer_factory(self): """ reuse timer if possible """ timer = self.timer func = self._update_position if timer is None: self._not_moving_count = 0 timer = Timer(250, func) else: if timer.isActive(): self.debug('reusing old timer') else: self._not_moving_count = 0 timer = Timer(250, func) return timer
def timer_factory(self): """ reuse timer if possible """ timer = self.timer func = self._update_position if timer is None: self._not_moving_count = 0 timer = Timer(250, func) else: if timer.isActive(): self.debug('reusing old timer') else: self._not_moving_count = 0 timer = Timer(250, func) return timer
class WaitControl(Loggable): page_name = Str('Wait') message = Str message_color = Color('black') high = Int duration = Float(10) current_time = Float current_display_time = Property(depends_on='current_time') auto_start = Bool(False) timer = None end_evt = None continue_button = Button('Continue') pause_button = Button('Pause') _paused = Bool _continued = Bool _canceled = Bool _no_update = False def __init__(self, *args, **kw): self.reset() super(WaitControl, self).__init__(*args, **kw) if self.auto_start: self.start(evt=self.end_evt) def is_active(self): if self.timer: return self.timer.isActive() def is_canceled(self): return self._canceled def is_continued(self): return self._continued def join(self, evt=None): if evt is None: evt = self.end_evt if self.duration > 1: evt.wait(self.duration - 1) while not evt.wait(timeout=0.25): time.sleep(0.25) self.debug('Join finished') def start(self, block=True, evt=None, duration=None, message=None): if self.end_evt: self.end_evt.set() if evt is None: evt = Event() if evt: evt.clear() self.end_evt = evt if self.timer: self.timer.stop() self.timer.wait_for_completion() if duration: # self.duration = 1 self.duration = duration self.reset() if message: self.message = message self.timer = Timer(1000, self._update_time, delay=1000) self._continued = False if block: self.join(evt=evt) if evt == self.end_evt: self.end_evt = None def stop(self): self._end() self.debug('wait dialog stopped') if self.current_time > 1: self.message = 'Stopped' self.message_color = 'red' # self.current_time = 0 def reset(self): with no_update(self, fire_update_needed=False): self.high = int(self.duration) self.current_time = self.duration self._paused = False # =============================================================================== # private # =============================================================================== def _continue(self): self._paused = False self._continued = True self._end() self.current_time = 0 def _end(self): self.message = '' if self.timer is not None: self.timer.Stop() if self.end_evt is not None: self.end_evt.set() def _update_time(self): if self._paused: return ct = self.current_time if self.timer and self.timer.isActive(): self.current_time -= 1 ct -= 1 # self.debug('Current Time={}/{}'.format(ct, self.duration)) if ct <= 0: self._end() self._canceled = False else: self.current_time = ct # def _current_time_changed(self): # if self.current_time <= 0: # self._end() # self._canceled = False def _get_current_display_time(self): return '{:03d}'.format(int(self.current_time)) # =============================================================================== # handlers # =============================================================================== def _pause_button_fired(self): self._paused = not self._paused def _continue_button_fired(self): self._continue() def _high_changed(self, v): if self._no_update: return self.duration = v self.current_time = v
class WaitControl(Loggable): page_name = Str('Wait') message = Str message_color = Color('black') high = Float duration = Float(10) current_time = Float auto_start = Bool(False) timer = None end_evt = None continue_button = Button('Continue') pause_button = Button('Pause') _paused = Bool _continued = Bool _canceled = Bool _no_update = False def __init__(self, *args, **kw): self.reset() super(WaitControl, self).__init__(*args, **kw) if self.auto_start: self.start(evt=self.end_evt) def is_active(self): if self.timer: return self.timer.isActive() def is_canceled(self): return self._canceled def is_continued(self): return self._continued def join(self, evt=None): if evt is None: evt = self.end_evt # time.sleep(0.25) evt.wait(0.25) # while not self.end_evt.is_set(): while not evt.wait(timeout=0.1): pass # while not evt.is_set(): # # time.sleep(0.005) # evt.wait(0.005) self.debug('Join finished') def start(self, block=True, evt=None, duration=None, message=None): if self.end_evt: self.end_evt.set() if evt is None: evt = Event() if evt: evt.clear() self.end_evt = evt if self.timer: self.timer.stop() self.timer.wait_for_completion() if duration: # self.duration = 1 self.duration = duration self.reset() if message: self.message = message self.timer = Timer(1000, self._update_time, delay=1000) self._continued = False if block: self.join(evt=evt) if evt == self.end_evt: self.end_evt = None def stop(self): self._end() self.debug('wait dialog stopped') if self.current_time > 1: self.message = 'Stopped' self.message_color = 'red' # self.current_time = 0 def reset(self): with no_update(self, fire_update_needed=False): self.high = self.duration self.current_time = self.duration self._paused = False # =============================================================================== # private # =============================================================================== def _continue(self): self._paused = False self._continued = True self._end() self.current_time = 0 def _end(self): self.message = '' if self.timer is not None: self.timer.Stop() if self.end_evt is not None: self.end_evt.set() def _update_time(self): if self._paused: return ct = self.current_time if self.timer and self.timer.isActive(): self.current_time -= 1 ct -= 1 # self.debug('Current Time={}/{}'.format(ct, self.duration)) if ct <= 0: self._end() self._canceled = False else: self.current_time = ct # def _current_time_changed(self): # if self.current_time <= 0: # self._end() # self._canceled = False # =============================================================================== # handlers # =============================================================================== def _pause_button_fired(self): self._paused = not self._paused def _continue_button_fired(self): self._continue() def _high_changed(self, v): if self._no_update: return self.duration = v self.current_time = v
class WaitControl(Loggable): page_name = Str('Wait') message = Str message_color = Color('black') high = Float wtime = Float(10) low_name = Float(1) current_time = Float # current_time = Property(depends_on='current_time') auto_start = Bool(False) timer = None end_evt = None continue_button = Button('Continue') _continued = Bool _canceled = Bool def __init__(self, *args, **kw): self.reset() super(WaitControl, self).__init__(*args, **kw) if self.auto_start: self.start(evt=self.end_evt) def is_active(self): if self.timer: return self.timer.isActive() def is_canceled(self): return self._canceled def is_continued(self): return self._continued def join(self): time.sleep(0.25) while not self.end_evt.is_set(): time.sleep(0.05) self.debug('Join finished') def start(self, block=True, evt=None, wtime=None): if self.timer: self.timer.stop() self.timer.wait_for_completion() if evt is None: evt = Event() if evt: evt.clear() self.end_evt = evt if wtime: self.wtime=wtime self.reset() self.timer = Timer(1000, self._update_time, delay=1000 ) self._continued = False if block: self.join() def stop(self): self._end() self.debug('wait dialog stopped') if self.current_time > 1: self.message = 'Stopped' self.message_color = 'red' self.current_time = 0 def reset(self): self.high = self.wtime self.current_time = self.wtime #=============================================================================== # private #=============================================================================== def _continue(self): self._continued = True self._end() self.current_time = 0 def _end(self): self.message = '' if self.timer is not None: self.timer.Stop() if self.end_evt is not None: self.end_evt.set() def _update_time(self): if self.timer and self.timer.isActive(): self.current_time -= 1 def _current_time_changed(self): if self.current_time <= 0: self._end() self._canceled = False #=============================================================================== # handlers #=============================================================================== def _continue_button_fired(self): self._continue() def _high_changed(self, v): self.wtime = v self.current_time = v def traits_view(self): v = View(VGroup( CustomLabel('message', size=14, weight='bold', color_name='message_color' ), HGroup( Spring(width=-5, springy=False), Item('high', label='Set Max. Seconds'), spring, UItem('continue_button') ), HGroup( Spring(width=-5, springy=False), Item('current_time', show_label=False, editor=RangeEditor(mode='slider', low_name='low_name', high_name='wtime', )) ), ), ) return v
class WaitControl(Loggable): page_name = Str('Wait') message = Str message_color = Color('black') high = Float wtime = Float(10) low_name = Float(1) current_time = Float # current_time = Property(depends_on='current_time') auto_start = Bool(False) timer = None end_evt = None continue_button = Button('Continue') _continued = Bool _canceled = Bool def __init__(self, *args, **kw): self.reset() super(WaitControl, self).__init__(*args, **kw) if self.auto_start: self.start(evt=self.end_evt) def is_active(self): if self.timer: return self.timer.isActive() def is_canceled(self): return self._canceled def is_continued(self): return self._continued def join(self): time.sleep(0.25) while not self.end_evt.is_set(): time.sleep(0.05) self.debug('Join finished') def start(self, block=True, evt=None, wtime=None): if self.timer: self.timer.stop() self.timer.wait_for_completion() if evt is None: evt = Event() if evt: evt.clear() self.end_evt = evt if wtime: self.wtime = wtime self.reset() self.timer = Timer(1000, self._update_time, delay=1000) self._continued = False if block: self.join() def stop(self): self._end() self.debug('wait dialog stopped') if self.current_time > 1: self.message = 'Stopped' self.message_color = 'red' self.current_time = 0 def reset(self): self.high = self.wtime self.current_time = self.wtime #=============================================================================== # private #=============================================================================== def _continue(self): self._continued = True self._end() self.current_time = 0 def _end(self): self.message = '' if self.timer is not None: self.timer.Stop() if self.end_evt is not None: self.end_evt.set() def _update_time(self): if self.timer and self.timer.isActive(): self.current_time -= 1 def _current_time_changed(self): if self.current_time <= 0: self._end() self._canceled = False #=============================================================================== # handlers #=============================================================================== def _continue_button_fired(self): self._continue() def _high_changed(self, v): self.wtime = v self.current_time = v def traits_view(self): v = View( VGroup( CustomLabel('message', size=14, weight='bold', color_name='message_color'), HGroup(Spring(width=-5, springy=False), Item('high', label='Set Max. Seconds'), spring, UItem('continue_button')), HGroup( Spring(width=-5, springy=False), Item('current_time', show_label=False, editor=RangeEditor( mode='slider', low_name='low_name', high_name='wtime', ))), ), ) return v