La til gtk tabeell

This commit is contained in:
2023-12-04 12:14:00 +01:00
parent 5e899fca62
commit 865abb5213
2 changed files with 807 additions and 0 deletions

260
otime/ui.py Normal file
View File

@@ -0,0 +1,260 @@
# Copyright (C) 2021 Tim Lauridsen < tla[at]rasmil.dk >
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to
# the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
"""
Sample Python Gtk4 Application
"""
import sys
import time
from typing import List
import copy
import gi
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
from gi.repository import Gtk, GObject, Gio, Adw
from widgets import ColumnViewListStore
import otime
import file_io
class ColumnElem(GObject.GObject):
"""custom data element for a ColumnView model (Must be based on GObject)"""
def __init__(self, name: str):
super(ColumnElem, self).__init__()
self.name = name
def __repr__(self):
return f"ColumnElem(name: {self.name})"
"""
class OtimeRunnerView(Gtk.ListView):
__gtype_name__ = "OtimeRunnerRowView"
__gsignals__ = {"refresh": (GObject.SignalFlags.RUN_FIRST, None, ())}
selection = Gtk.Template.Child()
class YumexQueueRow(Gtk.Box):
__gtype_name__ = "YumexQueueRow"
icon = Gtk.Template.Child()
text = Gtk.Template.Child()
dep = Gtk.Template.Child()
def __init__(self, view, **kwargs):
super().__init__(**kwargs)
self.view: OtimeRunnerView = view
self.pkg: YumexPackage = None
"""
class MyColumnViewColumn(ColumnViewListStore):
"""Custom ColumnViewColumn"""
def __init__(
self, win: Gtk.ApplicationWindow, col_view: Gtk.ColumnView, data: List
):
# Init ListView with store model class.
super(MyColumnViewColumn, self).__init__(ColumnElem, col_view)
self.win = win
# put some data into the model
for elem in data:
self.add(ColumnElem(elem))
def factory_setup(self, widget, item: Gtk.ListItem):
"""Gtk.SignalListItemFactory::setup signal callback
Handles the creation widgets to put in the ColumnViewColumn
"""
label = Gtk.Text()
label.set_halign(Gtk.Align.START)
label.set_hexpand(True)
label.set_margin_start(10)
item.set_child(label)
def factory_bind(self, widget, item: Gtk.ListItem):
"""Gtk.SignalListItemFactory::bind signal callback
Handles adding data for the model to the widgets created in setup
"""
label = item.get_child() # Get the Gtk.Label stored in the ListItem
data = item.get_item() # get the model item, connected to current ListItem
label.set_text(data.name) # Update Gtk.Label with data from model item
class MyWindow(Adw.ApplicationWindow):
def __init__(self, title, width, height, **kwargs):
super(MyWindow, self).__init__(**kwargs)
self.set_default_size(width, height)
box = Gtk.Box()
box.props.orientation = Gtk.Orientation.VERTICAL
header = Gtk.HeaderBar()
stack = Adw.ViewStack()
switcher = Adw.ViewSwitcherTitle()
switcher.set_stack(stack)
header.set_title_widget(switcher)
box.append(header)
content = self.setup_content()
page1 = stack.add_titled(content, "Løpere", "Løpere")
box_p2 = Gtk.Box()
page2 = stack.add_titled(box_p2, "page2", "Page 2")
box.append(stack)
self.set_content(box)
def setup_content(self):
"""Add a page with a text selector to the stack"""
# ColumnView with custom columns
self.columnview = Gtk.ColumnView()
self.columnview.set_show_column_separators(True)
sw = Gtk.ScrolledWindow()
self.columnview = Gtk.ColumnView()
factory_c1 = Gtk.SignalListItemFactory()
factory_c1.connect("setup", setup_c)
factory_c1.connect("bind", bind_c1)
factory_c2 = Gtk.SignalListItemFactory()
factory_c2.connect("setup", setup_c)
factory_c2.connect("bind", bind_c2)
factory_c3 = Gtk.SignalListItemFactory()
factory_c3.connect("setup", setup_c)
factory_c3.connect("bind", bind_c3)
factory_c4 = Gtk.SignalListItemFactory()
factory_c4.connect("setup", setup_c)
factory_c4.connect("bind", bind_c4)
factory_c5 = Gtk.SignalListItemFactory()
factory_c5.connect("setup", setup_c)
factory_c5.connect("bind", bind_c5)
selection = Gtk.SingleSelection()
store = Gio.ListStore.new(DataObject)
selection.set_model(store)
self.columnview.set_model(selection)
columns = []
columns.append(Gtk.ColumnViewColumn.new("Fornavn", factory_c1))
columns.append(Gtk.ColumnViewColumn.new("Etternavn ", factory_c2))
columns.append(Gtk.ColumnViewColumn.new("Klubb", factory_c3))
columns.append(Gtk.ColumnViewColumn.new("klasse", factory_c4))
columns.append(Gtk.ColumnViewColumn.new("eCard", factory_c5))
for i in columns:
self.columnview.append_column(i)
project_dir = '/home/trygve/Dokumenter/ÅbN&FC_2'
config_path = project_dir + '/config.yaml'
mtr_path = project_dir + '/mtr.yaml'
csv_path = project_dir + '/runners.csv'
event = file_io.event_from_yaml_and_csv(config_path, mtr_path, csv_path)
data = [DataObject(i.first, i.last, i.club, i.o_class, i.card_id) for i in event.runners]
for i in data:
store.append(i)
lw_frame = Gtk.Frame()
lw_frame.set_valign(Gtk.Align.FILL)
lw_frame.set_vexpand(True)
lw_frame.set_margin_start(20)
lw_frame.set_margin_end(20)
lw_frame.set_margin_top(10)
lw_frame.set_margin_bottom(10)
sw.set_child(self.columnview)
lw_frame.set_child(sw)
return lw_frame
class DataObject(GObject.GObject):
__gtype_name__ = 'DataObject'
text = GObject.Property(type=str, default=None)
def __init__(self, f_name, l_name, club, o_class, card):
super().__init__()
self.f_name = f_name
self.l_name = l_name
self.club = club
self.o_class = o_class
self.card = str(card)
def setup_c(widget, item):
"""Setup the widget to show in the Gtk.Listview"""
text = Gtk.Text()
item.set_child(text)
def bind_c1(widget, item):
"""bind data from the store object to the widget"""
label = item.get_child()
obj = item.get_item()
label.set_text(obj.f_name)
label.bind_property("f_name", obj, "f_name")
def bind_c2(widget, item):
"""bind data from the store object to the widget"""
text = item.get_child()
obj = item.get_item()
text.set_text(obj.l_name)
def bind_c3(widget, item):
"""bind data from the store object to the widget"""
text = item.get_child()
obj = item.get_item()
text.set_text(obj.club)
def bind_c4(widget, item):
"""bind data from the store object to the widget"""
text = item.get_child()
obj = item.get_item()
text.set_text(obj.o_class)
def bind_c5(widget, item):
"""bind data from the store object to the widget"""
text = item.get_child()
obj = item.get_item()
text.set_text(obj.card)
class Application(Adw.Application):
"""Main Aplication class"""
def __init__(self):
super().__init__(
application_id="net.trygve.otime.alpha", flags=Gio.ApplicationFlags.FLAGS_NONE
)
def do_activate(self):
win = self.props.active_window
if not win:
win = MyWindow("Otime veldig alpha", 800, 800, application=self)
win.present()
def main():
"""Run the main application"""
app = Application()
return app.run(sys.argv)
if __name__ == "__main__":
main()