class SimpleAdapter(ReadOnlyTabularAdapter): columns = [('Name', 'name'), ('Value', 'value')] font = Font('12') SectionHeading_bg_color = Color(0xE0E0E0) SectionHeading_font = Font('14 bold') SectionHeading_name_text = Property Setting_name_text = Property name_width = Float(175) def get_text(self, object, name, row, column): # Not interested in Name column if 0 == column: return super(SimpleAdapter, self).get_text(object, name, row, column) settings_list = getattr(object, 'settings_list') setting = settings_list[row] digits = getattr(setting, 'digits', None) if digits: return '%.{}f'.format(digits) % float(setting.value) return super(SimpleAdapter, self).get_text(object, name, row, column) def _get_SectionHeading_name_text(self): return self.item.name.replace('_', ' ') def _get_Setting_name_text(self): return self.item.name.replace('_', ' ')
class SimpleChangeAdapter(ReadOnlyTabularAdapter): columns = [('Name', 'name'), ('Current Value', 'value'), ('Recommended Value', 'rec_value')] font = Font('12') SectionHeading_bg_color = Color(0xE0E0E0) SectionHeading_font = Font('14 bold') SectionHeading_name_text = Property Setting_name_text = Property name_width = Float(175) def _get_SectionHeading_name_text(self): return self.item.name.replace('_', ' ') def _get_Setting_name_text(self): return self.item.name.replace('_', ' ')
class SimpleAdapter(TabularAdapter): columns = [('Name', 'name'), ('Value', 'value')] font = Font('12') can_edit = Bool(False) SectionHeading_bg_color = Color(0xE0E0E0) SectionHeading_font = Font('14 bold') SectionHeading_name_text = Property Setting_name_text = Property name_width = Float(175) def _get_SectionHeading_name_text(self): return self.item.name.replace('_', ' ') def _get_Setting_name_text(self): return self.item.name.replace('_', ' ')
class ArrayAdapter(TabularAdapter): """A minimalist array adapter class for managing the display of data""" font = Font('Courier 10') alignment = 'right' format = '%.4f' index_text = Property data = Array width = 75.0 def __init__(self, df): self.data = df.data def _get_index_text(self): return "Row:" + str(self.row) def delete(self, datafile, trait, row): datafile.del_rows(row) def insert(self, object, trait, row, value): object.insert_rows(row, value) self.data = object.data def get_default_value(self, object, trait): return numpy.zeros(object.data.shape[1])
class HexEditPreferences(PreferencesHelper): """ The preferences helper for the Framework application. """ #### 'PreferencesHelper' interface ######################################## # The path to the preference node that contains the preferences. preferences_path = 'omnivore.task.hex_edit' #### Preferences ########################################################## map_width_low = 1 map_width_high = 256 map_width = Range(low=map_width_low, high=map_width_high, value=16) bitmap_width_low = 1 bitmap_width_high = 16 bitmap_width = Range(low=bitmap_width_low, high=bitmap_width_high, value=1) # Font used for hex/disassembly text_font = Font(def_font) hex_grid_lower_case = Bool(True) assembly_lower_case = Bool(False) disassembly_column_widths = Tuple(0, 0, 0)
class LogItemOutputListAdapter(TabularAdapter): """ Tabular adapter for table of LogItems """ columns = [('Host timestamp', 'timestamp'), ('Log level', 'log_level_str'), ('Message', 'msg')] font = Font('12') can_edit = Bool(False) timestamp_width = Float(0.21) log_level_width = Float(0.07) msg_width = Float(0.72) can_drop = Bool(False) def get_tooltip(self, obj, name, row, column): """ Define the tooltip messages for user mouse-over. Depends on column. Column is an integer with 0 index starting from left. No tooltip for the "message" column. Parameters omitted as the """ if column == 0: return TIMESTAMP_TOOLTIP if column == 1: return LOG_LEVEL_TOOLTIP else: return None
class ArrayAdapter(TabularAdapter): font = Font('Courier 10') alignment = 'right' format = '%.4f' index_text = Property data = Instance(S.DataFile) width = 75.0 def _get_index_text(self): return str(self.row)
class AppSearchTableAdapter(SearchTableAdapter): """ Augment the Standard adapter so that it bolds the first two entries. """ ########################################################################## # AppSeachTabularAdapter traits ########################################################################## # Font for bolding the "add new function" row in table # fixme: Make system dependent (lookup standard font and apply bold to it) bold_font = Font("Arial 9 Bold Italic") ########################################################################## # TabularAdapter interface ########################################################################## def get_font(self, object, trait, row): """ The Add new function item should appear in bold. "Add new function" is always in the first row, so we can simply make that row bold. """ if row <= 2: font = self.bold_font else: font = self.normal_font return font ### Private methods ###################################################### def _get_tooltip(self): """ Tooltip text is the firt line of the doc-string for the function. We specialize this so that it can return a different tooltip if one of our NEW_*_ENTRY is selected. Otherwise, hand off the work to our base class. """ if self.item is NEW_FUNCTION_ENTRY: short_description = "Create an editable user defined function." elif self.item is NEW_EXPR_ENTRY: short_description = "Create a block of expressions." elif self.item is NEW_LOOP_ENTRY: short_description = "Create a loop over a block of expressions." else: short_description = super(AppSearchTableAdapter, self)._get_tooltip() return short_description
class FileInfoAdapter(TabularAdapter): columns = [('File Name', 'name'), ('Size', 'size'), ('', 'big'), ('Time', 'time'), ('Date', 'date')] even_bg_color = (201, 223, 241) # FIXME: Font fails with wx in OSX; see traitsui issue #13: font = Font('Courier 10') size_alignment = Str('right') time_alignment = Str('right') date_alignment = Str('right') big_text = Str big_width = Float(18) big_image = Property def _get_big_image(self): size = self.item.size if size > 65536: return ImageResource('red_ball') return (None, ImageResource('blue_ball'))[size > 16384]
class AcmePreferencesPage(PreferencesPage): """ The preferences page for the Acme workbench. """ #### 'PreferencesPage' interface ########################################## # The page's category (e.g. 'General/Appearance'). The empty string means # that this is a top-level page. category = 'General' # The page's help identifier (optional). If a help Id *is* provided then # there will be a 'Help' button shown on the preference page. help_id = '' # The page name (this is what is shown in the preferences dialog. name = 'Acme' # The path to the preference node that contains the preferences. preferences_path = 'acme.workbench' #### Preferences ########################################################## # Width. width = Int(100) # Height. height = Int(200) # Ratio. ratio = Float(0.1) # Background color. bgcolor = Color('red') # Text font. font = Font('helvetica') #### Traits UI views ###################################################### trait_view = View('width', 'height', 'ratio', 'font', 'bgcolor')
class ArrayViewEditor(BasicEditorFactory): # The editor implementation class: klass = Property # Should an index column be displayed: show_index = Bool(True) # List of (optional) column titles: titles = List(Str) # Should the array be logically transposed: transpose = Bool(False) # The format used to display each array element: format = Str('%s') # The font to use for displaying each array element: font = Font('Courier 10') def _get_klass(self): """ The class used to construct editor objects. """ return toolkit_object('array_view_editor:_ArrayViewEditor')
class TabularAdapter(HasPrivateTraits): """ The base class for adapting list items to values that can be edited by a TabularEditor. """ #-- Public Trait Definitions ----------------------------------------------- # A list of columns that should appear in the table. Each entry can have one # of two forms: string or ( string, any ), where *string* is the UI name of # the column, and *any* is a value that identifies that column to the # adapter. Normally this value is either a trait name or an index, but it # can be any value that the adapter wants. If only *string* is specified, # then *any* is the index of the *string* within *columns*. columns = List() #Maps UI name of column to value identifying column to the adapter, if different. column_dict = Property() # Specifies the default value for a new row: default_value = Any('') # The default text color for table rows (even, odd, any rows): odd_text_color = Color(None, update=True) even_text_color = Color(None, update=True) default_text_color = Color(None, update=True) # The default background color for table rows (even, odd, any rows): odd_bg_color = Color(None, update=True) even_bg_color = Color(None, update=True) default_bg_color = Color(None, update=True) # Alignment to use for a specified column: alignment = Enum('left', 'center', 'right') # The Python format string to use for a specified column: format = Str('%s') # Width of a specified column: width = Float(-1) # Can the text value of each item be edited: can_edit = Bool(True) # The value to be dragged for a specified row item: drag = Property # Can any arbitrary value be dropped onto the tabular view: can_drop = Bool(False) # Specifies where a dropped item should be placed in the table relative to # the item it is dropped on: dropped = Enum('after', 'before') # The font for a row item: font = Font(None) # The text color for a row item: text_color = Property # The background color for a row item: bg_color = Property # The name of the default image to use for column items: image = Str(None, update=True) # The text of a row/column item: text = Property # The content of a row/column item (may be any Python value): content = Property # The tooltip information for a row/column item: tooltip = Str # The context menu for a row/column item: menu = Any # The context menu for column header: column_menu = Any # List of optional delegated adapters: adapters = List(ITabularAdapter, update=True) #-- Traits Set by the Editor ----------------------------------------------- # The object whose trait is being edited: object = Instance(HasTraits) # The name of the trait being edited: name = Str # The row index of the current item being adapted: row = Int # The column index of the current item being adapted: column = Int # The current column id being adapted (if any): column_id = Any # Current item being adapted: item = Any # The current value (if any): value = Any #-- Private Trait Definitions ---------------------------------------------- # Cache of attribute handlers: cache = Any({}) # Event fired when the cache is flushed: cache_flushed = Event(update=True) # The mapping from column indices to column identifiers (defined by the # *columns* trait): column_map = Property(depends_on='columns') # The mapping from column indices to column labels (defined by the *columns* # trait): label_map = Property(depends_on='columns') # The name of the trait on a row item containing the value to use # as a row label. If None, the label will be the empty string. row_label_name = Either(None, Str) # For each adapter, specifies the column indices the adapter handles: adapter_column_indices = Property(depends_on='adapters,columns') # For each adapter, specifies the mapping from column index to column id: adapter_column_map = Property(depends_on='adapters,columns') #### TabularAdapter interface #### def cleanup(self): """ Clean up the adapter to remove references to objects. """ self.trait_setq( object=None, item=None, value=None, ) #-- Adapter methods that are sensitive to item type ------------------------ def get_alignment(self, object, trait, column): """ Returns the alignment style to use for a specified column. """ return self._result_for('get_alignment', object, trait, 0, column) def get_width(self, object, trait, column): """ Returns the width to use for a specified column. """ return self._result_for('get_width', object, trait, 0, column) def get_can_edit(self, object, trait, row): """ Returns whether the user can edit a specified *object.trait[row]* item. A True result indicates the value can be edited, while a False result indicates that it cannot be edited. """ return self._result_for('get_can_edit', object, trait, row, 0) def get_drag(self, object, trait, row): """ Returns the 'drag' value for a specified *object.trait[row]* item. A result of *None* means that the item cannot be dragged. """ return self._result_for('get_drag', object, trait, row, 0) def get_can_drop(self, object, trait, row, value): """ Returns whether the specified *value* can be dropped on the specified *object.trait[row]* item. A value of **True** means the *value* can be dropped; and a value of **False** indicates that it cannot be dropped. """ return self._result_for('get_can_drop', object, trait, row, 0, value) def get_dropped(self, object, trait, row, value): """ Returns how to handle a specified *value* being dropped on a specified *object.trait[row]* item. The possible return values are: 'before' Insert the specified *value* before the dropped on item. 'after' Insert the specified *value* after the dropped on item. """ return self._result_for('get_dropped', object, trait, row, 0, value) def get_font(self, object, trait, row, column=0): """ Returns the font for a specified *object.trait[row]* item. A result of None means use the default font. """ return self._result_for('get_font', object, trait, row, column) def get_text_color(self, object, trait, row, column=0): """ Returns the text color for a specified *object.trait[row]* item. A result of None means use the default text color. """ return self._result_for('get_text_color', object, trait, row, column) def get_bg_color(self, object, trait, row, column=0): """ Returns the background color for a specified *object.trait[row]* item. A result of None means use the default background color. """ return self._result_for('get_bg_color', object, trait, row, column) def get_image(self, object, trait, row, column): """ Returns the name of the image to use for a specified *object.trait[row].column* item. A result of None means no image should be used. Otherwise, the result should either be the name of the image, or an ImageResource item specifying the image to use. """ return self._result_for('get_image', object, trait, row, column) def get_format(self, object, trait, row, column): """ Returns the Python format string to use for a specified column. """ return self._result_for('get_format', object, trait, row, column) def get_text(self, object, trait, row, column): """ Returns the text to display for a specified *object.trait[row].column* item. """ return self._result_for('get_text', object, trait, row, column) def get_content(self, object, trait, row, column): """ Returns the content to display for a specified *object.trait[row].column* item. """ return self._result_for('get_content', object, trait, row, column) def set_text(self, object, trait, row, column, text): """ Sets the text for a specified *object.trait[row].column* item to *text*. """ self._result_for('set_text', object, trait, row, column, text) def get_tooltip(self, object, trait, row, column): """ Returns the tooltip for a specified row. """ return self._result_for('get_tooltip', object, trait, row, column) def get_menu(self, object, trait, row, column): """ Returns the context menu for a specified cell. """ return self._result_for('get_menu', object, trait, row, column) def get_column_menu(self, object, trait, row, column): """ Returns the context menu for a specified column. """ return self._result_for('get_column_menu', object, trait, row, column) #-- Adapter methods that are not sensitive to item type -------------------- def get_item(self, object, trait, row): """ Returns the value of the *object.trait[row]* item. """ try: return getattr(object, trait)[row] except: return None def len(self, object, trait): """ Returns the number of items in the specified *object.trait* list. """ # Sometimes, during shutdown, the object has been set to None. if object is None: return 0 else: return len(getattr(object, trait)) def get_default_value(self, object, trait): """ Returns a new default value for the specified *object.trait* list. """ return self.default_value def delete(self, object, trait, row): """ Deletes the specified *object.trait[row]* item. """ del getattr(object, trait)[row] def insert(self, object, trait, row, value): """ Inserts a new value at the specified *object.trait[row]* index. """ getattr(object, trait)[row:row] = [value] def get_column(self, object, trait, index): """ Returns the column id corresponding to a specified column index. """ self.object, self.name = object, trait return self.column_map[index] #-- Property Implementations ----------------------------------------------- def _get_drag(self): return self.item def _get_text_color(self): if (self.row % 2) == 1: return self.even_text_color_ or self.default_text_color return self.odd_text_color or self.default_text_color_ def _get_bg_color(self): if (self.row % 2) == 1: return self.even_bg_color_ or self.default_bg_color_ return self.odd_bg_color or self.default_bg_color_ def _get_text(self): return self.get_format( self.object, self.name, self.row, self.column) % self.get_content( self.object, self.name, self.row, self.column) def _set_text(self, value): if isinstance(self.column_id, int): self.item[self.column_id] = self.value else: # Convert value to the correct trait type. try: trait_handler = self.item.trait(self.column_id).handler setattr(self.item, self.column_id, trait_handler.evaluate(self.value)) except: setattr(self.item, self.column_id, value) def _get_content(self): if isinstance(self.column_id, int): return self.item[self.column_id] return getattr(self.item, self.column_id) #-- Property Implementations ----------------------------------------------- @cached_property def _get_column_dict(self): cols = {} for i, value in enumerate(self.columns): if isinstance(value, basestring): cols.update({value: value}) else: cols.update({value[0]: value[1]}) return cols @cached_property def _get_column_map(self): map = [] for i, value in enumerate(self.columns): if isinstance(value, basestring): map.append(i) else: map.append(value[1]) return map def get_label(self, section, obj=None): """Override this method if labels will vary from object to object.""" return self.label_map[section] def get_row_label(self, section, obj=None): if self.row_label_name is None: return None rows = getattr(obj, self.name, None) if rows is None: return None item = rows[section] return getattr(item, self.row_label_name, None) @cached_property def _get_label_map(self): map = [] for i, value in enumerate(self.columns): if isinstance(value, basestring): map.append(value) else: map.append(value[0]) return map @cached_property def _get_adapter_column_indices(self): labels = self.label_map map = [] for adapter in self.adapters: indices = [] for label in adapter.columns: if not isinstance(label, basestring): label = label[0] indices.append(labels.index(label)) map.append(indices) return map @cached_property def _get_adapter_column_map(self): labels = self.label_map map = [] for adapter in self.adapters: mapping = {} for label in adapter.columns: id = None if not isinstance(label, basestring): label, id = label key = labels.index(label) if id is None: id = key mapping[key] = id map.append(mapping) return map #-- Private Methods -------------------------------------------------------- def _result_for(self, name, object, trait, row, column, value=None): """ Returns/Sets the value of the specified *name* attribute for the specified *object.trait[row].column* item. """ self.object = object self.name = trait self.row = row self.column = column self.column_id = column_id = self.column_map[column] self.value = value self.item = item = self.get_item(object, trait, row) item_class = item.__class__ key = '%s:%s:%d' % (item_class.__name__, name, column) handler = self.cache.get(key) if handler is not None: return handler() prefix = name[:4] trait_name = name[4:] for i, adapter in enumerate(self.adapters): if column in self.adapter_column_indices[i]: adapter.row = row adapter.item = item adapter.value = value adapter.column = column_id = self.adapter_column_map[i][column] if adapter.accepts: get_name = '%s_%s' % (column_id, trait_name) if adapter.trait(get_name) is not None: if prefix == 'get_': handler = lambda: getattr( adapter.set(row=self.row, column=column_id, item=self.item), get_name) else: handler = lambda: setattr( adapter.set(row=self.row, column=column_id, item=self.item), get_name, self. value) if adapter.is_cacheable: break return handler() else: if item is not None and hasattr(item_class, '__mro__'): for klass in item_class.__mro__: handler = (self._get_handler_for( '%s_%s_%s' % (klass.__name__, column_id, trait_name), prefix) or self._get_handler_for( '%s_%s' % (klass.__name__, trait_name), prefix)) if handler is not None: break if handler is None: handler = (self._get_handler_for( '%s_%s' % (column_id, trait_name), prefix) or self._get_handler_for(trait_name, prefix)) self.cache[key] = handler return handler() def _get_handler_for(self, name, prefix): """ Returns the handler for a specified trait name (or None if not found). """ if self.trait(name) is not None: if prefix == 'get_': return lambda: getattr(self, name) return lambda: setattr(self, name, self.value) return None @on_trait_change('columns,adapters.+update') def _flush_cache(self): """ Flushes the cache when the columns or any trait on any adapter changes. """ self.cache = {} self.cache_flushed = True
class TraitsTest(HasTraits): #------------------------------------------------------------------------- # Trait definitions: #------------------------------------------------------------------------- enabled = Bool(True) integer_text = Int(1) enumeration = Trait('one', 'two', 'three', 'four', 'five', 'six', cols=3) float_range = Range(0.0, 10.0, 10.0) int_range = Range(1, 6) int_range2 = Range(1, 50) compound = Trait(1, Range(1, 6), 'one', 'two', 'three', 'four', 'five', 'six') boolean = Bool(True) instance = Trait(Instance()) color = Color('cyan') font = Font() check_list = List(editor=CheckListEditor( values=['one', 'two', 'three', 'four'], cols=4)) list = List(Str, ['East of Eden', 'The Grapes of Wrath', 'Of Mice and Men']) button = Event(0, editor=ButtonEditor(label='Click')) file = File() directory = Directory() image_enum = Trait( editor=ImageEnumEditor( values=origin_values, suffix='_origin', cols=4, klass=Instance), *origin_values) #------------------------------------------------------------------------- # View definitions: #------------------------------------------------------------------------- view = View( ('|{Enum}', ('enabled', ), ('|<[Enumeration]', 'f1:enumeration[Simple]', '_', 'f2:enumeration[Custom]@', '_', 'f3:enumeration[Text]*', '_', 'f4:enumeration[Readonly]~'), ('|<[Check List]', 'f5:check_list[Simple]', '_', 'f6:check_list[Custom]@', '_', 'f7:check_list[Text]*', '_', 'f8:check_list[Readonly]~')), ('|{Range}', ('|<[Float Range]', 'f9:float_range[Simple]', '_', 'f10:float_range[Custom]@', '_', 'f11:float_range[Text]*', '_', 'f12:float_range[Readonly]~'), ('|<[Int Range]', 'f13:int_range[Simple]', '_', 'f14:int_range[Custom]@', '_', 'f15:int_range[Text]*', '_', 'f16:int_range[Readonly]~'), ('|<[Int Range 2]', 'f17:int_range2[Simple]', '_', 'f18:int_range2[Custom]@', '_', 'f19:int_range2[Text]*', '_', 'f20:int_range2[Readonly]~')), ('|{Misc}', ('|<[Integer Text]', 'f21:integer_text[Simple]', '_', 'f22:integer_text[Custom]@', '_', 'f23:integer_text[Text]*', '_', 'f24:integer_text[Readonly]~'), ('|<[Compound]', 'f25:compound[Simple]', '_', 'f26:compound[Custom]@', '_', 'f27:compound[Text]*', '_', 'f28:compound[Readonly]~'), ('|<[Boolean]', 'f29:boolean[Simple]', '_', 'f30:boolean[Custom]@', '_', 'f31:boolean[Text]*', '_', 'f32:boolean[Readonly]~')), ('|{Color/Font}', ('|<[Color]', 'f33:color[Simple]', '_', 'f34:color[Custom]@', '_', 'f35:color[Text]*', '_', 'f36:color[Readonly]~'), ('|<[Font]', 'f37:font[Simple]', '_', 'f38:font[Custom]@', '_', 'f39:font[Text]*', '_', 'f40:font[Readonly]~')), ('|{List}', ('|<[List]', 'f41:list[Simple]', '_', 'f42:list[Custom]@', '_', 'f43:list[Text]*', '_', 'f44:list[Readonly]~')), ( '|{Button}', ('|<[Button]', 'f45:button[Simple]', '_', 'f46:button[Custom]@'), # 'button[Text]*', # 'button[Readonly]~' ), ('|<[Image Enum]', 'f47:image_enum[Simple]', '_', 'f48:image_enum[Custom]@', '_', 'f49:image_enum[Text]*', '_', 'f50:image_enum[Readonly]~'), ('|<[Instance]', 'f51:instance[Simple]', '_', 'f52:instance[Custom]@', '_', 'f53:instance[Text]*', '_', 'f54:instance[Readonly]~'), ), ('|{File}', ( '|<[File]', 'f55:file[Simple]', '_', 'f56:file[Custom]@', '_', 'f57:file[Text]*', '_', 'f58:file[Readonly]~', ), ('|<[Directory]', 'f59:directory[Simple]', '_', 'f60:directory[Custom]@', '_', 'f61:directory[Text]*', '_', 'f62:directory[Readonly]~')), buttons=['Apply', 'Revert', 'Undo', 'OK'], handler=TraitsTestHandler())
def test_font_deprecated(self): with self.assertWarnsRegex(DeprecationWarning, "'Font' in 'traits'"): Font()
class SearchTableAdapter(TabularAdapter): """ Adapter to map the traits of the function items into UI table columns. """ ########################################################################## # SearchTabularAdapter traits ########################################################################## # Font used for rendering text in each row. # # FIXME: Although Arial 10 is the default wx font, we really should # querying something and adding a bold 'style' to it. normal_font = Font("Arial 9") ########################################################################## # TabularAdapter traits ########################################################################## # The columns to display (along with the adapter trait they map to. columns = [('Name', 'name'), ('Module', 'module')] # Tooltip text to show for a variable. tooltip = Property # Image displayed next to search item. image = Property icon_mapping = Dict({'function': ImageResource('function_variable')}) ########################################################################## # SearchTableAdapter interface ########################################################################## ### Private methods ###################################################### def _get_description(self, function_info): """ Grab a short text description of the described function. The first line in the doc-string is returned if it is available. Otherwise, an empty string is returned. """ # Create a PythonFunctionInfo for the function. # fixme: This seems a little heavy weight to just get the # doc-string, but it is the shortest path between here # and there... # fixme: We should likely do some error handling here... func = PythonFunctionInfo(module=function_info.module, name=function_info.name) if func.doc_string is "": short_description = "No information about this function." else: # Use the first line as the "short" function description. short_description = func.doc_string.splitlines()[0] return short_description ########################################################################## # TableAdapter interface ########################################################################## def get_font(self, object, trait, row): """ The default font is to tall for the table rows. sigh... """ return self.normal_font ### Private methods ###################################################### def _get_tooltip(self): """ Tooltip text is the firt line of the doc-string for the function. fixme: Add the calling convention as first line to this. fixme: Can we format this? Send it html or something like that? """ return self._get_description(self.item) def _get_image(self): """ Retreive the image for each cell in the table. fixme: For now, everything is a "function". Later, we will add united function and perhaps class. Or, we may make the icons for different functionality (rockphysics, filters, etc...) Also, we may want to attach images to functions and ask the actual function for its image. """ if self.column == 0: result = self.icon_mapping['function'] else: result = None return result
class TabularAdapter(HasPrivateTraits): """ The base class for adapting list items to values that can be edited by a TabularEditor. """ # -- Public Trait Definitions --------------------------------------------- #: A list of columns that should appear in the table. Each entry can have #: one of two forms: ``string`` or ``(string, id)``, where ``string`` is #: the UI name of the column, and ``id`` is a value that identifies that #: column to the adapter. Normally this value is either a trait name or an #: index, but it can be any value that the adapter wants. If only #: ``string`` is specified, then ``id`` is the index of the ``string`` #: within :py:attr:`columns`. columns = List() #: Maps UI name of column to value identifying column to the adapter, if #: different. column_dict = Property() #: Specifies the default value for a new row. This will usually need to be #: overridden. default_value = Any("") #: The default text color for odd table rows. odd_text_color = Color(None, update=True) #: The default text color for even table rows. even_text_color = Color(None, update=True) #: The default text color for table rows. default_text_color = Color(None, update=True) #: The default background color for odd table rows. odd_bg_color = Color(None, update=True) #: The default background color for even table rows. even_bg_color = Color(None, update=True) #: The default background color for table rows. default_bg_color = Color(None, update=True) #: Horizontal alignment to use for a specified column. alignment = Enum("left", "center", "right") #: The Python format string to use for a specified column. format = Str("%s") #: Width of a specified column. width = Float(-1) #: Can the text value of each item be edited? can_edit = Bool(True) #: The value to be dragged for a specified row item. drag = Property #: Can any arbitrary value be dropped onto the tabular view. can_drop = Bool(False) #: Specifies where a dropped item should be placed in the table relative to #: the item it is dropped on. dropped = Enum("after", "before") #: The font for a row item. font = Font(None) #: The text color for a row item. text_color = Property #: The background color for a row item. bg_color = Property #: The name of the default image to use for column items. image = Str(None, update=True) #: The text of a row/column item. text = Property #: The content of a row/column item (may be any Python value). content = Property #: The tooltip information for a row/column item. tooltip = Str #: The context menu for a row/column item. menu = Any #: The context menu for column header. column_menu = Any #: List of optional delegated adapters. adapters = List(ITabularAdapter, update=True) # -- Traits Set by the Editor --------------------------------------------- #: The object whose trait is being edited. object = Instance(HasTraits) #: The name of the trait being edited. name = Str #: The row index of the current item being adapted. row = Int #: The column index of the current item being adapted. column = Int #: The current column id being adapted (if any). column_id = Any #: Current item being adapted. item = Any #: The current value (if any). value = Any # -- Private Trait Definitions -------------------------------------------- #: Cache of attribute handlers. cache = Any({}) #: Event fired when the cache is flushed. cache_flushed = Event(update=True) #: The mapping from column indices to column identifiers (defined by the #: :py:attr:`columns` trait). column_map = Property(depends_on="columns") #: The mapping from column indices to column labels (defined by the #: :py:attr:`columns` trait). label_map = Property(depends_on="columns") #: The name of the trait on a row item containing the value to use #: as a row label. If ``None``, the label will be the empty string. row_label_name = Either(None, Str) #: For each adapter, specifies the column indices the adapter handles. adapter_column_indices = Property(depends_on="adapters,columns") #: For each adapter, specifies the mapping from column index to column id. adapter_column_map = Property(depends_on="adapters,columns") # ------------------------------------------------------------------------- # TabularAdapter interface # ------------------------------------------------------------------------- def cleanup(self): """ Clean up the adapter to remove references to objects. """ self.trait_setq(object=None, item=None, value=None) # -- Adapter methods that are sensitive to item type ---------------------- def get_alignment(self, object, trait, column): """ Returns the alignment style to use for a specified column. The possible values that can be returned are: ``'left'``, ``'center'`` or ``'right'``. All table items share the same alignment for a specified column. """ return self._result_for("get_alignment", object, trait, 0, column) def get_width(self, object, trait, column): """ Returns the width to use for a specified column. If the value is <= 0, the column will have a *default* width, which is the same as specifying a width of 0.1. If the value is > 1.0, it is converted to an integer and the result is the width of the column in pixels. This is referred to as a *fixed width* column. If the value is a float such that 0.0 < value <= 1.0, it is treated as the *unnormalized fraction of the available space* that is to be assigned to the column. What this means requires a little explanation. To arrive at the size in pixels of the column at any given time, the editor adds together all of the *unnormalized fraction* values returned for all columns in the table to arrive at a total value. Each *unnormalized fraction* is then divided by the total to create a *normalized fraction*. Each column is then assigned an amount of space in pixels equal to the maximum of 30 or its *normalized fraction* multiplied by the *available space*. The *available space* is defined as the actual width of the table minus the width of all *fixed width* columns. Note that this calculation is performed each time the table is resized in the user interface, thus allowing columns of this type to increase or decrease their width dynamically, while leaving *fixed width* columns unchanged. """ return self._result_for("get_width", object, trait, 0, column) def get_can_edit(self, object, trait, row): """ Returns whether the user can edit a specified row. A ``True`` result indicates that the value can be edited, while a ``False`` result indicates that it cannot. """ return self._result_for("get_can_edit", object, trait, row, 0) def get_drag(self, object, trait, row): """ Returns the value to be *dragged* for a specified row. A result of ``None`` means that the item cannot be dragged. Note that the value returned does not have to be the actual row item. It can be any value that you want to drag in its place. In particular, if you want the drag target to receive a copy of the row item, you should return a copy or clone of the item in its place. Also note that if multiple items are being dragged, and this method returns ``None`` for any item in the set, no drag operation is performed. """ return self._result_for("get_drag", object, trait, row, 0) def get_can_drop(self, object, trait, row, value): """ Returns whether the specified ``value`` can be dropped on the specified row. A value of ``True`` means the ``value`` can be dropped; and a value of ``False`` indicates that it cannot be dropped. The result is used to provide the user positive or negative drag feedback while dragging items over the table. ``value`` will always be a single value, even if multiple items are being dragged. The editor handles multiple drag items by making a separate call to :py:meth:`get_can_drop` for each item being dragged. """ return self._result_for("get_can_drop", object, trait, row, 0, value) def get_dropped(self, object, trait, row, value): """ Returns how to handle a specified ``value`` being dropped on a specified row. The possible return values are: - ``'before'``: Insert the specified ``value`` before the dropped on item. - ``'after'``: Insert the specified ``value`` after the dropped on item. Note there is no result indicating *do not drop* since you will have already indicated that the ``object`` can be dropped by the result returned from a previous call to :py:meth:`get_can_drop`. """ return self._result_for("get_dropped", object, trait, row, 0, value) def get_font(self, object, trait, row, column=0): """ Returns the font to use for displaying a specified row or cell. A result of ``None`` means use the default font; otherwise a toolkit font object should be returned. Note that all columns for the specified table row will use the font value returned. """ return self._result_for("get_font", object, trait, row, column) def get_text_color(self, object, trait, row, column=0): """ Returns the text color to use for a specified row or cell. A result of ``None`` means use the default text color; otherwise a toolkit-compatible color should be returned. Note that all columns for the specified table row will use the text color value returned. """ return self._result_for("get_text_color", object, trait, row, column) def get_bg_color(self, object, trait, row, column=0): """ Returns the background color to use for a specified row or cell. A result of ``None`` means use the default background color; otherwise a toolkit-compatible color should be returned. Note that all columns for the specified table row will use the background color value returned. """ return self._result_for("get_bg_color", object, trait, row, column) def get_image(self, object, trait, row, column): """ Returns the image to display for a specified cell. A result of ``None`` means no image will be displayed in the specified table cell. Otherwise the result should either be the name of the image, or an :py:class:`~pyface.image_resource.ImageResource` object specifying the image to display. A name is allowed in the case where the image is specified in the :py:class:`~traitsui.editors.tabular_editor.TabularEditor` :py:attr:`~traitsui.editors.tabular_editor.TabularEditor.images` trait. In that case, the name should be the same as the string specified in the :py:class:`~pyface.image_resource.ImageResource` constructor. """ return self._result_for("get_image", object, trait, row, column) def get_format(self, object, trait, row, column): """ Returns the Python formatting string to apply to the specified cell. The resulting of formatting with this string will be used as the text to display it in the table. The return can be any Python string containing exactly one old-style Python formatting sequence, such as ``'%.4f'`` or ``'(%5.2f)'``. """ return self._result_for("get_format", object, trait, row, column) def get_text(self, object, trait, row, column): """ Returns a string containing the text to display for a specified cell. If the underlying data representation for a specified item is not a string, then it is your responsibility to convert it to one before returning it as the result. """ return self._result_for("get_text", object, trait, row, column) def get_content(self, object, trait, row, column): """ Returns the content to display for a specified cell. """ return self._result_for("get_content", object, trait, row, column) def set_text(self, object, trait, row, column, text): """ Sets the value for the specified cell. This method is called when the user completes an editing operation on a table cell. The string specified by ``text`` is the value that the user has entered in the table cell. If the underlying data does not store the value as text, it is your responsibility to convert ``text`` to the correct representation used. """ self._result_for("set_text", object, trait, row, column, text) def get_tooltip(self, object, trait, row, column): """ Returns a string containing the tooltip to display for a specified cell. You should return the empty string if you do not wish to display a tooltip. """ return self._result_for("get_tooltip", object, trait, row, column) def get_menu(self, object, trait, row, column): """ Returns the context menu for a specified cell. """ return self._result_for("get_menu", object, trait, row, column) def get_column_menu(self, object, trait, row, column): """ Returns the context menu for a specified column. """ return self._result_for("get_column_menu", object, trait, row, column) # -- Adapter methods that are not sensitive to item type ------------------ def get_item(self, object, trait, row): """ Returns the specified row item. The value returned should be the value that exists (or *logically* exists) at the specified ``row`` in your data. If your data is not really a list or array, then you can just use ``row`` as an integer *key* or *token* that can be used to retrieve a corresponding item. The value of ``row`` will always be in the range: 0 <= row < ``len(object, trait)`` (i.e. the result returned by the adapter :py:meth:`len` method). The default implementation assumes the trait defined by ``object.trait`` is a *sequence* and attempts to return the value at index ``row``. If an error occurs, it returns ``None`` instead. This definition should work correctly for lists, tuples and arrays, or any other object that is indexable, but will have to be overridden for all other cases. """ try: return getattr(object, trait)[row] except: return None def len(self, object, trait): """ Returns the number of row items in the specified ``object.trait``. The result should be an integer greater than or equal to 0. The default implementation assumes the trait defined by ``object.trait`` is a *sequence* and attempts to return the result of calling ``len(object.trait)``. It will need to be overridden for any type of data which for which :py:func:`len` will not work. """ # Sometimes, during shutdown, the object has been set to None. if object is None: return 0 else: return len(getattr(object, trait)) def get_default_value(self, object, trait): """ Returns a new default value for the specified ``object.trait`` list. This method is called when *insert* or *append* operations are allowed and the user requests that a new item be added to the table. The result should be a new instance of whatever underlying representation is being used for table items. The default implementation simply returns the value of the adapter's :py:attr:`default_value` trait. """ return self.default_value def delete(self, object, trait, row): """ Deletes the specified row item. This method is only called if the *delete* operation is specified in the :py:class:`~traitsui.editors.tabular_editor.TabularEditor` :py:attr:`~traitsui.editors.tabular_editor.TabularEditor.operation` trait, and the user requests that the item be deleted from the table. The adapter can still choose not to delete the specified item if desired, although that may prove confusing to the user. The default implementation assumes the trait defined by ``object.trait`` is a mutable sequence and attempts to perform a ``del object.trait[row]`` operation. """ del getattr(object, trait)[row] def insert(self, object, trait, row, value): """ Inserts ``value`` at the specified ``object.trait[row]`` index. The specified ``value`` can be: - An item being moved from one location in the data to another. - A new item created by a previous call to :py:meth:`~TabularAdapter.get_default_value`. - An item the adapter previously approved via a call to :py:meth:`~TabularAdapter.get_can_drop`. The adapter can still choose not to insert the item into the data, although that may prove confusing to the user. The default implementation assumes the trait defined by ``object.trait`` is a mutable sequence and attempts to perform an ``object.trait[row:row] = [value]`` operation. """ getattr(object, trait)[row:row] = [value] def get_column(self, object, trait, index): """ Returns the column id corresponding to a specified column index. """ self.object, self.name = object, trait return self.column_map[index] # -- Property Implementations --------------------------------------------- def _get_drag(self): return self.item def _get_text_color(self): if (self.row % 2) == 1: return self.even_text_color_ or self.default_text_color return self.odd_text_color or self.default_text_color_ def _get_bg_color(self): if (self.row % 2) == 1: return self.even_bg_color_ or self.default_bg_color_ return self.odd_bg_color or self.default_bg_color_ def _get_text(self): return self.get_format( self.object, self.name, self.row, self.column) % self.get_content( self.object, self.name, self.row, self.column) def _set_text(self, value): if isinstance(self.column_id, int): self.item[self.column_id] = self.value else: # Convert value to the correct trait type. try: trait_handler = self.item.trait(self.column_id).handler setattr( self.item, self.column_id, trait_handler.evaluate(self.value), ) except: setattr(self.item, self.column_id, value) def _get_content(self): if isinstance(self.column_id, int): return self.item[self.column_id] return getattr(self.item, self.column_id) # -- Property Implementations --------------------------------------------- @cached_property def _get_column_dict(self): cols = {} for i, value in enumerate(self.columns): if isinstance(value, six.string_types): cols.update({value: value}) else: cols.update({value[0]: value[1]}) return cols @cached_property def _get_column_map(self): map = [] for i, value in enumerate(self.columns): if isinstance(value, six.string_types): map.append(i) else: map.append(value[1]) return map def get_label(self, section, obj=None): """Override this method if labels will vary from object to object.""" return self.label_map[section] def get_row_label(self, section, obj=None): if self.row_label_name is None: return None rows = getattr(obj, self.name, None) if rows is None: return None item = rows[section] return getattr(item, self.row_label_name, None) @cached_property def _get_label_map(self): map = [] for i, value in enumerate(self.columns): if isinstance(value, six.string_types): map.append(value) else: map.append(value[0]) return map @cached_property def _get_adapter_column_indices(self): labels = self.label_map map = [] for adapter in self.adapters: indices = [] for label in adapter.columns: if not isinstance(label, six.string_types): label = label[0] indices.append(labels.index(label)) map.append(indices) return map @cached_property def _get_adapter_column_map(self): labels = self.label_map map = [] for adapter in self.adapters: mapping = {} for label in adapter.columns: id = None if not isinstance(label, six.string_types): label, id = label key = labels.index(label) if id is None: id = key mapping[key] = id map.append(mapping) return map # -- Private Methods ------------------------------------------------------ def _result_for(self, name, object, trait, row, column, value=None): """ Returns/Sets the value of the specified *name* attribute for the specified *object.trait[row].column* item. """ self.object = object self.name = trait self.row = row self.column = column self.column_id = column_id = self.column_map[column] self.value = value self.item = item = self.get_item(object, trait, row) item_class = item.__class__ key = "%s:%s:%d" % (item_class.__name__, name, column) handler = self.cache.get(key) if handler is not None: return handler() prefix = name[:4] trait_name = name[4:] for i, adapter in enumerate(self.adapters): if column in self.adapter_column_indices[i]: adapter.row = row adapter.item = item adapter.value = value adapter.column = column_id = self.adapter_column_map[i][column] if adapter.accepts: get_name = "%s_%s" % (column_id, trait_name) if adapter.trait(get_name) is not None: if prefix == "get_": handler = lambda: getattr( adapter.trait_set( row=self.row, column=column_id, item=self.item, ), get_name, ) else: handler = lambda: setattr( adapter.trait_set( row=self.row, column=column_id, item=self.item, ), get_name, self.value, ) if adapter.is_cacheable: break return handler() else: if item is not None and hasattr(item_class, "__mro__"): for klass in item_class.__mro__: handler = self._get_handler_for( "%s_%s_%s" % (klass.__name__, column_id, trait_name), prefix, ) or self._get_handler_for( "%s_%s" % (klass.__name__, trait_name), prefix) if handler is not None: break if handler is None: handler = self._get_handler_for( "%s_%s" % (column_id, trait_name), prefix) or self._get_handler_for(trait_name, prefix) self.cache[key] = handler return handler() def _get_handler_for(self, name, prefix): """ Returns the handler for a specified trait name (or None if not found). """ if self.trait(name) is not None: if prefix == "get_": return lambda: getattr(self, name) return lambda: setattr(self, name, self.value) return None @on_trait_change("columns,adapters.+update") def _flush_cache(self): """ Flushes the cache when the columns or any trait on any adapter changes. """ self.cache = {} self.cache_flushed = True
class Foo(HasTraits): font = Font()
class ByteEditPreferences(PreferencesHelper): """ The preferences helper for the Framework application. """ #### 'PreferencesHelper' interface ######################################## # The path to the preference node that contains the preferences. preferences_path = 'omnivore_framework.task.byte_edit' #### Preferences ########################################################## map_width_low = 1 map_width_high = 256 map_width = Range(low=map_width_low, high=map_width_high, value=40) bitmap_width_low = 1 bitmap_width_high = 16 bitmap_width = Range(low=bitmap_width_low, high=bitmap_width_high, value=1) hex_grid_width_low = 1 hex_grid_width_high = 256 hex_grid_width = Range(low=hex_grid_width_low, high=hex_grid_width_high, value=16) # Font used for hex/disassembly text_font = Font(def_font) text_font_char_width = Property(Int, depends_on='text_font') text_font_char_height = Property(Int, depends_on='text_font') image_caches = Property(Dict, depends_on='text_font') header_font = Font(def_font + " bold") hex_grid_lower_case = Bool(True) assembly_lower_case = Bool(False) int_display_format = Enum( "Hexadecimal", "Decimal", "Both", ) hex_display_format = Enum( "$XX", "0xXX", "XXh", ) disassembly_column_widths = Tuple(0, 0, 0) background_color = Color(wx.WHITE) text_color = Color(wx.BLACK) highlight_background_color = Color(wx.Colour(100, 200, 230)) data_background_color = Color(wx.Colour(224, 224, 224)) empty_background_color = Color( wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE).Get(False)) match_background_color = Color(wx.Colour(255, 255, 180)) comment_background_color = Color(wx.Colour(255, 180, 200)) error_background_color = Color(wx.Colour(255, 128, 128)) diff_text_color = Color(wx.Colour(255, 0, 0)) unfocused_caret_color = Color(wx.Colour(128, 128, 128)) row_header_bg_color = Color(wx.Colour(224, 224, 224)) col_header_bg_color = Color(wx.Colour(224, 224, 224)) col_label_border_width = Int(3) row_label_border_width = Int(3) row_height_extra_padding = Int(-3) base_cell_width_in_chars = Int(2) pixel_width_padding = Int(2) caret_pen = Any selected_brush = Any normal_brush = Any data_brush = Any match_brush = Any comment_brush = Any empty_brush = Any def _caret_pen_default(self): return wx.Pen(self.unfocused_caret_color, 1, wx.SOLID) def _selected_brush_default(self): return wx.Brush(self.highlight_background_color, wx.SOLID) def _normal_brush_default(self): return wx.Brush(self.background_color, wx.SOLID) def _data_brush_default(self): return wx.Brush(self.data_background_color, wx.SOLID) def _match_brush_default(self): return wx.Brush(self.match_background_color, wx.SOLID) def _comment_brush_default(self): return wx.Brush(self.comment_background_color, wx.SOLID) def _empty_brush_default(self): return wx.Brush(self.empty_background_color, wx.SOLID) @cached_property def _get_text_font_char_width(self): dc = wx.MemoryDC() dc.SetFont(self.text_font) return dc.GetCharWidth() @cached_property def _get_text_font_char_height(self): dc = wx.MemoryDC() dc.SetFont(self.text_font) return dc.GetCharHeight() @cached_property def _get_image_caches(self): return dict() def calc_cell_size_in_pixels(self, chars_per_cell): width = self.pixel_width_padding * 2 + self.text_font_char_width * chars_per_cell height = self.row_height_extra_padding + self.text_font_char_height return width, height def calc_text_width(self, text): return self.text_font_char_width * len(text) def calc_image_cache(self, cache_cls): try: c = self.image_caches[cache_cls] except KeyError: c = cache_cls(self) self.image_caches[cache_cls] = c return c @property def hex_format_character(self): return "x" if self.hex_grid_lower_case else "X"
class AnnotationEditor(HasTraits): component = Any border_visible = Bool(True) border_width = Range(0, 10) border_color = Color font = Font('modern 12') text_color = Color bgcolor = Color text = Str bg_visible = Bool(True) @on_trait_change('component:text') def _component_text_changed(self): self.text = self.component.text def _component_changed(self): if self.component: traits = ('border_visible', 'border_width', 'text') d = self.component.trait_get(traits) self.trait_set(self, **d) for c in ('border_color', 'text_color', 'bgcolor'): v = getattr(self.component, c) if not isinstance(v, str): v = v[0] * 255, v[1] * 255, v[2] * 255 self.trait_set(**{c: v}) def _bg_visible_changed(self): if self.component: if self.bg_visible: self.component.bgcolor = self.bgcolor else: self.component.bgcolor = transparent_color self.component.request_redraw() @on_trait_change('border_+, text_color, bgcolor, text') def _update(self, name, new): if self.component: self.component.trait_set(**{name: new}) self.component.request_redraw() @on_trait_change('font') def _update_font(self): if self.component: self.component.font = str(self.font) self.component.request_redraw() def traits_view(self): v = View( VGroup( Item('font', width=75, ), Item('text_color', label='Text'), HGroup( UItem('bg_visible', tooltip='Is the background transparent' ), Item('bgcolor', label='Background', enabled_when='bg_visible' ), ), UItem('text', style='custom'), Group( Item('border_visible'), Item('border_width', enabled_when='border_visible'), Item('border_color', enabled_when='border_visible'), label='Border' ), visible_when='component' ) ) return v