use model bindings for non-static ListBoxes

This commit is contained in:
Peter Eisenmann
2022-03-26 01:29:02 +01:00
parent f3f37fd5ae
commit 092bf07eee
4 changed files with 55 additions and 40 deletions

View File

@@ -2,14 +2,14 @@
from threading import Lock
from gi.repository import Gtk
from gi.repository import Gio, Gtk
from .disk_provider import disk_provider
from .global_state import global_state
from .installation_scripting import installation_scripting
from .page import Page
from .system_calls import is_booted_with_uefi, open_disks
from .widgets import DeviceRow, NoPartitionsRow, empty_list
from .widgets import DeviceRow, NoPartitionsRow
GIGABYTE_FACTOR = 1024 * 1024 * 1024
@@ -23,6 +23,7 @@ class DiskPage(Gtk.Box, Page):
text_stack = Gtk.Template.Child()
disk_list = Gtk.Template.Child()
disk_list_model = Gio.ListStore()
whole_disk_list = Gtk.Template.Child()
disk_size = Gtk.Template.Child()
@@ -30,6 +31,7 @@ class DiskPage(Gtk.Box, Page):
disk_device_path = Gtk.Template.Child()
partition_list = Gtk.Template.Child()
partition_list_model = Gio.ListStore()
settings_button = Gtk.Template.Child()
refresh_button = Gtk.Template.Child()
@@ -47,6 +49,10 @@ class DiskPage(Gtk.Box, Page):
self.partition_list.connect('row-activated', self._on_partition_row_activated)
self.whole_disk_list.connect('row-activated', self._use_whole_disk)
# models
self.disk_list.bind_model(self.disk_list_model, lambda x: x)
self.partition_list.bind_model(self.partition_list_model, lambda x: x)
self.settings_button.connect('clicked', self._on_clicked_disks_button)
self.refresh_button.connect('clicked', self._on_clicked_reload_button)
@@ -55,15 +61,14 @@ class DiskPage(Gtk.Box, Page):
self.text_stack.set_visible_child_name(state)
def _setup_disk_list(self):
# clear list
empty_list(self.disk_list)
# fill list
disks = disk_provider.get_disks()
for disk_info in disks:
disk_rows = []
for disk_info in disk_provider.get_disks():
too_small = disk_info.size < self.minimum_disk_size
row = DeviceRow(disk_info, too_small)
self.disk_list.append(row)
disk_rows.append(DeviceRow(disk_info, too_small))
n_items = self.disk_list_model.get_n_items()
self.disk_list_model.splice(0, n_items, disk_rows)
# show
self._set_stacks('disks')
@@ -71,23 +76,26 @@ class DiskPage(Gtk.Box, Page):
def _setup_partition_list(self, disk_info):
self.current_disk = disk_info
empty_list(self.partition_list)
# set disk info
self.disk_label.set_label(disk_info.name)
self.disk_device_path.set_label(disk_info.device_path)
self.disk_size.set_label(disk_info.size_text)
# fill partition list
partition_rows = []
disk_uefi_okay = not is_booted_with_uefi() or disk_info.efi_partition
if disk_uefi_okay and len(disk_info.partitions) > 0:
self.partition_list.set_sensitive(True)
for partition_info in disk_info.partitions:
too_small = partition_info.size < self.minimum_disk_size
row = DeviceRow(partition_info, too_small)
self.partition_list.append(row)
partition_rows.append(DeviceRow(partition_info, too_small))
else:
self.partition_list.set_sensitive(False)
self.partition_list.append(NoPartitionsRow())
partition_rows = [NoPartitionsRow()]
n_items = self.partition_list_model.get_n_items()
self.partition_list_model.splice(0, n_items, partition_rows)
# show
self._set_stacks('partitions')

View File

