def generate_combobox(self, wrapper, option_name, choices, label, value=None): """Generate a combobox (drop-down menu).""" hbox = Gtk.HBox() liststore = Gtk.ListStore(str, str) for choice in choices: if type(choice) is str: choice = [choice, choice] liststore.append(choice) combobox = Gtk.ComboBox.new_with_model(liststore) cell = Gtk.CellRendererText() combobox.pack_start(cell, True) combobox.add_attribute(cell, 'text', 0) index = selected_index = -1 if value: for choice in choices: if choice[1] == value: selected_index = index + 1 break index += 1 combobox.set_active(selected_index) combobox.connect('changed', self.on_combobox_change, option_name) label = Label(label) label.set_alignment(0.5, 0.5) hbox.pack_start(label, False, False, 20) hbox.pack_start(combobox, True, True, 20) hbox.show_all() wrapper.pack_start(hbox, False, False, PADDING)
def generate_file_chooser(self, wrapper, option, path=None): """Generate a file chooser button to select a file.""" option_name = option['option'] label = Label(option['label']) hbox = Gtk.HBox() file_chooser = Gtk.FileChooserButton("Choose a file for %s" % label) file_chooser.set_size_request(200, 30) if 'default_path' in option: config_key = option['default_path'] default_path = self.lutris_config.config['system'].get(config_key) if default_path and os.path.exists(default_path): file_chooser.set_current_folder(default_path) file_chooser.set_action(Gtk.FileChooserAction.OPEN) file_chooser.connect("file-set", self.on_chooser_file_set, option_name) if path: # If path is relative, complete with game dir if not os.path.isabs(path): path = os.path.join(self.game.directory, path) file_chooser.unselect_all() file_chooser.select_filename(path) label.set_alignment(0.5, 0.5) hbox.pack_start(label, False, False, 20) hbox.pack_start(file_chooser, True, True, 20) wrapper.pack_start(hbox, False, False, PADDING)
def on_query_tooltip(self, widget, x, y, keybmode, tooltip, text): """Prepare a custom tooltip with a fixed width""" label = Label(text) label.set_use_markup(True) label.set_max_width_chars(60) hbox = Gtk.HBox() hbox.pack_start(label, False, False, 0) hbox.show_all() tooltip.set_custom(hbox) return True
def generate_editable_grid(self, option_name, label, value=None): value = value or {} value = list(value.items()) label = Label(label) label.set_alignment(0.5, 0) grid = EditableGrid(value, columns=["Key", "Value"]) grid.connect('changed', self.on_grid_changed, option_name) self.wrapper.pack_start(label, False, False, 0) self.wrapper.pack_start(grid, True, True, 0) self.option_widget = grid
def generate_entry(self, option_name, label, value=None): """Generate an entry box.""" label = Label(label) entry = Gtk.Entry() if value: entry.set_text(value) entry.connect("changed", self.entry_changed, option_name) label.set_alignment(0.5, 0.5) self.wrapper.pack_start(label, False, False, 0) self.wrapper.pack_start(entry, True, True, 0) self.option_widget = entry
def generate_multiple_file_chooser(self, option_name, label, value=None): """Generate a multiple file selector.""" vbox = Gtk.VBox() label = Label(label + ':') self.files_chooser_dialog = Gtk.FileChooserDialog( title="Select files", parent=None, action=Gtk.FileChooserAction.OPEN, buttons=(Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE, Gtk.STOCK_ADD, Gtk.ResponseType.OK) ) self.files_chooser_dialog.set_select_multiple(True) files_chooser_button = Gtk.FileChooserButton(self.files_chooser_dialog) files_chooser_button.connect('file-set', self.add_files_callback, option_name) game_path = self.config.get('game_path', os.path.expanduser('~')) if game_path: files_chooser_button.set_current_folder(game_path) if value: files_chooser_button.set_current_folder(os.path.dirname(value[0])) label.set_halign(Gtk.Align.START) files_chooser_button.set_margin_left(10) files_chooser_button.set_margin_right(10) vbox.pack_start(label, False, False, 5) vbox.pack_start(files_chooser_button, False, False, 0) if value: if type(value) == str: self.files = [value] else: self.files = value else: self.files = [] self.files_list_store = Gtk.ListStore(str) for filename in self.files: self.files_list_store.append([filename]) cell_renderer = Gtk.CellRendererText() files_treeview = Gtk.TreeView(self.files_list_store) files_column = Gtk.TreeViewColumn("Files", cell_renderer, text=0) files_treeview.append_column(files_column) files_treeview.connect('key-press-event', self.on_files_treeview_event, option_name) treeview_scroll = Gtk.ScrolledWindow() treeview_scroll.set_min_content_height(130) treeview_scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) treeview_scroll.add(files_treeview) treeview_scroll.set_margin_left(10) treeview_scroll.set_margin_right(10) vbox.pack_start(treeview_scroll, True, True, 0) self.wrapper.pack_start(vbox, True, True, 0) self.option_widget = self.files_list_store
def generate_entry(self, wrapper, option_name, label, value=None): """Generate an entry box.""" hbox = Gtk.HBox() label = Label(label) entry = Gtk.Entry() if value: entry.set_text(value) entry.connect("changed", self.entry_changed, option_name) label.set_alignment(0.5, 0.5) hbox.pack_start(label, False, False, 20) hbox.pack_start(entry, True, True, 20) hbox.show_all() wrapper.pack_start(hbox, False, False, PADDING)
def generate_directory_chooser(self, option_name, label_text, value=None): """Generate a file chooser button to select a directory.""" label = Label(label_text) directory_chooser = FileChooserEntry( title='Select folder', action=Gtk.FileChooserAction.SELECT_FOLDER, default_path=reverse_expanduser(value)) directory_chooser.entry.connect('changed', self.on_chooser_dir_set, option_name) directory_chooser.set_valign(Gtk.Align.CENTER) label.set_alignment(0.5, 0.5) self.wrapper.pack_start(label, False, False, 0) self.wrapper.pack_start(directory_chooser, True, True, 0) self.option_widget = directory_chooser
def generate_multiple_file_chooser(self, wrapper, option_name, label, value=None): """Generate a multiple file selector.""" hbox = Gtk.HBox() label = Label(label) self.files_chooser_dialog = Gtk.FileChooserDialog( title="Select files", parent=self.get_parent_window(), action=Gtk.FileChooserAction.OPEN, buttons=(Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE, Gtk.STOCK_ADD, Gtk.ResponseType.OK)) self.files_chooser_dialog.set_select_multiple(True) files_chooser_button = Gtk.FileChooserButton(self.files_chooser_dialog) files_chooser_button.connect('file-set', self.add_files_callback, option_name) game_path = self.lutris_config.get_path(os.path.expanduser('~')) if game_path: files_chooser_button.set_current_folder(game_path) if value: files_chooser_button.set_filename(value[0]) label.set_alignment(0.5, 0.5) hbox.pack_start(label, False, False, 20) hbox.pack_start(files_chooser_button, True, True, 20) wrapper.pack_start(hbox, False, False, PADDING) if value: if type(value) == str: self.files = [value] else: self.files = value else: self.files = [] self.files_list_store = Gtk.ListStore(str) for filename in self.files: self.files_list_store.append([filename]) cell_renderer = Gtk.CellRendererText() files_treeview = Gtk.TreeView(self.files_list_store) files_column = Gtk.TreeViewColumn("Files", cell_renderer, text=0) files_treeview.append_column(files_column) files_treeview.connect('key-press-event', self.on_files_treeview_event, option_name) treeview_scroll = Gtk.ScrolledWindow() treeview_scroll.set_min_content_height(200) treeview_scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) treeview_scroll.add(files_treeview) wrapper.add(treeview_scroll)
def generate_directory_chooser(self, option_name, label_text, value=None): """Generate a file chooser button to select a directory.""" label = Label(label_text) directory_chooser = FileChooserEntry( action=Gtk.FileChooserAction.SELECT_FOLDER, default=reverse_expanduser(value) ) directory_chooser.entry.connect('changed', self.on_chooser_dir_set, option_name) directory_chooser.set_valign(Gtk.Align.CENTER) label.set_alignment(0.5, 0.5) self.wrapper.pack_start(label, False, False, 0) self.wrapper.pack_start(directory_chooser, True, True, 0) self.option_widget = directory_chooser
def generate_label(self, text): """Generate a simple label.""" label = Label(text) label.set_use_markup(True) label.set_halign(Gtk.Align.START) label.set_valign(Gtk.Align.CENTER) self.wrapper.pack_start(label, True, True, 0)
def generate_range(self, option_name, min_val, max_val, label, value=None): """Generate a ranged spin button.""" adjustment = Gtk.Adjustment(float(min_val), float(min_val), float(max_val), 1, 0, 0) spin_button = Gtk.SpinButton() spin_button.set_adjustment(adjustment) if value: spin_button.set_value(value) spin_button.connect('changed', self.on_spin_button_changed, option_name) label = Label(label) label.set_alignment(0.5, 0.5) self.wrapper.pack_start(label, False, False, 0) self.wrapper.pack_start(spin_button, True, True, 0) self.option_widget = spin_button
def generate_multiple_file_chooser(self, wrapper, option_name, label, value=None): """Generate a multiple file selector.""" hbox = Gtk.HBox() label = Label(label) self.files_chooser_dialog = Gtk.FileChooserDialog( title="Select files", parent=self.get_parent_window(), action=Gtk.FileChooserAction.OPEN, buttons=(Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE, Gtk.STOCK_ADD, Gtk.ResponseType.OK) ) self.files_chooser_dialog.set_select_multiple(True) files_chooser_button = Gtk.FileChooserButton(self.files_chooser_dialog) files_chooser_button.connect('file-set', self.add_files_callback, option_name) game_path = self.lutris_config.get_path(os.path.expanduser('~')) if game_path: files_chooser_button.set_current_folder(game_path) if value: files_chooser_button.set_filename(value[0]) label.set_alignment(0.5, 0.5) hbox.pack_start(label, False, False, 20) hbox.pack_start(files_chooser_button, True, True, 20) wrapper.pack_start(hbox, False, False, PADDING) if value: if type(value) == str: self.files = [value] else: self.files = value else: self.files = [] self.files_list_store = Gtk.ListStore(str) for filename in self.files: self.files_list_store.append([filename]) cell_renderer = Gtk.CellRendererText() files_treeview = Gtk.TreeView(self.files_list_store) files_column = Gtk.TreeViewColumn("Files", cell_renderer, text=0) files_treeview.append_column(files_column) files_treeview.connect('key-press-event', self.on_files_treeview_event, option_name) treeview_scroll = Gtk.ScrolledWindow() treeview_scroll.set_min_content_height(200) treeview_scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) treeview_scroll.add(files_treeview) wrapper.add(treeview_scroll)
def generate_directory_chooser(self, wrapper, option_name, label_text, value=None): """Generate a file chooser button to select a directory.""" hbox = Gtk.HBox() label = Label(label_text) directory_chooser = Gtk.FileChooserButton( title="Choose a directory for %s" % label_text ) directory_chooser.set_action(Gtk.FileChooserAction.SELECT_FOLDER) if value: directory_chooser.set_current_folder(value) directory_chooser.connect("file-set", self.on_chooser_file_set, option_name) label.set_alignment(0.5, 0.5) hbox.pack_start(label, False, False, 20) hbox.pack_start(directory_chooser, True, True, 20) wrapper.pack_start(hbox, False, False, PADDING)
def generate_range(self, wrapper, option_name, min_val, max_val, label, value=None): """Generate a ranged spin button.""" adjustment = Gtk.Adjustment(float(min_val), float(min_val), float(max_val), 1, 0, 0) spin_button = Gtk.SpinButton() spin_button.set_adjustment(adjustment) if value: spin_button.set_value(value) spin_button.connect('changed', self.on_spin_button_changed, option_name) hbox = Gtk.HBox() label = Label(label) label.set_alignment(0.5, 0.5) hbox.pack_start(label, False, False, 20) hbox.pack_start(spin_button, True, True, 20) hbox.show_all() wrapper.pack_start(hbox, False, False, PADDING)
def generate_combobox(self, option_name, choices, label, value=None, default=None, has_entry=False): """Generate a combobox (drop-down menu).""" liststore = Gtk.ListStore(str, str) for choice in choices: if type(choice) is str: choice = [choice, choice] if choice[1] == default: liststore.append([choice[0] + " (default)", default]) self.tooltip_default = choice[0] else: liststore.append(choice) # With entry ("choice_with_entry" type) if has_entry: combobox = Gtk.ComboBox.new_with_model_and_entry(liststore) combobox.set_entry_text_column(1) if value: combobox.get_child().set_text(value) # No entry ("choice" type) else: combobox = Gtk.ComboBox.new_with_model(liststore) cell = Gtk.CellRendererText() combobox.pack_start(cell, True) combobox.add_attribute(cell, 'text', 0) combobox.set_id_column(1) choices = list(v for k, v in choices) if value in choices: combobox.set_active_id(value) else: combobox.set_active_id(default) combobox.connect('changed', self.on_combobox_change, option_name) label = Label(label) label.set_alignment(0.5, 0.5) combobox.set_valign(Gtk.Align.CENTER) self.wrapper.pack_start(label, False, False, 0) self.wrapper.pack_start(combobox, True, True, 0) self.option_widget = combobox
def generate_directory_chooser(self, wrapper, option_name, label_text, value=None): """Generate a file chooser button to select a directory.""" hbox = Gtk.HBox() label = Label(label_text) directory_chooser = Gtk.FileChooserButton( title="Choose a directory for %s" % label_text) directory_chooser.set_action(Gtk.FileChooserAction.SELECT_FOLDER) if value: directory_chooser.set_current_folder(value) directory_chooser.connect("file-set", self.on_chooser_file_set, option_name) label.set_alignment(0.5, 0.5) hbox.pack_start(label, False, False, 20) hbox.pack_start(directory_chooser, True, True, 20) wrapper.pack_start(hbox, False, False, PADDING)
def generate_multiple_file_chooser(self, option_name, label, value=None): """Generate a multiple file selector.""" vbox = Gtk.VBox() label = Label(label + ':') label.set_halign(Gtk.Align.START) button = Gtk.Button('Add files') button.connect('clicked', self.on_add_files_clicked, option_name, value) button.set_margin_left(10) vbox.pack_start(label, False, False, 5) vbox.pack_end(button, False, False, 0) if value: if type(value) == str: self.files = [value] else: self.files = value else: self.files = [] self.files_list_store = Gtk.ListStore(str) for filename in self.files: self.files_list_store.append([filename]) cell_renderer = Gtk.CellRendererText() files_treeview = Gtk.TreeView(self.files_list_store) files_column = Gtk.TreeViewColumn("Files", cell_renderer, text=0) files_treeview.append_column(files_column) files_treeview.connect('key-press-event', self.on_files_treeview_keypress, option_name) treeview_scroll = Gtk.ScrolledWindow() treeview_scroll.set_min_content_height(130) treeview_scroll.set_margin_left(10) treeview_scroll.set_shadow_type(Gtk.ShadowType.ETCHED_IN) treeview_scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) treeview_scroll.add(files_treeview) vbox.pack_start(treeview_scroll, True, True, 0) self.wrapper.pack_start(vbox, True, True, 0) self.option_widget = self.files_list_store
def generate_widgets(self, config_section): """Parse the config dict and generates widget accordingly.""" if not self.options: label = Label("No options available") label.set_halign(Gtk.Align.CENTER) label.set_valign(Gtk.Align.CENTER) self.pack_start(label, True, True, 0) return # Select config section. if config_section == 'game': self.config = self.lutris_config.game_config self.raw_config = self.lutris_config.raw_game_config elif config_section == 'runner': self.config = self.lutris_config.runner_config self.raw_config = self.lutris_config.raw_runner_config elif config_section == 'system': self.config = self.lutris_config.system_config self.raw_config = self.lutris_config.raw_system_config # Go thru all options. for option in self.options: if 'scope' in option: if config_section not in option['scope']: continue option_key = option['option'] value = self.config.get(option_key) default = option.get('default') if callable(option.get('choices')): option['choices'] = option['choices']() if callable(option.get('condition')): option['condition'] = option['condition']() hbox = Gtk.HBox() hbox.set_margin_left(20) self.wrapper = Gtk.HBox() self.wrapper.set_spacing(20) placeholder = Gtk.HBox() placeholder.set_size_request(32, 32) hbox.pack_end(placeholder, False, False, 5) # Set tooltip's "Default" part default = option.get('default') self.tooltip_default = default if type(default) is str else None # Generate option widget self.option_widget = None self.call_widget_generator(option, option_key, value, default) # Reset button reset_btn = Gtk.Button.new_from_icon_name('edit-clear', Gtk.IconSize.MENU) reset_btn.set_relief(Gtk.ReliefStyle.NONE) reset_btn.set_tooltip_text("Reset option to global or " "default config") reset_btn.connect('clicked', self.on_reset_button_clicked, option, self.option_widget, self.wrapper) if option_key not in self.raw_config: reset_btn.set_visible(False) reset_btn.set_no_show_all(True) placeholder.pack_start(reset_btn, False, False, 0) # Tooltip helptext = option.get("help") if type(self.tooltip_default) is str: helptext = helptext + '\n\n' if helptext else '' helptext += "<b>Default</b>: " + self.tooltip_default if value != default and option_key not in self.raw_config: helptext = helptext + '\n\n' if helptext else '' helptext += ("<i>(Italic indicates that this option is " "modified in a lower configuration level.)</i>") if helptext: self.wrapper.props.has_tooltip = True self.wrapper.connect('query-tooltip', self.on_query_tooltip, helptext) # Grey out option if condition unmet if 'condition' in option and not option['condition']: hbox.set_sensitive(False) # Hide if advanced if option.get('advanced'): hbox.get_style_context().add_class('advanced') show_advanced = settings.read_setting('show_advanced_options') if not show_advanced == 'True': hbox.set_no_show_all(True) hbox.pack_start(self.wrapper, True, True, 0) self.pack_start(hbox, False, False, 5)
def generate_label(self, wrapper, text): """Generate a simple label.""" label = Label(text) label.show() wrapper.pack_start(label, False, False, PADDING)