s3manager-gtk/objectlist.py

96 lines
2.6 KiB
Python
Raw Permalink Normal View History

2024-10-08 02:29:15 +02:00
'''
Example of a list with two lines per row.
'''
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk, Gio, GObject, GLib
# content list to add to the listview
content = (
('Into the New', 'Angus Guild'),
('Now to the Sea', 'Anne Toner'),
('Sleeping Gypsy', 'Ralph Bennett-Eades'),
('Angels in Silence', 'The Wastrels'),
('Deeply in Love', 'Duo Montagne'),
('No Bridges', 'Martin Way')
)
class DataObject(GObject.GObject):
'''
object to hold the data for each row
'''
def __init__(self, text: str):
2024-09-09 21:24:14 +02:00
super().__init__()
2024-10-08 02:29:15 +02:00
self.text = text
2024-09-09 21:24:14 +02:00
2024-10-08 02:29:15 +02:00
class AppWindow(Gtk.ApplicationWindow):
def __init__(self, app):
super(AppWindow, self).__init__(application=app)
self.init_ui()
2024-09-09 21:24:14 +02:00
2024-10-08 02:29:15 +02:00
def init_ui(self):
self.title="Double List"
GLib.set_prgname (
'Adwaita Examples by Afacanc38'
)
GLib.set_application_name (
'Adwaita ListBox'
)
2024-09-09 21:24:14 +02:00
2024-10-08 02:29:15 +02:00
self.set_default_size (650, 500)
self.default_height=400
self.default_width=400
self.scroll = Gtk.ScrolledWindow ()
self.scroll.set_policy (Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
listview = Gtk.ListView()
factory = Gtk.SignalListItemFactory()
factory.connect("setup", self.setup)
factory.connect("bind", self.bind)
listview.set_factory(factory)
self.scroll.set_child (
listview
)
2024-09-09 21:24:14 +02:00
2024-10-08 02:29:15 +02:00
selection = Gtk.SingleSelection()
store = Gio.ListStore.new(DataObject)
selection.set_model(store)
listview.set_model(selection)
2024-09-09 21:24:14 +02:00
2024-10-08 02:29:15 +02:00
#add rows to the listview from the content list
for row in content:
line1, line2 = row
item = line1 + '\n' + line2
store.append(DataObject(item))
2024-09-09 21:24:14 +02:00
2024-10-08 02:29:15 +02:00
self.set_child(self.scroll)
2024-09-09 21:24:14 +02:00
2024-10-08 02:29:15 +02:00
def setup(self, widget, item):
"""Setup the widget (Gtk.ListItem) to show in the Gtk.ListView"""
label = Gtk.Label(
margin_top = 2,
margin_start = 8,
margin_end = 8,
halign = Gtk.Align.START
)
item.set_child(label)
2024-09-09 21:24:14 +02:00
2024-10-08 02:29:15 +02:00
def bind(self, widget, item):
"""bind data (Gtk.ListItem) from the store object to the widget"""
label = item.get_child()
obj = item.get_item()
label.set_label(obj.text)
2024-09-09 21:24:14 +02:00
2024-10-08 02:29:15 +02:00
def on_activate(app):
win = AppWindow(app)
win.present()
2024-09-09 21:24:14 +02:00
2024-10-08 02:29:15 +02:00
app = Gtk.Application(application_id='com.example.doublelist')
app.connect('activate', on_activate)
app.run(None)