def _timer_pop(self): """ Handles the timer popping. """ ui = self.ui control = ui.control if control is None: # Looks like someone forgot to tell us that the ui has been closed: self.stop() return # Make sure that the initial distance of the mouse pointer to the # control has been set: mx, my = toolkit().mouse_position() if self.mouse is None: self.mouse = (mx, my) self.distance = self._distance(mx, my) if self.is_activated: # Don't close the popup if any mouse buttons are currently pressed: if len(toolkit().mouse_buttons()) > 0: return # Check for the special case of the mouse pointer having to be # within the original bounds of the object the popup was created # for: if self.is_info: parent = control._parent if isinstance(parent, tuple): px, py, pdx, pdy = parent else: px, py = parent.screen_position pdx, pdy = parent.size if (mx < px) or (mx >= (px + pdx)) or (my < py) or (my >= (py + pdy)): do_later(ui.owner.close_popup) self.is_activated = False else: # Allow for a 'dead zone' border around the window to allow for # small motor control problems: if self._distance(mx, my) > self.border: control_at = toolkit().control_at(mx, my) while control_at is not None: if control_at is control: return control_at = control_at.parent do_later(ui.owner.close_popup) self.is_activated = False else: distance = self._distance(mx, my) if distance == 0: # If the pointer is now in the popup view, activate it: self.is_activated = True elif distance > (self.distance + 25): # If the mouse has moved too far away from the popup view, then # close it: do_later(ui.owner.close_popup)
def dispose(self): """ Disposes of the contents of a user interface. """ # Save the user preference information for the user interface: toolkit().save_window(self) # Finish disposing of the user interface: self.finish()
def dispose ( self ): """ Disposes of the contents of a user interface. """ # Save the user preference information for the user interface: toolkit().save_window( self ) # Finish disposing of the user interface: self.finish()
def prepare_ui ( self ): """ Performs all processing that occurs after the user interface is created. """ # Invoke all of the editor 'name_defined' methods we've accumulated: info = self.info for method in self._defined: method( info ) # Then reset the list, since we don't need it anymore: del self._defined[:] # Hook all events if the handler is an extended 'ViewHandler': handler = self.handler if isinstance( handler, ViewHandler ): toolkit().hook_events( self, self.control ) # Invoke the handler's 'init' method, and abort if it indicates failure: if handler.init( info ) == False: raise TraitError, 'User interface creation aborted' # For each Handler method whose name is of the form # 'object_name_changed', where 'object' is the name of an object in the # UI's 'context', create a trait notification handler that will call # the method whenever 'object's 'name' trait changes. Also invoke the # method immediately so initial user interface state can be correctly # set: context = self.context for name in self._each_trait_method( handler ): if name[-8:] == '_changed': prefix = name[:-8] col = prefix.find( '_', 1 ) if col >= 0: object = context.get( prefix[ : col ] ) if object is not None: method = getattr( handler, name ) trait_name = prefix[ col + 1: ] self._dispatchers.append( Dispatcher( method, info, object, trait_name ) ) if object.base_trait( trait_name ).type != 'event': method( info ) # If there are any Editor object's whose 'visible', 'enabled' or # 'checked' state is controlled by a 'visible_when', 'enabled_when' or # 'checked_when' expression, set up an 'anytrait' changed notification # handler on each object in the 'context' that will cause the 'visible', # 'enabled' or 'checked' state of each affected Editor to be set. Also # trigger the evaluation immediately, so the visible, enabled or checked # state of each Editor can be correctly initialized: if (len( self._visible ) + len( self._enabled ) + len( self._checked )) > 0: for object in context.values(): object.on_trait_change( self._evaluate_when, dispatch = 'ui' ) self._evaluate_when() # Indicate that the user interface has been initialized: info.initialized = True
def dispose ( self, result = None, abort = False ): """ Disposes of the contents of a user interface. """ # Save the user preference information for the user interface: if not abort: toolkit().save_window( self ) # Finish disposing of the user interface: self.finish( result )
def TextEditor ( *args, **traits ): """ Allows the user to modify a text string. The string value entered by the user is coerced to the appropriate type for the trait attribute being modified. """ return toolkit().text_editor( *args, **traits )
def ListEditor(*args, **facets): """ Allows the user to modify a list of values. The user can add, delete, or reorder items, or change the content of items. """ return toolkit().list_editor(*args, **facets)
def init(self, parent): """ Finishes initializing the editor by creating the underlying toolkit widget. """ if (self.item.resizable is True) or (self.item.height != -1.0): self.adapter = ( toolkit() .create_text_input(parent, read_only=True, multi_line=True) .set(value=self.str_value, background_color=WindowColor) ) else: self.adapter = toolkit().create_label(parent).set(value=self.str_value) # fixme: How to do this in GUI toolkit neutral manner?... ###self.layout_style = 0 self.set_tooltip()
def CheckListEditor ( *args, **traits ): """ Allows the user to select zero, one, or more values from a finite set of possibilities. Note that the "simple" style is limited to selecting a single value. """ return toolkit().check_list_editor( *args, **traits )
def set_error_state ( self, state = None, control = None ): """ Sets the editor's current error state. """ from facets.ui.colors import OKColor, ErrorColor state = self.get_error_state( state ) if control is None: control = self.get_error_control() controls = control if not isinstance( control, list ): controls = [ control ] for control in controls: # fixme: Eventually this code should not be necessary... control = toolkit().as_toolkit_adapter( control ) ui_control = control() if state: color = ErrorColor if getattr( ui_control, '_ok_color', None ) is None: ui_control._ok_color = control.background_color else: color = getattr( ui_control, '_ok_color', None ) if color is None: color = OKColor if control.is_panel: color = WindowColor control.background_color = color control.refresh()
def CodeEditor(*args, **traits): """ Allows the user to edit a multi-line string. The "simple" and "custom" styles of this editor display multiple lines of the string, with line numbers. """ return toolkit().code_editor(*args, **traits)
def CodeEditor(*args, **facets): """ Allows the user to edit a multi-line string. The "simple" and "custom" styles of this editor display multiple lines of the string, with line numbers. """ return toolkit().code_editor(*args, **facets)
def CheckListEditor(*args, **traits): """ Allows the user to select zero, one, or more values from a finite set of possibilities. Note that the "simple" style is limited to selecting a single value. """ return toolkit().check_list_editor(*args, **traits)
def TextEditor(*args, **traits): """ Allows the user to modify a text string. The string value entered by the user is coerced to the appropriate type for the trait attribute being modified. """ return toolkit().text_editor(*args, **traits)
def ListEditor(*args, **traits): """ Allows the user to modify a list of values. The user can add, delete, or reorder items, or change the content of items. """ return toolkit().list_editor(*args, **traits)
def _control_set ( self, control ): """ Handles the 'control' facet being changed. """ if control is None: self.adapter = None elif (self.adapter is None) or (self.adapter() is not control): self.adapter = toolkit().adapter_for( control ) control._editor = self
def _label_control_set ( self, control ): """ Handles the 'label_control' facet being changed. """ if control is None: self.label_adapter = None elif ((self.label_adapter is None) or (self.label_adapter() is not control)): self.label_adapter = toolkit().control_adapter_for( control )
def ui ( self, parent, kind ): """ Creates a user interface from the associated View template object. """ if (parent is None) and (kind in kind_must_have_parent): kind = 'live' self.rebuild = getattr( toolkit(), 'ui_' + kind ) self.rebuild( self, parent ) self.view.on_trait_change( self._updated_changed, 'updated', dispatch = 'ui' )
def ui ( self, parent ): """ Creates a user interface from the associated View template object. """ if (parent is None) and (self.kind in kind_must_have_parent): self.kind = 'live' self.view.on_facet_set( self._updated_set, 'updated', dispatch = 'ui' ) self.rebuild = getattr( self, '_create_' + self.kind ) self.rebuild( self, toolkit().as_toolkit_adapter( parent ) )
def facets_init(self): """ Completes the initialization of the object. """ kind = self.ui.view.kind self.is_activated = self.is_info = kind == "info" if kind == "popup": self.border = 10 self.timer = toolkit().create_timer(100, self._timer_pop)
def EnumEditor(*args, **facets): """ Allows the user to select a single value from an enumerated list of values. """ # from facets.ui.editors.enum_editor import EnumEditor # # return EnumEditor( *args, **facets ) return toolkit().enum_editor(*args, **facets)
def init(self, parent): """ Finishes initializing the editor by creating the underlying toolkit widget. """ self.adapter = control = toolkit().create_text_input(parent) control.value = self.str_value control.set_event_handler(lose_focus=self.update_object, text_enter=self.update_object) self.set_tooltip()
def init ( self, parent ): """ Finishes initializing the editor by creating the underlying toolkit widget. """ factory = self.factory self.editor_control = control = factory.klass() control.factory = factory control.editor = self control.init() scroller = None if control.virtual_size != UndefinedSize: scroller = parent = toolkit().create_scrolled_panel( parent ) control.parent = parent control.theme = factory.theme ui_control = control() fixed_size = control.fixed_size self.scrollable = (fixed_size == UndefinedSize) if factory.refresh != '': control.on_facet_set( control.refresh, factory.refresh ) if scroller is not None: control.scroll_control = scroller scroller.content = ui_control ui_control.virtual_size = control.virtual_size ui_control = scroller if fixed_size != UndefinedSize: ui_control.min_size = fixed_size self.adapter = ui_control if self.extended_name != 'None': object = self.context_object name = self.extended_name if not hasattr( control, 'value' ): if isinstance( object, BaseEditor ) and (name == 'value'): # FIXME: Handle the special case of an Editor's 'value' # facet, which is a property, which doesn't work well as is, # so we reach down into the editor to extract the actual # object facet being referenced: name = object.name object = object.object control.add_facet( 'value', object.facet( name ) ) object.sync_facet( name, control, 'value', True ) self.set_tooltip() control.post_init()
def position ( self, info ): """ Positions a dialog-based user interface on the display. Parameters ---------- info : UIInfo object The UIInfo object associated with the window Returns ------- Nothing. Description ----------- This method is called after the user interface is initialized (by calling init()), but before the user interface is displayed. Override this method to position the window on the display device. The default implementation calls the position() method of the current toolkit. Usually, you do not need to override this method, because you can control the window's placement using the **x** and **y** attributes of the View object. """ toolkit().position( info.ui )
def CompoundEditor(*args, **traits): """ Allows the user to select a value based on a compound trait. Because a compound trait is composed of multiple trait definitions, this editor factory displays trait editors for each of the constituent traits. For example, consider the following trait attribute, defined as a compound that accepts integer values in the range of 1 to 6, or text strings corresponding to those integers:: compound = Trait(1, Range(1, 6), 'one', 'two', 'three', 'four', 'five', 'six') The editor displayed for this trait attribute combines editors for integer ranges and for enumerations. """ return toolkit().compound_editor(*args, **traits)
def CompoundEditor ( *args, **traits ): """ Allows the user to select a value based on a compound trait. Because a compound trait is composed of multiple trait definitions, this editor factory displays trait editors for each of the constituent traits. For example, consider the following trait attribute, defined as a compound that accepts integer values in the range of 1 to 6, or text strings corresponding to those integers:: compound = Trait(1, Range(1, 6), 'one', 'two', 'three', 'four', 'five', 'six') The editor displayed for this trait attribute combines editors for integer ranges and for enumerations. """ return toolkit().compound_editor( *args, **traits )
def __getstate__(self): ftc = toolkit().from_toolkit_color return { "image": str(self.image), "tiled": self.tiled, "origin": self.origin, "border": eval(str(self.border)), "content": eval(str(self.content)), "label": eval(str(self.label)), "alignment": self.alignment, "content_font": str(self.content_font), "label_font": str(self.label_font), "content_color": ftc(self.content_color), "label_color": ftc(self.label_color), "bg_color": ftc(self.bg_color), }
def __str__(self): """ Returns the string respresentation of the theme. """ ftc = toolkit().from_toolkit_color return ThemeTemplate % ( self.image, self.tiled, self.origin, self.border, self.content, self.label, self.alignment, self.content_font, self.label_font, ftc(self.content_color), ftc(self.label_color), ftc(self.bg_color), )
def TreeEditor(*args, **facets): """ Allows the user to modify a tree data structure. """ return toolkit().tree_editor(*args, **facets)
def KivaFontTrait(*args, **traits): return toolkit().kiva_font_trait(*args, **traits)
def ListStrEditor(*args, **facets): """ Allows the user to modify a list of strings (or values that can be mapped to strings). """ return toolkit().list_str_editor(*args, **facets)
def init(self, ui, parent, style): self.is_modal = style == MODAL self.rc = None window_style = set() view = ui.view title = view.title if title == "": title = DefaultTitle history = ui.history window = ui.control if window is not None: if history is not None: history.on_facet_set(self._on_undoable, "undoable", remove=True) history.on_facet_set(self._on_redoable, "redoable", remove=True) history.on_facet_set(self._on_revertable, "undoable", remove=True) window.layout = None ui.reset() else: self.ui = ui if style == MODAL: if view.resizable: window_style.add("resizable") window_style.add("min_max") window_style.add("dialog") window = toolkit().create_frame(parent, window_style, title) elif style == NONMODAL: if parent is not None: window_style.add("non_modal_child") window_style.add("frame") window = toolkit().create_frame(parent, window_style, title) else: if len(window_style) == 0: window_style.add("simple") # Only use the parent if it is part of a modal window; otherwise # just make it a top-level window: while parent is not None: if parent.modal: break parent = parent.parent window = toolkit().create_frame(parent, window_style) window.set_event_handler(deactivate=self._on_close_popup) window.kind = ui.kind self._monitor = None if style != POPOUT: self._monitor = MouseMonitor(ui=ui) # Set the correct default window background color: window.background_color = WindowColor self.control = window window.set_event_handler(close=self._on_close_page, key=self._on_key) self.set_icon(view.icon) buttons = self.get_buttons(view) nbuttons = len(buttons) no_buttons = (nbuttons == 1) and self.is_button(buttons[0], "") has_buttons = (not no_buttons) and (nbuttons > 0) if has_buttons or ((view.menubar is not None) and (not view.undo)): if history is None: history = UndoHistory() else: history = None ui.history = history # Create the actual facets UI panel: sw = panel(ui, window) # Attempt an optimization that prevents creating nested vertical box # layouts: if isinstance(sw, Layout) and sw.is_vertical: sw_layout = sw else: sw_layout = toolkit().create_box_layout() sw_layout.add(sw, stretch=1) # Check to see if we need to add any of the special function buttons: if (not no_buttons) and has_buttons: sw_layout.add(toolkit().create_separator(window, False)) b_layout = toolkit().create_box_layout(False, align="right") # Create a button for each button action: for button in buttons: button = self.coerce_button(button) if self.is_button(button, "Undo"): self.undo = self.add_button(button, b_layout, self._on_undo, False) self.redo = self.add_button(button, b_layout, self._on_redo, False, "Redo") history.on_facet_set(self._on_undoable, "undoable", dispatch="ui") history.on_facet_set(self._on_redoable, "redoable", dispatch="ui") if history.can_undo: self._on_undoable(True) if history.can_redo: self._on_redoable(True) elif self.is_button(button, "Revert"): self.revert = self.add_button(button, b_layout, self._on_revert, False) history.on_facet_set(self._on_revertable, "undoable", dispatch="ui") if history.can_undo: self._on_revertable(True) elif self.is_button(button, "OK"): self.ok = self.add_button(button, b_layout, self._on_ok_button) ui.on_facet_set(self._on_error, "errors", dispatch="ui") elif self.is_button(button, "Cancel"): self.add_button(button, b_layout, self._on_cancel_button) elif self.is_button(button, "Help"): self.add_button(button, b_layout, self._on_help) elif not self.is_button(button, ""): self.add_button(button, b_layout) sw_layout.add(b_layout, fill=False, align="right", left=5, right=5, top=5, bottom=5) # Set the layout for the window: window.layout = sw_layout # Add the menu bar, tool bar and status bar (if any): self.add_menubar() self.add_toolbar() self.add_statusbar() # Lay out all of the dialog contents: window.shrink_wrap()
def BooleanEditor(*args, **traits): return toolkit().boolean_editor(*args, **traits)
def init ( self, ui, parent, is_modal ): self.is_modal = is_modal style = set() view = ui.view if view.resizable: style.add( 'resizable' ) title = view.title if title == '': title = DefaultTitle revert = apply = False window = ui.control if window is not None: window.layout = None ui.reset() if hasattr( self, 'revert' ): revert = self.revert.enabled if hasattr( self, 'apply' ): apply = self.apply.enabled else: self.ui = ui if is_modal: style.add( 'dialog' ) window = toolkit().create_frame( parent, style, title ) else: style.add( 'frame' ) window = toolkit().create_frame( parent, style, title ) window.background_color = WindowColor self.control = window self.set_icon( view.icon ) window.set_event_handler( close = self._on_close_page, key = self._on_key ) # Create the 'context' copies we will need while editing: context = ui.context ui._context = context ui.context = self._copy_context( context ) ui._revert = self._copy_context( context ) # Create the actual facet sheet panel: sw_layout = toolkit().create_box_layout() sw_layout.add( panel( ui, window ), stretch = 1 ) buttons = self.get_buttons( view ) nbuttons = len( buttons ) if (nbuttons != 1) or (not self.is_button( buttons[0], '' )): # Create the necessary special function buttons: sw_layout.add( toolkit().create_separator( window, False ) ) b_layout = toolkit().create_box_layout( False, align = 'right' ) for button in buttons: if self.is_button( button, 'Apply' ): self.apply = self.add_button( button, b_layout, self._on_apply, apply ) ui.on_facet_set( self._on_applyable, 'modified', dispatch = 'ui' ) elif self.is_button( button, 'Revert' ): self.revert = self.add_button( button, b_layout, self._on_revert, revert ) elif self.is_button( button, 'OK' ): self.ok = self.add_button( button, b_layout, self._on_ok ) ui.on_facet_set( self._on_error, 'errors', dispatch = 'ui' ) elif self.is_button( button, 'Cancel' ): self.add_button( button, b_layout, self._on_cancel ) elif self.is_button( button, 'Help' ): self.add_button( button, b_layout, self._on_help ) elif not self.is_button( button, '' ): self.add_button( button, b_layout ) sw_layout.add( b_layout, align = 'right', left = 5, right = 5, top = 5, bottom = 5 ) # Add the menu bar, tool bar and status bar (if any): self.add_menubar() self.add_toolbar() self.add_statusbar() # Lay all of the dialog contents out: window.layout = sw_layout window.shrink_wrap()
def ui ( self, parent, kind ): """ Creates a user interface from the associated View template object. """ getattr( toolkit(), 'ui_' + kind )( self, parent )
def FileEditor(*args, **traits): return toolkit().file_editor(*args, **traits)
def ButtonEditor(*args, **traits): return toolkit().button_editor(*args, **traits)
def EnableRGBAColorEditor(*args, **traits): return toolkit().enable_rgba_color_editor(*args, **traits)
def EnumEditor(*args, **traits): return toolkit().enum_editor(*args, **traits)
def DirectoryEditor(*args, **traits): return toolkit().directory_editor(*args, **traits)
def CompoundEditor(*args, **traits): return toolkit().compound_editor(*args, **traits)
def RGBAColorTrait(*args, **traits): return toolkit().rgba_color_trait(*args, **traits)
def TextEditor(*args, **traits): return toolkit().text_editor(*args, **traits)
#------------------------------------------------------------------------------- # License: See section (A) of the .../facets/LICENSE.txt file. #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # Imports: #------------------------------------------------------------------------------- from toolkit \ import toolkit #------------------------------------------------------------------------------- # Common Colors: #------------------------------------------------------------------------------- _constants = toolkit().constants() # Standard window frame background color: WindowColor = _constants.get( 'WindowColor', 0xFFFFFF ) # Standard color of valid input: OKColor = ( 255, 255, 255 ) # Standard color used to highlight input errors: ErrorColor = ( 255, 192, 192 ) # Standard color for the background of a read only control: ReadonlyColor = ( 244, 243, 238 ) # Standard color for marking a control as 'droppable': DropColor = ( 215, 242, 255 )
def default_show_help(info, control): """ Default handler for showing the help associated with a view. """ toolkit().show_help(info.ui, control)
def RGBAColorEditor(*args, **traits): return toolkit().rgba_color_editor(*args, **traits)
def FontEditor(*args, **traits): return toolkit().font_editor(*args, **traits)
def CheckListEditor(*args, **traits): return toolkit().check_list_editor(*args, **traits)
def _default_color_default(self): """ Returns the GUI toolkit specific value for the color black. """ return toolkit().to_toolkit_color((0, 0, 0))
def KivaFontEditor(*args, **traits): return toolkit().kiva_font_editor(*args, **traits)
def position(self, info): """ Positions a dialog-based user interface on the display. """ toolkit().position(info.ui)
def ImageEnumEditor(*args, **traits): return toolkit().image_enum_editor(*args, **traits)
def ui(self, parent, kind): """ Creates a user interface from the associated View template object. """ getattr(toolkit(), 'ui_' + kind)(self, parent)
def ColorTrait(*args, **traits): return toolkit().color_trait(*args, **traits)
def TupleEditor(*args, **traits): return toolkit().tuple_editor(*args, **traits)
def TreeEditor(*args, **traits): return toolkit().tree_editor(*args, **traits)
def CodeEditor(*args, **traits): return toolkit().code_editor(*args, **traits)
def FontTrait(*args, **traits): return toolkit().font_trait(*args, **traits)
def default_show_help ( info, control ): """ Default handler for showing the help associated with a view. """ toolkit().show_help( info.ui, control )
def ColorEditor(*args, **traits): return toolkit().color_editor(*args, **traits)