def __init__(self, action, label, shortcut=None, tooltip=None, icon=None, group=None, section=None, order=None, factory=None): self.action = wrapped_handler(self, action) self.label = label self.shortcut = shortcut self.tooltip = tooltip self.icon_id = icon self.group = group if group else Group.COMMANDS self.section = section if section else 0 self.order = order if order else 0 self._enabled = self.action is not None self._widgets = [] self.factory = get_platform_factory() self._impl = self.factory.Command(interface=self)
def create_toolbar(self): if self.toolbar_items is None: self.toolbar_native = Gtk.Toolbar() self.toolbar_items = {} else: for cmd, item_impl in self.toolbar_items.items(): self.toolbar_native.remove(item_impl) cmd._impl._widgets.remove(item_impl) self.toolbar_native.set_style(Gtk.ToolbarStyle.BOTH) for cmd in self.interface.toolbar: if cmd == GROUP_BREAK: item_impl = Gtk.SeparatorToolItem() item_impl.set_draw(True) elif cmd == SECTION_BREAK: item_impl = Gtk.SeparatorToolItem() item_impl.set_draw(False) else: item_impl = Gtk.ToolButton() cmd_impl = cmd.bind(self.interface.factory) icon_impl = cmd_impl.icon.bind(self.interface.factory) item_impl.set_icon_widget(icon_impl.native_32) item_impl.set_label(cmd.label) item_impl.set_tooltip_text(cmd.tooltip) item_impl.connect("clicked", wrapped_handler(cmd, cmd.action)) cmd._widgets.append(item_impl) self.toolbar_items[cmd] = item_impl self.toolbar_native.insert(item_impl, -1)
def on_draw(self, handler): """Set the handler to invoke when the canvas is drawn. Args: handler (:obj:`callable`): The handler to invoke when the canvas is drawn. """ self._on_draw = wrapped_handler(self, handler)
def select_folder_dialog(self, title, initial_directory=None, multiselect=False, on_result=None): """ This opens a native dialog where the user can select a folder. It is possible to set the initial folder. If no path is returned (eg. dialog is canceled), a ValueError is raised. Args: title (str): The title of the dialog window. initial_directory(str): Initial folder displayed in the dialog. multiselect (bool): Value showing whether a user can select multiple files. Returns: A list of absolute paths(str) if multiselect is True, a single path(str) otherwise. Returns None if no folder is selected. """ return self.factory.dialogs.SelectFolderDialog( self, title, initial_directory=Path(initial_directory) if initial_directory else None, multiselect=multiselect, on_result=wrapped_handler(self, on_result), )
def create_toolbar(self): if self.toolbar_items is None: self.toolbar_native = Gtk.Toolbar() self.toolbar_items = {} else: for cmd, item_impl in self.toolbar_items.items(): self.toolbar_native.remove(item_impl) cmd._impl.native.remove(item_impl) self.toolbar_native.set_style(Gtk.ToolbarStyle.BOTH) for cmd in self.interface.toolbar: if cmd == GROUP_BREAK: item_impl = Gtk.SeparatorToolItem() item_impl.set_draw(True) elif cmd == SECTION_BREAK: item_impl = Gtk.SeparatorToolItem() item_impl.set_draw(False) else: item_impl = Gtk.ToolButton() icon_impl = cmd.icon.bind(self.interface.factory) item_impl.set_icon_widget(icon_impl.native_32) item_impl.set_label(cmd.label) item_impl.set_tooltip_text(cmd.tooltip) item_impl.connect("clicked", wrapped_handler(cmd, cmd.action)) cmd._impl.native.append(item_impl) self.toolbar_items[cmd] = item_impl self.toolbar_native.insert(item_impl, -1)
def __init__(self, action, label, shortcut=None, tooltip=None, icon=None, group=None, section=None, order=None, enabled=True, factory=None): self.factory = factory self.action = wrapped_handler(self, action) self.label = label self.shortcut = shortcut self.tooltip = tooltip self.icon = icon self.group = group if group else Group.COMMANDS self.section = section if section else 0 self.order = order if order else 0 self._impl = None self.enabled = enabled and self.action is not None
def open_file_dialog(self, title, initial_directory=None, file_types=None, multiselect=False, on_result=None): """ This opens a native dialog where the user can select the file to open. It is possible to set the initial folder and only show files with specified file extensions. If no path is returned (eg. dialog is canceled), a ValueError is raised. Args: title (str): The title of the dialog window. initial_directory(str): Initial folder displayed in the dialog. file_types: A list of strings with the allowed file extensions. multiselect: Value showing whether a user can select multiple files. Returns: A list of absolute paths(str) if multiselect is True, a single path(str) otherwise. Returns None if no file is selected. """ return self.factory.dialogs.OpenFileDialog( self, title, initial_directory=Path(initial_directory) if initial_directory else None, file_types=file_types, multiselect=multiselect, on_result=wrapped_handler(self, on_result))
def save_file_dialog(self, title, suggested_filename, file_types=None, on_result=None): """ This opens a native dialog where the user can select a place to save a file. It is possible to suggest a filename and force the user to use a specific file extension. If no path is returned (eg. dialog is canceled), a ValueError is raised. Args: title (str): The title of the dialog window. suggested_filename(str): The automatically filled in filename. file_types: A list of strings with the allowed file extensions. Returns: The absolute path(str) to the selected location. May be None. """ # Convert suggested filename to a path (if it isn't already), # and break it into a filename and a directory suggested_path = Path(suggested_filename) initial_directory = suggested_path.parent if initial_directory == Path("."): initial_directory = None filename = suggested_path.name return self.factory.dialogs.SaveFileDialog( self, title, filename=filename, initial_directory=initial_directory, file_types=file_types, on_result=wrapped_handler(self, on_result), )
def stack_trace_dialog(self, title, message, content, retry=False, on_result=None): """ Calling this function opens a dialog that allows to display a large text body in a scrollable fashion. Args: title (str): The title of the dialog window. message (str): The dialog message to display. content (str): retry (bool): Returns: Returns `None` after the user pressed the 'OK' button. """ return self.factory.dialogs.StackTraceDialog( self, title, message, content=content, retry=retry, on_result=wrapped_handler(self, on_result), )
def on_exit(self, handler): """Set the handler to invoke before the app exits. Args: handler (:obj:`callable`): The handler to invoke before the app exits. """ self._on_exit = wrapped_handler(self, handler) self._impl.set_on_exit(self._on_exit)
def on_resize(self, handler): """Set the handler to invoke when the canvas is resized. Args: handler (:obj:`callable`): The handler to invoke when the canvas is resized. """ self._on_resize = wrapped_handler(self, handler) self._impl.set_on_resize(self._on_resize)
def on_change(self, handler): """Set the handler to invoke when the value is changeed. Args: handler (:obj:`callable`): The handler to invoke when the value is changeed. """ self._on_change = wrapped_handler(self, handler) self._impl.set_on_change(self._on_change)
def on_change(self, handler): """Set the handler to invoke when the value is changed. Args: handler (:obj:`callable`): The handler to invoke when the value is changed. """ self._on_change = wrapped_handler(self, handler) self._impl.set_on_change(self._on_change)
def on_close(self, handler): """Set the handler to invoke when the window is closed. Args: handler (:obj:`callable`): The handler to invoke when the window is closing. """ self._on_close = wrapped_handler(self, handler) self._impl.set_on_close(self._on_close)
def on_key_down(self, handler): """Set the handler to invoke when a key is pressed. Args: handler (:obj:`callable`): The handler to invoke when a key is pressed. """ self._on_key_down = wrapped_handler(self, handler) self._impl.set_on_key_down(self._on_key_down)
def on_webview_load(self, handler): """Set the handler to invoke when the button is pressed. Args: handler (:obj:`callable`): The handler to invoke when the button is pressed. """ self._on_webview_load = wrapped_handler(self, handler) self._impl.set_on_webview_load(self._on_webview_load)
def on_double_click(self, handler): """ Set the function to be executed on node double click :param handler: callback function :type handler: ``callable`` """ self._on_double_click = wrapped_handler(self, handler) self._impl.set_on_double_click(self._on_double_click)
def on_select(self, handler): """ Set the function to be executed on option selection :param handler: callback function :type handler: ``callable`` """ self._on_select = wrapped_handler(self, handler) self._impl.set_on_select(self._on_select)
def on_select(self, handler): """ Set the function to be executed on node select :param handler: callback function :type handler: ``callable`` """ self._on_select = wrapped_handler(self, handler) self._impl.set_on_select(self._on_select)
def on_drag(self, handler): """Set the handler to invoke when the mouse button is dragged with the primary (usually the left) button pressed. Args: handler (:obj:`callable`): The handler to invoke when the mouse is dragged with the primary button pressed. """ self._on_drag = wrapped_handler(self, handler) self._impl.set_on_drag(self._on_drag)
def on_alt_release(self, handler): """Set the handler to invoke when the alternate (usually the right) mouse button is released. Args: handler (:obj:`callable`): The handler to invoke when the alternate mouse button is released. """ self._on_alt_release = wrapped_handler(self, handler) self._impl.set_on_alt_release(self._on_alt_release)
def on_release(self, handler): """Set the handler to invoke when the primary (usually the left) mouse button is released. Args: handler (:obj:`callable`): The handler to invoke when the primary mouse button is released. """ self._on_release = wrapped_handler(self, handler) self._impl.set_on_release(self._on_release)
def on_alt_drag(self, handler): """Set the handler to invoke when the mouse is dragged with the alternate (usually the right) button pressed. Args: handler (:obj:`callable`): The handler to invoke when the mouse is dragged with the alternate button pressed. """ self._on_alt_drag = wrapped_handler(self, handler) self._impl.set_on_alt_drag(self._on_alt_drag)
def on_close(self, handler): """Set the handler to invoke when before window is closed. If the handler returns ``False``, the window will not be closed. This can be used for example for confirmation dialogs. Args: handler (:obj:`callable`): The handler to invoke before the window is closed. """ self._on_close = wrapped_handler(self, handler) self._impl.set_on_close(self._on_close)
def add_background_task(self, handler): """Schedule a task to run in the background. Schedules a coroutine or a generator to run in the background. Control will be returned to the event loop during await or yield statements, respectively. Use this to run background tasks without blocking the GUI. If a regular callable is passed, it will be called as is and will block the GUI until the call returns. :param handler: A coroutine, generator or callable. """ self._impl.add_background_task(wrapped_handler(self, handler))
def on_exit(self, handler): """Set the handler to invoke before the app exits. Args: handler (:obj:`callable`): The handler to invoke before the app exits. """ def cleanup(app, should_exit): if should_exit: app._impl.exit() self._on_exit = wrapped_handler(self, handler, cleanup=cleanup) self._impl.set_on_exit(self._on_exit)
def question_dialog(self, title, message, on_result=None): """ Opens a dialog with a 'YES' and 'NO' button. Args: title (str): The title of the dialog window. message (str): The dialog message to display. Returns: Returns `True` when the 'YES' button was pressed, `False` when the 'NO' button was pressed. """ return self.factory.dialogs.QuestionDialog(self, title, message, on_result=wrapped_handler( self, on_result))
def confirm_dialog(self, title, message, on_result=None): """ Opens a dialog with a 'Cancel' and 'OK' button. Args: title (str): The title of the dialog window. message (str): The dialog message to display. Returns: Returns `True` when the 'OK' button was pressed, `False` when the 'CANCEL' button was pressed. """ return self.factory.dialogs.ConfirmDialog(self, title, message, on_result=wrapped_handler( self, on_result))
def error_dialog(self, title, message, on_result=None): """ Opens a error dialog with a 'OK' button to close the dialog. Args: title (str): The title of the dialog window. message (str): The dialog message to display. Returns: Returns `None` after the user pressed the 'OK' button. """ return self.factory.dialogs.ErrorDialog(self, title, message, on_result=wrapped_handler( self, on_result))
def action(self, action): if self._checkable: def new_action(*args): self.checked = not self.checked if action: action(args) else: new_action = action self._action = wrapped_handler(self, new_action) self._impl.set_action(self._action) if self._action: self.enabled = True
def __init__( self, label, url=None, locate=False, id=None, style=None, enabled=True, factory=private_factory, ): icon = Icon(template=ImageTemplate.FollowLink) self.url = url self.locate = locate super().__init__(label, icon=icon, id=id, enabled=enabled, style=style, factory=factory) def handler(widget): click.launch(widget.url, locate=widget.locate) self._on_press = wrapped_handler(self, handler)
def on_select(self, handler: callable): self._on_select = wrapped_handler(self, handler) self._impl.set_on_select(self._on_select)
def on_toggle(self, handler): self._on_toggle = wrapped_handler(self, handler) self._impl.set_on_toggle(self._on_toggle)
def add_background_task(self, handler): self.loop.call_soon(wrapped_handler(self, handler), self)
def create_menus(self): # Only create the menu if the menu item index has been created. if hasattr(self, '_actions'): self._actions = {} menubar = Gio.Menu() label = None submenu = None section = None for cmd in self.interface.commands: if cmd == GROUP_BREAK: if section: submenu.append_section(None, section) if label == '*': self.native.set_app_menu(submenu) else: menubar.append_submenu(label, submenu) label = None submenu = None section = None elif cmd == SECTION_BREAK: submenu.append_section(None, section) section = None else: if submenu is None: label = cmd.group.label submenu = Gio.Menu() if section is None: section = Gio.Menu() try: action = self._actions[cmd] except KeyError: cmd_id = "command-%s" % id(cmd) action = Gio.SimpleAction.new(cmd_id, None) if cmd.action: action.connect("activate", wrapped_handler(cmd, cmd.action)) cmd._widgets.append(action) self._actions[cmd] = action self.native.add_action(action) # cmd.bind(self.interface.factory).set_enabled(cmd.enabled) item = Gio.MenuItem.new(cmd.label, 'app.' + cmd_id) if cmd.shortcut: item.set_attribute_value('accel', GLib.Variant('s', '<Primary>%s' % cmd.shortcut.upper())) # item.set_attribute_value('accel', GLib.Variant(cmd.shortcut, '<Primary>%s' % cmd.shortcut.upper())) section.append_item(item) if section: submenu.append_section(None, section) if submenu: if label == '*': self.native.set_app_menu(submenu) else: menubar.append_submenu(label, submenu) # Set the menu for the app. self.native.set_menubar(menubar)
def on_slide(self, handler): self._on_slide = wrapped_handler(self, handler) self._impl.set_on_slide(self._on_slide)
def on_delete(self, handler: callable): self._on_delete = wrapped_handler(self, handler) self._impl.set_on_delete(self._on_delete)
def on_refresh(self, handler: callable): self._on_refresh = wrapped_handler(self, handler, self._impl.after_on_refresh) self._impl.set_on_refresh(self._on_refresh)