import sys import gi gi.require_version('Gtk', '4.0') gi.require_version('Adw', '1') from gi.repository import Gtk, Adw import otime class MainWindow(Gtk.ApplicationWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.set_default_size(500, 300) self.set_title("ttime xml resultat generator") self.box1 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) self.box1.set_halign(Gtk.Align.CENTER) self.box2 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) self.mclamp = Adw.Clamp() self.box1.append(self.mclamp) self.mclamp.set_child(self.box2) self.mainlist = Gtk.ListBox() self.mainlist.set_selection_mode(Gtk.SelectionMode.NONE) self.mainlist.set_margin_top(16) self.mainlist.set_margin_bottom(8) self.mainlist.get_style_context().add_class('boxed-list') self.set_child(self.box1) self.box2.append(self.mainlist) self.cnf_row = Adw.ActionRow() self.cnf_row.set_title('tTime konfigurasjons-fil') self.cnf_row.set_subtitle('Ingen fil valgt') self.mainlist.append(self.cnf_row) self.cnf_btn_box = Gtk.Box() self.cnf_f_btn = Gtk.Button(label='tTime konfigurasjon') self.cnf_f_btn.set_valign(Gtk.Align.CENTER) self.cnf_f_btn.set_icon_name("document-open-symbolic") self.cnf_btn_box.append(self.cnf_f_btn) self.cnf_row.add_suffix(self.cnf_btn_box) self.cnf_f_dialog = Gtk.FileChooserNative.new(title="Velg konfigurasfila", parent=self, action=Gtk.FileChooserAction.OPEN) self.cnf_f_dialog.connect("response", self.sel_cnf) self.cnf_f_btn.connect("clicked", self.show_cnf_dialog) self.db_row = Adw.ActionRow() self.db_row.set_title('tTime database-fil') self.db_row.set_subtitle('Ingen fil valgt') self.mainlist.append(self.db_row) self.db_btn_box = Gtk.Box() self.db_f_btn = Gtk.Button(label='tTime database') self.db_f_btn.set_valign(Gtk.Align.CENTER) self.db_f_btn.set_icon_name("document-open-symbolic") self.db_btn_box.append(self.db_f_btn) self.db_row.add_suffix(self.db_btn_box) self.db_f_dialog = Gtk.FileChooserNative.new(title="Velg databasefila", parent=self, action=Gtk.FileChooserAction.OPEN) self.db_f_dialog.connect("response", self.sel_db) self.db_f_btn.connect("clicked", self.show_db_dialog) self.mtr_row = Adw.ActionRow() self.mtr_row.set_title('MTR fil') self.mtr_row.set_subtitle('Ingen fil valgt') self.mainlist.append(self.mtr_row) self.mtr_btn_box = Gtk.Box() self.mtr_f_btn = Gtk.Button(label='mtr') self.mtr_f_btn.set_valign(Gtk.Align.CENTER) self.mtr_f_btn.set_icon_name("document-open-symbolic") self.mtr_btn_box.append(self.mtr_f_btn) self.mtr_row.add_suffix(self.mtr_btn_box) self.mtr_f_dialog = Gtk.FileChooserNative.new(title="Velg mtr fila", parent=self, action=Gtk.FileChooserAction.OPEN) self.mtr_f_dialog.connect("response", self.sel_mtr) self.mtr_f_btn.connect("clicked", self.show_mtr_dialog) self.save_btn = Gtk.Button(label="Generer xml fil") self.save_btn.set_halign(Gtk.Align.CENTER) self.save_btn.get_style_context().add_class('suggested-action') self.box2.append(self.save_btn) self.save_dialog = Gtk.FileChooserNative.new(title="lagre xml resultat fil", parent=self, action=Gtk.FileChooserAction.SAVE) self.save_dialog.connect("response", self.save_xml) self.save_btn.connect("clicked", self.show_save_dialog) def show_cnf_dialog(self, button): self.cnf_f_dialog.show() def show_db_dialog(self, button): self.db_f_dialog.show() def show_mtr_dialog(self, button): self.mtr_f_dialog.show() def show_save_dialog(self, button): self.save_dialog.show() def sel_cnf(self, dialog, response): print('test') if response == Gtk.ResponseType.ACCEPT: file = dialog.get_file() filename = file.get_path() self.cnf_row.set_subtitle(filename) global ttime_cnf_file ttime_cnf_file = filename # Here you could handle opening or saving the file def sel_db(self, dialog, response): if response == Gtk.ResponseType.ACCEPT: file = dialog.get_file() filename = file.get_path() self.db_row.set_subtitle(filename) global ttime_db_file ttime_db_file = filename # Here you could handle opening or saving the file def sel_mtr(self, dialog, response): if response == Gtk.ResponseType.ACCEPT: file = dialog.get_file() filename = file.get_path() self.mtr_row.set_subtitle(filename) global mtr_file mtr_file = filename def save_xml(self, dialog, response): print(ttime_cnf_file, ttime_db_file, mtr_file) if response == Gtk.ResponseType.ACCEPT: file = dialog.get_file() filename = file.get_path() courses = otime.courses_from_ttime_conf(ttime_cnf_file) o_classes = otime.classes_from_ttime_conf(ttime_cnf_file, courses) runners = otime.ttime_db_to_class(ttime_db_file, o_classes) otime.ttime_mtr_to_class(mtr_file, runners) otime.gen_xml_result(runners, o_classes).write(filename) class MyApp(Adw.Application): def __init__(self, **kwargs): super().__init__(**kwargs) self.connect('activate', self.on_activate) def on_activate(self, app): self.win = MainWindow(application=app) self.win.present() ttime_cnf_file = '' ttime_db_file = '' mtr_file = '' def main(): app = MyApp(application_id='me.trygve.gtktest') app.run(sys.argv) if __name__ == '__main__': main()