class PopupTextField(popup_view.IContent): def __init__(self): self._root = VerticalLayout() self._tf = TextField('Edit me') self._root.setSizeUndefined() self._root.setSpacing(True) self._root.setMargin(True) self._root.addComponent(Label(('The changes made to any components ' 'inside the popup are reflected automatically when the popup ' 'is closed, but you might want to provide explicit action ' 'buttons for the user, like \"Save\" or \"Close\".'))) self._root.addComponent(self._tf) self._tf.setValue('Initial dynamic content') self._tf.setWidth('300px') def getMinimizedValueAsHTML(self): return str(self._tf.getValue()) def getPopupComponent(self): return self._root
class TextEditor ( Editor, IValueChangeListener ): """ Base class for text style editors, which displays an editable text field, containing a text representation of the object trait value. """ #--------------------------------------------------------------------------- # Finishes initializing the editor by creating the underlying toolkit # widget: #--------------------------------------------------------------------------- def init ( self, parent ): """ Finishes initializing the editor by creating the underlying toolkit widget. """ self.control = TextField() self.control.setValue( str(self.str_value) ) self.control.addListener(self, IValueChangeListener) self.set_tooltip() #--------------------------------------------------------------------------- # Handles the user changing the contents of the edit control: #--------------------------------------------------------------------------- def valueChange(self, event): """ Handles the user changing the contents of the edit control. """ try: self.value = unicode(self.control.getValue()) except TraitError: pass
class TextFieldSingleExample(VerticalLayout, IValueChangeListener): def __init__(self): super(TextFieldSingleExample, self).__init__() self.setSpacing(True) self._editor = TextField('Echo this:') self._editor.addListener(self, IValueChangeListener) self._editor.setImmediate(True) # editor.setColumns(5) # guarantees that at least 5 chars fit self.addComponent(self._editor) # Catch the valuechange event of the textfield and update the value of the # label component def valueChange(self, event): # Show the new value we received self.getWindow().showNotification(self._editor.getValue())
class PopupTextField(popup_view.IContent): def __init__(self): self._root = VerticalLayout() self._tf = TextField('Edit me') self._root.setSizeUndefined() self._root.setSpacing(True) self._root.setMargin(True) self._root.addComponent( Label( ('The changes made to any components ' 'inside the popup are reflected automatically when the popup ' 'is closed, but you might want to provide explicit action ' 'buttons for the user, like \"Save\" or \"Close\".'))) self._root.addComponent(self._tf) self._tf.setValue('Initial dynamic content') self._tf.setWidth('300px') def getMinimizedValueAsHTML(self): return str(self._tf.getValue()) def getPopupComponent(self): return self._root
class TreeSingleSelectExample(HorizontalLayout, IValueChangeListener, button.IClickListener, action.IHandler): # Actions for the context menu _ACTION_ADD = Action('Add child item') _ACTION_DELETE = Action('Delete') def __init__(self): super(TreeSingleSelectExample, self).__init__() self.setSpacing(True) # Create the Tree,a dd to layout self._tree = Tree('Hardware Inventory') self.addComponent(self._tree) # Contents from a (prefilled example) hierarchical container: self._tree.setContainerDataSource(ExampleUtil.getHardwareContainer()) # Add Valuechangelistener and Actionhandler self._tree.addListener(self, IValueChangeListener) # Add actions (context menu) self._tree.addActionHandler(self) # Cause valueChange immediately when the user selects self._tree.setImmediate(True) # Set tree to show the 'name' property as caption for items self._tree.setItemCaptionPropertyId(ExampleUtil.hw_PROPERTY_NAME) self._tree.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY) # Expand whole tree for idd in self._tree.rootItemIds(): self._tree.expandItemsRecursively(idd) # Create the 'editor bar' (textfield and button in a horizontallayout) self._editBar = HorizontalLayout() self._editBar.setMargin(False, False, False, True) self._editBar.setEnabled(False) self.addComponent(self._editBar) # textfield self._editor = TextField('Item name') self._editor.setImmediate(True) self._editBar.addComponent(self._editor) # apply-button self._change = Button('Apply', self)#, 'buttonClick') FIXME: listener self._editBar.addComponent(self._change) self._editBar.setComponentAlignment(self._change, Alignment.BOTTOM_LEFT) def valueChange(self, event): if event.getProperty().getValue() is not None: # If something is selected from the tree, get it's 'name' and # insert it into the textfield val = self._tree.getItem( event.getProperty().getValue()).getItemProperty( ExampleUtil.hw_PROPERTY_NAME) self._editor.setValue(val) self._editor.requestRepaint() self._editBar.setEnabled(True) else: self._editor.setValue('') self._editBar.setEnabled(False) def buttonClick(self, event): # If the edited value contains something, set it to be the item's new # 'name' property if not (self._editor.getValue() == ''): item = self._tree.getItem(self._tree.getValue()) name = item.getItemProperty(ExampleUtil.hw_PROPERTY_NAME) name.setValue(self._editor.getValue()) # Returns the set of available actions def getActions(self, target, sender): return [self._ACTION_ADD, self._ACTION_DELETE] # Handle actions def handleAction(self, a, sender, target): if a == self._ACTION_ADD: # Allow children for the target item, and expand it self._tree.setChildrenAllowed(target, True) self._tree.expandItem(target) # Create new item, set parent, disallow children (= leaf node) itemId = self._tree.addItem() self._tree.setParent(itemId, target) self._tree.setChildrenAllowed(itemId, False) # Set the name for this item (we use it as item caption) item = self._tree.getItem(itemId) name = item.getItemProperty(ExampleUtil.hw_PROPERTY_NAME) name.setValue('New Item') elif a == self._ACTION_DELETE: parent = self._tree.getParent(target) self._tree.removeItem(target) # If the deleted object's parent has no more children, set it's # childrenallowed property to false (= leaf node) if parent is not None and len(self._tree.getChildren(parent)) == 0: self._tree.setChildrenAllowed(parent, False)
class MuntjacField(MuntjacControl, AbstractTkField): """ A Muntjac implementation of a Field which uses a TextField to provide a single line of editable text. """ # -------------------------------------------------------------------------- # SetupMethods # -------------------------------------------------------------------------- def create(self, parent): """ Creates the underlying TextField. """ self.widget = TextField() parent.addComponent(self.widget) def initialize(self): """ Initializes the attributes of the Muntjac widget. """ super(MuntjacField, self).initialize() shell = self.shell_obj self.set_read_only(shell.read_only) self.set_placeholder_text(shell.placeholder_text) text = shell.field_text if text is not None: self.set_text(text) shell._modified = False self.set_cursor_position(shell.cursor_position) self.set_password_mode(shell.password_mode) self.set_max_length(shell.max_length) def bind(self): """ Binds the event handlers for the TextField. """ super(MuntjacField, self).bind() # widget = self.widget # widget.textEdited.connect(self.on_text_edited) # widget.textChanged.connect(self.on_text_changed) # widget.returnPressed.connect(self.on_return_pressed) # widget.selectionChanged.connect(self.on_selection_changed) # widget.cursorPositionChanged.connect(self.on_cursor_changed) # -------------------------------------------------------------------------- # Shell Object Change Handlers # -------------------------------------------------------------------------- def shell_max_length_changed(self, max_length): """ The change handler for the 'max_length' attribute on the shell object. """ self.set_max_length(max_length) def shell_read_only_changed(self, read_only): """ The change handler for the 'read_only' attribute on the shell object. """ self.set_read_only(read_only) def shell_placeholder_text_changed(self, placeholder_text): """ The change handler for the 'placeholder_text' attribute on the shell object. """ self.set_placeholder_text(placeholder_text) def shell_cursor_position_changed(self, cursor_position): """ The change handler for the 'cursor_position' attribute on the shell object. """ if not guard.guarded(self, "updating_cursor"): self.set_cursor_position(cursor_position) def shell_field_text_changed(self, text): """ The change handler for the 'field_text' attribute on the shell object. """ if text is not None: if not guard.guarded(self, "updating_text"): self.set_text(text) self.shell_obj._modified = False def shell_password_mode_changed(self, mode): """ The change handler for the 'password_mode' attribute on the shell object. """ self.set_password_mode(mode) # -------------------------------------------------------------------------- # Manipulation Methods # -------------------------------------------------------------------------- def set_selection(self, start, end): """ Sets the selection in the widget between the start and end positions, inclusive. """ self.widget.setSelectionRange(start, end - start) def select_all(self): """ Select all the text in the line edit. If there is no text in the line edit, the selection will be empty. """ self.widget.selectAll() def deselect(self): """ Deselect any selected text. Sets a selection with start == stop to deselect the current selection. The cursor is placed at the beginning of selection. """ self.widget.setSelectionRange(0, 0) def clear(self): """ Clear the line edit of all text. """ self.widget.setValue("") def backspace(self): """ Simple backspace functionality. If no text is selected, deletes the character to the left of the cursor. Otherwise, it deletes the selected text. """ # FIXME: get selection pos = self.widget.getCursorPosition() val = self.widget.getValue() if (len(val) > 0) and 0 < pos <= len(val): new_val = val[: pos - 1] + val[pos:] self.widget.setValue(new_val) def delete(self): """ Simple delete functionality. If no text is selected, deletes the character to the right of the cursor. Otherwise, it deletes the selected text. """ # FIXME: get selection pos = self.widget.getCursorPosition() val = self.widget.getValue() if (len(val) > 0) and 0 <= pos < len(val): new_val = val[:pos] + val[pos + 1 :] self.widget.setValue(new_val) def end(self, mark=False): """ Moves the cursor to the end of the line. Arguments --------- mark : bool, optional If True, select the text from the current position to the end of the line edit. Defaults to False. """ widget = self.widget if mark: start = widget.getCursorPosition() end = len(widget.getValue()) widget.setSelectionRange(start, end) else: end = len(widget.getValue()) widget.setCursorPosition(end) def home(self, mark=False): """ Moves the cursor to the beginning of the line. Arguments --------- mark : bool, optional If True, select the text from the current position to the beginning of the line edit. Defaults to False. """ widget = self.widget if mark: start = 0 end = widget.getCursorPosition() widget.setSelectionRange(start, end) else: widget.setCursorPosition(0) def cut(self): """ Cuts the selected text from the line edit. Copies the selected text to the clipboard then deletes the selected text from the line edit. """ pass def copy(self): """ Copies the selected text to the clipboard. """ pass def paste(self): """ Paste the contents of the clipboard into the line edit. Inserts the contents of the clipboard into the line edit at the current cursor position, replacing any selected text. """ pass def insert(self, text): """ Insert the text into the line edit. Inserts the given text at the current cursor position, replacing any selected text. Arguments --------- text : str The text to insert into the line edit. """ widget = self.widget pos = widget.getCursorPosition() val = widget.getValue() new_val = val[:pos] + text + val[pos:] self.widget.setValue(new_val) def undo(self): """ Undoes the last operation. """ pass def redo(self): """ Redoes the last operation """ pass # -------------------------------------------------------------------------- # Signal Handlers # -------------------------------------------------------------------------- def on_text_edited(self): """ The event handler for when the user edits the text through the ui. """ # The textEdited signal will be emitted along with the # textChanged signal if the user edits from the ui. In # that case, we only want to do one update. if not guard.guarded(self, "updating_text"): with guard(self, "updating_text"): shell = self.shell_obj text = self.widget.getValue() shell.field_text = text shell.text_edited = text shell._modified = True def on_text_changed(self): """ The event handler for when the user edits the text programmatically. """ # The textEdited signal will be emitted along with the # textChanged signal if the user edits from the ui. In # that case, we only want to do one update. if not guard.guarded(self, "updating_text"): with guard(self, "updating_text"): shell = self.shell_obj text = self.widget.getValue() shell.field_text = text def on_return_pressed(self): """ The event handler for the return pressed event. """ self.shell_obj.return_pressed = True def on_selection_changed(self): """ The event handler for a selection event. """ # with guard(self, 'updating_selection'): # self.shell_obj._selected_text = self.widget.selectedText() pass def on_cursor_changed(self): """ The event handler for a cursor change event. """ with guard(self, "updating_cursor"): self.shell_obj.cursor_position = self.widget.getCursorPosition() # -------------------------------------------------------------------------- # Update methods # -------------------------------------------------------------------------- def set_text(self, text): """ Updates the text control with the new text from the shell object. """ self.widget.setValue(text) def set_max_length(self, max_length): """ Set the max length of the control to max_length. If the max length is <= 0 or > 32767 then the control will be set to hold 32kb of text. """ if (max_length <= 0) or (max_length > 32767): max_length = 32767 self.widget.setMaxLength(max_length) def set_read_only(self, read_only): """ Sets read only state of the widget. """ self.widget.setReadOnly(read_only) def set_placeholder_text(self, placeholder_text): """ Sets the placeholder text in the widget. """ self.widget.setInputPrompt(placeholder_text) def set_cursor_position(self, cursor_position): """ Sets the cursor position of the widget. """ self.widget.setCursorPosition(cursor_position) def set_password_mode(self, password_mode): """ Sets the password mode of the wiget. """ self.widget.setSecret(_PASSWORD_MODES[password_mode])
class MuntjacField(MuntjacControl, AbstractTkField): """ A Muntjac implementation of a Field which uses a TextField to provide a single line of editable text. """ #-------------------------------------------------------------------------- # SetupMethods #-------------------------------------------------------------------------- def create(self, parent): """ Creates the underlying TextField. """ self.widget = TextField() parent.addComponent(self.widget) def initialize(self): """ Initializes the attributes of the Muntjac widget. """ super(MuntjacField, self).initialize() shell = self.shell_obj self.set_read_only(shell.read_only) self.set_placeholder_text(shell.placeholder_text) text = shell.field_text if text is not None: self.set_text(text) shell._modified = False self.set_cursor_position(shell.cursor_position) self.set_password_mode(shell.password_mode) self.set_max_length(shell.max_length) def bind(self): """ Binds the event handlers for the TextField. """ super(MuntjacField, self).bind() # widget = self.widget # widget.textEdited.connect(self.on_text_edited) # widget.textChanged.connect(self.on_text_changed) # widget.returnPressed.connect(self.on_return_pressed) # widget.selectionChanged.connect(self.on_selection_changed) # widget.cursorPositionChanged.connect(self.on_cursor_changed) #-------------------------------------------------------------------------- # Shell Object Change Handlers #-------------------------------------------------------------------------- def shell_max_length_changed(self, max_length): """ The change handler for the 'max_length' attribute on the shell object. """ self.set_max_length(max_length) def shell_read_only_changed(self, read_only): """ The change handler for the 'read_only' attribute on the shell object. """ self.set_read_only(read_only) def shell_placeholder_text_changed(self, placeholder_text): """ The change handler for the 'placeholder_text' attribute on the shell object. """ self.set_placeholder_text(placeholder_text) def shell_cursor_position_changed(self, cursor_position): """ The change handler for the 'cursor_position' attribute on the shell object. """ if not guard.guarded(self, 'updating_cursor'): self.set_cursor_position(cursor_position) def shell_field_text_changed(self, text): """ The change handler for the 'field_text' attribute on the shell object. """ if text is not None: if not guard.guarded(self, 'updating_text'): self.set_text(text) self.shell_obj._modified = False def shell_password_mode_changed(self, mode): """ The change handler for the 'password_mode' attribute on the shell object. """ self.set_password_mode(mode) #-------------------------------------------------------------------------- # Manipulation Methods #-------------------------------------------------------------------------- def set_selection(self, start, end): """ Sets the selection in the widget between the start and end positions, inclusive. """ self.widget.setSelectionRange(start, end - start) def select_all(self): """ Select all the text in the line edit. If there is no text in the line edit, the selection will be empty. """ self.widget.selectAll() def deselect(self): """ Deselect any selected text. Sets a selection with start == stop to deselect the current selection. The cursor is placed at the beginning of selection. """ self.widget.setSelectionRange(0, 0) def clear(self): """ Clear the line edit of all text. """ self.widget.setValue('') def backspace(self): """ Simple backspace functionality. If no text is selected, deletes the character to the left of the cursor. Otherwise, it deletes the selected text. """ # FIXME: get selection pos = self.widget.getCursorPosition() val = self.widget.getValue() if (len(val) > 0) and 0 < pos <= len(val): new_val = val[:pos - 1] + val[pos:] self.widget.setValue(new_val) def delete(self): """ Simple delete functionality. If no text is selected, deletes the character to the right of the cursor. Otherwise, it deletes the selected text. """ # FIXME: get selection pos = self.widget.getCursorPosition() val = self.widget.getValue() if (len(val) > 0) and 0 <= pos < len(val): new_val = val[:pos] + val[pos + 1:] self.widget.setValue(new_val) def end(self, mark=False): """ Moves the cursor to the end of the line. Arguments --------- mark : bool, optional If True, select the text from the current position to the end of the line edit. Defaults to False. """ widget = self.widget if mark: start = widget.getCursorPosition() end = len(widget.getValue()) widget.setSelectionRange(start, end) else: end = len(widget.getValue()) widget.setCursorPosition(end) def home(self, mark=False): """ Moves the cursor to the beginning of the line. Arguments --------- mark : bool, optional If True, select the text from the current position to the beginning of the line edit. Defaults to False. """ widget = self.widget if mark: start = 0 end = widget.getCursorPosition() widget.setSelectionRange(start, end) else: widget.setCursorPosition(0) def cut(self): """ Cuts the selected text from the line edit. Copies the selected text to the clipboard then deletes the selected text from the line edit. """ pass def copy(self): """ Copies the selected text to the clipboard. """ pass def paste(self): """ Paste the contents of the clipboard into the line edit. Inserts the contents of the clipboard into the line edit at the current cursor position, replacing any selected text. """ pass def insert(self, text): """ Insert the text into the line edit. Inserts the given text at the current cursor position, replacing any selected text. Arguments --------- text : str The text to insert into the line edit. """ widget = self.widget pos = widget.getCursorPosition() val = widget.getValue() new_val = val[:pos] + text + val[pos:] self.widget.setValue(new_val) def undo(self): """ Undoes the last operation. """ pass def redo(self): """ Redoes the last operation """ pass #-------------------------------------------------------------------------- # Signal Handlers #-------------------------------------------------------------------------- def on_text_edited(self): """ The event handler for when the user edits the text through the ui. """ # The textEdited signal will be emitted along with the # textChanged signal if the user edits from the ui. In # that case, we only want to do one update. if not guard.guarded(self, 'updating_text'): with guard(self, 'updating_text'): shell = self.shell_obj text = self.widget.getValue() shell.field_text = text shell.text_edited = text shell._modified = True def on_text_changed(self): """ The event handler for when the user edits the text programmatically. """ # The textEdited signal will be emitted along with the # textChanged signal if the user edits from the ui. In # that case, we only want to do one update. if not guard.guarded(self, 'updating_text'): with guard(self, 'updating_text'): shell = self.shell_obj text = self.widget.getValue() shell.field_text = text def on_return_pressed(self): """ The event handler for the return pressed event. """ self.shell_obj.return_pressed = True def on_selection_changed(self): """ The event handler for a selection event. """ # with guard(self, 'updating_selection'): # self.shell_obj._selected_text = self.widget.selectedText() pass def on_cursor_changed(self): """ The event handler for a cursor change event. """ with guard(self, 'updating_cursor'): self.shell_obj.cursor_position = self.widget.getCursorPosition() #-------------------------------------------------------------------------- # Update methods #-------------------------------------------------------------------------- def set_text(self, text): """ Updates the text control with the new text from the shell object. """ self.widget.setValue(text) def set_max_length(self, max_length): """ Set the max length of the control to max_length. If the max length is <= 0 or > 32767 then the control will be set to hold 32kb of text. """ if (max_length <= 0) or (max_length > 32767): max_length = 32767 self.widget.setMaxLength(max_length) def set_read_only(self, read_only): """ Sets read only state of the widget. """ self.widget.setReadOnly(read_only) def set_placeholder_text(self, placeholder_text): """ Sets the placeholder text in the widget. """ self.widget.setInputPrompt(placeholder_text) def set_cursor_position(self, cursor_position): """ Sets the cursor position of the widget. """ self.widget.setCursorPosition(cursor_position) def set_password_mode(self, password_mode): """ Sets the password mode of the wiget. """ self.widget.setSecret(_PASSWORD_MODES[password_mode])
class TreeSingleSelectExample(HorizontalLayout, IValueChangeListener, button.IClickListener, action.IHandler): # Actions for the context menu _ACTION_ADD = Action('Add child item') _ACTION_DELETE = Action('Delete') def __init__(self): super(TreeSingleSelectExample, self).__init__() self.setSpacing(True) # Create the Tree,a dd to layout self._tree = Tree('Hardware Inventory') self.addComponent(self._tree) # Contents from a (prefilled example) hierarchical container: self._tree.setContainerDataSource(ExampleUtil.getHardwareContainer()) # Add Valuechangelistener and Actionhandler self._tree.addListener(self, IValueChangeListener) # Add actions (context menu) self._tree.addActionHandler(self) # Cause valueChange immediately when the user selects self._tree.setImmediate(True) # Set tree to show the 'name' property as caption for items self._tree.setItemCaptionPropertyId(ExampleUtil.hw_PROPERTY_NAME) self._tree.setItemCaptionMode( AbstractSelect.ITEM_CAPTION_MODE_PROPERTY) # Expand whole tree for idd in self._tree.rootItemIds(): self._tree.expandItemsRecursively(idd) # Create the 'editor bar' (textfield and button in a horizontallayout) self._editBar = HorizontalLayout() self._editBar.setMargin(False, False, False, True) self._editBar.setEnabled(False) self.addComponent(self._editBar) # textfield self._editor = TextField('Item name') self._editor.setImmediate(True) self._editBar.addComponent(self._editor) # apply-button self._change = Button('Apply', self) #, 'buttonClick') FIXME: listener self._editBar.addComponent(self._change) self._editBar.setComponentAlignment(self._change, Alignment.BOTTOM_LEFT) def valueChange(self, event): if event.getProperty().getValue() is not None: # If something is selected from the tree, get it's 'name' and # insert it into the textfield val = self._tree.getItem( event.getProperty().getValue()).getItemProperty( ExampleUtil.hw_PROPERTY_NAME) self._editor.setValue(val) self._editor.requestRepaint() self._editBar.setEnabled(True) else: self._editor.setValue('') self._editBar.setEnabled(False) def buttonClick(self, event): # If the edited value contains something, set it to be the item's new # 'name' property if not (self._editor.getValue() == ''): item = self._tree.getItem(self._tree.getValue()) name = item.getItemProperty(ExampleUtil.hw_PROPERTY_NAME) name.setValue(self._editor.getValue()) # Returns the set of available actions def getActions(self, target, sender): return [self._ACTION_ADD, self._ACTION_DELETE] # Handle actions def handleAction(self, a, sender, target): if a == self._ACTION_ADD: # Allow children for the target item, and expand it self._tree.setChildrenAllowed(target, True) self._tree.expandItem(target) # Create new item, set parent, disallow children (= leaf node) itemId = self._tree.addItem() self._tree.setParent(itemId, target) self._tree.setChildrenAllowed(itemId, False) # Set the name for this item (we use it as item caption) item = self._tree.getItem(itemId) name = item.getItemProperty(ExampleUtil.hw_PROPERTY_NAME) name.setValue('New Item') elif a == self._ACTION_DELETE: parent = self._tree.getParent(target) self._tree.removeItem(target) # If the deleted object's parent has no more children, set it's # childrenallowed property to false (= leaf node) if parent is not None and len(self._tree.getChildren(parent)) == 0: self._tree.setChildrenAllowed(parent, False)