def __init__(self): super(ShortcutBasicsExample, self).__init__() self.setSpacing(True) # Firstname input with an input prompt for demo clarity firstname = TextField('Firstname') firstname.setInputPrompt('ALT-SHIFT-F to focus') self.addComponent(firstname) # Add global shortcut that focuses the field firstname.addShortcutListener(FocusShortcut(firstname, KeyCode.F, ModifierKey.ALT, ModifierKey.SHIFT)) # Lastname input with an input prompt for demo clarity lastname = TextField('Lastname') lastname.setInputPrompt('ALT-SHIFT-L to focus') self.addComponent(lastname) # Add global shortcut that focuses the field lastname.addShortcutListener(FocusShortcut(lastname, KeyCode.L, ModifierKey.ALT, ModifierKey.SHIFT)) # Button with a simple click-listener enter = Button('Enter', EnterListener(self)) self.addComponent(enter) enter.setStyleName('primary') # make it look like it's default # Add global shortcut using the built-in helper enter.setClickShortcut(KeyCode.ENTER)
def __init__(self): super(TextFieldInputPromptExample, self).__init__() # add some 'air' to the layout self.setSpacing(True) self.setMargin(True, False, False, False) # Username field + input prompt username = TextField() username.setInputPrompt('Username') # configure & add to layout username.setImmediate(True) username.addListener(self, IValueChangeListener) self.addComponent(username) # Password field + input prompt password = PasswordField() password.setInputPrompt('Password') # configure & add to layout password.setImmediate(True) password.addListener(self, IValueChangeListener) self.addComponent(password) # Comment field + input prompt comment = TextArea() comment.setInputPrompt('Comment') # configure & add to layout comment.setRows(3) comment.setImmediate(True) comment.addListener(self, IValueChangeListener) self.addComponent(comment)
def __init__(self): super(ShortcutBasicsExample, self).__init__() self.setSpacing(True) # Firstname input with an input prompt for demo clarity firstname = TextField('Firstname') firstname.setInputPrompt('ALT-SHIFT-F to focus') self.addComponent(firstname) # Add global shortcut that focuses the field firstname.addShortcutListener( FocusShortcut(firstname, KeyCode.F, ModifierKey.ALT, ModifierKey.SHIFT)) # Lastname input with an input prompt for demo clarity lastname = TextField('Lastname') lastname.setInputPrompt('ALT-SHIFT-L to focus') self.addComponent(lastname) # Add global shortcut that focuses the field lastname.addShortcutListener( FocusShortcut(lastname, KeyCode.L, ModifierKey.ALT, ModifierKey.SHIFT)) # Button with a simple click-listener enter = Button('Enter', EnterListener(self)) self.addComponent(enter) enter.setStyleName('primary') # make it look like it's default # Add global shortcut using the built-in helper enter.setClickShortcut(KeyCode.ENTER)
def createPanel(self, number): p = Panel('Panel %d' % number) p.getContent().setSpacing(True) # Let's create a customized shortcut that jumps to the next field p.addAction(NextFieldListener("Next field", KeyCode.ARROW_DOWN, None)) # Firstname input with an input prompt for demo clarity firstname = TextField('Firstname') firstname.setInputPrompt('ALT-SHIFT-F to focus') p.addComponent(firstname) # Using firstname.addShortcutListener() would add globally, # but we want the shortcut only in this panel: p.addAction( FocusShortcut(firstname, KeyCode.F, ModifierKey.ALT, ModifierKey.SHIFT)) # additinally we'll add a global shortcut for this field using the # shorthand notation (^1 == CTRL-1,NextFieldListener etc) firstname.addShortcutListener( FocusShortcut(firstname, 'Focus panel &_' + str(number))) p.setDescription('CTRL-' + str(number) + ' to focus') # Lastname input with an input prompt for demo clarity lastname = TextField('Lastname') lastname.setInputPrompt('ALT-SHIFT-L to focus') p.addComponent(lastname) # Using firstname.addShortcutListener() would add globally, # but we want the shortcut only in this panel: p.addAction( FocusShortcut(lastname, KeyCode.L, ModifierKey.ALT, ModifierKey.SHIFT)) # Button with a simple click-listener save = Button('Save', SaveListener(self, p)) p.addComponent(save) # setClickShortcut() would add global shortcut, instead we # 'scope' the shortcut to the panel: p.addAction( ClickShortcut(save, KeyCode.S, ModifierKey.ALT, ModifierKey.SHIFT)) return p
def createPanel(self, number): p = Panel('Panel %d' % number) p.getContent().setSpacing(True) # Let's create a customized shortcut that jumps to the next field p.addAction(NextFieldListener("Next field", KeyCode.ARROW_DOWN, None)) # Firstname input with an input prompt for demo clarity firstname = TextField('Firstname') firstname.setInputPrompt('ALT-SHIFT-F to focus') p.addComponent(firstname) # Using firstname.addShortcutListener() would add globally, # but we want the shortcut only in this panel: p.addAction(FocusShortcut(firstname, KeyCode.F, ModifierKey.ALT, ModifierKey.SHIFT)) # additinally we'll add a global shortcut for this field using the # shorthand notation (^1 == CTRL-1,NextFieldListener etc) firstname.addShortcutListener(FocusShortcut(firstname, 'Focus panel &_' + str(number))) p.setDescription('CTRL-' + str(number) + ' to focus') # Lastname input with an input prompt for demo clarity lastname = TextField('Lastname') lastname.setInputPrompt('ALT-SHIFT-L to focus') p.addComponent(lastname) # Using firstname.addShortcutListener() would add globally, # but we want the shortcut only in this panel: p.addAction(FocusShortcut(lastname, KeyCode.L, ModifierKey.ALT, ModifierKey.SHIFT)) # Button with a simple click-listener save = Button('Save', SaveListener(self, p)) p.addComponent(save) # setClickShortcut() would add global shortcut, instead we # 'scope' the shortcut to the panel: p.addAction(ClickShortcut(save, KeyCode.S, ModifierKey.ALT, ModifierKey.SHIFT)) return p
def __init__(self): super(ErrorsExample, self).__init__() self.setSpacing(True) self.addComponent( Label('<h3>Errors in caption</h3>', Label.CONTENT_XHTML)) self.addComponent( Label('Error indicators are usually placed on the ' 'right side of the component\'s caption.')) inpt = TextField('Field caption') inpt.setComponentError(UserError('This field is never satisfied')) self.addComponent(inpt) self.addComponent( Label('<h3>Errors without caption</h3>', Label.CONTENT_XHTML)) self.addComponent( Label('If the component has no caption, the error ' 'indicator is usually placed on the right side of the ' 'component.')) inpt = TextField() inpt.setInputPrompt('This field has an error') inpt.setComponentError(UserError('This field is never satisfied.')) self.addComponent(inpt) self.addComponent( Label('<h3>Error icon placement depends on the ' 'layout</h3>', Label.CONTENT_XHTML)) self.addComponent( Label('FormLayout for example places the error ' 'between the component caption and the actual field.')) fl = FormLayout() fl.setMargin(False) fl.setSpacing(False) self.addComponent(fl) inpt = TextField('Field caption') inpt.setInputPrompt('This field has an error') inpt.setComponentError(UserError('This field is never satisfied.')) fl.addComponent(inpt)
def __init__(self): super(ErrorsExample, self).__init__() self.setSpacing(True) self.addComponent(Label('<h3>Errors in caption</h3>', Label.CONTENT_XHTML)) self.addComponent(Label('Error indicators are usually placed on the ' 'right side of the component\'s caption.')) inpt = TextField('Field caption') inpt.setComponentError(UserError('This field is never satisfied')) self.addComponent(inpt) self.addComponent(Label('<h3>Errors without caption</h3>', Label.CONTENT_XHTML)) self.addComponent(Label('If the component has no caption, the error ' 'indicator is usually placed on the right side of the ' 'component.')) inpt = TextField() inpt.setInputPrompt('This field has an error') inpt.setComponentError(UserError('This field is never satisfied.')) self.addComponent(inpt) self.addComponent(Label('<h3>Error icon placement depends on the ' 'layout</h3>', Label.CONTENT_XHTML)) self.addComponent(Label('FormLayout for example places the error ' 'between the component caption and the actual field.')) fl = FormLayout() fl.setMargin(False) fl.setSpacing(False) self.addComponent(fl) inpt = TextField('Field caption') inpt.setInputPrompt('This field has an error') inpt.setComponentError(UserError('This field is never satisfied.')) fl.addComponent(inpt)
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])