# SPDX-License-Identifier: WTFPL import os from gi.repository import Gtk, Gdk from .view import View from yms import gtkutils, combobox, treeview from yms.lookup import Dropdown from yms.model import cable class CablesView(View): def __init__(self, context): super().__init__(context, _("Cables"), "cables.ui") self.cable = cable.Cable(context.db) # current record self.newrecord = False self.lst_cables = Gtk.ListStore(int, str, str, str) self.load_list() treeview.add_column(self.tree['cables'], _('ID'), 0) treeview.add_column(self.tree['cables'], _('Name'), 1) treeview.add_column(self.tree['cables'], _('Condition'), 2) treeview.add_column(self.tree['cables'], _('Length'), 3) self.tree['cables'].set_model(self.lst_cables) # Cable type self.dd_type = self.context.lookup.dropdown(Dropdown.CABLE_TYPE) combobox.load_from_dict(self.combo['type'], {-1: _("─── none ───")} | self.dd_type) # Cable tech type self.dd_techname = self.context.lookup.dropdown(Dropdown.CABLE_TECHNAME) combobox.load_from_dict(self.combo['techname'], {-1: _("─── none ───")} | self.dd_techname) # Supplier self.dd_supplier = self.context.lookup.company(1) combobox.load_from_dict(self.combo['supplier'], {-1: _("─── none ───")} | self.dd_supplier) # Manufacturer self.dd_manufacturer = self.context.lookup.company(2) combobox.load_from_dict(self.combo['manufacturer'], {-1: _("─── none ───")} | self.dd_manufacturer) # Condition self.dd_condition = self.context.lookup.enum('cable.cablecond') combobox.load_from_dict(self.combo['condition'], self.dd_condition, 'unknown') # subscribe to data change events self.context.subscribe("dd_cabletype_modified", self.type_modified) self.context.subscribe("dd_techname_modified", self.techname_modified) self.context.subscribe("company_modified", self.company_modified) self.context.subscribe("vessel_modified", self.vessel_modified) def get_list_row(self): return ( self.cable.cableid, self.cable.cablename, self.dd_condition.get(self.cable.cablecond, ''), self.format_float(self.cable.length, suffix="m") ) def add_row(self): treeiter = self.lst_cables.append(self.get_list_row()) self.tree['cables'].get_selection().select_iter(treeiter) def refresh_row(self): treeview.set_row_values(self.tree['cables'], self.get_list_row()) def load_list(self): selected_id = treeview.get_value(self.tree['cables']) self.lst_cables.clear() sql = ( "SELECT cableid, cablename, cablecond, length " "FROM cable " "WHERE vid=%s " "ORDER BY cableid" ) self.db.dcur.execute(sql, (self.context.vessel.vid,)) selection = self.tree['cables'].get_selection() for row in self.db.dcur.fetchall(): data = ( row['cableid'], row['cablename'], row['cablecond'], self.format_float(row['length'], suffix='m') ) treeiter = self.lst_cables.append(data) if row['cableid'] == selected_id: # restore previous selected row selection.select_iter(treeiter) def type_modified(self, eventtype, data): self.dd_type[data[0]] = data[1] lst = self.combo['type'].get_model() if eventtype == self.context.EVENT_UPD: for row in lst: if row[0] == data[0]: row[1] = data[1] return elif eventtype == self.context.EVENT_ADD: lst.append(data) else: # not implemented pass def techname_modified(self, eventtype, data): self.dd_techname[data[0]] = data[1] lst = self.combo['techname'].get_model() if eventtype == self.context.EVENT_UPD: for row in lst: if row[0] == data[0]: row[1] = data[1] return elif eventtype == self.context.EVENT_ADD: lst.append(data) else: # not implemented pass def company_modified(self, eventtype, data): if data[1] == 1: # supplier self.dd_supplier[data[0]] = data[2] lst = self.combo['supplier'].get_model() elif data[1] == 2: # manufacturer self.dd_manufacturer[data[0]] = data[2] lst = self.combo['manufacturer'].get_model() else: # all other return if eventtype == self.context.EVENT_UPD: for row in lst: if row[0] == data[0]: row[1] = data[2] elif eventtype == self.context.EVENT_ADD: lst.append((data[0], data[2])) def vessel_modified(self, eventtype, data): self.tree['cables'].get_selection().unselect_all() self.load_list() self.topwidget.set_visible_child(self.box['cables']) def display_record(self): self.label['name'].set_text(self.cable.cablename or '') if self.cable.cabletype: self.label['type'].set_text(self.dd_type[self.cable.cabletype]) else: self.label['type'].set_text('') if self.cable.techname: self.label['techname'].set_text(self.dd_techname[self.cable.techname]) else: self.label['techname'].set_text('') self.label['length'].set_text(self.format_float(self.cable.length, suffix="m")) self.label['conductors'].set_text(self.format_int(self.cable.conductors)) self.label['diameter'].set_text(self.format_float(self.cable.diameter, 1, suffix="mm")) self.label['xsection'].set_text(self.format_float(self.cable.xsection, 1, suffix="mm²")) self.label['weight'].set_text(self.format_float(self.cable.weight, suffix="kg/m")) if self.cable.color: self.label['color'].set_markup(f'#{self.cable.color} ●') else: self.label['color'].set_text('') self.label['supplier'].set_text(self.dd_supplier.get(self.cable.supplier, '-')) self.label['manufacturer'].set_text(self.dd_manufacturer.get(self.cable.manufacturer, '-')) self.label['condition'].set_text(self.cable.cablecond) self.label['remarks'].set_text(self.cable.remarks or '') def edit_record(self): # fill UI fields from database record self.entry['name'].set_text(self.cable.cablename or '') combobox.activate_row(self.combo['type'], self.cable.cabletype or -1) combobox.activate_row(self.combo['techname'], self.cable.techname or -1) self.spin['length'].set_value(self.cable.length or 0) self.spin['conductors'].set_value(self.cable.conductors or 0) self.spin['diameter'].set_value(self.cable.diameter or 0) self.spin['xsection'].set_value(self.cable.xsection or 0) self.spin['weight'].set_value(self.cable.weight or 0) self.entry['color'].set_text(self.format_color(self.cable.color)) if self.cable.color is None: rgba = Gdk.RGBA(0, 0, 0, 0) else: rgba = Gdk.RGBA() rgba.parse('#' + self.cable.color) self.button['color'].set_rgba(rgba) combobox.activate_row(self.combo['supplier'], self.cable.supplier or -1) combobox.activate_row(self.combo['manufacturer'], self.cable.manufacturer or -1) combobox.activate_row(self.combo['condition'], self.cable.cablecond or 'unknown') self.text['remarks'].get_buffer().set_text(self.cable.remarks or '') self.entry['name'].grab_focus() def save_record(self): # fields must have been validated beforehand self.cable.vid = self.context.vessel.vid self.cable.cablename = self.entry['name'].get_text().strip() self.cable.cabletype = combobox.get_list_value(self.combo['type']) if self.cable.cabletype == -1: self.cable.cabletype = None self.cable.length = int(self.spin['length'].get_value()) self.cable.conductors = int(self.spin['conductors'].get_value()) self.cable.diameter = self.spin['diameter'].get_value() self.cable.xsection = self.spin['xsection'].get_value() self.cable.weight = int(self.spin['weight'].get_value()) if self.button['color'].get_rgba().alpha > 0: self.cable.color = self.entry['color'].get_text().lstrip('#') else: self.cable.color = None self.cable.supplier = combobox.get_list_value(self.combo['supplier']) if self.cable.supplier == -1: self.cable.supplier = None self.cable.manufacturer = combobox.get_list_value(self.combo['manufacturer']) if self.cable.manufacturer == -1: self.cable.manufacturer = None self.cable.cablecond = combobox.get_list_value(self.combo['condition']) self.cable.remarks = gtkutils.get_text_contents(self.text['remarks']) def on_but_color_set(self, button): rgba = button.get_rgba() r = round(rgba.red * 255) g = round(rgba.green * 255) b = round(rgba.blue * 255) a = round(rgba.alpha * 255) if a > 0: self.entry['color'].set_text(f"#{r:02X}{g:02X}{b:02X}") else: self.entry['color'].set_text('') def on_ent_color_activate(self, entry): self.set_color_from_entry(entry, self.button['color']) def on_ent_color_focus_out_event(self, entry, event): self.set_color_from_entry(entry, self.button['color']) return False # buttons on list page def on_but_new_clicked(self, widget): self.newrecord = True self.cable.clear() self.edit_record() self.topwidget.set_visible_child(self.box['cable_edit']) def on_but_edit_clicked(self, widget): cableid = treeview.get_value(self.tree['cables'], 0) if not cableid: return self.newrecord = False self.label['title_edit'].set_text(_("Edit Cable")) if not self.record_lock('cable', cableid): return self.cable.load(cableid) self.edit_record() self.topwidget.set_visible_child(self.box['cable_edit']) def on_but_detail_clicked(self, widget): cableid = treeview.get_value(self.tree['cables'], 0) if not cableid: return self.cable.load(cableid) self.display_record() self.topwidget.set_visible_child(self.box['cable_detail']) self.button['close'].grab_focus() def on_tvw_cables_row_activated(self, widget, path, column): cableid = treeview.get_value(widget, 0) self.cable.load(cableid) self.display_record() self.topwidget.set_visible_child(self.box['cable_detail']) # buttons on detail page # edit2 uses edit callback def on_but_close_clicked(self, widget): self.topwidget.set_visible_child(self.box['cables']) def on_but_delete_clicked(self, widget): answer = gtkutils.questionbox( _("Delete cable!\n" "Are you sure?"), title="YMS" ) if answer == False: return self.topwidget.set_visible_child(self.box['cables']) # buttons on edit page def on_but_save_clicked(self, widget): # check mandatory fields: # name, type, length, conductors, xsection cname = self.entry['name'].get_text().strip() if not cname: self.show_message(_("Name is mandatory. Please enter one."), Gtk.MessageType.WARNING) self.entry['name'].grab_focus() return self.save_record() print(self.cable) if not self.cable.save(): (etype, ecode, etext) = self.context.db.last_error() self.show_message(f"{etype} ({ecode}): {etext}", Gtk.MessageType.ERROR) return self.record_unlock() if self.newrecord: self.add_row() self.context.publish("cable_modified", self.context.EVENT_ADD, (self.cable.pk_value, self.cable.cablename) ) else: self.refresh_row() self.context.publish("cable_modified", self.context.EVENT_UPD, (self.cable.pk_value, self.cable.cablename) ) self.newrecord = False self.hide_message() self.display_record() self.topwidget.set_visible_child(self.box['cable_detail']) def on_but_cancel_clicked(self, widget): self.hide_message() self.record_unlock() self.topwidget.set_visible_child(self.box['cables'])