@@ -1,14 +1,13 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from gi.repository import Gtk
from gi.repository import Gio, Gtk
from .global_state import global_state
from .keyboard_layout_provider import get_layouts_for
from .language_provider import language_provider
from .page import Page
from .system_calls import set_system_keyboard_layout
from .widgets import LanguageRow, SelectionRow, empty_list
from .widgets import LanguageRow, SelectionRow
@Gtk.Template(resource_path='/com/github/p3732/os-installer/ui/pages/keyboard_layout.ui')
class KeyboardLayoutPage(Gtk.Box, Page):
@@ -21,6 +20,7 @@ class KeyboardLayoutPage(Gtk.Box, Page):
stack = Gtk.Template.Child()
language_list = Gtk.Template.Child()
layout_list = Gtk.Template.Child()
layout_list_model = Gio.ListStore()
continue_button = Gtk.Template.Child()
@@ -37,6 +37,9 @@ class KeyboardLayoutPage(Gtk.Box, Page):
self.language_list.connect('row-activated', self._on_language_row_activated)
self.layout_list.connect('row-activated', self._on_layout_row_activated)
# models
self.layout_list.bind_model(self.layout_list_model, lambda x: x)
def _setup_languages_list(self):
all_languages = language_provider.get_all_languages()
@@ -45,22 +48,23 @@ class KeyboardLayoutPage(Gtk.Box, Page):
self.language_list.append(row)
def _load_layout_list(self, language, short_hand):
self.stack.set_visible_child_name('layouts')
if self.loaded_language == short_hand:
return
self.loaded_language = short_hand
empty_list(self.layout_list)
self.language_label.set_label(language)
# fill list with all keyboard layouts for given language
layouts = get_layouts_for(short_hand, language)
assert len(layouts) > 0, 'Language {} has no keyboard layouts! Please report this.'.format(language)
for keyboard_layout, name in layouts:
row = SelectionRow(name, keyboard_layout)
self.layout_list.append(row)
layout_rows = []
for keyboard_layout, name in get_layouts_for(short_hand, language):
layout_rows.append(SelectionRow(name, keyboard_layout))
assert len(layout_rows) > 0, f'Language {language} has no keyboard layouts! Please report this.'
n_items = self.layout_list_model.get_n_items()
self.layout_list_model.splice(0, n_items, layout_rows)
self.stack.set_visible_child_name('layouts')
def _unselect_current_row(self):
if self.current_row:
@@ -82,7 +86,6 @@ class KeyboardLayoutPage(Gtk.Box, Page):
self._unselect_current_row()
self.current_row = row
row.set_activated(True)
self.layout_list.select_row(row)
# use selected keyboard layout

View File

@@ -1,13 +1,13 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from gi.repository import GLib, Gtk, GWeather
from gi.repository import Gio, Gtk, GWeather
from .global_state import global_state
from .installation_scripting import installation_scripting
from .locale_provider import get_current_formats, get_formats, get_timezone
from .page import Page
from .system_calls import set_system_formats, set_system_timezone
from .widgets import ProgressRow, empty_list
from .widgets import ProgressRow
def get_location_children(location):
@@ -46,7 +46,9 @@ class LocalePage(Gtk.Box, Page):
# locale
continents_list = Gtk.Template.Child()
countries_list = Gtk.Template.Child()
countries_list_model = Gio.ListStore()
subzones_list = Gtk.Template.Child()
subzones_list_model = Gio.ListStore()
continents_list_loaded = False
def __init__(self, **kwargs):
@@ -58,6 +60,8 @@ class LocalePage(Gtk.Box, Page):
self.formats_list.connect('row-activated', self._on_formats_row_activated)
for timezone_list in [self.continents_list, self.countries_list, self.subzones_list]:
timezone_list.connect('row-activated', self._on_timezone_row_activated)
self.countries_list.bind_model(self.countries_list_model, lambda x: x)
self.subzones_list.bind_model(self.subzones_list_model, lambda x: x)
def _load_continents_list(self):
if not self.continents_list_loaded:
@@ -73,10 +77,13 @@ class LocalePage(Gtk.Box, Page):
self.text_stack.set_visible_child_name('timezone')
def _load_countries_list(self, continent):
empty_list(self.countries_list)
# take me home
country_rows = []
for country in get_location_children(continent):
self.countries_list.append(ProgressRow(country.get_name(), country))
country_rows.append(ProgressRow(country.get_name(), country))
n_items = self.countries_list_model.get_n_items()
self.countries_list_model.splice(0, n_items, country_rows)
self.list_stack.set_visible_child_name('timezone_countries')
@@ -91,11 +98,13 @@ class LocalePage(Gtk.Box, Page):
self.list_stack.set_visible_child_name('formats')
def _load_subzones_list(self, country):
empty_list(self.subzones_list)
subzone_rows = []
for subzone in get_location_children(country):
if subzone.get_timezone():
self.subzones_list.append(ProgressRow(subzone.get_name(), subzone))
subzone_rows.append(ProgressRow(subzone.get_name(), subzone))
n_items = self.subzones_list_model.get_n_items()
self.subzones_list_model.splice(0, n_items, subzone_rows)
self.list_stack.set_visible_child_name('timezone_subzones')

View File

@@ -3,11 +3,6 @@
from gi.repository import Gtk
def empty_list(list_box):
for row in list_box:
del row
@Gtk.Template(resource_path='/com/github/p3732/os-installer/ui/widgets/device_row.ui')
class DeviceRow(Gtk.ListBoxRow):
__gtype_name__ = 'DeviceRow'