def check_init_and_dispose(self, style): # Test init and dispose by opening and closing the UI view = View(Item("dir_path", editor=DirectoryEditor(), style=style)) obj = DirectoryModel() with reraise_exceptions(), \ create_ui(obj, dict(view=view)): pass
class HistoryDemo(HasTraits): name = Str() file = File() directory = Directory() view = View( Item('name', id='name', editor=HistoryEditor(entries=5) ), Item('file', id='file1', editor=FileEditor(entries=10) ), Item('file', id='file2', editor=FileEditor(entries=10, filter=['All files (*.*)|*.*', 'Python files (*.py)|*.py']) ), Item('directory', id='directory', editor=DirectoryEditor(entries=10) ), title='History Editor Demo', id='enthought.test.history_demo.HistoryDemo', width=0.33, resizable=True )
class Person(HasTraits): #--------------------------------------------------------------------------- # Trait definitions: #--------------------------------------------------------------------------- name = Str('David Morrill') age = Int(39) sex = Trait('Male', 'Female') coolness = Range(0.0, 10.0, 10.0) number = Trait(1, Range(1, 6), 'one', 'two', 'three', 'four', 'five', 'six') human = Bool(True) employer = Trait(Employer(company='Enthought, Inc.', boss='eric')) eye_color = RGBAColor set = List( editor=CheckListEditor(values=['one', 'two', 'three', 'four'], cols=4)) font = KivaFont street = Str city = Str state = Str zip = Int(78663) password = Str books = List(Str, ['East of Eden', 'The Grapes of Wrath', 'Of Mice and Men']) call = Event(0, editor=ButtonEditor(label='Click to call')) info = Str(editor=FileEditor()) location = Str(editor=DirectoryEditor()) origin = Trait(editor=ImageEnumEditor(values=origin_values, suffix='_origin', cols=4, klass=Employer), *origin_values) nm = Item('name', enabled_when='object.age >= 21') pw = Item('password', defined_when='object.zip == 78664') view = View( ((nm, 'age', 'coolness', '_', 'eye_color', 'eye_color@', 'eye_color*', 'eye_color~', '_', 'font', 'font@', 'font*', 'font~', '_', 'set', 'set@', 'set*', 'set~', '_', 'sex', 'sex@', 'sex*', 'sex~', '_', 'human', 'human@', 'human*', 'human~', '_', 'number', 'number@', 'number*', 'number~', '_', 'books', '_', 'books@', '_', 'books*', '_', 'books~', '_', 'info', 'location', 'origin', 'origin@', 'call', 'employer', 'employer[]@', 'employer*', 'employer~', pw, '|<[Person:]'), (' ', 'street', 'city', 'state', 'zip', '|<[Address:]'), (nm, nm, nm, nm, nm, nm, nm, nm, nm, nm, nm, nm, nm, nm, '|<[Names:]'), '|'), title='Traits 2 User Interface Test', handler=PersonHandler(), buttons=['Apply', 'Revert', 'Undo', 'OK'], height=0.5) wizard = View(('|p1:', 'name', 'age', 'sex'), ('|p2:', 'street', 'city', 'state', 'zip'), ('|p3:', 'eye_color', 'origin', 'human'), handler=WizardHandler())
def test_simple_editor_auto_set_text(self): # Test with auto_set set to True. view = View(Item("dir_path", editor=DirectoryEditor(auto_set=True))) obj = DirectoryModel() tester = UITester() with tester.create_ui(obj, dict(view=view)) as ui: dirpath_field = tester.find_by_name(ui, "dir_path") dirpath_field.perform(KeySequence("some_dir")) self.assertEqual(obj.dir_path, "some_dir")
def test_simple_editor_display_path(self): # Test the filepath widget is updated to show path view = View(Item("dir_path", editor=DirectoryEditor())) obj = DirectoryModel() tester = UITester() with tester.create_ui(obj, dict(view=view)) as ui: dirpath_field = tester.find_by_name(ui, "dir_path") self.assertEqual(dirpath_field.inspect(DisplayedText()), "") obj.dir_path = "some_dir" self.assertEqual(dirpath_field.inspect(DisplayedText()), "some_dir")
def test_simple_editor_set_text_to_nonexisting_path(self): # Test setting the editor to a nonexisting dirpath # e.g. use case for creating a new file. view = View(Item("dir_path", editor=DirectoryEditor())) obj = DirectoryModel() tester = UITester() with tester.create_ui(obj, dict(view=view)) as ui: dirpath_field = tester.find_by_name(ui, "dir_path") dirpath_field.perform(KeySequence("some_dir")) dirpath_field.perform(KeyClick("Enter")) self.assertEqual(obj.dir_path, "some_dir")
def test_custom_editor_reload_changed_after_dispose(self): # Test firing reload event on the model after the UI is disposed. view = View( Item( "dir_path", editor=DirectoryEditor(reload_name="reload_event"), style="custom", ), ) obj = DirectoryModel() with UITester().create_ui(obj, dict(view=view)): pass # should not fail. obj.reload_event = True
def open_file_view(self): item = Item('file_name', id='file_tree', style='custom', show_label=False, width=0.5, editor=DirectoryEditor( filter=self.filter, allow_dir=True, reload_name='reload', dclick_name='dclick', )) width = height = 0.20 if len(self.extensions) > 0: raise Exception('extensions are not supported') return View(VGroup( VGroup(item), HGroup( Item('create', id='create', show_label=False, style='custom', defined_when='is_save_file', enabled_when='can_create_dir', tooltip='Create a new directory'), Item('file_name', id='history', editor=HistoryEditor(entries=self.entries, auto_set=True), springy=True), Item('ok', id='ok', show_label=False, enabled_when='is_valid_file'), Item('cancel', show_label=False))), title=self.title, id=self.id, kind='livemodal', width=width, height=height, close_result=False, resizable=True)
def _get_root_item(self): return Item('root_directory', label='Directory', editor=DirectoryEditor())
class FileSieve ( HasPrivateTraits ): # The name of the plugin: name = Str( 'File Sieve' ) # The persistence id for this object: id = Str( 'etsdevtools.developer.tools.file_sieve.state', save_state_id = True ) # The path specifying the files to be listed: path = Directory( save_state = True, connect = 'to: path to sieve' ) # Base path (in normalized form): base_path = Str # Extension to match on: ext = Str # The list of files containing in the specified path: files = List( File ) # The list of currently selected files: selected = List( File ) # The list of selected file names: selected_files = Property( depends_on = 'selected', connect = 'from: selected file names' ) # The filter used to 'sieve' the list of files: filter = Instance( FileFilter, () ) # Select all listed files button: select_all = Button( 'Select All' ) # The indices of the table items currently passing the table filter: filtered_indices = List( Int ) # The list of filtered file names: filtered_files = Property( depends_on = 'filtered_indices', connect = 'from: filtered file names' ) # The total number of files being displayed: num_files = Property( depends_on = 'filtered_indices' ) # The total size of all filtered files: size = Property( depends_on = 'filtered_indices' ) # Should subdirectories of the path be included? recursive = Bool( True ) # The current file worker thread object we are using: worker = Instance( FileWorker ) #-- Traits View Definitions ------------------------------------------------ view = View( VSplit( Item( 'path', id = 'path', editor = DirectoryEditor( entries = 10 ), dock = 'vertical' ), HGroup( Item( 'filter', show_label = False, style = 'custom' ), # '_', # Item( 'recursive' ), spring, '_', Item( 'num_files', label = 'Files', style = 'readonly', format_func = commatize, width = -40 ), '_', Item( 'size', style = 'readonly', format_func = commatize, width = -70 ), '_', Item( 'select_all', show_label = False, enabled_when = 'len( files ) > 0' ), dock = 'vertical' ), VGroup( HGroup( Item( 'base_path', label = 'Path', editor = TitleEditor(), springy = True ), '_', Item( 'ext', editor = TitleEditor() ) ), Item( 'files', id = 'files', editor = files_table_editor ), dock = 'horizontal', show_labels = False ), id = 'splitter', ), title = 'File Sieve', id = 'etsdevtools.developer.tools.file_sieve.FileSieve', width = 0.6, height = 0.7, resizable = True, handler = FileSieveHandler ) #-- Property Implementations ----------------------------------------------- def _get_num_files ( self ): return len( self.filtered_indices ) @cached_property def _get_size ( self ): files = self.files return reduce( lambda l, r: l + files[r].size, self.filtered_indices, 0L ) @cached_property def _get_selected_files ( self ): return [ file.path for file in self.selected ] @cached_property def _get_filtered_files ( self ): files = self.files return [ files[i].path for i in self.filtered_indices ] #-- Event Handlers --------------------------------------------------------- def _path_changed ( self, path ): """ Handles the 'path' trait being changed. """ # Make sure the path has been normalized: path = abspath( path ) # Determine the correct starting directory and optional file extension # to match on: ext = '' if not isdir( path ): path, name = split( path ) if not isdir( path ): del self.files[:] return root, ext = splitext( name ) self.base_path = path self.ext = ext if self.worker is not None: self.worker.abort = True self.worker = FileWorker( sieve = self, ext = ext ).set( path = path ) def _select_all_changed ( self ): """ Handles the 'Select All' button being pressed. """ files = self.files self.selected = [ files[i] for i in self.filtered_indices ] def _recursive_changed ( self, recursive ): """ Handles the 'recursive' trait being changed.