229 lines
8.9 KiB
Python
229 lines
8.9 KiB
Python
# SPDX-License-Identifier: WTFPL
|
|
|
|
import os
|
|
from gi.repository import Gtk
|
|
|
|
from .view import View
|
|
from yms import gtkutils, combobox, treeview
|
|
from yms.lookup import Dropdown
|
|
from yms.model import fuse
|
|
|
|
class FusesView(View):
|
|
|
|
def __init__(self, context):
|
|
super().__init__(context, _("Fuses"), "fuses.ui")
|
|
|
|
self.fuse = fuse.Fuse(context.db) # current record
|
|
self.newrecord = False
|
|
|
|
self.lst_fuses = Gtk.ListStore(int, str, str, str)
|
|
self.load_list()
|
|
|
|
treeview.add_column(self.tree['fuses'], _('ID'), 0)
|
|
treeview.add_column(self.tree['fuses'], _('Name'), 1)
|
|
treeview.add_column(self.tree['fuses'], _('Current'), 2)
|
|
treeview.add_column(self.tree['fuses'], _('Description'), 3)
|
|
self.tree['fuses'].set_model(self.lst_fuses)
|
|
|
|
# Fuse Type
|
|
self.dd_ftype = self.context.lookup.dropdown(Dropdown.FUSE_TYPE)
|
|
combobox.load_from_dict(self.combo['ftype'], {-1: _("─── unknown ───")} | self.dd_ftype)
|
|
# Cables
|
|
self.dd_cable = self.context.lookup.cable(context.vessel.vid)
|
|
combobox.load_from_dict(self.combo['cable'], {-1: _("─── none ───")} | self.dd_cable)
|
|
# Equipment
|
|
self.dd_equipment = self.context.lookup.equipment(context.vessel.vid)
|
|
combobox.load_from_dict(self.combo['equipment'], {-1: _("─── none ───")} | self.dd_equipment)
|
|
# Location
|
|
self.dd_location = self.context.lookup.dropdown(Dropdown.FUSE_BLOCK)
|
|
combobox.load_from_dict(self.combo['location'], {-1: _("─── unknown ───")} | self.dd_location)
|
|
|
|
def load_list(self):
|
|
selected_id = treeview.get_value(self.tree['fuses'])
|
|
self.lst_fuses.clear()
|
|
sql = (
|
|
"SELECT fuseid, fnumber, current, description "
|
|
"FROM fuse "
|
|
"WHERE vid=%s "
|
|
"ORDER BY fuseid"
|
|
)
|
|
self.db.dcur.execute(sql, (self.context.vessel.vid,))
|
|
selection = self.tree['fuses'].get_selection()
|
|
for row in self.db.dcur.fetchall():
|
|
data = (
|
|
row['fuseid'],
|
|
'F{}'.format(row['fnumber']),
|
|
'{} A'.format(row['current']), # TODO better formatting
|
|
row['description']
|
|
)
|
|
treeiter = self.lst_fuses.append(data)
|
|
if row['fuseid'] == selected_id:
|
|
# restore previous selected row
|
|
selection.select_iter(treeiter)
|
|
|
|
def clear_record(self):
|
|
self.fuse.clear()
|
|
self.spin['fnumber'].set_value(0)
|
|
self.entry['description'].set_text('')
|
|
combobox.activate_row(self.combo['ftype'], -1)
|
|
self.spin['current'].set_value(0)
|
|
combobox.activate_row(self.combo['cable'], -1)
|
|
combobox.activate_row(self.combo['equipment'], -1)
|
|
combobox.activate_row(self.combo['location'], -1)
|
|
self.text['remarks'].get_buffer().set_text('')
|
|
self.spin['fnumber'].grab_focus()
|
|
|
|
def display_record(self):
|
|
self.label['fnumber'].set_text(f"F{self.fuse.fnumber}")
|
|
if self.fuse.ftype:
|
|
self.label['type'].set_text(self.dd_ftype[self.fuse.ftype] or '')
|
|
else:
|
|
self.label['type'].set_text('')
|
|
self.label['current'].set_text(self.format_float(self.fuse.current, 2, suffix="A"))
|
|
if self.fuse.cableid:
|
|
self.label['cable'].set_text(self.dd_cable[self.fuse.cableid])
|
|
else:
|
|
self.label['cable'].set_text('')
|
|
if self.fuse.eid:
|
|
self.label['equipment'].set_text(self.dd_equipment[self.fuse.eid])
|
|
else:
|
|
self.label['equipment'].set_text('')
|
|
if self.fuse.location:
|
|
self.label['location'].set_text(self.dd_location[self.fuse.location])
|
|
else:
|
|
self.label['location'].set_text('')
|
|
self.label['description'].set_text(str(self.fuse.description or ''))
|
|
if self.fuse.color:
|
|
self.label['color'].set_markup(f'#{self.cable.color} <span foreground="#{self.fuse.color}">●</span>')
|
|
else:
|
|
self.label['color'].set_text('')
|
|
self.label['remarks'].set_text(str(self.fuse.remarks or ''))
|
|
|
|
def edit_record(self):
|
|
self.spin['fnumber'].set_value(self.fuse.fnumber)
|
|
self.entry['description'].set_text(self.fuse.description)
|
|
combobox.activate_row(self.combo['ftype'], self.fuse.ftype)
|
|
self.spin['current'].set_value(self.fuse.current)
|
|
combobox.activate_row(self.combo['cable'], self.fuse.cableid)
|
|
combobox.activate_row(self.combo['equipment'], self.fuse.eid)
|
|
combobox.activate_row(self.combo['location'], self.fuse.location)
|
|
self.text['remarks'].get_buffer().set_text(self.fuse.remarks or '')
|
|
self.spin['fnumber'].grab_focus()
|
|
|
|
def save_record(self):
|
|
# fields must have been validated beforehand
|
|
self.fuse.vid = self.context.vessel.vid
|
|
self.fuse.fnumber = self.spin['fnumber'].get_value_as_int()
|
|
self.fuse.current = self.spin['current'].get_value()
|
|
self.fuse.description = self.entry['description'].get_text()
|
|
self.fuse.ftype = combobox.get_list_value(self.combo['ftype'])
|
|
if self.fuse.ftype == -1:
|
|
self.fuse.ftype = None
|
|
self.fuse.cableid = combobox.get_list_value(self.combo['cable'])
|
|
if self.fuse.cableid == -1:
|
|
self.fuse.cableid = None
|
|
self.fuse.eid = combobox.get_list_value(self.combo['equipment'])
|
|
if self.fuse.eid == -1:
|
|
self.fuse.eid = None
|
|
self.fuse.location = combobox.get_list_value(self.combo['location'])
|
|
if self.fuse.location == -1:
|
|
self.fuse.location = None
|
|
if self.button['color'].get_rgba().alpha > 0:
|
|
self.fuse.color = self.entry['color'].get_text().lstrip('#')
|
|
else:
|
|
self.fuse.color = None
|
|
self.fuse.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.label['title_edit'].set_text(_("Add new Fuse"))
|
|
self.clear_record()
|
|
self.topwidget.set_visible_child(self.box['fuse_edit'])
|
|
|
|
def on_but_edit_clicked(self, widget):
|
|
if fuseid is None:
|
|
return
|
|
self.label['title_edit'].set_text(_("Edit Fuse"))
|
|
self.newrecord = False
|
|
fuseid = treeview.get_value(self.tree['fuses'], 0)
|
|
if not self.record_lock('fuse', fuseid):
|
|
return
|
|
self.fuse.load(fuseid)
|
|
self.edit_record()
|
|
self.topwidget.set_visible_child(self.box['fuse_edit'])
|
|
|
|
def on_but_detail_clicked(self, widget):
|
|
fuseid = treeview.get_value(self.tree['fuses'], 0)
|
|
if not fuseid:
|
|
return
|
|
self.fuse.load(fuseid)
|
|
self.display_record()
|
|
self.topwidget.set_visible_child(self.box['fuse_detail'])
|
|
self.button['close'].grab_focus()
|
|
|
|
def on_tvw_fuses_row_activated(self, widget, path, column):
|
|
fuseid = treeview.get_value(widget, 0)
|
|
self.fuse.load(fuseid)
|
|
self.display_record()
|
|
self.topwidget.set_visible_child(self.box['fuse_detail'])
|
|
self.button['close'].grab_focus()
|
|
|
|
# buttons on detail page
|
|
# edit2 uses edit callback
|
|
def on_but_close_clicked(self, widget):
|
|
self.topwidget.set_visible_child(self.box['fuses'])
|
|
|
|
def on_but_delete_clicked(self, widget):
|
|
answer = gtkutils.questionbox(
|
|
_("Delete fuse!\n"
|
|
"Are you sure?"), title="YMS"
|
|
)
|
|
if answer == False:
|
|
return
|
|
self.topwidget.set_visible_child(self.box['fuses'])
|
|
|
|
# buttons on edit page
|
|
def on_but_save_clicked(self, widget):
|
|
fnumber = self.spin['fnumber'].get_value_as_int()
|
|
current = self.spin['current'].get_value()
|
|
# TODO check for duplicate number!
|
|
sql = "SELECT COUNT(fnumber) FROM fuse WHERE fnumber=%s"
|
|
|
|
if (fnumber == 0) or (current == 0):
|
|
# TODO error marker
|
|
return
|
|
self.save_record()
|
|
print(self.fuse)
|
|
if not self.fuse.save():
|
|
pass
|
|
self.record_unlock()
|
|
if self.newrecord:
|
|
self.load_list()
|
|
self.newrecord = False
|
|
self.display_record()
|
|
self.topwidget.set_visible_child(self.box['fuse_detail'])
|
|
|
|
def on_but_cancel_clicked(self, widget):
|
|
self.record_unlock()
|
|
self.topwidget.set_visible_child(self.box['fuses'])
|
|
|