def run_callbacks(self, callback, event): if callback in self.methods: for m in self.methods[callback]: try: m(self,event) except AttributeError: #print("DEBUG -- skip this except run_callbacks()", __file__) from timechart import raise_to_debug raise_to_debug(2, __file__) pass ''' if not hasattr(m,"num_exc"): m.num_exc = 0 m.num_exc += 1 if m.num_exc <10: print("bug in ", m, "still continue..") traceback.print_exc() print(event) if m.num_exc == 10: print(m, "is too buggy, disabling, please report bug!") self.methods[callback].remove(m) if len(self.methods[callback])==0: del self.methods[callback] ''' return True return False
def add_to_toolbar(self, parent, tool_bar, image_cache, controller, show_labels=True): from timechart import raise_to_debug raise_to_debug(2, __file__) """ Adds the item to a tool bar. Parameters ---------- parent : toolkit control The parent of the new menu item control. tool_bar : toolkit toolbar The toolbar to add the action item to. image_cache : ImageCache instance The image cache for resized images. controller : ActionController instance or None The controller to use. show_labels : bool Should the toolbar item show a label. """ if (controller is None) or controller.can_add_to_toolbar(self.action): wrapper = _Tool(parent, tool_bar, image_cache, self, controller, show_labels) # fixme: Martin, who uses this information? if controller is None: self.control = wrapper.control self.control_id = wrapper.control_id self._wrappers.append(wrapper)
def _get_max_latency_ts(self): #print("DEBUG -- skip max_latency_ts ", __file__) from timechart import raise_to_debug raise_to_debug(1, __file__) if self.max_latency > 0: indices = np.nonzero((self.end_ts - self.start_ts) > self.max_latency)[0] return np.array(sorted([self.start_ts[i] for i in indices])) return []
def _get_max_latency(self): #print("DEBUG -- skip max_latency --- ", __file__) from timechart import raise_to_debug raise_to_debug(1, __file__) if self.pid==0 and self.comm.startswith("irq"): return 1000 else: return 10
def normal_left_down(self, event): """ Handles the left mouse button being pressed when the tool is in the 'normal' state. Starts panning if the left mouse button is the drag button. """ from timechart import raise_to_debug raise_to_debug(1, __file__) if self.drag_button == "left": self._start_pan(event) return
def load_ftrace(fn): from model import tcProject proj = tcProject() proj.filename = fn proj.start_parsing(get_partial_text) parse_ftrace(fn, proj.handle_trace_event) proj.finish_parsing() from timechart import raise_to_debug raise_to_debug(0, 'Finish parsing ' + __file__) return proj
def set_pointer(self, pointer): "Set the current pointer (i.e. cursor) shape" #print("ASSERT", __file__) assert True, 'set_pointer' from timechart import raise_to_debug raise_to_debug(1, __file__) ptr = POINTER_MAP[pointer] if type(ptr) is int: POINTER_MAP[pointer] = ptr = wx.StockCursor(ptr) # self.control.SetCursor( ptr ) self.control.SetCursor(wx.Cursor(wx.CURSOR_HAND)) return
def _paint(self, event=None): """ This method is called directly by the UI toolkit's callback mechanism on the paint event. """ from timechart import raise_to_debug raise_to_debug(2, __file__) if self.control is None: # the window has gone away, but let the window implementation # handle the event as needed self._window_paint(event) return # Create a new GC if necessary size = self._get_control_size() if (self._size != tuple(size)) or (self._gc is None): self._size = tuple(size) self._gc = self._create_gc(size) # Always give the GC a chance to initialize self._init_gc() # Layout components and draw if hasattr(self.component, "do_layout"): self.component.do_layout() gc = self._gc self.component.draw(gc, view_bounds=(0, 0, size[0], size[1])) # damaged_regions = draw_result['damaged_regions'] # FIXME: consolidate damaged regions if necessary if not self.use_damaged_region: self._update_region = None # Perform a paint of the GC to the window (only necessary on backends # that render to an off-screen buffer) self._window_paint(event) self._update_region = [] return
class Window(BaseWindow): from timechart import raise_to_debug raise_to_debug(3, __file__) def _create_gc(self, size, pix_format="bgra32"): "Create a Kiva graphics context of a specified size" # We have to set bottom_up=0 or otherwise the PixelMap will # appear upside down when blitting. Note that this is not the # case on Windows. bottom_up = 0 if sys.platform != 'win32' else 1 gc = GraphicsContext((size[0] + 1, size[1] + 1), pix_format=pix_format, bottom_up=bottom_up) gc.translate_ctm(0.5, 0.5) return gc def _window_paint(self, event): "Do a GUI toolkit specific screen update" if self.control is None: event.Skip() return control = self.control pixel_map = self._gc.pixel_map wdc = control._dc = wx.PaintDC(control) if hasattr(pixel_map, 'draw_to_wxwindow'): pixel_map.draw_to_wxwindow(control, 0, 0) else: # This should just be the Mac OS X code path bmp = _wx_bitmap_from_buffer(pixel_map.convert_to_argb32string(), self._gc.width(), self._gc.height()) wdc.DrawBitmap(bmp, 0, 0) control._dc = None return
class ActionController(HasTraits): """ The default action controller for menus, menu bars and tool bars. """ # ------------------------------------------------------------------------ # 'ActionController' interface. # ------------------------------------------------------------------------ from timechart import raise_to_debug raise_to_debug(3, __file__) def perform(self, action, event): """ Control an action invocation. Parameters ---------- action : Action instance The action to perform. event : ActionEvent instance The event that triggered the action. Returns ------- result : any The result of the action's perform method (usually None). """ return action.perform(event) def can_add_to_menu(self, action): """ Can add an action to a menu Parameters ---------- action : Action instance The action to consider. Returns ------- can_add : bool ``True`` if the action can be added to a menu/menubar. """ return True def add_to_menu(self, action): """ Called when an action is added to the a menu/menubar. Parameters ---------- action : Action instance The action added to the menu. """ pass def can_add_to_toolbar(self, action): """ Returns True if the action can be added to a toolbar. Parameters ---------- action : Action instance The action to consider. Returns ------- can_add : bool ``True`` if the action can be added to a toolbar. """ return True def add_to_toolbar(self, action): """ Called when an action is added to the a toolbar. Parameters ---------- action : Action instance The action added to the toolbar. """ pass
def _draw ( self, gc, view_bounds, mode): from timechart import raise_to_debug raise_to_debug(3, __file__) # Set up all the control variables for quick access: ml = self.margin_left mr = self.margin_right mt = self.margin_top mb = self.margin_bottom pl = self.padding_left pr = self.padding_right pt = self.padding_top pb = self.padding_bottom bs = self.border_size bsd = bs + bs bsh = bs / 2.0 x, y, dx, dy = self.bounds ix, iy, idx, idy, tx, ty, tdx, tdy, itdx, itdy = self._info # Fill the background region (if required); bg_color = self.bg_color_ if bg_color is not transparent_color: with gc: gc.set_fill_color( bg_color ) gc.begin_path() gc.rect( x + ml + bs, y + mb + bs, dx - ml - mr - bsd, dy - mb - mt - bsd ) gc.fill_path() # Draw the border (if required): if bs > 0: border_color = self.border_color_ if border_color is not transparent_color: with gc: gc.set_stroke_color( border_color ) gc.set_line_width( bs ) gc.begin_path() gc.rect( x + ml + bsh, y + mb + bsh, dx - ml - mr - bs, dy - mb - mt - bs ) gc.stroke_path() # Calculate the origin of the image/text box: text_position = self.text_position_ if self.image_orientation == 'text': # Handle the 'image relative to text' case: if text_position & RIGHT: itx = x + dx - mr - bs - pr - itdx else: itx = x + ml + bs + pl if text_position & HCENTER: itx += (dx - ml - mr - bsd - pl - pr - itdx) / 2.0 if text_position & TOP: ity = y + dy - mt - bs - pt - itdy else: ity = y + mb + bs + pb if text_position & VCENTER: ity += (dy - mb - mt - bsd - pb - pt - itdy) / 2.0 else: # Handle the 'image relative to component' case: itx = ity = 0.0 if text_position & RIGHT: tx = x + dx - mr - bs - pr - tdx else: tx = x + ml + bs + pl if text_position & HCENTER: tx += (dx - ml - mr - bsd - pl - pr - tdx) / 2.0 if text_position & TOP: ty = y + dy - mt - bs - pt - tdy else: ty = y + mb + bs + pb if text_position & VCENTER: ty += (dy - mb - mt - bsd - pb - pt - tdy) / 2.0 image_position = self.image_position_ if image_position & RIGHT: ix = x + dx - mr - bs - pr - idx else: ix = x + ml + bs + pl if image_position & HCENTER: ix += (dx - ml - mr - bsd - pl - pr - idx) / 2.0 if image_position & TOP: iy = y + dy - mt - bs - pt - idy else: iy = y + mb + bs + pb if image_position & VCENTER: iy += (dy - mb - mt - bsd - pb - pt - idy) / 2.0 with gc: # Draw the image (if required): image = self._image if image is not None: gc.draw_image( image, ( itx + ix, ity + iy, idx, idy ) ) # Draw the text (if required): gc.set_font( self.font ) _text = self._text _tdx = self._tdx tx += itx ty += ity + tdy * len( _text ) style = self.style_ shadow_color = self.shadow_color_ text_color = self.color_ for i, text in enumerate( _text ): ty -= tdy _tx = tx if text_position & RIGHT: _tx += tdx - _tdx[i] elif text_position & HCENTER: _tx += (tdx - _tdx[i]) / 2.0 # Draw the 'shadow' text, if requested: if (style != 0) and (shadow_color is not transparent_color): if style == EMBOSSED: gc.set_fill_color( shadow_color ) gc.set_text_position( _tx - 1.0, ty + 1.0 ) elif style == ENGRAVED: gc.set_fill_color( shadow_color ) gc.set_text_position( _tx + 1.0, ty - 1.0 ) else: gc.set_fill_color( shadow_color ) gc.set_text_position( _tx + 2.0, ty - 2.0 ) gc.show_text( text ) # Draw the normal text: gc.set_fill_color( text_color ) gc.set_text_position( _tx, ty ) gc.show_text( text )
class GUIApplication(Application): from timechart import raise_to_debug raise_to_debug(3, __file__) """ A basic Pyface GUI application. """ # 'GUIApplication' traits ------------------------------------------------- # Branding --------------------------------------------------------------- #: The splash screen for the application. No splash screen by default splash_screen = Instance(ISplashScreen) #: The about dialog for the application. about_dialog = Instance(IDialog) #: Icon for the application (used in window titlebars) icon = Image #: Logo of the application (used in splash screens and about dialogs) logo = Image # Window management ------------------------------------------------------ #: The window factory to use when creating a window for the application. window_factory = Callable(default_window_factory) #: Default window size window_size = Tuple((800, 600)) #: Currently active Window if any active_window = Instance(IWindow) #: List of all open windows in the application windows = List(Instance(IWindow)) #: The Pyface GUI instance for the application gui = ReadOnly # Protected interface ---------------------------------------------------- #: Flag if the exiting of the application was explicitely requested by user # An 'explicit' exit is when the 'exit' method is called. # An 'implicit' exit is when the user closes the last open window. _explicit_exit = Bool(False) # ------------------------------------------------------------------------- # 'GUIApplication' interface # ------------------------------------------------------------------------- # Window lifecycle methods ----------------------------------------------- def create_window(self, **kwargs): """ Create a new application window. By default uses the :py:attr:`window_factory` to do this. Subclasses can override if they want to do something different or additional. Parameters ---------- **kwargs : dict Additional keyword arguments to pass to the window factory. Returns ------- window : IWindow instance or None The new IWindow instance. """ window = self.window_factory(application=self, **kwargs) if window.size == (-1, -1): window.size = self.window_size if not window.title: window.title = self.name if self.icon: window.icon = self.icon return window def add_window(self, window): """ Add a new window to the windows we are tracking. """ # Keep a handle on all windows created so that non-active windows don't # get garbage collected self.windows.append(window) # Something might try to veto the opening of the window. opened = window.open() if opened: window.activate() # Action handlers -------------------------------------------------------- def do_about(self): """ Display the about dialog, if it exists. """ if self.about_dialog is not None: self.about_dialog.open() # ------------------------------------------------------------------------- # 'Application' interface # ------------------------------------------------------------------------- def start(self): """ Start the application, setting up things that are required Subclasses should open at least one ApplicationWindow or subclass in their start method, and should call the superclass start() method before doing any work themselves. """ from pyface.gui import GUI ok = super(GUIApplication, self).start() if ok: # create the GUI so that the splash screen comes up first thing if self.gui is Undefined: self.gui = GUI(splash_screen=self.splash_screen) # create the initial windows to show self._create_windows() return ok # ------------------------------------------------------------------------- # 'GUIApplication' Private interface # ------------------------------------------------------------------------- def _create_windows(self): """ Create the initial windows to display. By default calls :py:meth:`create_window` once. Subclasses can override this method. """ window = self.create_window() self.add_window(window) # ------------------------------------------------------------------------- # 'Application' private interface # ------------------------------------------------------------------------- def _run(self): """ Actual implementation of running the application: starting the GUI event loop. """ # Fire a notification that the app is running. This is guaranteed to # happen after all initialization has occurred and the event loop has # started. A listener for this event is a good place to do things # where you want the event loop running. self.gui.invoke_later(self._fire_application_event, "application_initialized") # start the GUI - script blocks here self.gui.start_event_loop() return True # Destruction methods ----------------------------------------------------- def _can_exit(self): """ Check with each window to see if it can be closed The fires closing events for each window, and returns False if any listener vetos. """ if not super(GUIApplication, self)._can_exit(): return False for window in reversed(self.windows): window.closing = event = Vetoable() if event.veto: return False else: return True def _prepare_exit(self): """ Close each window """ # ensure copy of list, as we modify original list while closing for window in list(reversed(self.windows)): window.destroy() window.closed = window def _exit(self): """ Shut down the event loop """ self.gui.stop_event_loop() # Trait default handlers ------------------------------------------------ def _window_factory_default(self): """ Default to ApplicationWindow This is almost never the right thing, but allows users to get off the ground with the base class. """ from pyface.application_window import ApplicationWindow return lambda application, **kwargs: ApplicationWindow(**kwargs) def _splash_screen_default(self): """ Default SplashScreen """ from pyface.splash_screen import SplashScreen dialog = SplashScreen() if self.logo: dialog.image = self.logo return dialog def _about_dialog_default(self): """ Default AboutDialog """ from html import escape from pyface.about_dialog import AboutDialog additions = [ "<h1>{}</h1>".format(escape(self.name)), "Copyright © 2018 {}, all rights reserved".format( escape(self.company)), "", ] additions += [escape(line) for line in self.description.split("\n\n")] dialog = AboutDialog(title="About {}".format(self.name), additions=additions) if self.logo: dialog.image = self.logo return dialog # Trait listeners -------------------------------------------------------- @on_trait_change("windows:activated") def _on_activate_window(self, window, trait, old, new): """ Listener that tracks currently active window. """ if window in self.windows: self.active_window = window @on_trait_change("windows:deactivated") def _on_deactivate_window(self, window, trait, old, new): """ Listener that tracks currently active window. """ self.active_window = None @on_trait_change("windows:closed") def _on_window_closed(self, window, trait, old, new): """ Listener that ensures window handles are released when closed. """ if window in self.windows: self.windows.remove(window)
# the conditions described in the aforementioned license. The license # is also available online at http://www.enthought.com/licenses/BSD.txt # # Thanks for using Enthought open source! """ Defines the TraitType class. The ``TraitType`` class a trait handler that is the base class for modern traits, and provides a richer API than the old-style traits derived from ``TraitHandler``. """ import warnings from timechart import raise_to_debug raise_to_debug(3, __file__) from .base_trait_handler import BaseTraitHandler from .constants import ComparisonMode, DefaultValue, TraitKind from .trait_base import Missing, Self, TraitsCache, Undefined, class_of from .trait_dict_object import TraitDictObject from .trait_errors import TraitError from .trait_list_object import TraitListObject from .trait_set_object import TraitSetObject #: Mapping from trait metadata 'type' to CTrait 'type': trait_types = {"python": 1, "event": 2} def _infer_default_value_type(default_value):